maths

package
v0.0.0-...-98f3f52 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTol     = 1e-12
	DefaultMaxIter = 10000
)

Variables

View Source
var (
	ErrNoSolution        = errors.New("no solution")
	ErrInfiniteSolutions = errors.New("infinite solutions")
	ErrInvalidInput      = errors.New("invalid input")
	ErrNonSquareSystem   = errors.New("linear system must be square")
	ErrSingularMatrix    = errors.New("singular matrix")
	ErrNotConverged      = errors.New("not converged")
	ErrDegreeTooHigh     = errors.New("polynomial degree too high")
	ErrZeroPolynomial    = errors.New("zero polynomial")
)

Functions

func AbsCosTheta

func AbsCosTheta(d Direction) float64

func AddVec

func AddVec(res, a, b *mat.VecDense) *mat.VecDense

func AddVecs

func AddVecs(res *mat.VecDense, vecs ...*mat.VecDense) *mat.VecDense

func CalculateStrideForTensor

func CalculateStrideForTensor(shape []int) ([]int, int)

func Clamp

func Clamp(value, minimum, maximum float64) float64

func ClampUnit

func ClampUnit(value float64) float64

func CosTheta

func CosTheta(d Direction) float64

func CosineHemisphereIntegral

func CosineHemisphereIntegral(dim int) float64

func CosineHemispherePDF

func CosineHemispherePDF(w Direction) float64

func Cross

func Cross(res, u, v *mat.VecDense) *mat.VecDense

func Cross2

func Cross2(u, v *mat.VecDense) *mat.VecDense

func Cross4

func Cross4(u, v, w *mat.VecDense) *mat.VecDense

func Distance

func Distance(a, b *mat.VecDense) float64

func DurandKerner

func DurandKerner(coeffs []complex128, tol float64, maxIter int) ([]complex128, error)

func FormatVec

func FormatVec(v *mat.VecDense) string

FormatVec 格式化向量为可读字符串

func GramSchmidt

func GramSchmidt(v ...*mat.VecDense) []*mat.VecDense

GramSchmidt Perform Gram Schmidt orthogonalization on any number of vectors

func IdentityTransform4

func IdentityTransform4() [4][4]float64

func IsFinite

func IsFinite(value float64) bool

func IsUpperHemisphere

func IsUpperHemisphere(d Direction) bool

func Lerp

func Lerp(minimum, maximum, t float64) float64

func MatrixToSlice

func MatrixToSlice(m *mat.Dense) [][]float64

将单个 mat.Dense 矩阵转换为二维 float64 切片

func MaxVec

func MaxVec(a, b *mat.VecDense) *mat.VecDense

func MinVec

func MinVec(a, b *mat.VecDense) *mat.VecDense

func MulVec

func MulVec(res *mat.VecDense, a *mat.Dense, b *mat.VecDense) *mat.VecDense

func NewtonRaphson

func NewtonRaphson(
	f func([]float64) []float64,
	x0 []float64,
	options *NewtonOptions,
) ([]float64, error)

NewtonRaphson solves a nonlinear square system:

f(x) = 0

f must return the same number of equations as len(x0).

func Normalize

func Normalize(v *mat.VecDense) *mat.VecDense

func NumericalJacobian

func NumericalJacobian(
	f func([]float64) []float64,
	x []float64,
	eps float64,
) ([][]float64, error)

NumericalJacobian computes the central-difference numerical Jacobian.

It returns an m*n matrix, where:

m = len(f(x))
n = len(x)

func PositiveMod

func PositiveMod(value, period float64) float64

func Project

func Project(v, u *mat.VecDense) *mat.VecDense

project 计算向量投影: proj_u(v) = (v·u / u·u) * u

func RealRoots

func RealRoots(roots []complex128, tol float64) []float64

RealRoots filters approximately real roots from complex roots.

func SameHemisphere

func SameHemisphere(a, b Direction) bool

func ScaleVec

func ScaleVec(res *mat.VecDense, s float64, v *mat.VecDense) *mat.VecDense

func ScaleVec2

func ScaleVec2(s float64, v *mat.VecDense) *mat.VecDense

func SignChanged

func SignChanged(a, b float64) bool

func SolveCubicEquationAnalytical

func SolveCubicEquationAnalytical(a, b, c, d float64) ([]complex128, error)

SolveCubicEquationAnalytical solves:

a*x^3 + b*x^2 + c*x + d = 0

It returns all complex roots, including repeated roots.

func SolveCubicEquationReal

func SolveCubicEquationReal(a, b, c, d float64) ([]float64, error)

SolveCubicEquationReal solves:

a*x^3 + b*x^2 + c*x + d = 0

It returns only real roots.

func SolveLinearEquation

func SolveLinearEquation(a, b float64) ([]float64, error)

SolveLinearEquation solves:

a*x + b = 0

Return values:

  • one root: []float64{x}
  • no solution: ErrNoSolution
  • infinite solutions: ErrInfiniteSolutions

func SolveLinearEquationAnalytical

func SolveLinearEquationAnalytical(a, b float64) ([]complex128, error)

SolveLinearEquationAnalytical solves:

a*x + b = 0

func SolveLinearSystem

func SolveLinearSystem(A [][]float64, b []float64) ([]float64, error)

SolveLinearSystem solves:

A*x = b

using Gaussian elimination with partial pivoting.

func SolvePolynomial

func SolvePolynomial(coeffs []complex128) ([]complex128, error)

SolvePolynomial solves a polynomial with complex coefficients.

Coefficients must be ordered from highest degree to constant term:

a_n*x^n + a_{n-1}*x^{n-1} + ... + a_1*x + a_0

Example:

x^3 - 1 = 0

should be passed as:

[]complex128{1, 0, 0, -1}

For degree 1 and 2, it uses direct formulas. For degree 3 and above, it uses the Durand-Kerner method.

func SolvePolynomialReal

func SolvePolynomialReal(coeffs []float64) ([]float64, error)

SolvePolynomialReal solves a real-coefficient polynomial and returns its real roots.

Coefficients must be ordered from highest degree to constant term:

coeffs = []float64{a_n, a_{n-1}, ..., a_1, a_0}

Supported degree:

0: no roots
1: linear
2: quadratic
3: cubic
4: quartic

For degree > 4, this function falls back to numeric real-root isolation.

func SolvePolynomialRealNumeric

func SolvePolynomialRealNumeric(coeffs []float64) ([]float64, error)

SolvePolynomialRealNumeric solves real roots for arbitrary-degree real polynomials.

Coefficients must be ordered from highest degree to constant term:

coeffs = []float64{a_n, a_{n-1}, ..., a_1, a_0}

This function returns only real roots.

Method:

  • recursively solve roots of the derivative
  • split the real line into monotonic intervals
  • use bisection where sign changes occur
  • check derivative critical points to catch even-multiplicity roots

func SolveQuadraticEquationAnalytical

func SolveQuadraticEquationAnalytical(a, b, c float64) ([]complex128, error)

SolveQuadraticEquationAnalytical solves:

a*x^2 + b*x + c = 0

It returns all complex roots, including repeated roots.

func SolveQuadraticEquationReal

func SolveQuadraticEquationReal(a, b, c float64) ([]float64, error)

SolveQuadraticEquationReal solves:

a*x^2 + b*x + c = 0

It returns only real roots.

func SolveQuarticEquationAnalytical

func SolveQuarticEquationAnalytical(a4, a3, a2, a1, a0 float64) ([]complex128, error)

SolveQuarticEquationAnalytical solves:

a4*x^4 + a3*x^3 + a2*x^2 + a1*x + a0 = 0

using Ferrari's closed-form method.

It returns all complex roots, including repeated roots.

func SolveQuarticEquationReal

func SolveQuarticEquationReal(a4, a3, a2, a1, a0 float64) ([]float64, error)

SolveQuarticEquationReal solves:

a4*x^4 + a3*x^3 + a2*x^2 + a1*x + a0 = 0

It returns only real roots.

This implementation uses derivative critical points to split the real line into monotonic intervals, then applies bisection on intervals with sign changes. It also checks critical points directly so even-multiplicity roots are not missed.

func SquaredDistance

func SquaredDistance(a, b *mat.VecDense) float64

func SubVec

func SubVec(res, a, b *mat.VecDense) *mat.VecDense

Types

type Direction

type Direction []float64

func CosineSampleHemisphere

func CosineSampleHemisphere(sample Sample2D) Direction

func CosineSampleHemisphereND

func CosineSampleHemisphereND(sample Sample2D, dim int) Direction

func NewDirection

func NewDirection(x, y, z float64) Direction

func NewDirectionFromComponents

func NewDirectionFromComponents(values []float64) Direction

func UniformHemisphereDirection

func UniformHemisphereDirection(i, n int) Direction

func (Direction) Add

func (d Direction) Add(other Direction) Direction

func (Direction) Component

func (d Direction) Component(i int) float64

func (Direction) Dot

func (d Direction) Dot(other Direction) float64

func (Direction) IsFinite

func (d Direction) IsFinite() bool

func (Direction) Len

func (d Direction) Len() int

func (Direction) Length

func (d Direction) Length() float64

func (Direction) MulScalar

func (d Direction) MulScalar(v float64) Direction

func (Direction) Normalize

func (d Direction) Normalize() Direction

type Frame

type Frame struct {
	Geometry  geometry.Geometry
	Point     *mat.VecDense
	Tangent   *mat.VecDense
	Bitangent *mat.VecDense
	Normal    *mat.VecDense
	Tangents  []*mat.VecDense
}

func NewFrameFromNormal

func NewFrameFromNormal(normal *mat.VecDense) (Frame, bool)

func NewFrameFromNormalInGeometry

func NewFrameFromNormalInGeometry(g geometry.Geometry, p, n *mat.VecDense) (Frame, bool)

NewFrameFromNormalInGeometry builds a metric-orthonormal surface frame at p. n must be an intrinsic tangent-space normal vector (an ambient gradient should first be converted with Geometry.IntrinsicNormal).

func (Frame) LocalToWorld

func (f Frame) LocalToWorld(v Direction) *mat.VecDense

func (Frame) LocalToWorldInto

func (f Frame) LocalToWorldInto(res *mat.VecDense, v Direction)

func (Frame) WorldToLocal

func (f Frame) WorldToLocal(v *mat.VecDense) Direction

func (Frame) WorldToLocalNegated

func (f Frame) WorldToLocalNegated(v *mat.VecDense) Direction

type NewtonOptions

type NewtonOptions struct {
	Tol         float64
	MaxIter     int
	JacobianEps float64
	Damping     bool
}

NewtonOptions controls Newton-Raphson behavior.

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 |
		~complex64 | ~complex128
}

type Sample2D

type Sample2D struct {
	U float64
	V float64
}

type SparseTensor

type SparseTensor[T Number] struct {
	Shape   []int                  `json:"shape"`
	Format  SparseTensorFormat     `json:"format"`
	Default T                      `json:"default"`
	Entries []SparseTensorEntry[T] `json:"entries"`
	// contains filtered or unexported fields
}

func NewSparseTensor

func NewSparseTensor[T Number](shape []int, format SparseTensorFormat) *SparseTensor[T]

func NewSparseTensorFromEntries

func NewSparseTensorFromEntries[T Number](
	shape []int,
	format SparseTensorFormat,
	entries []SparseTensorEntry[T],
) (*SparseTensor[T], error)

func (*SparseTensor[T]) Add

func (t *SparseTensor[T]) Add(other *SparseTensor[T]) (*SparseTensor[T], error)

func (*SparseTensor[T]) Get

func (t *SparseTensor[T]) Get(index []int) (T, error)

func (*SparseTensor[T]) IterNonZero

func (t *SparseTensor[T]) IterNonZero(fn func(index []int, value T))

func (*SparseTensor[T]) MustGet

func (t *SparseTensor[T]) MustGet(index []int) T

func (*SparseTensor[T]) NNZ

func (t *SparseTensor[T]) NNZ() int

func (*SparseTensor[T]) ScalarMul

func (t *SparseTensor[T]) ScalarMul(scalar T) *SparseTensor[T]

func (*SparseTensor[T]) Set

func (t *SparseTensor[T]) Set(index []int, value T) error

func (*SparseTensor[T]) ToCOO

func (t *SparseTensor[T]) ToCOO() *SparseTensor[T]

func (*SparseTensor[T]) ToHash

func (t *SparseTensor[T]) ToHash() *SparseTensor[T]

type SparseTensorEntry

type SparseTensorEntry[T Number] struct {
	Index []int `json:"index"`
	Value T     `json:"value"`
}

type SparseTensorFormat

type SparseTensorFormat string
const (
	SparseTensorCOO   SparseTensorFormat = "coo"
	SparseTensorHash  SparseTensorFormat = "hash"
	SparseTensorCSR   SparseTensorFormat = "csr"
	SparseTensorCSC   SparseTensorFormat = "csc"
	SparseTensorBlock SparseTensorFormat = "block"
)

type Tensor

type Tensor[T Number] struct {
	Data   []T   `json:"data"`
	Shape  []int `json:"shape"`
	Stride []int `json:"stride"`
	Offset int   `json:"offset"`
}

func NewTensor

func NewTensor[T Number](shape []int) *Tensor[T]

func NewTensorFromSlice

func NewTensorFromSlice[T Number](data []T, shape []int) *Tensor[T]

func (*Tensor[T]) Add

func (t *Tensor[T]) Add(a, b *Tensor[T]) *Tensor[T]

func (*Tensor[T]) CoordinateToIndex

func (t *Tensor[T]) CoordinateToIndex(coordinate ...int) int

func (*Tensor[T]) Get

func (t *Tensor[T]) Get(coordinate ...int) T

func (*Tensor[T]) GetCoordinates

func (t *Tensor[T]) GetCoordinates(i int) []int

func (*Tensor[T]) Reshape

func (t *Tensor[T]) Reshape(newShape []int) *Tensor[T]

func (*Tensor[T]) ScalarMul

func (t *Tensor[T]) ScalarMul(scalar T) *Tensor[T]

func (*Tensor[T]) Set

func (t *Tensor[T]) Set(value T, coordinate ...int)

func (*Tensor[T]) Sub

func (t *Tensor[T]) Sub(a, b *Tensor[T]) *Tensor[T]

Directories

Path Synopsis
Package geometry defines the abstract space in which rays propagate.
Package geometry defines the abstract space in which rays propagate.

Jump to

Keyboard shortcuts

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