Skip to content

kmolan/multicalc-rust

Repository files navigation

multicalc

On crates.io Downloads CI Docs License: MIT

Single- and multi-variable calculus, automatic differentiation, nonlinear least squares, and Lie groups in pure safe no_std Rust: the same code from a 64-bit server down to a bare-metal microcontroller.

combined_demo_reel.mp4

A reel of the live showcase demos: the 3D and 2D arm IK solvers, then the Newton fractal and the gradient-driven marbles; every number on screen is measured live.

Highlights

  • Runs the same math from a server to a microcontroller. Every commit is built and tested on six targets: the x86_64 and aarch64 Linux hosts and on four bare-metal ABIs (thumbv7em soft-float, thumbv7em hardware-FPU, thumbv6m, and riscv32imc), running the real math under QEMU. no_std, no-alloc, and no-panic rules hold on every one.
  • Every module checked against reference libraries. Each module's results are verified against established libraries like numpy and scipy fixtures within ~1 ulp, thus validating the rust implementation.
  • Fast, and measured (i7-12650H): a third derivative in 26.7 ns, a 10×10 LU solve in 239 ns, a full Levenberg-Marquardt fit in 2 µs. In the live demos: ~4 million Newton solves/sec on one core, and a 1 kHz arm IK at ~6 µs/solve. See the benchmarks.
  • Exact derivatives, not estimates. Differentiation, Jacobians, Hessians, Newton steps, and Levenberg-Marquardt fits use forward-mode automatic differentiation, so derivatives are exact to machine precision; finite differences remain available for black-box functions.
  • Pure safe and panic-free. #![forbid(unsafe_code)], no C dependencies, and unwrap/ panic denied on library paths; every fallible call returns a typed error.
  • One dependency. no_std, no heap by default, with transcendentals from libm.

What it does

  • Automatic differentiation: Dual, HyperDual, and Jet scalars for exact first, second, and nth-order derivatives.
  • Differentiation: derivatives of any order (total and partial), plus Jacobian and Hessian matrices; autodiff by default, finite differences for black-box functions.
  • Integration: iterative Newton-Cotes rules (Boole, Simpson, Trapezoidal) and Gaussian quadrature (Legendre, Hermite, Laguerre) over finite, semi-infinite, and infinite limits.
  • Linear algebra: fixed-size, stack-allocated Matrix and Vector with LU, Cholesky, column-pivoted QR, and SVD: solves, determinant, inverse, pseudo-inverse, and condition number.
  • Least-squares optimization: LevenbergMarquardt and GaussNewton solvers for nonlinear curve fitting.
  • Root finding: bracketed bisection and Newton solvers for scalar equations and square systems, with an optional damped line search.
  • Vector calculus: curl, divergence, and line and flux integrals.
  • Approximation: linear and quadratic Taylor models with goodness-of-fit metrics.
  • ODE integrators: fixed-step Rk4 and adaptive Rk45 (Dormand-Prince 5(4)) with PI step control and dense output.
  • Discretization: zero-order hold, Van Loan, and discrete white-noise models for continuous-time linear systems.
  • Spatial math: Quaternion and the SO2/SE2/SO3/SE3 Lie groups for 2D and 3D rotations and rigid-body transforms.
  • Kinematics: differential-drive and unicycle maps between wheel and body motion, with exact SE(2) odometry.
  • Estimation: linear and extended KalmanFilters with Joseph-form covariance updates, optional control input, and innovation access for measurement gating. ExtendedKalmanFilter takes nonlinear models as functions and differentiates them for the Jacobians — no hand-derived Jacobians.

Install

cargo add multicalc

Tutorial

The guide is a comprehesive tutorial for each module. It shows the full imports, expected outputs in comments, error-path notes, and pointers to runnable demos. Start there when you need the complete picture of a feature.

Documentation

  • Guide: A worked section for every module: imports, a runnable snippet, error paths, and a demo pointer.
  • Crate README / API docs: the crates.io page and full API reference, with notes on no_std, error handling, and heap allocation.
  • Examples: Self-contained, self-checking programs for each module in the demos/ crate. Run one with cargo run -p multicalc-demos --example <name>.
  • Benchmarks: Per-module accuracy tables and latency measurements, generated from the QA fixtures and checked in CI.
  • Live showcases: Five animated Rerun demos: a 1 kHz IK on a 3-link arm, an 8-link SE(3) arm tracking a moving 3D pose, a Newton fractal, Fourier epicycles drawing Ferris, and gradient-driven marbles, each streaming live-measured speed and accuracy.
  • QA crate: multicalc-qa holds the CI-enforced accuracy fixtures and generates the benchmarks tables from them.

Repository layout

The published library crate lives in crates/multicalc; the repository root is a Cargo workspace. Runnable demos live in the dev-only demos/ crate (basics and live Rerun showcases), and tools/embedded-smoke runs multicalc on the four bare-metal targets (three Cortex-M targets + riscv32imc) under QEMU every PR.

Contributing

Contributions are welcome. See CONTRIBUTING.md.

Acknowledgements

The least-squares solvers and QR factorization port the public-domain MINPACK routines (Moré, Garbow, Hillstrom; netlib); the full citation is in the crate README.

License

Licensed under the MIT License.

Contact

anmolkathail@gmail.com

About

Calculus, autodiff, nonlinear least squares & linear algebra in safe, no-std rust. Works on any target: 64-bit servers to bare-metal microcontrollers

Topics

Resources

License

Contributing

Stars

86 stars

Watchers

2 watching

Forks

Packages

 
 
 

Contributors