mathr
A pure-Rust mathematics library and CLI for symbolic and numerical computation — FFT from scratch, calculus, equation solving, plotting, matrix operations, statistics, number theory, ODE solvers, Taylor series, interpolation, and special functions.
Quick Start
CLI Usage
Just give it a string — mathr figures out what to do:
|
TeX / LaTeX Input
mathr accepts LaTeX math formulas — with or without Markdown delimiters:
Supported delimiters: $...$, $$...$$, \[...\], \(...\), or raw TeX
with no delimiters at all.
Supported TeX commands: \frac, \sqrt, \pi, \tau, \infty, \cdot,
\times, \left(, \right), ^{...}, \sin, \cos, \tan, \arcsin,
\arccos, \arctan, \sinh, \cosh, \tanh, \exp, \ln, \log,
\log_2, \log_{10}, \Gamma, \operatorname{...}, \text{...}.
REPL
mathr> sin(pi/4)
0.7071067812
mathr> let x = 3
mathr> x^2 + 1
10
mathr> fn f(x) = x^2 + 2*x + 1
mathr> f(5)
36
mathr> diff sin(x^2)
2*x*cos(x^2)
mathr> taylor exp(x) 0 5
1 + x + 0.5*x^2 + 0.1666666667*x^3 + 0.0416666667*x^4
mathr> quit
Features
- Expression parsing & evaluation: Recursive-descent parser, implicit multiplication, scientific notation, constants (
pi,e,tau,inf), 27+ built-in functions - Symbolic differentiation: Product, quotient, chain rules with simplification
- Algebraic simplification: Constant folding, identity flattening
- Numerical calculus: 5-point stencil derivatives, trapezoidal/Simpson's/adaptive quadrature, partial derivatives, gradients
- Equation solving: Bisection, Newton–Raphson, secant, Durand–Kerner polynomial roots
- FFT from scratch: Cooley–Tukey radix-2 (forward, inverse, 2D, real-input), magnitude/power spectra, convolution, cross-correlation, window functions (Hann, Hamming, Blackman)
- Complex numbers: Generic
Complex<T>with arithmetic,abs,arg,exp,from_polar - Plotting: PNG output via
plotters(line, multi-series, scatter) - Matrix operations: Arithmetic, determinant, inverse, linear system solve, trace, transpose
- Statistics: Mean, median, variance, stddev, quartiles, IQR, correlation, linear regression
- Number theory: GCD, LCM, extended GCD, modular inverse, primality, factorization, sieve, binomial, factorial, Fibonacci, Euler's totient, Miller–Rabin, Chinese Remainder Theorem, modular exponentiation
- ODE solvers: Euler, RK4, RK4 systems, adaptive RKF45
- Taylor series: Symbolic Taylor expansion around any point
- Interpolation: Lagrange, Newton divided-difference, linear
- Special functions: Gamma, log-Gamma, Beta, erf, erfc, sinc, incomplete gamma P
- Expression equality: Canonical form comparison with commutative sorting and constant folding
- Interactive REPL: rustyline-powered with history, bracket matching, variable/function bindings
Crate API
use *;
// Evaluate
let val = eval_str?;
// Symbolic differentiation
let expr = parse?;
let deriv = differentiate?;
// FFT
let samples = vec!;
let mags = magnitude_spectrum?;
// Matrix
let m = from_rows?;
let det = m.determinant?;
let inv = m.inverse?;
// Statistics
let data = vec!;
let s = summary?;
// Number theory
let primes = sieve_primes;
let fib50 = fibonacci;
// ODE
let y = rk4?;
// Taylor series
let series = taylor_series_str?;
// Interpolation
let pts = vec!;
let y = lagrange_interp?;
// Special functions
let g = gamma; // √π
let e = erf;
// Miller–Rabin primality
let is_prime = is_prime_miller_rabin;
Dependencies
| Crate | Purpose |
|---|---|
clap |
CLI argument parsing |
anyhow |
Error handling in binary |
thiserror |
Error types in library |
rustyline |
REPL line editing |
plotters |
PNG plotting |
num-traits |
Numeric traits |
approx (dev) |
Float comparison in tests |
License
Apache-2.0