Documentation
¶
Overview ¶
Package floats provides a set of helper routines for dealing with slices of float64. The functions avoid allocations to allow for use within tight loops without garbage collection overhead.
The convention used is that when a slice is being modified in place, it has the name dst.
Index ¶
- func Add(dst, s []float64)
- func AddConst(c float64, dst []float64)
- func AddScaled(dst []float64, alpha float64, s []float64)
- func AddScaledTo(dst, y []float64, alpha float64, s []float64) []float64
- func AddTo(dst, s, t []float64) []float64
- func Argsort(dst []float64, inds []int)
- func Count(f func(float64) bool, s []float64) int
- func CumProd(dst, s []float64) []float64
- func CumSum(dst, s []float64) []float64
- func Distance(s, t []float64, L float64) float64
- func Div(dst, s []float64)
- func DivTo(dst, s, t []float64) []float64
- func Dot(s1, s2 []float64) float64
- func Equal(s1, s2 []float64) bool
- func EqualApprox(s1, s2 []float64, tol float64) bool
- func EqualFunc(s1, s2 []float64, f func(float64, float64) bool) bool
- func EqualLengths(slices ...[]float64) bool
- func EqualWithinAbs(a, b, tol float64) bool
- func EqualWithinAbsOrRel(a, b, absTol, relTol float64) bool
- func EqualWithinRel(a, b, tol float64) bool
- func EqualWithinULP(a, b float64, ulp uint) bool
- func Find(inds []int, f func(float64) bool, s []float64, k int) ([]int, error)
- func HasNaN(s []float64) bool
- func LogSpan(dst []float64, l, u float64) []float64
- func LogSumExp(s []float64) float64
- func Max(s []float64) float64
- func MaxIdx(s []float64) int
- func Min(s []float64) float64
- func MinIdx(s []float64) int
- func Mul(dst, s []float64)
- func MulTo(dst, s, t []float64) []float64
- func Nearest(s []float64, v float64) int
- func NearestWithinSpan(n int, l, u float64, v float64) int
- func Norm(s []float64, L float64) float64
- func Prod(s []float64) float64
- func Reverse(s []float64)
- func Round(x float64, prec int) float64
- func RoundEven(x float64, prec int) float64
- func Same(s, t []float64) bool
- func Scale(c float64, dst []float64)
- func Span(dst []float64, l, u float64) []float64
- func Sub(dst, s []float64)
- func SubTo(dst, s, t []float64) []float64
- func Sum(s []float64) float64
- func Within(s []float64, v float64) int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add(dst, s []float64)
Add adds, element-wise, the elements of s and dst, and stores in dst. Panics if the lengths of dst and s do not match.
Example (Newslice) ¶
// If one wants to store the result in a // new container, just make a new slice s1 := []float64{1, 2, 3, 4} s2 := []float64{5, 6, 7, 8} s3 := []float64{1, 1, 1, 1} dst := make([]float64, len(s1)) AddTo(dst, s1, s2) Add(dst, s3) fmt.Println("dst =", dst) fmt.Println("s1 =", s1) fmt.Println("s2 =", s2) fmt.Println("s3 =", s3)
Output: dst = [7 9 11 13] s1 = [1 2 3 4] s2 = [5 6 7 8] s3 = [1 1 1 1]
Example (Simple) ¶
// Adding three slices together. Note that // the result is stored in the first slice s1 := []float64{1, 2, 3, 4} s2 := []float64{5, 6, 7, 8} s3 := []float64{1, 1, 1, 1} Add(s1, s2) Add(s1, s3) fmt.Println("s1 =", s1) fmt.Println("s2 =", s2) fmt.Println("s3 =", s3)
Output: s1 = [7 9 11 13] s2 = [5 6 7 8] s3 = [1 1 1 1]
Example (Unequallengths) ¶
// If the lengths of the slices are unknown, // use Eqlen to check s1 := []float64{1, 2, 3} s2 := []float64{5, 6, 7, 8} eq := EqualLengths(s1, s2) if eq { Add(s1, s2) } else { fmt.Println("Unequal lengths") }
Output: Unequal lengths
func AddConst ¶
AddConst adds the scalar c to all of the values in dst.
Example ¶
s := []float64{1, -2, 3, -4} c := 5.0 AddConst(c, s) fmt.Println("s =", s)
Output: s = [6 3 8 1]
func AddScaled ¶
AddScaled performs dst = dst + alpha * s. It panics if the lengths of dst and s are not equal.
func AddScaledTo ¶
AddScaledTo performs dst = y + alpha * s, where alpha is a scalar, and dst, y and s are all slices. It panics if the lengths of dst, y, and s are not equal.
At the return of the function, dst[i] = y[i] + alpha * s[i]
func AddTo ¶
AddTo adds, element-wise, the elements of s and t and stores the result in dst. Panics if the lengths of s, t and dst do not match.
func Argsort ¶
Argsort sorts the elements of s while tracking their original order. At the conclusion of Argsort, s will contain the original elements of s but sorted in increasing order, and inds will contain the original position of the elements in the slice such that dst[i] = origDst[inds[i]]. It panics if the lengths of dst and inds do not match.
func Count ¶
Count applies the function f to every element of s and returns the number of times the function returned true.
func CumProd ¶
CumProd finds the cumulative product of the first i elements in s and puts them in place into the ith element of the destination dst. A panic will occur if the lengths of arguments do not match.
At the return of the function, dst[i] = s[i] * s[i-1] * s[i-2] * ...
Example ¶
s := []float64{1, -2, 3, -4} dst := make([]float64, len(s)) CumProd(dst, s) fmt.Println("dst =", dst) fmt.Println("s =", s)
Output: dst = [1 -2 -6 24] s = [1 -2 3 -4]
func CumSum ¶
CumSum finds the cumulative sum of the first i elements in s and puts them in place into the ith element of the destination dst. A panic will occur if the lengths of arguments do not match.
At the return of the function, dst[i] = s[i] + s[i-1] + s[i-2] + ...
Example ¶
s := []float64{1, -2, 3, -4} dst := make([]float64, len(s)) CumSum(dst, s) fmt.Println("dst =", dst) fmt.Println("s =", s)
Output: dst = [1 -1 2 -2] s = [1 -2 3 -4]
func Distance ¶
Distance computes the L-norm of s - t. See Norm for special cases. A panic will occur if the lengths of s and t do not match.
func Div ¶
func Div(dst, s []float64)
Div performs element-wise division dst / s and stores the value in dst. It panics if the lengths of s and t are not equal.
func DivTo ¶
DivTo performs element-wise division s / t and stores the value in dst. It panics if the lengths of s, t, and dst are not equal.
func Dot ¶
Dot computes the dot product of s1 and s2, i.e. sum_{i = 1}^N s1[i]*s2[i]. A panic will occur if lengths of arguments do not match.
func Equal ¶
Equal returns true if the slices have equal lengths and all elements are numerically identical.
func EqualApprox ¶
EqualApprox returns true if the slices have equal lengths and all element pairs have an absolute tolerance less than tol or a relative tolerance less than tol.
func EqualFunc ¶
EqualFunc returns true if the slices have the same lengths and the function returns true for all element pairs.
func EqualLengths ¶
EqualLengths returns true if all of the slices have equal length, and false otherwise. Returns true if there are no input slices.
func EqualWithinAbs ¶
EqualWithinAbs returns true if a and b have an absolute difference of less than tol.
func EqualWithinAbsOrRel ¶
EqualWithinAbsOrRel returns true if a and b are equal to within the absolute tolerance.
func EqualWithinRel ¶
EqualWithinRel returns true if the difference between a and b is not greater than tol times the greater value.
func EqualWithinULP ¶
EqualWithinULP returns true if a and b are equal to within the specified number of floating point units in the last place.
func Find ¶
Find applies f to every element of s and returns the indices of the first k elements for which the f returns true, or all such elements if k < 0. Find will reslice inds to have 0 length, and will append found indices to inds. If k > 0 and there are fewer than k elements in s satisfying f, all of the found elements will be returned along with an error. At the return of the function, the input inds will be in an undetermined state.
func LogSpan ¶
LogSpan returns a set of n equally spaced points in log space between, l and u where N is equal to len(dst). The first element of the resulting dst will be l and the final element of dst will be u. Panics if len(dst) < 2 Note that this call will return NaNs if either l or u are negative, and will return all zeros if l or u is zero. Also returns the mutated slice dst, so that it can be used in range, like:
for i, x := range LogSpan(dst, l, u) { ... }
func LogSumExp ¶
LogSumExp returns the log of the sum of the exponentials of the values in s. Panics if s is an empty slice.
func MaxIdx ¶
MaxIdx returns the index of the maximum value in the input slice. If several entries have the maximum value, the first such index is returned. If the slice is empty, MaxIdx will panic.
func MinIdx ¶
MinIdx returns the index of the minimum value in the input slice. If several entries have the maximum value, the first such index is returned. If the slice is empty, MinIdx will panic.
func Mul ¶
func Mul(dst, s []float64)
Mul performs element-wise multiplication between dst and s and stores the value in dst. Panics if the lengths of s and t are not equal.
func MulTo ¶
MulTo performs element-wise multiplication between s and t and stores the value in dst. Panics if the lengths of s, t, and dst are not equal.
func Nearest ¶
Nearest returns the index of the element in s whose value is nearest to v. If several such elements exist, the lowest index is returned. Panics if len(s) == 0.
func NearestWithinSpan ¶
NearestWithinSpan return the index of a hypothetical vector created by Span with length n and bounds l and u whose value is closest to v. NearestWithinSpan panics if u < l. If the value is greater than u or less than l, the function returns -1.
func Norm ¶
Norm returns the L norm of the slice S, defined as (sum_{i=1}^N s[i]^L)^{1/L} Special cases: L = math.Inf(1) gives the maximum absolute value. Does not correctly compute the zero norm (use Count).
func Round ¶
Round returns the half away from zero rounded value of x with prec precision.
Special cases are:
Round(±0) = +0 Round(±Inf) = ±Inf Round(NaN) = NaN
func RoundEven ¶
RoundEven returns the half even rounded value of x with prec precision.
Special cases are:
RoundEven(±0) = +0 RoundEven(±Inf) = ±Inf RoundEven(NaN) = NaN
func Same ¶
Same returns true if the input slices have the same length and the all elements have the same value with NaN treated as the same.
func Span ¶
Span returns a set of N equally spaced points between l and u, where N is equal to the length of the destination. The first element of the destination is l, the final element of the destination is u. Panics if len(dst) < 2.
Also returns the mutated slice dst, so that it can be used in range expressions, like:
for i, x := range Span(dst, l, u) { ... }
func Sub ¶
func Sub(dst, s []float64)
Sub subtracts, element-wise, the elements of s from dst. Panics if the lengths of dst and s do not match.
Types ¶
This section is empty.