Documentation
¶
Overview ¶
Package gnum provides generic numerical functions.
Index ¶
- func Abs[N constraints.Signed](n N) N
- func Add[S ~[]N, N Number](a S, b ...S) S
- func Add1[S ~[]N, N Number](a S, m N) S
- func ArgMax[S ~[]E, E constraints.Ordered](s S) int
- func ArgMin[S ~[]E, E constraints.Ordered](s S) int
- func Cast[S ~[]N, T ~[]M, N Number, M Number](a S) T
- func Copy[S ~[]N, N any](a S) S
- func Corr[S ~[]N, N Number](a, b S) float64
- func Cov[S ~[]N, N Number](a, b S) float64
- func Diff[N Number](a, b N) N
- func Dot[S ~[]N, N Number](a, b S) N
- func Entropy[S ~[]N, N Number](a S) float64
- func ExpMean[S ~[]N, N Number](a S) float64
- func Idiv[T constraints.Integer](a, b T) T
- func L1[S ~[]N, N Number](a, b S) N
- func L2[S ~[]N, N Number](a, b S) float64
- func LogFactorial(n int) float64
- func Lp[S ~[]N, N Number](p int) func(S, S) float64
- func Max[S ~[]N, N constraints.Ordered](s S) N
- func Mean[S ~[]N, N Number](a S) float64
- func Min[S ~[]N, N constraints.Ordered](s S) N
- func Mul[S ~[]N, N Number](a S, b ...S) S
- func Mul1[S ~[]N, N Number](a S, m N) S
- func NQuantiles[T any](a []T, n int) []T
- func Norm[S ~[]N, N constraints.Float](a S) float64
- func Ones[S ~[]N, N Number](n int) S
- func Quantiles[T any](a []T, qq ...float64) []T
- func Std[S ~[]N, N Number](a S) float64
- func Sub[S ~[]N, N Number](a S, b ...S) S
- func Sub1[S ~[]N, N Number](a S, m N) S
- func Sum[S ~[]N, N Number](a S) N
- func Var[S ~[]N, N Number](a S) float64
- type Number
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
func Abs[N constraints.Signed](n N) N
Abs returns the absolute value of n.
For floats use math.Abs.
func Add ¶
func Add[S ~[]N, N Number](a S, b ...S) S
Add adds b to a and returns a. b is unchanged. If a is nil, creates a new vector.
func ArgMax ¶ added in v0.5.0
func ArgMax[S ~[]E, E constraints.Ordered](s S) int
ArgMax returns the index of the maximal value in the slice or -1 if the slice is empty.
func ArgMin ¶ added in v0.5.0
func ArgMin[S ~[]E, E constraints.Ordered](s S) int
ArgMin returns the index of the minimal value in the slice or -1 if the slice is empty.
func Entropy ¶
Entropy returns the Shannon-entropy of a. The elements in a don't have to sum up to 1.
func ExpMean ¶ added in v1.3.0
ExpMean returns the exponential average of the slice. Non-positive values result in NaN.
func Idiv ¶ added in v0.5.0
func Idiv[T constraints.Integer](a, b T) T
Idiv divides a by b, rounded to the nearest integer.
func L1 ¶
func L1[S ~[]N, N Number](a, b S) N
L1 returns the L1 (Manhattan) distance between a and b. Equivalent to Lp(1) but returns the same type.
func LogFactorial ¶ added in v1.3.0
LogFactorial returns an approximation of log(n!), calculated in constant time.
func Lp ¶
Lp returns an Lp distance function. Lp is calculated as follows:
Lp(v) = (sum_i(v[i]^p))^(1/p)
func Max ¶
func Max[S ~[]N, N constraints.Ordered](s S) N
Max returns the maximal value in the slice or zero if the slice is empty.
func Min ¶
func Min[S ~[]N, N constraints.Ordered](s S) N
Min returns the maximal value in the slice or zero if the slice is empty.
func Mul ¶
func Mul[S ~[]N, N Number](a S, b ...S) S
Mul multiplies a by b and returns a. b is unchanged. If a is nil, creates a new vector.
func Mul1 ¶
func Mul1[S ~[]N, N Number](a S, m N) S
Mul1 multiplies the values of a by m and returns a.
func NQuantiles ¶ added in v1.3.0
NQuantiles returns the elements that divide the given slice into n equal parts (up to rounding), including the first and last elements.
For example, for n=2 it returns the first element, the middle, and the last element.
func Norm ¶
func Norm[S ~[]N, N constraints.Float](a S) float64
Norm returns the L2 norm of the vector.
func Quantiles ¶ added in v1.3.0
Quantiles returns the elements that divide the given slice at the given ratios.
For example, 0.5 returns the middle element, 0.25 returns the element at a quarter of the length, etc. 0 and 1 return the first and last element, respectively.
Types ¶
type Number ¶
type Number interface {
constraints.Float | constraints.Integer
}
Number is a constraint that contains comparable numbers.