mathverse-calculus
Numerical calculus for Rust: derivatives, integration, vector calculus, and ODE solvers.
Part of the MathVerse ecosystem.
Features
- Derivatives: Central differences, partial derivatives, nth-order, discrete gradient
- Integration: Trapezoid, Simpson, adaptive, Gaussian quadrature, Romberg, 2D
- ODE Solvers: Euler, midpoint, RK4 with builder API
- Vector Calculus: Gradient, divergence, curl, Laplacian, Jacobian, Hessian
- Root Finding: Newton-Raphson with auto-differentiation, critical point finding
#![forbid(unsafe_code)]— fully safe Rustno_stdcompatible (with feature flag)
Installation
[]
= "0.1"
Quick Start
use *;
Usage
Derivatives
use *;
// First derivative: f'(x)
let d = derivative; // ≈ 12.0
// Second derivative: f''(x)
let d2 = second_derivative; // ≈ 0.0
// Nth derivative with error estimate
let = nth_derivative; // ≈ 120.0
// Partial derivative: ∂f/∂x_i
let pd = partial_derivative; // ≈ 3.0
Integration
use *;
// Trapezoid rule
let t = trapezoid.unwrap;
// Simpson's rule
let s = simpson.unwrap;
// Adaptive Simpson (recommended for most uses)
let a = integrate;
// Gaussian quadrature (exact for polynomials up to degree 2n-1)
let g = gaussian_quadrature.unwrap;
// Romberg integration
let r = romberg.unwrap;
// 2D integration
let i2d = integrate_2d.unwrap;
ODE Solvers
use *;
// Direct function calls
let sol = runge_kutta_4.unwrap;
// Builder API (scipy-like)
let sol = new
.method
.steps
.solve
.unwrap;
// Systems of ODEs (harmonic oscillator)
let osc = runge_kutta_4_system.unwrap;
Vector Calculus
use *;
let f = ;
// Gradient: ∇f
let g = gradient; // ≈ [2.0, 4.0]
// Laplacian: ∇²f
let l = laplacian; // ≈ 4.0
// Directional derivative: ∇f · v
let d = directional_derivative.unwrap; // ≈ 2.0
Feature Flags
| Flag | Default | Description |
|---|---|---|
std |
Yes | Enable standard library; disabling gives no_std |
Performance
- All algorithms are cache-friendly with minimal allocations
- Hot paths designed for future SIMD acceleration
- Benchmarks available via
cargo bench
Python Parity
| Rust function | SciPy/NumPy equivalent |
|---|---|
nth_derivative |
scipy.misc.derivative |
gaussian_quadrature |
scipy.integrate.fixed_quad |
romberg |
scipy.integrate.romberg |
integrate_2d |
scipy.integrate.dblquad |
OdeProblem |
scipy.integrate.solve_ivp |
discrete_gradient |
numpy.gradient |
License
Licensed under MIT OR Apache-2.0.
Contributing
See the MathVerse contributing guide.