Monocurl
Download

std.math

math.mcl

Reference for the symbols exported by math.mcl.

Index

Constants

Basis Vectors

Scalar Functions

Combinatorics & Integer Utilities

Statistics

Vector Operations

Interpolation

Constants

constant

Euler's number.

let E = 2.7182818284590452
constant

Pi in radians.

let PI = 3.1415926535897932
constant

Full circle constant, equal to 2 * PI.

let TAU = 6.2831853071795864
let wave = |x| sin(x * TAU)
constant

Origin point [0, 0, 0].

let ORIGIN = [0, 0, 0]
constant

Zero vector [0, 0, 0].

let ZERO_V = [0, 0, 0]
constant

Vector with all components equal to 1.

let ONE_V = [1, 1, 1]
constant

Unit vector pointing left in scene coordinates.

let LEFT = 1l
constant

Unit vector pointing right in scene coordinates.

let RIGHT = 1r
mesh dot = center{RIGHT * 2} Circle(0.2)
constant

Unit vector pointing up in scene coordinates.

let UP = 1u
constant

Unit vector pointing down in scene coordinates.

let DOWN = 1d
constant

Unit vector pointing forward; with the default camera this is negative z.

let FORWARD = 1f
constant

Unit vector pointing backward; with the default camera this is positive z, from the origin toward the camera.

let BACKWARD = 1b

Basis Vectors

constant

X-axis basis vector, equal to RIGHT.

let X_HAT = RIGHT
constant

Y-axis basis vector, equal to UP.

let Y_HAT = UP
constant

Z-axis basis vector, equal to BACKWARD.

let Z_HAT = BACKWARD

Scalar Functions

function

Square root.

let sqrt = |x| ...
function

Cube root.

let cbrt = |x| ...
function

Exponential function e^x.

let exp = |x| ...
function

Natural logarithm.

let ln = |x| ...
function

Logarithm with optional base, defaulting to E.

let log = |x, base = E| ln(x) / ln(base)
function

Base-2 logarithm.

let log2 = |x| log(x, 2)
function

Base-10 logarithm.

let log10 = |x| log(x, 10)
function

Sine of an angle in radians.

let sin = |x| ...
function

Cosine of an angle in radians.

let cos = |x| ...
function

Tangent of an angle in radians.

let tan = |x| ...
function

Cotangent of an angle in radians.

let cot = |x| 1 / tan(x)
function

Secant of an angle in radians.

let sec = |x| 1 / cos(x)
function

Cosecant of an angle in radians.

let csc = |x| 1 / sin(x)
function

Inverse sine, returning radians.

let arcsin = |x| ...
function

Inverse cosine, returning radians.

let arccos = |x| ...
function

Inverse tangent, returning radians.

let arctan = |x| ...
function

Two-argument inverse tangent, returning radians.

let arctan2 = |y, x| ...
function

Hyperbolic sine.

let sinh = |x| ...
function

Hyperbolic cosine.

let cosh = |x| ...
function

Hyperbolic tangent.

let tanh = |x| ...
function

Absolute value.

let abs = |x| ...
function

Sign of a number: -1, 0, or 1.

let sign = |x| ...
function

Largest integer not greater than x.

let floor = |x| ...
function

Smallest integer not less than x.

let ceil = |x| ...
function

Round to the nearest integer.

let round = |x| ...
function

Truncate fractional part.

let trunc = |x| ...
function

Euclidean remainder of n divided by m.

The result has the sign of the divisor, so mod(-1, 4) returns 3.

let mod = |n, m| ...
function

Pairwise minimum of two numbers.

let min = |a, b| ...
function

Pairwise maximum of two numbers.

let max = |a, b| ...
function

Pin x to the inclusive range [low, high].

let clamp = |low, x, high| min(high, max(low, x))
let opacity = clamp(0, raw_opacity, 1)
function

Rectified linear unit, max(0, x).

let relu = |x| max(0, x)
function

Logistic sigmoid.

let sigmoid = |x| 1 / (1 + exp(-x))

Combinatorics & Integer Utilities

importantfunction

Deterministic scene random float in [low, high].

Random helpers advance executor state and are only allowed in the root frame, not inside lambdas or default arguments. Re-running the same scene uses the same deterministic sequence.

let random = |low = 0, high = 1| ...
let jitter = random(-0.1, 0.1)
importantfunction

Deterministic scene random integer in [low, high).

high is exclusive and must be greater than low. Like random, this can only be called from the root frame.

let randint = |low, high| ...
function

Factorial of a non-negative integer.

let factorial = |n| ...
function

Number of ways to choose r items from n.

Returns 0 when r < 0 or r > n.

let choose = |n, r| ...
function

Number of ordered arrangements of r items from n.

Returns 0 when r < 0 or r > n.

let permute = |n, r| ...
function

Greatest common divisor.

let gcd = |n, m| ...
function

Least common multiple.

let lcm = |n, m| ...
function

Test whether n is prime.

let is_prime = |n| ...
function

A random-looking float in [0, 1) that depends only on its arguments.

Unlike random, this is a pure function: the same seed and salt always give the same value, and it can be used anywhere, including inside lambdas evaluated per vertex, pixel or particle. Pass an index as the seed and a different salt for each quantity you need.

let seeded_random = |seed, salt = 0| mod(sin(seed * 12.9898 + salt * 78.233) * 43758.5453, 1)
let Star = |i| {
    let u = seeded_random(i)
    let v = seeded_random(i, 1)
    ...
}

Statistics

function

Mean of a numeric list.

let mean = |v| ...
function

Variance of a numeric list.

Population variance: divides by len(v), not len(v) - 1.

let variance = |v| ...
function

Standard deviation of a numeric list.

let std_dev = |v| sqrt(variance(v))

Vector Operations

function

Dot product of two vectors.

let dot = |u, v| ...
function

Cross product of two 3-D vectors.

let cross = |u, v| ...
function

Euclidean length of a vector.

let norm = |v| sqrt(dot(v, v))
function

Unit vector in the same direction as v.

Does not guard against the zero vector.

let normalize = |v| v * (1 / norm(v))
function

Projection of vector u onto vector v.

let proj = |u, v| v * (dot(u, v) / dot(v, v))
function

Angle between two vectors, in radians.

let angle_between = |u, v| arccos(dot(u, v) / (norm(u) * norm(v)))

Interpolation

importantfunction

Interpolate between values according to Monocurl's value interpolation rules.

Rules are applied in order: equal values return the first value unchanged; numbers and complex numbers blend linearly; invoked operators are matched by operator-stack depth, with deeper chains peeled through their operator embed; matching invoked operators must use the same operator and labels, then lerp the operand and labeled arguments; matching live functions must call the same lambda with the same labels, then lerp labeled arguments and rerun the function; lists require equal length and maps require equal keys, then lerp elementwise. Differing unlabeled call arguments and unrelated non-numeric values are interpolation errors.

let lerp = |a, b, t| ...
let midway = lerp(LEFT, RIGHT, 0.5)
let purple = lerp(BLUE, MAGENTA, 0.5)
importantfunction

Piecewise interpolation through keyed values.

Keyframes must be a map from numeric times to values. Entries are sorted by numeric key, and values outside the first/last keyframe clamp to the endpoint value.

let keyframe_lerp = |keyframes, t| ...
keyframesmap of t -> value pairs
tnormalized lookup point
let col = keyframe_lerp([0 -> BLUE, 0.5 -> CYAN, 1 -> WHITE], phase)
function

Remap x linearly from one interval into another.

let map_range = |x, in_min, in_max, out_min, out_max| out_min + (x - in_min) / (in_max - in_min) * (out_max - out_min)
let opacity = map_range(frame, 0, 30, 0, 1)
function

Convert degrees to radians.

Numeric literals can use the dg suffix; use this for runtime values.

let deg_to_rad = |x| x * (PI / 180)
function

Convert radians to degrees.

let rad_to_deg = |x| x * (180 / PI)