mathutil

package
v0.0.0-...-89aa834 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package mathutil provides various mathematical utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T constraints.SignedReal](x T) T

Abs returns the absolute value of x.

func Add

func Add[T constraints.Number](x, y T) T

Add returns the sum of x and y.

func Clamp

func Clamp[T cmp.Ordered](x, min, max T) T

Clamp restricts x to the range [min, max].

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 Deg2Rad

func Deg2Rad[T constraints.Float](deg T) T

Deg2Rad converts degrees to radians.

func Div

func Div[T constraints.Number](x, y T) T

Div returns the quotient of x and y.

func EuclideanModulo

func EuclideanModulo[T constraints.Float](x, y T) T

EuclideanModulo computes the Euclidean modulo of x % y.

func Identity

func Identity[T constraints.Number](x T) T

Identity returns its input unchanged.

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 Mul

func Mul[T constraints.Number](x, y T) T

Mul returns the product of x and y.

func One

func One[T constraints.Number](T) T

One always returns 1.

func PingPong

func PingPong[T constraints.Float](x, length T) T

PingPong calculates a value that ping-pongs between 0 and length.

func Pow

func Pow[T constraints.Real](x, y T) T

Pow returns x raised to the power of y.

func Predict

func Predict[T constraints.Integer | constraints.Float](ok bool) T

Predict returns 1 if ok is true, otherwise 0.

func Rad2Deg

func Rad2Deg[T constraints.Float](rad T) T

Rad2Deg converts radians to degrees.

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.

func Square

func Square[T constraints.Number](x T) T

Square returns the square of its input.

func Sub

func Sub[T constraints.Number](x, y T) T

Sub returns the difference of x and y.

func UpperPow2

func UpperPow2(n int) int

UpperPow2 returns the smallest power of 2 greater than or equal to n.

func Zero

func Zero[T constraints.Number](T) T

Zero always returns 0.

Types

type BinaryFn

type BinaryFn[T constraints.Number] func(x, y T) T

BinaryFn represents a binary function.

func (BinaryFn[T]) Add

func (f BinaryFn[T]) Add(f2 BinaryFn[T]) BinaryFn[T]

Add returns a new BinaryFn that adds the results of two BinaryFn.

func (BinaryFn[T]) Div

func (f BinaryFn[T]) Div(f2 BinaryFn[T]) BinaryFn[T]

Div returns a new BinaryFn that divides the result of f by f2.

func (BinaryFn[T]) Mul

func (f BinaryFn[T]) Mul(f2 BinaryFn[T]) BinaryFn[T]

Mul returns a new BinaryFn that multiplies the results of two BinaryFn.

func (BinaryFn[T]) Sub

func (f BinaryFn[T]) Sub(f2 BinaryFn[T]) BinaryFn[T]

Sub returns a new BinaryFn that subtracts the result of f2 from f.

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.

func (UnaryFn[T]) Add

func (f UnaryFn[T]) Add(f2 UnaryFn[T]) UnaryFn[T]

Add returns a new UnaryFn that adds the results of two UnaryFn.

func (UnaryFn[T]) Div

func (f UnaryFn[T]) Div(f2 UnaryFn[T]) UnaryFn[T]

Div returns a new UnaryFn that divides the result of f by f2.

func (UnaryFn[T]) Mul

func (f UnaryFn[T]) Mul(f2 UnaryFn[T]) UnaryFn[T]

Mul returns a new UnaryFn that multiplies the results of two UnaryFn.

func (UnaryFn[T]) Sub

func (f UnaryFn[T]) Sub(f2 UnaryFn[T]) UnaryFn[T]

Sub returns a new UnaryFn that subtracts the result of f2 from f.

Jump to

Keyboard shortcuts

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