Numeric helpers (math)
General numeric helpers and numerical calculus (BOSL2 math.scad).
-
pybosl2.math.lerp(a, b, t)[source]
Linearly interpolate between a and b by fraction t (scalar or vector).
- Parameters:
- a : float | Sequence[float] | ndarray
-
- b : float | Sequence[float] | ndarray
-
- t : float
-
- Return type:
float | ndarray
-
pybosl2.math.lerpn(a, b, sides, endpoint=
True)[source]
Return sides points linearly interpolated between a and b, as an (sides, dim) ndarray.
(or a length-sides 1-D ndarray for scalar a/b).
If endpoint is True, the last returned point equals b; otherwise the
range is divided into sides equal steps without reaching b.
- Parameters:
- a : float | Sequence[float] | ndarray
-
- b : float | Sequence[float] | ndarray
-
- sides : int
-
- endpoint : bool
-
- Return type:
ndarray
-
pybosl2.math.deriv(data, height=
1, closed=False)[source]
Numeric first-derivative estimate of data (scalar- or vector-valued points), as an ndarray.
Uses a symmetric derivative approximation for internal points and a
two-point method at the endpoints of an open path. If height is a list it
is treated as the (possibly non-uniform) per-segment sampling distance.
- Parameters:
- data : Sequence[float] | Sequence[Sequence[float]] | ndarray
-
- height : float | Sequence[float] | ndarray
-
- closed : bool
-
- Return type:
ndarray
-
pybosl2.math.deriv2(data, height=
1, closed=False)[source]
Numeric second-derivative estimate of data (scalar- or vector-valued points), as an.
ndarray.
- Parameters:
- data : Sequence[float] | Sequence[Sequence[float]] | ndarray
-
- height : float
-
- closed : bool
-
- Return type:
ndarray
-
pybosl2.math.deriv3(data, height=
1, closed=False)[source]
Numeric third-derivative estimate of data (scalar- or vector-valued points), as an ndarray.
Requires at least 5 points.
- Parameters:
- data : Sequence[float] | Sequence[Sequence[float]] | ndarray
-
- height : float
-
- closed : bool
-
- Return type:
ndarray
-
pybosl2.math.slerp(a, b, t)[source]
Spherical linear interpolation between two 3-D vectors.
Interpolates between vectors a and b along the great-circle arc
on a unit sphere and returns a unit-length vector. Input vectors need
not be unit length.
- Parameters:
- a : Sequence[float]
First 3-D vector.
- b : Sequence[float]
Second 3-D vector.
- t : float
Interpolation fraction (0 returns a, 1 returns b).
- Returns:
A unit-length interpolated vector as a list of 3 floats.
- Raises:
ValueError – If either vector has zero length or the vectors are 180° apart.
- Return type:
list[float]
-
pybosl2.math.slerpn(a, b, n, endpoint=
True)[source]
Return n evenly-spaced unit vectors on the great-circle arc between a and b.
- Parameters:
- a : Sequence[float]
First 3-D vector (need not be unit length).
- b : Sequence[float]
Second 3-D vector (need not be unit length).
- n : int
Number of points to return.
- endpoint : bool
If True the last point equals unit(b); otherwise it is one step short.
- Returns:
A list of n unit-length vectors as lists of 3 floats.
- Raises:
ValueError – If either vector has zero length or the vectors are 180° apart.
- Return type:
list[list[float]]
-
pybosl2.math.modang(x)[source]
Normalize an angle in degrees to the range [-180, 180).
- Parameters:
- x : float
An angle in degrees.
- Returns:
The equivalent angle in [-180, 180).
- Return type:
float
-
pybosl2.math.constrain(v, minval=
None, maxval=None)[source]
Clamp v to the range [minval, maxval].
If either bound is None, that side is unconstrained.
- Parameters:
- v : float
The value to constrain.
- minval : float | None
Lower bound, or None for no lower constraint.
- maxval : float | None
Upper bound, or None for no upper constraint.
- Returns:
The constrained value.
- Return type:
float
-
pybosl2.math.quant(v, unit)[source]
Quantize v to the nearest integer multiple of unit.
- Parameters:
- v : float
The value to quantize.
- unit : float
The positive quantum to quantize to.
- Returns:
The quantized value.
- Raises:
ValueError – If unit is not positive.
- Return type:
float
-
pybosl2.math.mean(v)[source]
Arithmetic mean of the elements in v.
- Parameters:
- v : Sequence[float]
A non-empty sequence of numeric values.
- Returns:
The mean value.
- Raises:
ValueError – If v is empty.
- Return type:
float