floats

package
v0.8.1-0...-a25970e Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2021 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Gonum floats GoDoc

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.

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

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 the result in dst. It panics if the argument lengths do not match.

Example (Newslice)
package main

import (
	"fmt"

	"gonum.org/v1/gonum/floats"
)

func main() {
	// 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))

	floats.AddTo(dst, s1, s2)
	floats.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)
package main

import (
	"fmt"

	"gonum.org/v1/gonum/floats"
)

func main() {
	// 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}
	floats.Add(s1, s2)
	floats.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)
package main

import (
	"fmt"

	"gonum.org/v1/gonum/floats"
)

func main() {
	// If the lengths of the slices are unknown,
	// use EqualLengths to check
	s1 := []float64{1, 2, 3}
	s2 := []float64{5, 6, 7, 8}

	eq := floats.EqualLengths(s1, s2)
	if eq {
		floats.Add(s1, s2)
	} else {
		fmt.Println("Unequal lengths")
	}

}
Output:

Unequal lengths

func AddConst

func AddConst(c float64, dst []float64)

AddConst adds the scalar c to all of the values in dst.

Example
package main

import (
	"fmt"

	"gonum.org/v1/gonum/floats"
)

func main() {
	s := []float64{1, -2, 3, -4}
	c := 5.0

	floats.AddConst(c, s)

	fmt.Println("s =", s)

}
Output:

s = [6 3 8 1]

func AddScaled

func AddScaled(dst []float64, alpha float64, s []float64)

AddScaled performs dst = dst + alpha * s. It panics if the slice argument lengths do not match.

func AddScaledTo

func AddScaledTo(dst, y []float64, alpha float64, s []float64) []float64

AddScaledTo performs dst = y + alpha * s, where alpha is a scalar, and dst, y and s are all slices. It panics if the slice argument lengths do not match.

At the return of the function, dst[i] = y[i] + alpha * s[i]

func AddTo

func AddTo(dst, s, t []float64) []float64

AddTo adds, element-wise, the elements of s and t and stores the result in dst. It panics if the argument lengths do not match.

func Argsort

func Argsort(dst []float64, inds []int)

Argsort sorts the elements of dst while tracking their original order. At the conclusion of Argsort, dst will contain the original elements of dst 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 argument lengths do not match.

func Count

func Count(f func(float64) bool, s []float64) int

Count applies the function f to every element of s and returns the number of times the function returned true.

func CumProd

func CumProd(dst, s []float64) []float64

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. It panics if the argument lengths do not match.

At the return of the function, dst[i] = s[i] * s[i-1] * s[i-2] * ...

Example
package main

import (
	"fmt"

	"gonum.org/v1/gonum/floats"
)

func main() {
	s := []float64{1, -2, 3, -4}
	dst := make([]float64, len(s))

	floats.CumProd(dst, s)

	fmt.Println("dst =", dst)
	fmt.Println("s =", s)

}
Output:

dst = [1 -2 -6 24]
s = [1 -2 3 -4]

func CumSum

func CumSum(dst, s []float64) []float64

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. It panics if the argument lengths do not match.

At the return of the function, dst[i] = s[i] + s[i-1] + s[i-2] + ...

Example
package main

import (
	"fmt"

	"gonum.org/v1/gonum/floats"
)

func main() {
	s := []float64{1, -2, 3, -4}
	dst := make([]float64, len(s))

	floats.CumSum(dst, s)

	fmt.Println("dst =", dst)
	fmt.Println("s =", s)

}
Output:

dst = [1 -1 2 -2]
s = [1 -2 3 -4]

func Distance

func Distance(s, t []float64, L float64) float64

Distance computes the L-norm of s - t. See Norm for special cases. It panics if the slice argument lengths 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 argument lengths do not match.

func DivTo

func DivTo(dst, s, t []float64) []float64

DivTo performs element-wise division s / t and stores the value in dst. It panics if the argument lengths do not match.

func Dot

func Dot(s1, s2 []float64) float64

Dot computes the dot product of s1 and s2, i.e. sum_{i = 1}^N s1[i]*s2[i]. It panics if the argument lengths do not match.

func Equal

func Equal(s1, s2 []float64) bool

Equal returns true when the slices have equal lengths and all elements are numerically identical.

func EqualApprox

func EqualApprox(s1, s2 []float64, tol float64) bool

EqualApprox returns true when 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

func EqualFunc(s1, s2 []float64, f func(float64, float64) bool) bool

EqualFunc returns true when the slices have the same lengths and the function returns true for all element pairs.

func EqualLengths

func EqualLengths(slices ...[]float64) bool

EqualLengths returns true when all of the slices have equal length, and false otherwise. It also returns true when there are no input slices.

func Find

func Find(inds []int, f func(float64) bool, s []float64, k int) ([]int, error)

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 HasNaN

func HasNaN(s []float64) bool

HasNaN returns true when the slice s has any values that are NaN and false otherwise.

func LogSpan

func LogSpan(dst []float64, l, u float64) []float64

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. It panics if the length of dst is less than 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

func LogSumExp(s []float64) float64

LogSumExp returns the log of the sum of the exponentials of the values in s. Panics if s is an empty slice.

func Max

func Max(s []float64) float64

Max returns the maximum value in the input slice. If the slice is empty, Max will panic.

func MaxIdx

func MaxIdx(s []float64) int

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. It panics if s is zero length.

func Min

func Min(s []float64) float64

Min returns the minimum value in the input slice. It panics if s is zero length.

func MinIdx

func MinIdx(s []float64) int

MinIdx returns the index of the minimum value in the input slice. If several entries have the minimum value, the first such index is returned. It panics if s is zero length.

func Mul

func Mul(dst, s []float64)

Mul performs element-wise multiplication between dst and s and stores the value in dst. It panics if the argument lengths do not match.

func MulTo

func MulTo(dst, s, t []float64) []float64

MulTo performs element-wise multiplication between s and t and stores the value in dst. It panics if the argument lengths do not match.

func NearestIdx

func NearestIdx(s []float64, v float64) int

NearestIdx returns the index of the element in s whose value is nearest to v. If several such elements exist, the lowest index is returned. It panics if s is zero length.

func NearestIdxForSpan

func NearestIdxForSpan(n int, l, u float64, v float64) int

NearestIdxForSpan return the index of a hypothetical vector created by Span with length n and bounds l and u whose value is closest to v. That is, NearestIdxForSpan(n, l, u, v) is equivalent to Nearest(Span(make([]float64, n),l,u),v) without an allocation. It panics if n is less than two.

func Norm

func Norm(s []float64, L float64) float64

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 Prod

func Prod(s []float64) float64

Prod returns the product of the elements of the slice. Returns 1 if len(s) = 0.

func Reverse

func Reverse(s []float64)

Reverse reverses the order of elements in the slice.

func Same

func Same(s, t []float64) bool

Same returns true when the input slices have the same length and all elements have the same value with NaN treated as the same.

func Scale

func Scale(c float64, dst []float64)

Scale multiplies every element in dst by the scalar c.

func ScaleTo

func ScaleTo(dst []float64, c float64, s []float64) []float64

ScaleTo multiplies the elements in s by c and stores the result in dst. It panics if the slice argument lengths do not match.

func Span

func Span(dst []float64, l, u float64) []float64

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. It panics if the length of dst is less than 2.

Span 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. It panics if the argument lengths do not match.

func SubTo

func SubTo(dst, s, t []float64) []float64

SubTo subtracts, element-wise, the elements of t from s and stores the result in dst. It panics if the argument lengths do not match.

func Sum

func Sum(s []float64) float64

Sum returns the sum of the elements of the slice.

func SumCompensated

func SumCompensated(s []float64) float64

SumCompensated returns the sum of the elements of the slice calculated with greater accuracy than Sum at the expense of additional computation.

func Within

func Within(s []float64, v float64) int

Within returns the first index i where s[i] <= v < s[i+1]. Within panics if:

  • len(s) < 2
  • s is not sorted

Types

This section is empty.

Directories

Path Synopsis
Package scalar provides a set of helper routines for dealing with float64 values.
Package scalar provides a set of helper routines for dealing with float64 values.

Jump to

Keyboard shortcuts

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