# exprkit
[](https://crates.io/crates/exprkit)
[](https://docs.rs/exprkit)
[](#license)
A mathematical expression parser and evaluator library for Rust, inspired by the [ExprTk C++ library](https://www.partow.net/programming/exprtk/).
## Features
- Parse and evaluate mathematical expressions at runtime
- Variables, constants, and arrays
- User-defined functions
- Control flow (if/else, loops, switch)
- 100+ built-in mathematical functions
- String operations
- Vector/array operations with BLAS-like functions
## Quick Start
Add to your `Cargo.toml`:
```toml
[dependencies]
exprkit = "0.1"
```
### Basic Usage
```rust
use exprkit::{Expression, SymbolTable};
fn main() {
let mut symbol_table = SymbolTable::<f64>::new();
symbol_table.add_variable("x", 2.0);
symbol_table.add_variable("y", 3.0);
let mut expr = Expression::new();
expr.compile_with_symbol_table("x + y * 2", symbol_table).unwrap();
let result = expr.value();
assert_eq!(result, 8.0); // 2 + 3 * 2 = 8
}
```
### Variables and Arrays
```rust
use exprkit::{Expression, SymbolTable};
let mut symbol_table = SymbolTable::<f64>::new();
symbol_table.add_variable("x", 1.0);
symbol_table.add_vector("v", vec![1.0, 2.0, 3.0, 4.0, 5.0]);
let mut expr = Expression::new();
expr.compile_with_symbol_table("sum(v) + x", symbol_table).unwrap();
assert_eq!(expr.value(), 16.0); // 15 + 1 = 16
```
### Control Flow
```rust
use exprkit::{Expression, SymbolTable};
let mut symbol_table = SymbolTable::<f64>::new();
symbol_table.add_variable("x", 10.0);
let mut expr = Expression::new();
expr.compile_with_symbol_table(
"if (x > 5) { x * 2 } else { x / 2 }",
symbol_table
).unwrap();
assert_eq!(expr.value(), 20.0);
```
### User-Defined Functions
```rust
use exprkit::{Expression, SymbolTable};
let mut symbol_table = SymbolTable::<f64>::new();
let mut expr = Expression::new();
expr.compile_with_symbol_table("double(x) + 1", symbol_table).unwrap();
assert_eq!(expr.value(), 11.0);
```
## Built-in Functions
### Mathematical Functions
`sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `sinh`, `cosh`, `tanh`, `exp`, `log`, `log10`, `log2`, `sqrt`, `abs`, `ceil`, `floor`, `round`, `trunc`, `erf`, `erfc`, `pow`, `mod`, `min`, `max`, `clamp`, `hypot`, `atan2`
### Aggregation Functions
`sum`, `avg`, `mul`, `min`, `max`
### Vector Functions
`sum`, `avg`, `min`, `max`, `dot`, `axpy`, `axpby`, `axpyz`, `nrm2`
### Combinatorics
`fib` (Fibonacci), `ncr` (combinations), `npr` (permutations)
### Statistical
`erf`, `erfc`, `ncdf` (normal CDF)
## CLI Tool
The `exprkit` binary provides a command-line expression evaluator. Install with the `cli` feature:
```bash
cargo install exprkit --features cli
```
Usage:
```bash
$ exprkit "2 + 2"
4
$ exprkit "sin(pi/2)"
1
$ exprkit --var x=5 "x * 2"
10
```
## License
Licensed under either of:
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
## Acknowledgments
This library is inspired by [ExprTk](https://www.partow.net/programming/exprtk/) by Arash Partow.