Skip to content

Calculus

Auto-generated from src/stdlib/calculus.mcrs — do not edit manually.

API


deriv_forward v1.3.0

Forward-difference derivative: (f(x+h) − f(x)) / h.

redscript
fn deriv_forward(f1: int, f0: int, h_fx: int): int

Parameters

ParameterDescription
f1f(x + h) × 10000
f0f(x) × 10000
h_fxStep size × 10000 (must not be zero)

Returns: df/dx × 10000; returns 0 if h_fx == 0


deriv_central v1.3.0

Central-difference derivative: (f(x+h) − f(x−h)) / (2h). More accurate than forward difference.

redscript
fn deriv_central(f_plus: int, f_minus: int, h_fx: int): int

Parameters

ParameterDescription
f_plusf(x + h) × 10000
f_minusf(x − h) × 10000
h_fxStep size × 10000 (must not be zero)

Returns: df/dx × 10000; returns 0 if h_fx == 0


second_deriv v1.3.0

Second derivative via the central finite-difference formula: (f(x+h) − 2f(x) + f(x−h)) / h².

redscript
fn second_deriv(f_plus: int, f0: int, f_minus: int, h_fx: int): int

Parameters

ParameterDescription
f_plusf(x + h) × 10000
f0f(x) × 10000
f_minusf(x − h) × 10000
h_fxStep size × 10000 (must not be zero)

Returns: d²f/dx² × 10000; returns 0 if h_fx == 0


integrate_trapezoid v1.3.0

Numerical integration using the trapezoidal rule.

redscript
fn integrate_trapezoid(vals: int[], n: int, h_fx: int): int

Parameters

ParameterDescription
valsArray of n function values × 10000 at equally spaced points
nNumber of sample points (must be ≥ 2)
h_fxStep width between samples × 10000

Returns: Approximate integral × 10000 (area under the curve)

Example

redscript
let ys: int[] = [0, 5000, 10000]; // f(0)=0, f(0.5)=0.5, f(1)=1  (linear)
let area: int = integrate_trapezoid(ys, 3, 5000); // ≈ 5000  (∫₀¹ x dx = 0.5)

integrate_simpson v1.3.0

Numerical integration using Simpson's 1/3 rule (more accurate than trapezoid for smooth functions). Uses n−1 intervals if n is even. Requires n ≥ 3.

redscript
fn integrate_simpson(vals: int[], n: int, h_fx: int): int

Parameters

ParameterDescription
valsArray of n function values × 10000 at equally spaced points
nNumber of sample points (should be odd and ≥ 3)
h_fxStep width × 10000

Returns: Approximate integral × 10000


riemann_left v1.3.0

Left Riemann sum: sum of f(x_i) × h for i = 0 … n−2.

redscript
fn riemann_left(vals: int[], n: int, h_fx: int): int

Parameters

ParameterDescription
valsArray of n function values × 10000
nNumber of sample points
h_fxStep width × 10000

Returns: Approximate integral × 10000


riemann_right v1.3.0

Right Riemann sum: sum of f(x_i) × h for i = 1 … n−1.

redscript
fn riemann_right(vals: int[], n: int, h_fx: int): int

Parameters

ParameterDescription
valsArray of n function values × 10000
nNumber of sample points
h_fxStep width × 10000

Returns: Approximate integral × 10000


riemann_mid v1.3.0

Midpoint Riemann sum using precomputed midpoint values.

redscript
fn riemann_mid(vals: int[], n: int, h_fx: int): int

Parameters

ParameterDescription
valsArray of n midpoint values × 10000 (one per interval)
nNumber of intervals (i.e. length of vals)
h_fxStep width × 10000

Returns: Approximate integral × 10000


curve_length_2d v1.3.0

Approximate arc length of a 2-D polyline through n points using the Euclidean distance between consecutive points.

redscript
fn curve_length_2d(xs: int[], ys: int[], n: int): int

Parameters

ParameterDescription
xsX coordinates × 10000 of the n points
ysY coordinates × 10000 of the n points
nNumber of points (must be ≥ 2)

Returns: Approximate arc length × 10000


running_mean v1.3.0

Welford's online algorithm: update a running mean with a new sample.

redscript
fn running_mean(prev_mean: int, new_val: int, n: int): int

Parameters

ParameterDescription
prev_meanPrevious mean × 10000
new_valNew sample value × 10000
nTotal count after adding the new sample (n ≥ 1)

Returns: Updated mean × 10000


running_m2 v1.3.0

Welford's online algorithm: update the M2 accumulator used to compute variance. Variance = running_m2 / (n − 1) after n samples.

redscript
fn running_m2(prev_m2: int, prev_mean: int, new_mean: int, new_val: int): int

Parameters

ParameterDescription
prev_m2Previous M2 accumulator × 10000
prev_meanMean before adding the new sample × 10000
new_meanMean after adding the new sample × 10000
new_valNew sample value × 10000

Returns: Updated M2 accumulator × 10000


variance_from_m2 v1.3.0

Compute sample variance from a Welford M2 accumulator and sample count.

redscript
fn variance_from_m2(m2: int, n: int): int

Parameters

ParameterDescription
m2M2 accumulator from running_m2 × 10000
nNumber of samples collected

Returns: Sample variance × 10000; returns 0 for n ≤ 1


std_dev_approx v1.3.0

Approximate standard deviation as the integer square root of the variance.

redscript
fn std_dev_approx(variance_fx: int): int

Parameters

ParameterDescription
variance_fxVariance × 10000 (e.g. from variance_from_m2)

Returns: √variance × 10000; returns 0 for non-positive input


Released under the MIT License.