High-performance symbolic mathematics compiler for Rust
🚀 Production Ready - Comprehensive optimization pipeline with proven performance gains
A compilation pipeline for mathematical expressions with symbolic optimization and code generation capabilities.
use dslcompile::prelude::*;
// Create mathematical expressions with natural syntax
let math = DynamicContext::new();
let x = math.var();
let expr = &x * &x + 2.0 * &x + 1.0; // x² + 2x + 1
// Direct evaluation
let result = math.eval(&expr, &[3.0]); // x = 3.0
assert_eq!(result, 16.0); // 9 + 6 + 1 = 16
// Symbolic optimization (optional for performance)
let mut optimizer = SymbolicOptimizer::new()?;
let optimized = optimizer.optimize(&expr.into())?;
let optimized_result = optimized.eval_with_vars(&[3.0]);
assert_eq!(optimized_result, 16.0);Status: Performance benchmarks need verification - current compilation issues prevent accurate measurements.
- Symbolic optimization: Uses egg e-graph optimization for algebraic simplification
- Code generation: Multiple compilation backends:
- Rust hot-loading compilation (primary)
- LLVM JIT compilation (maximum performance)
- Memory efficiency: Zero-copy expression trees with shared subexpressions
Note: Performance claims require verification with current codebase.
Transform mathematical expressions at compile time with full symbolic simplification:
use dslcompile::prelude::*;
let math = DynamicContext::new();
let x = math.var();
// Original: (x + 1)²
let expr = (&x + math.constant(1.0)).pow(math.constant(2.0));
// Optimize symbolically
let mut optimizer = SymbolicOptimizer::new()?;
let optimized = optimizer.optimize(&expr.into())?;
// Results in optimized form: x² + 2x + 1
// Operation count: 2 → 3 (expanded but optimized for evaluation)
// Execution speedup: 1.2-50x depending on input complexityPretty-printed transformation:
Original: ((x0 + 1) ^ 2)
Optimized: ((x0 + 1) * (x0 + 1))
Handle dynamically generated expressions with sophisticated pattern recognition:
use dslcompile::prelude::*;
let math = DynamicContext::new();
let x = math.var();
let y = math.var();
// Complex expression with optimization opportunities:
// ln(exp(x)) + (y + 0) * 1 + sin²(x) + cos²(x) + exp(ln(y)) - 0
let complex_expr = x.clone().exp().ln() // ln(exp(x)) = x
+ (&y + math.constant(0.0)) * math.constant(1.0) // (y + 0) * 1 = y
+ x.clone().sin().pow(math.constant(2.0)) // sin²(x)
+ x.clone().cos().pow(math.constant(2.0)) // cos²(x)
+ y.clone().ln().exp() // exp(ln(y)) = y
- math.constant(0.0); // - 0
let mut optimizer = SymbolicOptimizer::new()?;
let optimized = optimizer.optimize(&complex_expr.into())?;
// Optimizes to: x + y + 1 + y = x + 2y + 1
// Operation count: 17 → 3 (82% reduction)
// Execution speedup: 7.3-87.9xGenerate optimized Rust code with advanced features:
use dslcompile::prelude::*;
// Generate optimized Rust code
let codegen = RustCodeGenerator::new();
let rust_code = codegen.generate_function(&optimized_expr, "my_function")?;
// Compile and load dynamically
let compiler = RustCompiler::new();
let compiled_func = compiler.compile_and_load(&rust_code, "my_function")?;
// Execute with native performance
let result = compiled_func.call_two_vars(2.5, 3.7)?;Generated code example:
#[target_feature(enable = "avx2")]
#[no_mangle]
pub extern "C" fn my_function(var_0: f64, var_1: f64, var_2: f64) -> f64 {
return (((var_1 + var_2) + (((var_1).sin() * (var_1).sin()) +
((var_1).cos() * (var_1).cos()))) + var_2);
}Performance benchmarks are currently unavailable due to compilation issues. The library requires fixes before accurate performance measurements can be taken.
Optimization Capabilities:
- egg e-graph optimization: Algebraic simplification and pattern recognition
- Rust code generation: Hot-loading compilation for maximum performance
- Zero-overhead static contexts: Compile-time optimization with no runtime cost
Run benchmarks when compilation issues are resolved:
cargo run --example readme_demo -- --benchDSLCompile uses a final tagless approach with three-layer optimization:
┌─────────────────────────────────────────────────────────────┐
│ Expression Building │
│ (Final Tagless Design + Type-Safe Variables) │
└─────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────▼───────────────────────────────────────┐
│ Symbolic Optimization │
│ (Algebraic Simplification + Pattern Recognition) │
└─────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────▼───────────────────────────────────────┐
│ Compilation Backends │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ Rust │ │ Future Backends │ │
│ │ Hot-Loading │ │ (Cranelift, LLVM, etc.) │ │
│ │ (Primary) │ │ │ │
│ └─────────────┘ └─────────────┘ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
Natural syntax with operator overloading and type safety:
use dslcompile::prelude::*;
let math = DynamicContext::new();
let x = math.var();
let y = math.var();
// Natural mathematical syntax
let expr = &x * &x + 2.0 * &x + &y;
let result = math.eval(&expr, &[3.0, 1.0]); // x=3, y=1 → 16
// Transcendental functions
let complex = x.clone().sin() * y.exp() + (x.clone() * &x + 1.0).ln();
let complex_result = math.eval(&complex, &[1.0, 2.0]);
// Mathematical summations with optimization
let sum_result = math.sum(1..=10, |i| i * math.constant(2.0))?; // Σ(2i) = 110Symbolic differentiation with subexpression optimization:
use dslcompile::prelude::*;
let math = MathBuilder::new();
let x = math.var();
let f = math.poly(&[1.0, 2.0, 1.0], &x); // 1 + 2x + x²
// Convert to optimized AST
let optimized_f = math.optimize(&f)?;
// Compute function and derivatives symbolically
let mut ad = SymbolicAD::new()?;
let result = ad.compute_with_derivatives(&optimized_f)?;
println!("f(x) = 1 + 2x + x²");
println!("f'(x) = 2 + 2x (computed symbolically)");Multi-dimensional summations with convergence analysis:
use dslcompile::prelude::*;
let mut simplifier = SummationSimplifier::new();
let range = IntRange::new(1, 10);
// Arithmetic series: Σ(i=1 to 10) i = 55
let function = ASTFunction::new("i", ASTRepr::Variable(0));
let result = simplifier.simplify_finite_sum(&range, &function)?;
if let Some(closed_form) = &result.closed_form {
let value = closed_form.eval_with_vars(&[]);
assert_eq!(value, 55.0);
}Add to your Cargo.toml:
[dependencies]
dslcompile = "0.1"
# Optional features:
# Enable LLVM JIT compilation backend (requires LLVM 18)
# dslcompile = { version = "0.1", features = ["llvm_jit"] }To use the LLVM JIT backend, you need:
- LLVM 18 installed on your system (LLVM 19 is not yet supported by inkwell 0.6.0)
- Set the
LLVM_SYS_181_PREFIXenvironment variable to your LLVM installation:export LLVM_SYS_181_PREFIX=/usr/lib/llvm-18
Version Compatibility Matrix:
| LLVM Version | DSLCompile Support | Status |
|---|---|---|
| 18.x | ✅ Fully supported | llvm_jit feature |
| 19.x | ❌ Not yet supported | Planned for future release |
| 17.x and older | ❌ Not supported | Use all-no-llvm feature |
Note: If you have a different LLVM version installed, use the all-no-llvm feature to enable all functionality except LLVM JIT compilation.
Ubuntu/Debian:
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 18macOS:
brew install llvm@18
export LLVM_SYS_181_PREFIX=$(brew --prefix llvm@18)Arch Linux:
sudo pacman -S llvm llvm-libsWindows: Download pre-built binaries from LLVM releases
Run the comprehensive demo:
cargo run --example readme_demoRun performance benchmarks (using Divan):
cargo run --example readme_demo -- --benchNote: The demo shows how easy it is to add benchmarks - just put #[divan::bench] above any function!
- Final Tagless Design: Type-safe expression building with multiple interpreters
- Symbolic Optimization: Algebraic simplification using egg e-graph optimization
- Compilation Backend: Rust hot-loading compilation (primary backend)
- Automatic Differentiation: Forward and reverse mode with symbolic optimization
- Advanced Summation: Multi-dimensional sums with convergence analysis
- Domain Analysis: Abstract interpretation for mathematical transformation safety
- A-Normal Form: Intermediate representation with scope-aware common subexpression elimination
- Symbolic optimization: egg e-graph based algebraic simplification
- Compilation backend: Rust code generation with hot-loading
- Memory efficiency: Zero-copy expression trees with shared subexpressions
- Type safety: Compile-time optimization with zero runtime overhead (StaticContext)
Performance benchmarks require verification with current implementation.
- Developer Notes - Architecture overview and expression types
- Roadmap - Project status and planned features
- Examples - Usage examples and demonstrations
- API Documentation - Complete API reference