vecf32

package module
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2017 License: MIT Imports: 1 Imported by: 0

README

vecf32 GoDoc Build Status Coverage Status

Package vecf32 provides common functions and methods for slices of float32

Installation

go get -u github.com/chewxy/vecf32

Dependencies

This package math32. For testing this package uses testify/assert, which is licenced with a MIT/BSD-like licence

Build Tags

The point of this package is to provide operations that are accelerated by SIMD. However, this pakcage by default does not use SIMD. To use SIMD, build tags must be used. The supported build tags are sse and avx. Here's an example on how to use them:

  • SSE - `go build -tags='sse' ...
  • AVX - `go build -tags='avx' ...

FAQ

Why are there so many a = a[:len(a)] lines?

This is mainly done to eliminate bounds checking in a loop. The idea is the bounds of the slice is checked early on, and if need be, panics early. Then if everything is normal, there won't be bounds checking while in the loop.

To check for boundschecking and bounds check elimination (an amazing feature that landed in Go 1.7), compile your programs with -gcflags='-d=ssa/check_bce/debug=1'.

Contributing

Contributions are welcome. The typical process works like this:

  1. File an issue on the topic you want to contribute
  2. Fork this repo
  3. Add your contribution
  4. Make a pull request
  5. The pull request will be merged once tests pass, and code reviewed.

Pull Requests

This package is very well tested. Please ensure tests are written if any new features are added. If bugs are fixed, please add the bugs to the tests as well.

Licence

Package vecf32 is licenced under the MIT licence.

Documentation

Overview

Package vecf32 provides common functions and methods for slices of float32.

Name

In the days of yore, scientists who computed with computers would use arrays to represent vectors, each value representing magnitude and/or direction. Then came the C++ Standard Templates Library, which sought to provide this data type in the standard library. Now, everyone conflates a term "vector" with dynamic arrays.

In the C++ book, Bjarne Stroustrup has this to say:

One could argue that valarray should have been called vector because is is a traditional mathematical vector
and that vector should have been called array.
However, this is not the way that the terminology evolved.
A valarray is a vector optimized for numeric computation;
a vector is a flexible container designed for holding and manipulating objects of a wide variety of types;
and an array is a low-level built-in type

Go has a better name for representing dynamically allocated arrays of any type - "slice". However, "slice" is both a noun and verb and many libraries that I use already use "slice"-as-a-verb as a name, so I had to settle for the second best name: "vector".

It should be noted that while the names used in this package were definitely mathematically inspired, they bear only little resemblance the actual mathematical operations performed.

Naming Convention

The names of the operations assume you're working with slices of float32s. Hence `Add` performs elementwise addition between two []float32.

Operations between []float32 and float32 are also supported, however they are differently named. Here are the equivalents:

+------------------------+--------------------------------------------+
| []float32-[]float32 Op |         []float32-float32 Op               |
+------------------------+--------------------------------------------+
| Add(a, b []float32)    | Trans(a float32, b []float32)              |
| Sub(a, b []float32)    | TransInv/TransInvR(a float32, b []float32) |
| Mul(a, b []float32)    | Scale(a float32, b []float32)              |
| Div(a, b []float32)    | ScaleInv/ScaleInvR(a float32, b []float32) |
| Pow(a, b []float32)    | PowOf/PowOfR(a float32, b []float32)       |
+------------------------+--------------------------------------------+

You may note that for the []float64 - float64 binary operations, the scalar (float64) is always the first operand. In operations that are not commutative, an additional function is provided, suffixed with "R" (for reverse)

Range Check and BCE

This package does not provide range checking. If indices are out of range, the functions will panic. This package should play well with BCE.

TODO(anyone): provide SIMD vectorization for Incr and []float32-float64 functions Pull requests accepted

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(a, b []float32)

Add performs a̅ + b̅. a̅ will be clobbered

func Argmax

func Argmax(a []float32) int

Argmax returns the index of the min in a slice

func Argmin

func Argmin(a []float32) int

Argmin returns the index of the min in a slice

func Div

func Div(a, b []float32)

Div performs a̅ ÷ b̅. a̅ will be clobbered

func IncrAdd

func IncrAdd(a, b, incr []float32)

IncrAdd performs a̅ + b̅ and then adds it elementwise to the incr slice

func IncrDiv

func IncrDiv(a, b, incr []float32)

func IncrMod

func IncrMod(a, b, incr []float32)

IncrMod performs a̅ % b̅ then adds it to incr

func IncrMul

func IncrMul(a, b, incr []float32)

IncrMul performs a̅ × b̅ and then adds it elementwise to the incr slice

func IncrPow

func IncrPow(a, b, incr []float32)

IncrDiv performs a̅ ÷ b̅. then adds it to incr

func IncrPowOf

func IncrPowOf(a []float32, s float32, incr []float32)

IncrPowOf performs elementwise power function and then increments the incr slice

incr += a̅ ^ s

func IncrPowOfR

func IncrPowOfR(a []float32, s float32, incr []float32)

PowOfR performs elementwise power function below and then increments the incr slice.

incr += s ^ a̅

func IncrScale

func IncrScale(a []float32, s float32, incr []float32)

Scale multiplies all values in the slice by the scalar and then increments the incr slice

incr += a̅ * s

func IncrScaleInv

func IncrScaleInv(a []float32, s float32, incr []float32)

IncrScaleInv divides all values in the slice by the scalar and then increments the incr slice

incr += a̅ / s

func IncrScaleInvR

func IncrScaleInvR(a []float32, s float32, incr []float32)

/ IncrScaleInvR divides all numbers in the slice by a scalar and then increments the incr slice

incr += s / a̅

func IncrSub

func IncrSub(a, b, incr []float32)

IncrSub performs a̅ = b̅ and then adds it elementwise to the incr slice

func IncrTrans

func IncrTrans(a []float32, s float32, incr []float32)

IncrTrans adds all the values in the slice by a scalar and then increments the incr slice

incr += a̅ + s

func IncrTransInv

func IncrTransInv(a []float32, s float32, incr []float32)

IncrTransInv subtracts all the values in the slice by a scalar and then increments the incr slice

incr += a̅ - s

func IncrTransInvR

func IncrTransInvR(a []float32, s float32, incr []float32)

IncrTransInvR subtracts all the numbers in a slice from a scalar and then increments the incr slice

incr += s - a̅

func InvSqrt

func InvSqrt(a []float32)

InvSqrt performs 1/√a̅ elementwise. a̅ will be clobbered

func Max

func Max(a, b []float32)

Max takes two slices, a̅ + b̅, and compares them elementwise. The highest value is put into a̅.

func MaxOf

func MaxOf(a []float32) (retVal float32)

MaxOf finds the max of a []float32. it panics if the slice is empty

func Min

func Min(a, b []float32)

Min takes two slices, a̅ + b̅ and compares them elementwise. The lowest value is put into a̅.

func MinOf

func MinOf(a []float32) (retVal float32)

MinOf finds the max of a []float32. it panics if the slice is empty

func Mod

func Mod(a, b []float32)

func Mul

func Mul(a, b []float32)

Mul performs a̅ × b̅. a̅ will be clobbered

func Pow

func Pow(a, b []float32)

Pow performs elementwise

a̅ ^ b̅

func PowOf

func PowOf(a []float32, s float32)

PowOf performs elementwise

a̅ ^ s

func PowOfR

func PowOfR(a []float32, s float32)

PowOfR performs elementwise

s ^ a̅

func Range

func Range(start, end int) []float32

Range is a function to create arithmetic progressions of float32

func Reduce

func Reduce(f func(a, b float32) float32, def float32, l ...float32) (retVal float32)

Reduce takes a function to reduce by, a defalut, and a splatted list of float32s

func Scale

func Scale(a []float32, s float32)

Scale multiplies all values in the slice by the scalar. It performs elementwise

a̅ * s

func ScaleInv

func ScaleInv(a []float32, s float32)

ScaleInv divides all values in the slice by the scalar. It performs elementwise

a̅ / s

func ScaleInvR

func ScaleInvR(a []float32, s float32)

/ ScaleInvR divides all numbers in the slice by a scalar

s / a̅

func Sqrt

func Sqrt(a []float32)

Sqrt performs √a̅ elementwise. a̅ will be clobbered

func Sub

func Sub(a, b []float32)

Sub performs a̅ - b̅. a̅ will be clobbered

func Sum

func Sum(a []float32) float32

Sum sums a slice of float32 and returns a float32

func Trans

func Trans(a []float32, s float32)

Trans adds all the values in the slice by a scalar

a̅ + s

func TransInv

func TransInv(a []float32, s float32)

TransInv subtracts all the values in the slice by a scalar

a̅ - s

func TransInvR

func TransInvR(a []float32, s float32)

TransInvR subtracts all the numbers in a slice from a scalar

s - a̅

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL