formulac
A Rust crate for parsing and evaluating mathematical expressions with full complex-number support and extensible user-defined functions.
Features
- Complex number support — Evaluate expressions involving real and imaginary components using
num_complex::Complex<f64>. - Reverse Polish Notation (RPN) — Converts infix expressions to RPN using the Shunting-Yard algorithm for efficient evaluation.
- Built-in mathematical operators & functions — Supports
+,-,*,/,^, and standard functions likesin,cos,exp,log, and more. - User-defined functions — Easily register custom functions or constants at runtime using a simple API.
- Safe and dependency-light — No use of unsafe Rust or heavyweight external parsers.
Usage
Add to your Cargo.toml:
or
[]
= "0.3"
= "0.4"
Basic Example
use Complex;
use ;
Registering a Custom Function
use Complex;
use ;
// Create user-defined function table
let mut users = new;
// Define a function f(x) = x^2 + 1
let f_token = Function;
users.register;
let mut vars = new;
let expr = compile.unwrap;
assert_eq!;
Core Types & API Overview
-
compileCompiles a formula string into a Rust closureFn(&[Complex<f64>]) -> Complex<f64>that evaluates the expression for given variable values. -
VariablesA lookup table mapping variable names (strings) toComplex<f64>values, used during parsing. -
UserDefinedTableAllows registration of custom functions or constants under user-defined names for use in expressions.
License
Licensed under MIT OR Apache-2.0 — choose the license that best suits your project.
Contribution & Contact
Contributions, feature requests, and bug reports are welcome! Please feel free to open issues or submit pull requests via the GitHub repository.