MathCompile
Symbolic mathematics compiler for Rust
Transform symbolic mathematical expressions into optimized native code with automatic differentiation support.
Overview
MathCompile provides a compilation pipeline for mathematical expressions:
- Symbolic optimization using algebraic simplification before compilation
- Native code generation through Rust's compiler or optional Cranelift JIT
- Automatic differentiation with shared subexpression optimization
- Final tagless design for type-safe, extensible expression building
Core Capabilities
Expression Building and Optimization
use *;
// Define symbolic expression
let mut math = new;
let x = math.var;
let expr = math.poly; // 1 + 2x + 3x² (coefficients in ascending order)
// Algebraic simplification
let optimized = math.optimize?;
// Direct evaluation (fastest for immediate use)
let result = eval_with_vars; // x = 3.0
assert_eq!; // 1 + 2*3 + 3*9 = 34
// Generate Rust code for compilation
let codegen = new;
let rust_code = codegen.generate_function?;
// Compile and load the function
let compiler = new;
let compiled_func = compiler.compile_and_load?;
let compiled_result = compiled_func.call?;
assert_eq!;
Automatic Differentiation
// Define function using MathBuilder
let mut math = new;
let x = math.var;
let f = math.poly; // 1 + 2x + x²
// Convert to optimized AST
let optimized_f = math.optimize?;
// Compute function and derivatives
let mut ad = new?;
let result = ad.compute_with_derivatives?;
println!;
println!;
println!;
Multiple Compilation Backends
// Rust code generation (primary backend)
let codegen = new;
let rust_code = codegen.generate_function?;
let compiler = new;
let compiled_func = compiler.compile_and_load?;
let result = compiled_func.call?;
// Cranelift JIT (optional, requires "cranelift" feature)
Installation
Add to your Cargo.toml:
[]
= "0.1"
# Optional: Enable Cranelift JIT backend
# mathcompile = { version = "0.1", features = ["cranelift"] }
Basic Usage
use *;
// Create mathematical expressions
let mut math = new;
let x = math.var;
let expr = math.add; // x² + 2x + 1
// Optimize symbolically
let optimized = math.optimize?;
// Evaluate efficiently
let result = eval_with_vars; // x = 3.0
assert_eq!; // 9 + 6 + 1
// Generate and compile Rust code
let codegen = new;
let rust_code = codegen.generate_function?;
let compiler = new;
let compiled_func = compiler.compile_and_load?;
let compiled_result = compiled_func.call?;
assert_eq!;
// JIT compilation (if cranelift feature enabled)
Documentation
- Developer Notes - Architecture overview and expression types
- Roadmap - Project status and planned features
- Examples - Usage examples and demonstrations
- API Documentation - Complete API reference
Architecture
MathCompile uses a final tagless approach to solve the expression problem:
- Extensible operations - Add new mathematical functions without modifying existing code
- Multiple interpreters - Same expressions work with evaluation, optimization, and compilation
- Type safety - Compile-time guarantees for mathematical operations
┌─────────────────────────────────────────────────────────────┐
│ Expression Building │
│ (Final Tagless Design + Ergonomic API) │
└─────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────▼───────────────────────────────────────┐
│ Symbolic Optimization │
│ (Algebraic Simplification + Egglog Integration) │
└─────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────▼───────────────────────────────────────┐
│ Compilation Backends │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ Rust │ │ Cranelift │ │ Future Backends │ │
│ │ Hot-Loading │ │ JIT │ │ (LLVM, etc.) │ │
│ │ (Primary) │ │ (Optional) │ │ │ │
│ └─────────────┘ └─────────────┘ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
Features
default- Core functionality with symbolic optimizationcranelift- Enable Cranelift JIT compilation backendall- All available features
Technical Notes
- Polynomial coefficients: The
poly()function uses ascending order:[c₀, c₁, c₂]representsc₀ + c₁x + c₂x² - Variable indexing: Variables are internally managed by index for efficient evaluation
- Thread safety: Each
MathBuildermaintains isolated variable registries - Compilation requirements: Rust backend requires
rustcandcargoavailable in PATH