Skip to content

Standard Library: Math

The std::core::math module provides optimized mathematical and statistical functions. Many operations use SIMD-accelerated intrinsics for high performance on large arrays.

from std::core::math use { sum, mean, std, variance, correlation, covariance, percentile, median }

Compute the sum of all values in an array.

from std::core::math use { sum }
let total = sum([1.0, 2.0, 3.0, 4.0])
print(total) // 10.0

Compute the arithmetic mean of an array.

let avg = mean([10.0, 20.0, 30.0]) // 20.0

Compute the standard deviation of an array.

let s = std([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])

Compute the variance of an array.

let v = variance([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])

Compute the Pearson correlation coefficient between two arrays.

let r = correlation(prices, volumes)
print(f"Correlation: {r}")

Compute the covariance between two arrays.

let cov = covariance(series_a, series_b)

Return the p-th percentile of an array.

let p95 = percentile(latencies, 95.0)
let p50 = percentile(latencies, 50.0) // same as median

Return the median (50th percentile) of an array.

let mid = median([3.0, 1.0, 4.0, 1.0, 5.0]) // 3.0

Return std(series) / mean(series). Returns None when the mean is zero.

from std::core::math use { coefficient_of_variation }
let cv = coefficient_of_variation(returns)

Return the difference between the maximum and minimum values.

from std::core::math use { spread }
let range = spread([1.0, 5.0, 3.0]) // 4.0

Standardize an array into z-scores: (x - mean) / std.

from std::core::math use { zscore }
let z = zscore([2.0, 4.0, 6.0])

Map a function across an array. For arrays larger than 1000 elements, uses a parallel intrinsic path for better performance.

from std::core::math use { parallel_map }
let doubled = parallel_map(large_array, |x| x * 2)

Filter an array with a predicate. For arrays larger than 1000 elements, uses a parallel intrinsic path.

from std::core::math use { parallel_filter }
let positives = parallel_filter(large_array, |x| x > 0.0)
FunctionSignatureDescription
sum(series)(Vec<number>) -> numberSum of values
mean(series)(Vec<number>) -> numberArithmetic mean
std(series)(Vec<number>) -> numberStandard deviation
variance(series)(Vec<number>) -> numberVariance
correlation(a, b)(Vec<number>, Vec<number>) -> numberPearson correlation
covariance(a, b)(Vec<number>, Vec<number>) -> numberCovariance
percentile(series, p)(Vec<number>, number) -> numberp-th percentile
median(series)(Vec<number>) -> numberMedian value
coefficient_of_variation(series)(Vec<number>) -> number?CV (std/mean)
spread(series)(Vec<number>) -> numbermax - min
zscore(series)(Vec<number>) -> Vec<number>Z-score normalization
parallel_map(arr, fn)(Vec<T>, (T) => U) -> Vec<U>Parallel map
parallel_filter(arr, fn)(Vec<T>, (T) => bool) -> Vec<T>Parallel filter