num32

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

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

View Source
const (
	NoTranspose = MatrixTranspose(blas.NoTrans)
	Transpose   = MatrixTranspose(blas.Trans)
)

Variables

This section is empty.

Functions

func Abs

func Abs(x float32) float32

Abs returns the absolute value of x.

Special cases are:

Abs(±Inf) = +Inf
Abs(NaN) = NaN

func Add

func Add(dst []float32, s []float32)

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

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

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

func Dot(s []float32, t []float32) float32

Dot returns the dot product of t1 and t2, also called the inner product. It panics if the argument lengths do not match.

func IsNaN

func IsNaN(v float32) bool

IsNaN reports whether f is an IEEE 754 “not-a-number” value.

func L1Distance

func L1Distance(s []float32, t []float32) float32

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

func L2Distance(s, t []float32) float32

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

func L2SquaredDistance(s, t []float32) float32

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 Max

func Max(s []float32) float32

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

func MaxIdx

func MaxIdx(s []float32) 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 []float32) float32

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

func MinIdx

func MinIdx(s []float32) 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 []float32, s []float32)

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

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

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 Norm

func Norm(t []float32) float32

Norm returns the L2 norm of t.

func Round

func Round(dst []float32, prec int)

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 Scale

func Scale(c float32, dst []float32)

Scale multiplies every element in dst by the scalar c.

func ScaleTo

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

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

func Sqrt(x float32) float32

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

func Sub(dst []float32, s []float32)

Sub subtracts, element-wise, the elements of s from dst. It panics if the argument lengths do not match.

func SubTo

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

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(x []float32) float32

Sum returns the sum of the elements of the given slice.

func Zero

func Zero(dst []float32)

Zero sets every element of the given slice to zero.

Types

type Matrix

type Matrix = blas32.General

Matrix represents an N x M matrix that stores float32 values in row-wise format.

func MakeRandomOrthoMatrix

func MakeRandomOrthoMatrix(rng *rand.Rand, dims int) Matrix

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.

Jump to

Keyboard shortcuts

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