Library
Module
Module type
Parameter
Class
Class type
Various operations on floats (e.g. approximate equality, linear interpolation), and basic arbitrary size 2d matrix operations.
clamp ~min ~max v
Clamp the float v
between min
and max
.
lerpn ?endpoint a b n
Linearly interpolate n
values between a
and b
. If endpoint
is true
, the last value will be equal to b
, otherwise, it will be about a + (b - a) * (1 - 1 / n)
.
quant ~q v
Quantize v
to a multiple of the quantum size q
. For example:
quant ~q:0.2 1.5 = 1.6
quant_down ~q v
Quantize v
to a multiple of the quantum size q
, always rounding down. For example:
quant_down ~q:0.2 1.75 = 1.6
quant_up ~q v
Quantize v
to a multiple of the quantum size q
, always rounding up. For example:
quant_up ~q:0.2 1.51 = 1.6
approx ?eps a b
Return true
if a
is within the tolerance eps
of b
.
posmod a m
Compute the positive modulo m
of a
. The resulting value will be in the range of 0
to m - 1
.
law_of_cosines a b c
Apply the Law of Cosines for a triangle with side lengths a
, b
, and c
. The angle in radians of the corner opposite of the third side c
is returned.
mat_dims m
Return the dimensions ((n_rows, n_cols)
of the 2d matrix m
. An Invalid_argument
exception is raised if the row lengths are inconsistent.
matmul a b
Matrix multiplication of a
and b
. Invalid_argument
is raised if the inner dimensions do not match, or if the rows of either matrix are ragged.
transpose m
Transpose rows and columns of the 2d matrix m
.
real_roots ?eps ?tol p
Compute the real roots of the real polynomial p
. The polynomial is specified as [|a_n; ...; a_1; a_0|]
where a_n
is the x ** n
coefficient.
eps
is used to determine if the imaginary parts of the roots are zerotol
is the tolerance for the complex polynomial root finderAdapted from the real_roots
function found in the BOSL2 math module.
val bisection :
?max_iter:int ->
?tolerance:float ->
lower:float ->
upper:float ->
(float -> float) ->
float
bisection ?max_iter ?tolerance ~lower ~upper f
Perform a bisection search for the value that minimizes the function f
between the bounds lower
and upper
. x
is returned either when f x = 0.
, or the bound range falls below tolerance
. Raises Failure
if the iteration count reaches max_iter
(default = 100
).