ms1

package
v0.0.0-...-cc41fca Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: BSD-3-Clause Imports: 2 Imported by: 5

Documentation

Overview

package ms1 implements basic 1D math useful for 3D graphics applications. Functions in this package have their OpenGL equivalent which is usually of the same name.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp

func Clamp(v, Min, Max float32) float32

Clamp returns value v clamped between Min and Max.

func EqualWithinAbs

func EqualWithinAbs(a, b, tol float32) bool

EqualWithinAbs checks if a and b are within tol of eachother.

func GridSubdomain

func GridSubdomain(domMin, domMax float32, nGridPoints int, subMin, subMax float32) (iStart, nSub int)

GridSubdomain returns the indexing for a 1D subdomain contained within a larger gridded domain of equally spaced nGridPoints points.

  • iStart contains the starting index of the first point contained within the subdomain.
  • nSub is the number of consecutive grid points contained within the subdomain.

This function is primarily used for higher dimension GridSubdomain calls in 2D and 3D packages

func Interp

func Interp(x, y, a float32) float32

Interp performs the linear interpolation between x and y, mapping with a in interval [0,1]. This function is known as "mix" in OpenGL.

func InterpWrap

func InterpWrap(wrapAt, x, y, a float32) float32

InterpWrap returns the linear interpolation between x and y on the cyclic domain [0,wrapAt].

  • x and y define interpolation domain and must be contained within cyclic domain [0,wrapAt]
  • a is the interpolation parameter and must be contained within [0,1]. a=0 returns x, a=1 returns y.
  • wrapAt defines the period of the cyclic domain, specifically the point at which the domain wraps back to 0. Typically 2*math.Pi radians, 360 degrees or 1 for unit phases. wrapAt must be positive.

If above rules are followed the result is guaranteed to lie in [x,y] which in turn is contained in [0,wrapAt].

func Sign

func Sign(x float32) float32

Sign returns -1, 0, or 1 for negative, zero or positive x argument, respectively, just like OpenGL's "sign" function.

func SmoothStep

func SmoothStep(edge0, edge1, x float32) float32

SmoothStep performs smooth cubic hermite interpolation between 0 and 1 when edge0 < x < edge1.

Types

type NewtonRaphsonSolver

type NewtonRaphsonSolver struct {
	// MaxIterations specifies how many iterations of Newton's succesive
	// approximations to perform. Each iteration evaluates function 3 times. Parameter is required.
	MaxIterations int
	// Tolerance sets the criteria for ending the root search when f(x)/f'(x) <= Tolerance.
	Tolerance float32
	// Dx is the step with which the gradient is calculated with central-finite-differences.
	Dx float32

	// Relaxation is optional parameter to avoid overshooting during gradient descent for ill conditioned functions, i.e: large gradient near root.
	Relaxation float32
	// AdaptiveDxMaxIterations sets maximum amount of changes to step (Dx) throughout root search when encountering numerical issues.
	// If not set then not used.
	AdaptiveDxMaxIterations int
	// RootLims clamps search for root to x_min=RootLims[0], x_max=RootLims[1].
	RootLims [2]float32
}

NewtonRaphsonSolver implements Newton-Raphson root finding algorithm for an arbitrary function.

func DefaultNewtonRaphsonSolver

func DefaultNewtonRaphsonSolver() NewtonRaphsonSolver

DefaultNewtonRaphsonSolver returns a NewtonRaphsonSolver with recommended parameters.

func (NewtonRaphsonSolver) Root

func (nra NewtonRaphsonSolver) Root(x0 float32, f func(xGuess float32) float32) (x_root float32, convergedIn int)

Root solves for a root of f such that f(x)=0 by starting guessing at x0 solving using Newton-Raphson method. Root returns the first root found and the amount of interations before converging.

If the convergence parameter returned is negative a solution was not found within the desired tolerance.

Jump to

Keyboard shortcuts

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