mathutil

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPolyRegArraysSameLength is a common error.
	ErrPolyRegArraysSameLength = errors.New("polynomial array inputs must be the same length")
)

Functions

func AbsInt

func AbsInt(value int) int

AbsInt returns the absolute value of an int.

func CirclePoint

func CirclePoint(cx, cy int, radius, thetaRadians float64) (x, y int)

CirclePoint returns the absolute position of a circle diameter point given by the radius and the theta.

func DegreesAdd

func DegreesAdd(baseDegrees, deltaDegrees float64) float64

DegreesAdd adds a delta to a base in radians.

func DegreesToCompass

func DegreesToCompass(deg float64) float64

DegreesToCompass returns the degree value in compass / clock orientation.

func DegreesToRadians

func DegreesToRadians(degrees float64) float64

DegreesToRadians returns degrees as radians.

func MaxInt

func MaxInt(values ...int) int

MaxInt returns the maximum int.

func Mean

func Mean(values ...float64) float64

Mean returns the mean of a set of values

func MeanInt

func MeanInt(values ...int) int

MeanInt returns the mean of a set of integer values.

func MinInt

func MinInt(values ...int) int

MinInt returns the minimum int.

func MinMax

func MinMax(values ...float64) (min, max float64)

MinMax returns the minimum and maximum of a given set of values.

func Normalize

func Normalize(values ...float64) []float64

Normalize translates the input set of numbers in the [0,1] interval. The total may be < 1.0. There are going to be issues with irrational numbers. E.g: 4,3,2,1 => 0.4, 0.3, 0.2, 0.1

func PercentDifference

func PercentDifference(v1, v2 float64) float64

PercentDifference computes the percentage difference between two values. The formula is (v2-v1)/v1.

func PercentToRadians

func PercentToRadians(pct float64) float64

PercentToRadians converts a normalized value (0,1) to radians.

func PolyRegression

func PolyRegression(xvalues, yvalues []float64, degree int) ([]float64, error)

PolyRegression returns the polynomial regression of a given degree over the given values.

func RadiansAdd

func RadiansAdd(base, delta float64) float64

RadiansAdd adds a delta to a base in radians.

func RadiansToDegrees

func RadiansToDegrees(value float64) float64

RadiansToDegrees translates a radian value to a degree value.

func RotateCoordinate

func RotateCoordinate(cx, cy, x, y int, thetaRadians float64) (rx, ry int)

RotateCoordinate rotates a coordinate around a given center by a theta in radians.

func RoundDown

func RoundDown(value, roundTo float64) float64

RoundDown rounds down to a given roundTo value.

func RoundPlaces

func RoundPlaces(input float64, places int) (rounded float64)

RoundPlaces rounds an input to a given places.

func RoundTo

func RoundTo(delta float64) float64

RoundTo returns a `round to` value for a given `delta`.

func RoundUp

func RoundUp(value, roundTo float64) float64

RoundUp rounds up to a given roundTo value.

func Sum

func Sum(values ...float64) float64

Sum sums a set of values.

func SumInt

func SumInt(values ...int) int

SumInt sums a set of values.

Types

type Matrix

type Matrix struct {
	// contains filtered or unexported fields
}

Matrix represents a 2d dense array of floats.

func EyeMatrix

func EyeMatrix(n int) *Matrix

EyeMatrix returns the eye matrix.

func IdentityMatrix

func IdentityMatrix(order int) *Matrix

IdentityMatrix returns the identity matrix of a given order.

func NewMatrix

func NewMatrix(rows, cols int, values ...float64) *Matrix

NewMatrix returns a new matrix.

func NewMatrixFromArrays

func NewMatrixFromArrays(a [][]float64) *Matrix

NewMatrixFromArrays creates a matrix from a jagged array set.

func OnesMatrix

func OnesMatrix(rows, cols int) *Matrix

OnesMatrix returns an matrix of ones.

func ZeroMatrix

func ZeroMatrix(rows, cols int) *Matrix

ZeroMatrix returns a matrix of a given size zeroed.

func (*Matrix) Arrays

func (m *Matrix) Arrays() [][]float64

Arrays returns the matrix as a two dimensional jagged array.

func (*Matrix) Augment

func (m *Matrix) Augment(m2 *Matrix) (*Matrix, error)

Augment concatenates two matrices about the horizontal.

func (*Matrix) Col

func (m *Matrix) Col(col int) Vector

Col returns a column of the matrix as a vector.

func (*Matrix) Copy

func (m *Matrix) Copy() *Matrix

Copy returns a duplicate of a given matrix.

func (*Matrix) Diagonal

func (m *Matrix) Diagonal() *Matrix

Diagonal returns a matrix from the diagonal of a matrix.

func (*Matrix) DiagonalVector

func (m *Matrix) DiagonalVector() Vector

DiagonalVector returns a vector from the diagonal of a matrix.

func (*Matrix) Each

func (m *Matrix) Each(action func(row, col int, value float64))

Each applies the action to each element of the matrix in rows => cols order.

func (*Matrix) Epsilon

func (m *Matrix) Epsilon() float64

Epsilon returns the maximum precision for math operations.

func (*Matrix) Equals

func (m *Matrix) Equals(other *Matrix) bool

Equals returns if a matrix equals another matrix.

func (*Matrix) Get

func (m *Matrix) Get(row, col int) float64

Get returns the element at the given row, col.

func (*Matrix) Inverse

func (m *Matrix) Inverse() (*Matrix, error)

Inverse returns a matrix such that M*I==1.

func (*Matrix) IsSquare

func (m *Matrix) IsSquare() bool

IsSquare returns if the row count is equal to the column count.

func (*Matrix) IsSymmetric

func (m *Matrix) IsSymmetric() bool

IsSymmetric returns if the matrix is symmetric about its diagonal.

func (*Matrix) L

func (m *Matrix) L() *Matrix

L returns the matrix with zeros below the diagonal.

func (*Matrix) LU

func (m *Matrix) LU() (l, u, p *Matrix)

LU performs the LU decomposition.

func (*Matrix) Multiply

func (m *Matrix) Multiply(m2 *Matrix) (m3 *Matrix, err error)

Multiply multiplies two matrices.

func (*Matrix) Pivotize

func (m *Matrix) Pivotize() *Matrix

Pivotize does something i'm not sure what.

func (*Matrix) QR

func (m *Matrix) QR() (q, r *Matrix)

QR performs the qr decomposition.

func (*Matrix) Round

func (m *Matrix) Round() *Matrix

Round rounds all the values in a matrix to its epsilon, returning a reference to the original.

func (*Matrix) Row

func (m *Matrix) Row(row int) Vector

Row returns a row of the matrix as a vector.

func (*Matrix) ScaleRow

func (m *Matrix) ScaleRow(row int, scale float64)

ScaleRow applies a scale to an entire row.

func (*Matrix) Set

func (m *Matrix) Set(row, col int, val float64)

Set sets a value.

func (*Matrix) Size

func (m *Matrix) Size() (rows, cols int)

Size returns the dimensions of the matrix.

func (*Matrix) String

func (m *Matrix) String() string

String returns a string representation of the matrix.

func (*Matrix) SubMatrix

func (m *Matrix) SubMatrix(i, j, rows, cols int) *Matrix

SubMatrix returns a sub matrix from a given outer matrix.

func (*Matrix) SwapRows

func (m *Matrix) SwapRows(i, j int)

SwapRows swaps a row in the matrix in place.

func (*Matrix) Times

func (m *Matrix) Times(m2 *Matrix) (*Matrix, error)

Times returns the product of a matrix and another.

func (*Matrix) Transpose

func (m *Matrix) Transpose() *Matrix

Transpose flips a matrix about its diagonal, returning a new copy.

func (*Matrix) U

func (m *Matrix) U() *Matrix

U returns the matrix with zeros above the diagonal. Does not include the diagonal.

func (*Matrix) WithEpsilon

func (m *Matrix) WithEpsilon(epsilon float64) *Matrix

WithEpsilon sets the epsilon on the matrix and returns a reference to the matrix.

type Vector

type Vector []float64

Vector is just an array of values.

func (Vector) DotProduct

func (v Vector) DotProduct(v2 Vector) (result float64, err error)

DotProduct returns the dot product of two vectors.

Jump to

Keyboard shortcuts

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