mathr — Pure-Rust Math Library & CLI
A pure-Rust mathematics library and command-line tool for symbolic and numerical computation — built from scratch with zero external math dependencies.
Features
- Symbolic differentiation — product, quotient, and chain rules with automatic simplification
- Numerical integration — trapezoidal, Simpson's, and adaptive quadrature
- FFT (Fast Fourier Transform) — Cooley–Tukey radix-2 from scratch: forward, inverse, 2D, real-input, magnitude/power spectra, convolution, cross-correlation, window functions
- Equation solving — bisection, Newton–Raphson, secant method, Durand–Kerner polynomial root finding
- Matrix operations — arithmetic, determinant, inverse, linear system solver, trace, transpose
- Statistics — mean, median, variance, standard deviation, quartiles, IQR, correlation, linear regression
- Number theory — GCD, LCM, primality (trial + Miller–Rabin), factorization, sieve of Eratosthenes, binomial coefficients, factorial, Fibonacci, Euler's totient, Chinese Remainder Theorem, modular exponentiation
- ODE solvers — Euler, RK4, RK4 systems, adaptive RKF45
- Taylor series — symbolic expansion around any point
- Interpolation — Lagrange, Newton divided-difference, linear
- Special functions — Gamma, log-Gamma, Beta, erf, erfc, sinc, incomplete gamma P
- Complex numbers — generic
Complex<T>with arithmetic, polar conversion, powers - Plotting — PNG output via
plotters(line, multi-series, scatter) - LaTeX/TeX input — parse
\frac,\sqrt,\sin,\pi,\left(\right),^{...},\Gamma,\log_2, and more; supports$...$,$$...$$,\[...\],\(...\)delimiters - Interactive REPL — rustyline-powered with history, bracket matching, variable/function bindings
- Expression parser — recursive-descent, implicit multiplication, scientific notation, 27+ built-in functions
Quick Start
Or build from source:
CLI Usage
Just pass a string — mathr figures out what to do:
|
LaTeX / TeX Input
mathr accepts LaTeX math formulas — with or without Markdown delimiters:
Supported delimiters: $...$, $$...$$, \[...\], \(...\), or raw TeX with no delimiters.
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
Crate API
use *;
// Evaluate an expression
let val = eval_str?;
// Symbolic differentiation
let expr = parse?;
let deriv = differentiate?;
// FFT magnitude spectrum
let samples = vec!;
let mags = magnitude_spectrum?;
// Matrix determinant and inverse
let m = from_rows?;
let det = m.determinant?;
let inv = m.inverse?;
// Descriptive statistics
let data = vec!;
let s = summary?;
// Number theory: sieve, Fibonacci, Miller–Rabin
let primes = sieve_primes;
let fib50 = fibonacci;
let is_prime = is_prime_miller_rabin;
// ODE: solve y' = y, y(0) = 1, on [0, 1]
let y = rk4?;
// Taylor series expansion
let series = taylor_series_str?;
// Lagrange interpolation
let pts = vec!;
let y = lagrange_interp?;
// Special functions
let g = gamma; // √π
let e = erf;
Modules
| Module | Description |
|---|---|
expr |
Expression AST (Expr) with canonicalization and equality |
parser |
Recursive-descent parser with LaTeX/TeX support |
eval |
Tree-walking evaluator with Context (variables, functions) |
simplify |
Constant folding and algebraic identity simplification |
symbolic |
Symbolic differentiation rules |
calculus |
Numerical derivatives, quadrature, gradients |
solver |
Bisection, Newton–Raphson, secant, polynomial roots |
fft |
Cooley–Tukey FFT, convolution, cross-correlation, windows |
complex |
Generic complex number type |
matrix |
Matrix arithmetic, determinant, inverse, linear solve |
stats |
Descriptive statistics, correlation, regression |
numtheory |
GCD, LCM, primality, factorization, sieve, CRT, totient |
ode |
Euler, RK4, RK4 systems, adaptive RKF45 |
taylor |
Symbolic Taylor series expansion |
interpolate |
Lagrange, Newton divided-difference, linear |
special |
Gamma, Beta, erf, erfc, sinc, incomplete gamma |
plot |
PNG plotting via plotters |
Dependencies
| Crate | Purpose |
|---|---|
clap |
CLI argument parsing |
anyhow |
Error handling in binary |
thiserror |
Error types in library |
rustyline |
REPL line editing with history |
plotters |
PNG plot rendering |
num-traits |
Numeric trait bounds |
approx (dev) |
Float comparison in tests |
License
Apache-2.0