MathCore
A symbolic math library for Rust. Think of it as a computer algebra system (CAS) that can do symbolic differentiation, integration, equation solving, and more.
What it does
Basic stuff
- Parse math expressions from strings (with proper precedence)
- Work with symbols, not just numbers
- Differentiate and integrate symbolically
- Solve equations (linear, quadratic, and some higher degree)
- Complex number support
- ASCII plots (for quick visualization)
- Expression simplification
- Variables and substitution
Fancier features
- Limits (including one-sided and at infinity)
- Matrix operations and linear algebra
- Arbitrary precision arithmetic (BigInt/BigRational)
- Optimization (gradients, Hessian, autodiff)
- Taylor series expansion
- Numerical methods (Newton's method, gradient descent)
- ODEs and PDEs solvers
- FFT and signal processing
Installation
Add to your Cargo.toml
:
[]
= "0.1.0"
Quick example
use MathCore;
use HashMap;
Advanced Usage
Limits
use ;
let expr = parse.unwrap;
let limit = limit.unwrap;
println!; // Should be 1
// Check continuity
let continuous = is_continuous_at.unwrap;
println!;
Matrix Operations
use ;
use ;
// Symbolic matrices
let matrix = from_vec.unwrap;
let det = matrix.determinant.unwrap;
println!;
// Solve linear system Ax = b
let a = from_row_slice;
let b = from_row_slice;
let solution = solve_system.unwrap;
println!;
Arbitrary Precision
use ;
// Exact rational arithmetic
let a = from_str_with_precision.unwrap;
let b = from_str_with_precision.unwrap;
let sum = a.add;
println!; // Outputs: 1/2
// Compute π with arbitrary precision
let pi = compute_pi;
println!;
Optimization and Calculus
use ;
// Compute gradient
let loss = parse.unwrap;
let vars = vec!;
let gradient = gradient.unwrap;
println!;
// Taylor series expansion
let func = parse.unwrap;
let taylor = taylor_series.unwrap;
println!;
// Gradient descent optimization
let mut params = new;
params.insert;
params.insert;
let optimized = gradient_descent.unwrap;
println!;
Working with Variables
let math = new;
let mut vars = new;
vars.insert;
vars.insert;
let result = math.evaluate_with_vars.unwrap;
println!;
Symbolic Integration
let integral = integrate.unwrap;
println!;
// Numerical integration
let area = numerical_integrate.unwrap;
println!;
Function Plotting
let plot = plot_ascii.unwrap;
println!;
Complex Numbers
let math = new;
let result = math.evaluate.unwrap;
println!;
Supported Functions
Arithmetic Operations
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Power:
^
- Modulo:
%
- Factorial:
!
- Absolute value:
|x|
Trigonometric Functions
sin(x)
,cos(x)
,tan(x)
sec(x)
(through derivatives)
Exponential & Logarithmic
exp(x)
- e^xln(x)
- Natural logarithmlog(x, base)
- Logarithm with custom basesqrt(x)
- Square root
Utility Functions
min(a, b, ...)
- Minimum valuemax(a, b, ...)
- Maximum valueabs(x)
- Absolute value
Mathematical Constants
The following constants are predefined:
pi
- π (3.14159...)e
- Euler's number (2.71828...)tau
- τ = 2π (6.28318...)
Expression Syntax
Basic Examples
2 + 3 * 4 # Arithmetic
x^2 - 5*x + 6 # Polynomial
sin(x) + cos(x) # Trigonometric
e^x # Exponential (using constant e)
3! + 4! # Factorials
|x - 5| # Absolute value
3 + 4i # Complex numbers
Differentiation
// Returns: (cos(x) * x^2 + sin(x) * 2*x)
differentiate
Integration
// Returns: x^2
integrate
Equation Solving
// Returns: [2, -3]
solve
Performance
Pretty fast. Uses LTO in release builds. Some rough numbers:
- Expression parsing: ~1μs
- Differentiation: ~10μs for polynomials
- Matrix ops use nalgebra (which uses BLAS when available)
- Exact arithmetic with rationals (no precision loss)
When to use this
- Scientific computing (physics simulations, engineering calcs)
- ML/optimization (automatic differentiation)
- Education (demonstrating calculus concepts)
- Financial calculations (need exact arithmetic)
- Any time you need symbolic math in Rust
Contributing
PRs welcome!
# run tests
# benchmarks
# docs
License
MIT
© 2025 Nonanti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.