Documentation
¶
Overview ¶
Package mathutil provides various mathematical utility functions.
Index ¶
- func Abs[T constraints.SignedReal](x T) T
- func Add[T constraints.Number](x, y T) T
- func Clamp[T cmp.Ordered](x, min, max T) T
- func ClampedLerp[T constraints.Float](x, y, t, min, max T) T
- func Damp[T constraints.Float](x, y, lambda, dt T) T
- func Deg2Rad[T constraints.Float](deg T) T
- func Div[T constraints.Number](x, y T) T
- func EuclideanModulo[T constraints.Float](x, y T) T
- func Identity[T constraints.Number](x T) T
- func InverseLerp[T constraints.Field](x, y, value T) T
- func IsPowerOfTwo[T constraints.Integer](value T) bool
- func IsZero[T constraints.SignedReal](x T) T
- func Lerp[T constraints.Field](x, y, t T) T
- func MapLinear[T constraints.Field](x, a1, a2, b1, b2 T) T
- func Mul[T constraints.Number](x, y T) T
- func One[T constraints.Number](T) T
- func PingPong[T constraints.Float](x, length T) T
- func Pow[T constraints.Real](x, y T) T
- func Predict[T constraints.Integer | constraints.Float](ok bool) T
- func Rad2Deg[T constraints.Float](rad T) T
- func Sigmoid[T constraints.Real](x T) T
- func SigmoidPrime[T constraints.Real](x T) T
- func Sign[T constraints.SignedReal](x T) T
- func SmoothStep[T constraints.Float](x, min, max T) T
- func SmoothStepFunc[T constraints.Float](x, min, max T, fn func(T) T) T
- func Square[T constraints.Number](x T) T
- func Sub[T constraints.Number](x, y T) T
- func UpperPow2(n int) int
- func Zero[T constraints.Number](T) T
- type BinaryFn
- type UnaryFn
- func Affine[T constraints.Number](k, b T) UnaryFn[T]
- func Constant[T constraints.Number](c T) UnaryFn[T]
- func KSigmoid[T constraints.Real](k T) UnaryFn[T]
- func KSigmoidPrime[T constraints.Real](k T) UnaryFn[T]
- func Offset[T constraints.Number](b T) UnaryFn[T]
- func Power[T constraints.Real](p T) UnaryFn[T]
- func Scale[T constraints.Number](k T) UnaryFn[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClampedLerp ¶
func ClampedLerp[T constraints.Float](x, y, t, min, max T) T
ClampedLerp performs a linear interpolation and clamps the result.
func Damp ¶
func Damp[T constraints.Float](x, y, lambda, dt T) T
Damp performs frame rate independent damping.
func EuclideanModulo ¶
func EuclideanModulo[T constraints.Float](x, y T) T
EuclideanModulo computes the Euclidean modulo of x % y.
func InverseLerp ¶
func InverseLerp[T constraints.Field](x, y, value T) T
InverseLerp calculates the inverse of linear interpolation.
func IsPowerOfTwo ¶
func IsPowerOfTwo[T constraints.Integer](value T) bool
IsPowerOfTwo checks if the given value is a power of two.
func IsZero ¶
func IsZero[T constraints.SignedReal](x T) T
IsZero returns 1 if the input is zero, otherwise 0.
func Lerp ¶
func Lerp[T constraints.Field](x, y, t T) T
Lerp performs linear interpolation between x and y based on t.
func MapLinear ¶
func MapLinear[T constraints.Field](x, a1, a2, b1, b2 T) T
MapLinear performs linear mapping from range [a1, a2] to range [b1, b2].
func PingPong ¶
func PingPong[T constraints.Float](x, length T) T
PingPong calculates a value that ping-pongs between 0 and length.
func Predict ¶
func Predict[T constraints.Integer | constraints.Float](ok bool) T
Predict returns 1 if ok is true, otherwise 0.
func Sigmoid ¶
func Sigmoid[T constraints.Real](x T) T
Sigmoid applies the sigmoid function to the input.
func SigmoidPrime ¶
func SigmoidPrime[T constraints.Real](x T) T
SigmoidPrime applies the derivative of the sigmoid function to the input.
func Sign ¶
func Sign[T constraints.SignedReal](x T) T
Sign returns the sign of the input (-1, 0, or 1).
func SmoothStep ¶
func SmoothStep[T constraints.Float](x, min, max T) T
SmoothStep performs smooth interpolation between min and max.
func SmoothStepFunc ¶
func SmoothStepFunc[T constraints.Float](x, min, max T, fn func(T) T) T
SmoothStepFunc applies a custom function to the smoothstep interpolation.
Types ¶
type BinaryFn ¶
type BinaryFn[T constraints.Number] func(x, y T) T
BinaryFn represents a binary function.
type UnaryFn ¶
type UnaryFn[T constraints.Number] func(T) T
UnaryFn represents a unary function.
func Affine ¶
func Affine[T constraints.Number](k, b T) UnaryFn[T]
Affine returns a UnaryFn that applies an affine transformation (kx + b).
func Constant ¶
func Constant[T constraints.Number](c T) UnaryFn[T]
Constant returns a UnaryFn that always returns c.
func KSigmoid ¶
func KSigmoid[T constraints.Real](k T) UnaryFn[T]
KSigmoid returns a UnaryFn that applies a sigmoid function with slope k.
func KSigmoidPrime ¶
func KSigmoidPrime[T constraints.Real](k T) UnaryFn[T]
KSigmoidPrime returns a UnaryFn that applies the derivative of a sigmoid function with slope k.
func Offset ¶
func Offset[T constraints.Number](b T) UnaryFn[T]
Offset returns a UnaryFn that adds b to its input.
func Power ¶
func Power[T constraints.Real](p T) UnaryFn[T]
Power returns a UnaryFn that raises its input to the power of p.
func Scale ¶
func Scale[T constraints.Number](k T) UnaryFn[T]
Scale returns a UnaryFn that scales its input by k.