A revolutionary client-side mathematical expression compiler that transforms user-entered mathematical expressions into optimized WebAssembly (WASM) bytecode for real-time execution in web browsers. This platform enables true mathematical programming where users can define their own formulation models, optimization algorithms, and domain-specific calculations without server dependencies.
- Client-Side Compilation: Parse and compile mathematical expressions to WASM entirely in the browser
- Real-Time Execution: Execute compiled expressions with microsecond-level performance
- Dynamic Code Generation: Generate and link new WASM modules at runtime
- Multi-Level Optimization: Advanced algebraic simplification, constant folding, and strength reduction
- Persistent Caching: Intelligent caching system with browser storage integration
- Zero Server Dependencies: Complete mathematical compilation stack runs locally
- Expression Validation: Comprehensive syntax and semantic validation
- Performance Profiling: Built-in benchmarking and optimization analysis
- Memory Management: Efficient memory sharing between compiled modules
- Error Handling: Robust error reporting with detailed diagnostics
- Type Safety: Compile-time type checking for mathematical expressions
- Modular Architecture: Clean separation between parsing, optimization, and code generation
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Client Application โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ UI โ โMath Compilerโ โ Rust/WASM โ โ
โ โ- Expression โโโโค- Parser โโโโค- Database โ โ
โ โ Editor โ โ- ASTโWASM โ โ- Calculations โ โ
โ โ- Results โ โ- Optimizer โ โ- Validation โ โ
โ โ- Validation โ โ- Cache Mgmt โ โ- Model Execute โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Dynamic WASM Runtime โ
โ โโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โFunction โ โGenerated โ โ Memory โ โ
โ โ Table โ โModules โ โ Sharing โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Rust (latest stable version)
- wasm-pack for building WebAssembly modules
- Web server for serving files (due to CORS restrictions)
- Clone and build the project:
git clone <repository-url: https://github.com/ardeshir/dynamic-math>
cd dynamic-math
chmod +x build.sh
./build.sh- Start a local server:
# Using Python
python -m http.server 8000
# Using Node.js
npx http-server -p 8000
# Using VS Code Live Server extension
# Right-click on index.html and select "Open with Live Server"- Open in browser:
Navigate to
http://localhost:8000/index.html
import init, { MathCompilerPlatform } from './pkg/math_compiler_wasm.js';
await init();
const platform = new MathCompilerPlatform();Key Methods:
validate_expression(expression)- Validate mathematical syntaxcompile_model(id, name, expression, variables)- Compile expression to WASMload_model(id)- Load compiled model for executionexecute_model(id, values)- Execute with given variable valueslist_models()- Get all compiled modelsremove_model(id)- Remove compiled model
// Validate expression
const validation = platform.validate_expression("x^2 + sin(y)");
if (validation.valid) {
// Compile to WASM
await platform.compile_model(
"my_model",
"Quadratic with Sine",
"x^2 + sin(y)",
["x", "y"]
);
// Load and execute
await platform.load_model("my_model");
const result = platform.execute_model("my_model", [3.0, 1.57]);
console.log("Result:", result); // ~10.0 (9 + sin(ฯ/2))
}import { AdvancedMathCompiler } from './integration_example.js';
const compiler = new AdvancedMathCompiler({
enableCache: true,
maxCacheEntries: 100,
optimizationLevel: 'aggressive',
enableLogging: true
});
// Simple evaluation
const result = await compiler.evaluate('sqrt(x^2 + y^2)', { x: 3, y: 4 });
// Batch evaluation
const expressions = ['x + y', 'x * y', 'x^y'];
const results = await compiler.evaluateBatch(expressions, { x: 2, y: 3 });
// Compile reusable function
const distance = await compiler.compileFunction('sqrt(x^2 + y^2)', ['x', 'y']);
const dist = await distance(3, 4); // Returns 5.0import { RealTimeMathEvaluator } from './integration_example.js';
const evaluator = new RealTimeMathEvaluator();
// Register expressions for real-time use
await evaluator.registerExpression(
'physics_sim',
'sin(t) * exp(-t/10)',
['t']
);
// Real-time evaluation loop
for (let t = 0; t < 100; t += 0.1) {
const result = await evaluator.evaluateById('physics_sim', [t]);
// Use result for real-time visualization
}- Operators:
+,-,*,/,^,% - Precedence: Follows standard mathematical precedence rules
- Associativity: Right-associative for power operations
// Trigonometric
sin(x), cos(x), tan(x), asin(x), acos(x), atan(x)
// Exponential & Logarithmic
exp(x), log(x), ln(x), sqrt(x)
// Utility Functions
abs(x), ceil(x), floor(x), round(x)
min(x, y), max(x, y), pow(x, y)// Specialized for feed/chemical formulation
nutritional_value(ingredient, nutrient)
cost_per_unit(ingredient)
digestibility(ingredient, species)
bioavailability(nutrient, ingredient)
interaction_factor(ingredient1, ingredient2)// Conditional expressions
"if(x > 0) sqrt(x) else 0"
// Complex mathematical expressions
"sin(x) * exp(-x^2) + log(max(y, 0.1))"
// Multi-variable optimization
"minimize cost subject to protein >= 18 and fiber <= 7"- Animal Feed Formulation: Optimize nutritional profiles and costs
- Chemical Mixing: Calculate precise chemical compositions
- Recipe Development: Optimize taste, cost, and nutritional balance
const formulationCompiler = new FormulationMathCompiler();
// Define ingredients and constraints
formulationCompiler.registerIngredient('corn', {
protein: 8.5, fat: 3.8, cost: 0.25
});
// Create optimization model
await formulationCompiler.createFormulation('feed_recipe', {
objective: 'minimize cost',
constraints: ['protein >= 18', 'fat <= 6'],
targets: { protein: 18.0, fat: 5.0 }
});- Physics Simulations: Real-time mathematical modeling
- Financial Modeling: Dynamic pricing and risk calculations
- Scientific Computing: Live parameter adjustment and visualization
- Graphing Calculators: User-defined function plotting
- Engineering Calculators: Custom formula libraries
- Educational Platforms: Student-programmable math environments
- Custom Metrics: User-defined KPI calculations
- Signal Processing: Real-time filter and transform applications
- Statistical Analysis: Dynamic statistical model creation
- Expression Parsing: ~1-5ms for typical expressions
- WASM Generation: ~10-50ms depending on complexity
- Optimization: ~5-20ms with aggressive optimization
- Cache Hit: ~0.1ms for cached expressions
- Simple Arithmetic: ~0.001-0.01ms per evaluation
- Trigonometric Functions: ~0.01-0.05ms per evaluation
- Complex Expressions: ~0.05-0.5ms per evaluation
- Memory Overhead: ~1-10KB per compiled expression
const benchmark = await compiler.benchmark(
'sin(x) * cos(y) + sqrt(x^2 + y^2)',
{ x: 1.5, y: 2.5 },
10000 // iterations
);
console.log(benchmark);
// {
// averageTimeMs: 0.023,
// executionsPerSecond: 43478,
// compilationTimeMs: 45.2,
// medianTimeMs: 0.021
// }const compiler = new AdvancedMathCompiler({
// Caching options
enableCache: true,
maxCacheEntries: 100,
maxCacheSizeMB: 10,
// Optimization levels
optimizationLevel: 'basic' | 'aggressive',
// Debugging options
enableLogging: false,
enableProfiling: false,
// Memory management
maxMemoryUsageMB: 50,
gcInterval: 60000 // milliseconds
});const cache = new WasmExpressionCache(
100, // max memory entries
10 // max storage size MB
);
// Cache maintenance
cache.maintenance(); // Cleanup old entries
cache.clear_cache(); // Clear all cached data
const stats = cache.get_stats(); // Get cache statistics# Run all tests
wasm-pack test --headless --firefox
# Run specific test suite
wasm-pack test --headless --firefox -- integration_tests
# Run with browser debugging
wasm-pack test --firefox- โ Expression Parsing: 45+ test cases covering syntax validation
- โ Code Generation: WASM bytecode correctness verification
- โ Runtime Execution: Performance and accuracy testing
- โ Cache System: Multi-level cache integrity tests
- โ Error Handling: Comprehensive error condition coverage
- โ Memory Management: Leak detection and resource cleanup
- โ Integration: End-to-end workflow validation
#[wasm_bindgen_test]
fn test_complex_mathematical_expressions() {
let mut platform = MathCompilerPlatform::new();
let result = platform.evaluate_expression(
"sin(x) * exp(-x^2) + log(max(y, 0.1))",
vec!["x".to_string(), "y".to_string()],
vec![1.0, 0.5]
).unwrap();
assert!((result - expected_value).abs() < 1e-10);
}- Conditional Expressions: Limited WASM codegen support (roadmap item)
- Array Operations: Basic array access implemented, advanced operations planned
- Complex Numbers: Real numbers only, complex support in development
- Parallel Execution: Single-threaded execution, SIMD optimization available
- Memory Model: Fixed-size memory pools, dynamic allocation planned
- Chrome/Edge: Full support (v90+)
- Firefox: Full support (v89+)
- Safari: Partial support (v14+, some SIMD limitations)
- Mobile Browsers: Good support on modern devices
- Cold Start: Initial compilation overhead (~50-100ms)
- Memory Usage: ~1-10MB typical usage, scales with expression complexity
- Cache Size: Recommend <10MB for optimal performance
- Expression Complexity: Exponential compilation time for very complex expressions
- โ Conditional Expression Codegen: Full if/else support in WASM generation
- โ Array Operations: Advanced vector and matrix operations
- โ Performance Optimizations: SIMD vectorization and loop unrolling
- โ Error Recovery: Better error reporting and partial compilation recovery
- โ Mobile Optimization: Optimized builds for mobile devices
- ๐ WebAssembly SIMD: Leverage WASM SIMD for vectorized operations
- ๐ Multi-threading: Web Workers integration for parallel compilation
- ๐ Advanced Optimization: SSA-form optimization and register allocation
- ๐ Domain-Specific Languages: Custom syntax for specialized domains
- ๐ Visual Expression Editor: Drag-and-drop mathematical expression builder
- ๐ GPU Acceleration: WebGL compute shader integration
- ๐ Machine Learning: Neural network expression optimization
- ๐ Symbolic Mathematics: Computer algebra system integration
- ๐ Real-time Collaboration: Multi-user mathematical model editing
- ๐ Cloud Integration: Optional cloud compilation for complex expressions
We welcome contributions! Please see our contributing guidelines:
# Install Rust and wasm-pack
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo install wasm-pack
# Clone and build
git clone <repository-url>
cd math-compiler-platform
./build.sh- Rust: Use
cargo fmtandcargo clippy - JavaScript: Use Prettier and ESLint
- Documentation: Update README.md and inline docs
- Tests: Add tests for new features
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Update documentation
- Submit pull request with detailed description
This project is licensed under the MIT License - see the LICENSE file for details.
- WebAssembly Community: For the foundational WASM technology
- Rust WASM Working Group: For excellent tooling and libraries
- Pest Parser: For the powerful parsing framework
- Walrus: For WASM bytecode manipulation
- Mathematical Software Community: For inspiration and domain expertise
- GitHub Issues: Bug reports and feature requests
- Discussions: General questions and community support
- Documentation: Comprehensive API and usage documentation
- Examples: Real-world integration examples and tutorials
Built with โค๏ธ for the mathematical programming community
Transform your mathematical ideas into executable reality with client-side compilation power.