numerical

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2022 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultGSSIters = 64

Variables

This section is empty.

Functions

func GSS

func GSS(min, max float64, iters int, f func(float64) float64) float64

GSS performs golden section search to find the minimum of a unimodal function, given correct bounds on the minimum.

If iters is 0, DefaultGSSIters is used.

Types

type KMeans added in v0.3.3

type KMeans struct {
	// Centers is the K-means cluster centers.
	Centers []Vec

	// Data stores all of the data points.
	Data []Vec
}

KMeans stores the (intermediate) results of K-means clustering.

This type can be created using NewKMeans() to create an initial clustering using K-means++. Then, Iterate() can be called repeatedly to refine the clustering as desired.

func NewKMeans added in v0.3.3

func NewKMeans(data []Vec, numCenters int) *KMeans

NewKMeans creates a KMeans object with K-means++ initialization.

func (*KMeans) Assign added in v0.3.3

func (k *KMeans) Assign(vecs []Vec) []int

Assign gets the closest center index for every vector.

func (*KMeans) Iterate added in v0.3.3

func (k *KMeans) Iterate() float64

Iterate performs a step of k-means and returns the current MSE loss. If the MSE loss does not decrease, then the process has converged.

type Polynomial

type Polynomial []float64

A Polynomial is an equation of the form

a0 + a1*x + a2*x^2 + a3*x^3 + ...

Here, the polynomial is represented as an array of [a0, a1, ...].

func (Polynomial) Add

func (p Polynomial) Add(p1 Polynomial) Polynomial

Add returns the sum of p and p1.

func (Polynomial) Derivative

func (p Polynomial) Derivative() Polynomial

Derivative computes the derivative of the polynomial.

func (Polynomial) Eval

func (p Polynomial) Eval(x float64) float64

Eval evaluates the polynomial at the given x value.

func (Polynomial) IterRealRoots

func (p Polynomial) IterRealRoots(f func(x float64) bool)

IterRealRoots iterates over the real roots of p. This is similar to RealRoots(), but allows the caller to stop iteration early be returning false.

func (Polynomial) Mul

func (p Polynomial) Mul(p1 Polynomial) Polynomial

Mul computes the product of two polynomials.

func (Polynomial) RealRoots

func (p Polynomial) RealRoots() []float64

RealRoots computes the real roots of p, i.e. values of X such that p(x) = 0. The result may have duplicates since roots can be repeated.

If the polynomial has an infinite number of roots, one NaN root is returned.

func (Polynomial) Scale added in v0.3.0

func (p Polynomial) Scale(c float64) Polynomial

Scale returns a new polynomial with the coefficients of p multiplied by c.

func (Polynomial) String

func (p Polynomial) String() string

String returns a string representation of p.

type SparseCholesky

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

SparseCholesky is a sparse LU decomposition of a symmetric matrix.

Once instantiated, this object can be used to quickly apply the inverse of a matrix to vectors.

func NewSparseCholesky

func NewSparseCholesky(mat *SparseMatrix) *SparseCholesky

func (*SparseCholesky) ApplyInverseVec3

func (a *SparseCholesky) ApplyInverseVec3(x []Vec3) []Vec3

ApplyInverseVec3 computes (A^-1*x, A^-1*y, A^-1*z).

func (*SparseCholesky) ApplyVec3

func (a *SparseCholesky) ApplyVec3(x []Vec3) []Vec3

ApplyVec3 computes (A*x, A*y, A*z).

type SparseMatrix

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

A SparseMatrix is a square matrix where entries can be set to non-zero values in any order, and not all entries must be set.

func NewSparseMatrix

func NewSparseMatrix(size int) *SparseMatrix

func (*SparseMatrix) ApplyVec3

func (a *SparseMatrix) ApplyVec3(x []Vec3) []Vec3

ApplyVec3 computes (A*x, A*y, A*z).

func (*SparseMatrix) Iterate

func (a *SparseMatrix) Iterate(row int, f func(col int, x float64))

Iterate loops through the non-zero entries in a row.

func (*SparseMatrix) Permute

func (a *SparseMatrix) Permute(perm []int) *SparseMatrix

Permute permutes the rows and columns by perm, where perm is the result of applying the permutation to the list [0...n-1].

func (*SparseMatrix) RCM

func (a *SparseMatrix) RCM() []int

RCM computes the reverse Cuthill-McKee permutation for the matrix.

func (*SparseMatrix) Set

func (a *SparseMatrix) Set(row, col int, x float64)

Set adds an entry to the matrix.

The entry should not already be set.

type Vec

type Vec []float64

Vec is a vector of arbitrary dimension.

func (Vec) Add

func (v Vec) Add(v1 Vec) Vec

Add returns v + v1.

func (Vec) Dist added in v0.3.3

func (v Vec) Dist(v1 Vec) float64

Dist computes ||v-v1||.

func (Vec) DistSquared added in v0.3.3

func (v Vec) DistSquared(v1 Vec) float64

DistSquared computes ||v-v1||^2.

func (Vec) Dot

func (v Vec) Dot(v1 Vec) float64

Dot returns the dot product of v and v1.

func (Vec) Norm

func (v Vec) Norm() float64

Norm returns ||v||.

func (Vec) NormSquared

func (v Vec) NormSquared() float64

NormSquared returns ||v||^2.

func (Vec) Normalize

func (v Vec) Normalize() Vec

Normalize returns v/||v||.

func (Vec) ProjectOut

func (v Vec) ProjectOut(v1 Vec) Vec

ProjectOut projects the direction v1 out of v.

func (Vec) Scale

func (v Vec) Scale(s float64) Vec

Scale returns v * s.

func (Vec) Sub

func (v Vec) Sub(v1 Vec) Vec

Sub returns v - v1.

type Vec3

type Vec3 [3]float64

A Vec3 is a 3-dimensional tuple of floats.

func NewVec3RandomNormal

func NewVec3RandomNormal() Vec3

NewVec3RandomNormal creates a random normal vector.

func (Vec3) Add

func (v Vec3) Add(v1 Vec3) Vec3

Add returns v + v1.

func (Vec3) Dist

func (v Vec3) Dist(v1 Vec3) float64

Dist gets the Euclidean distance ||v - v1||.

func (Vec3) Scale

func (v Vec3) Scale(f float64) Vec3

Scale returns v * f.

func (Vec3) Sub

func (v Vec3) Sub(v1 Vec3) Vec3

Sub returns v - v1.

func (Vec3) Sum

func (v Vec3) Sum() float64

Sum gets the sum of the components.

Jump to

Keyboard shortcuts

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