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.
Importing
Section titled “Importing”from std::core::math use { sum, mean, std, variance, correlation, covariance, percentile, median }Basic Statistics
Section titled “Basic Statistics”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.0Compute the arithmetic mean of an array.
let avg = mean([10.0, 20.0, 30.0]) // 20.0Compute 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])variance
Section titled “variance”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])Advanced Statistics
Section titled “Advanced Statistics”correlation
Section titled “correlation”Compute the Pearson correlation coefficient between two arrays.
let r = correlation(prices, volumes)print(f"Correlation: {r}")covariance
Section titled “covariance”Compute the covariance between two arrays.
let cov = covariance(series_a, series_b)percentile
Section titled “percentile”Return the p-th percentile of an array.
let p95 = percentile(latencies, 95.0)let p50 = percentile(latencies, 50.0) // same as medianmedian
Section titled “median”Return the median (50th percentile) of an array.
let mid = median([3.0, 1.0, 4.0, 1.0, 5.0]) // 3.0Derived Functions
Section titled “Derived Functions”coefficient_of_variation
Section titled “coefficient_of_variation”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)spread
Section titled “spread”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.0zscore
Section titled “zscore”Standardize an array into z-scores: (x - mean) / std.
from std::core::math use { zscore }
let z = zscore([2.0, 4.0, 6.0])Parallel Operations
Section titled “Parallel Operations”parallel_map
Section titled “parallel_map”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)parallel_filter
Section titled “parallel_filter”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)Function Reference
Section titled “Function Reference”| Function | Signature | Description |
|---|---|---|
sum(series) | (Vec<number>) -> number | Sum of values |
mean(series) | (Vec<number>) -> number | Arithmetic mean |
std(series) | (Vec<number>) -> number | Standard deviation |
variance(series) | (Vec<number>) -> number | Variance |
correlation(a, b) | (Vec<number>, Vec<number>) -> number | Pearson correlation |
covariance(a, b) | (Vec<number>, Vec<number>) -> number | Covariance |
percentile(series, p) | (Vec<number>, number) -> number | p-th percentile |
median(series) | (Vec<number>) -> number | Median value |
coefficient_of_variation(series) | (Vec<number>) -> number? | CV (std/mean) |
spread(series) | (Vec<number>) -> number | max - 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 |
See Also
Section titled “See Also”- Standard Library: Random — random number generation
- Objects and Arrays — array methods (
map,filter,reduce, etc.)