Documentation
¶
Overview ¶
Package num32 contains basic numeric functions that operate on scalar, vector, and matrix float32 values. Inputs and outputs deliberately use simple float types so that they can be used in multiple contexts. It uses the gonum library when possible, since it offers assembly language implementations of various useful primitives.
Using the same convention as gonum, when a slice is being modified in place, it has the name dst and the function does not return a value. In addition, many of the functions have the same name and semantics as those in gonum.
Where possible, functions in this package are written with the assumption that the caller prevents bad input. They will panic with assertion errors if this is not the case, rather than returning error values. Callers should generally have panic recovery logic further up the stack to gracefully handle these assertions, as they indicate buggy code.
Index ¶
- Constants
- func Abs(x float32) float32
- func Add(dst []float32, s []float32)
- func AddTo(dst []float32, s []float32, t []float32) []float32
- func Dot(s []float32, t []float32) float32
- func IsNaN(v float32) bool
- func L1Distance(s []float32, t []float32) float32
- func L2Distance(s, t []float32) float32
- func L2SquaredDistance(s, t []float32) float32
- func Max(s []float32) float32
- func MaxIdx(s []float32) int
- func Min(s []float32) float32
- func MinIdx(s []float32) int
- func Mul(dst []float32, s []float32)
- func MulMatrixByVector(matrix *Matrix, s []float32, dst []float32, transpose MatrixTranspose) []float32
- func MulTo(dst []float32, s []float32, t []float32) []float32
- func Norm(t []float32) float32
- func Round(dst []float32, prec int)
- func Scale(c float32, dst []float32)
- func ScaleTo(dst []float32, c float32, s []float32) []float32
- func Sqrt(x float32) float32
- func Sub(dst []float32, s []float32)
- func SubTo(dst []float32, s []float32, t []float32) []float32
- func Sum(x []float32) float32
- func Zero(dst []float32)
- type Matrix
- type MatrixTranspose
Constants ¶
const ( NoTranspose = MatrixTranspose(blas.NoTrans) Transpose = MatrixTranspose(blas.Trans) )
Variables ¶
This section is empty.
Functions ¶
func Add ¶
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.
func AddTo ¶
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 Dot ¶
Dot returns the dot product of t1 and t2, also called the inner product. It panics if the argument lengths do not match.
func L1Distance ¶
L1Distance returns the L1 norm of s - t, which is the Manhattan distance between the two vectors. It panics if the argument lengths do not match.
func L2Distance ¶
L2Distance returns the L2 norm of s - t, which is the Euclidean distance between the two vectors. It panics if the argument lengths do not match.
func L2SquaredDistance ¶
L2SquaredDistance returns the squared L2 norm of s - t, which is the squared Euclidean distance between the two vectors. Comparing squared distance is equivalent to comparing distance, but the squared distance avoids an expensive square-root operation. It panics if the argument lengths do not match.
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. It panics if s is zero length.
func MinIdx ¶
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 ¶
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 MulMatrixByVector ¶
func MulMatrixByVector( matrix *Matrix, s []float32, dst []float32, transpose MatrixTranspose, ) []float32
MulMatrixByVector multiplies a matrix (or its transpose) by the "s" vector, stores the result in the "dst" vector, and returns "dst". The vectors are treated as column matrices:
matrix = rows x cols s = cols x 1 dst = rows x 1 (by rules of matrix multiplication)
Therefore, len(s) must equal matrix.Cols and len(dst) must equal matrix.Rows. If Transpose is specified, then the transposed matrix is multiplied by the vector. In that case, len(s) must equal matrix.Rows and len(dst) must equal matrix.Cols.
func MulTo ¶
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 Round ¶
Round sets every element of the "dst" slice to its rounded value, using gonum's scalar.Round function.
NB: Upcasting from float32 to float64 to perform the rounding can cause precision issues that affect the result.
func ScaleTo ¶
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 Sqrt ¶
Sqrt returns the square root of x.
Special cases are:
Sqrt(+Inf) = +Inf Sqrt(±0) = ±0 Sqrt(x < 0) = NaN Sqrt(NaN) = NaN
func Sub ¶
Sub subtracts, element-wise, the elements of s from dst. It panics if the argument lengths do not match.
Types ¶
type Matrix ¶
Matrix represents an N x M matrix that stores float32 values in row-wise format.
func MakeRandomOrthoMatrix ¶
MakeRandomOrthoMatrix returns a square N x N random orthogonal matrix. Rows and columns are orthonormal vectors - they have length 1 and any two rows or columns are perpendicular. Multiplying a vector by this matrix will randomly transform it while preserving its relative distance and angle to any other vector that's also multiplied by the same matrix.
type MatrixTranspose ¶
type MatrixTranspose byte
MatrixTranspose indicates whether a matrix operation should operate on the untransposed or transposed form of the matrix.