gnum

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2023 License: MIT Imports: 3 Imported by: 4

Documentation

Overview

Package gnum provides generic numerical functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[N Number](n N) N

Abs returns the absolute value of n.

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 Add1

func Add1[S ~[]N, N Number](a S, m N) S

Add1 adds m to a and returns a.

func Cast

func Cast[S ~[]N, T ~[]M, N Number, M Number](a S) T

Cast casts the values of a and places them in a new slice.

func Copy

func Copy[S ~[]N, N any](a S) S

Copy returns a copy of the given slice.

func Corr

func Corr[S ~[]N, N Number](a, b S) float64

Corr returns the Pearson correlation between the a and b.

func Cov

func Cov[S ~[]N, N Number](a, b S) float64

Cov returns the covariance of a and b.

func Diff

func Diff[N Number](a, b N) N

Diff returns the non-negative difference between a and b.

func Dot

func Dot[S ~[]N, N Number](a, b S) N

Dot returns the dot product of the input vectors.

func Entropy

func Entropy[S ~[]N, N Number](a S) float64

Entropy returns the Shannon-entropy of a. The elements in a don't have to sum up to 1.

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 L2

func L2[S ~[]N, N Number](a, b S) float64

L2 returns the L2 (Euclidean) distance between a and b. Equivalent to Lp(2).

func Lp

func Lp[S ~[]N, N Number](p int) func(S, S) float64

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 Mean

func Mean[S ~[]N, N Number](a S) float64

Mean returns the average of the slice.

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 Norm

func Norm[S ~[]N, N constraints.Float](a S) float64

Norm returns the L2 norm of the vector.

func Ones

func Ones[S ~[]N, N Number](n int) S

Ones returns a slice of n ones. Panics if n is negative.

func Std

func Std[S ~[]N, N Number](a S) float64

Std returns the standard deviation of a.

func Sub

func Sub[S ~[]N, N Number](a S, b ...S) S

Sub subtracts b from a and returns a. b is unchanged. If a is nil, creates a new vector.

func Sub1

func Sub1[S ~[]N, N Number](a S, m N) S

Sub1 subtracts m from a and returns a.

func Sum

func Sum[S ~[]N, N Number](a S) N

Sum returns the sum of the slice.

func Var

func Var[S ~[]N, N Number](a S) float64

Var returns the variance of a.

Types

type Number

type Number interface {
	constraints.Float | constraints.Integer
}

Number is a constraint that contains comparable numbers.

Jump to

Keyboard shortcuts

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