A library for evaluating math expressions via the shunting-yard algorithm. Handles operators, functions, variables, and random variables.
use shunting::{ShuntingParser, MathContext};
let input = "sin(0.2)^2 + cos(0.2)^2";
let expr = ShuntingParser::parse_str(input).unwrap();
let result = MathContext::new().eval(&expr).unwrap();
println!("{} = {}", expr, result);MathContext keeps state across multiple evaluations. Built-in variables: pi, e.
let ctx = MathContext::new();
// Set a variable
let expr = ShuntingParser::parse_str("a = sin(0.2)^2 + cos(0.2)^2").unwrap();
ctx.eval(&expr).unwrap();
let result = ctx.eval(&ShuntingParser::parse_str("a").unwrap()).unwrap();
assert_eq!(result, 1.0);Use compile to build expressions with random variables:
let expr = ShuntingParser::parse_str("normal(0, 1) + 5").unwrap();
let rv = MathContext::new().compile(&expr).unwrap();
println!("{}", rv.eval());Sample a random variable and build a histogram:
let expr = ShuntingParser::parse_str("normal(0, 1)").unwrap();
let rv = MathContext::new().compile(&expr).unwrap();
let hist = rv.histogram::<10>(1000);
println!("{:?}", hist);The MathOp enum represents a value in the expression system:
MathOp::Number(f64)— a constant valueMathOp::RandVar— a random variable (e.g.normal(0, 1))MathOp::Dynamic— a lazily-evaluated expression (e.g.a + bwhereaorbis a variable)
| Function | Description |
|---|---|
sin, cos |
Trigonometric |
abs |
Absolute value |
log |
Base-10 log |
ln |
Natural log |
atan2 |
Arc tangent of two arguments |
max, min |
Min/max of all arguments |
nCr |
Combinations |
nPr |
Permutations |
nMCr |
Multicombinations |
nMPr |
n^r (ordered with replacement) |
rand |
Random value in [0, arg) |
normal(μ, σ) |
Normal distribution |
uniform(a, b) |
Uniform distribution |
lognormal(μ, σ) |
Log-normal distribution |
Binary: +, -, *, /, %, ^ (or **)
Unary: - (negation), ! (factorial via gamma)
The math REPL is available via the toxtools workspace crate:
$ cargo run -p toxtools --bin shunting
>> 4!
24
>> a = sin(0.2)^2 + cos(0.2)^2
>> a
1
>> (-3)!
NaN
>> (84 % (5/2)) !
1.32934
>> pi * 2.1^2 / cbrt(-(6+3))
-6.660512