exprkit 0.1.0

A mathematical expression parser and evaluator library, inspired by the ExprTk C++ library
Documentation
  • Coverage
  • 5.81%
    18 out of 310 items documented3 out of 109 items with examples
  • Size
  • Source code size: 535.57 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 10.83 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 16s Average build duration of successful builds.
  • all releases: 16s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • krzysztofwos

exprkit

Crates.io Documentation License: MIT OR Apache-2.0

A mathematical expression parser and evaluator library for Rust, inspired by the ExprTk C++ library.

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:

[dependencies]
exprkit = "0.1"

Basic Usage

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

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

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

use exprkit::{Expression, SymbolTable};

let mut symbol_table = SymbolTable::<f64>::new();
symbol_table.add_function("double", Box::new(|args: &[f64]| args[0] * 2.0));
symbol_table.add_variable("x", 5.0);

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:

cargo install exprkit --features cli

Usage:

$ exprkit "2 + 2"
4

$ exprkit "sin(pi/2)"
1

$ exprkit --var x=5 "x * 2"
10

License

Licensed under either of:

at your option.

Acknowledgments

This library is inspired by ExprTk by Arash Partow.