lieval
lieval is a lightweight Rust crate for parsing and evaluating mathematical expressions from strings.
Features
- Parse and evaluate simple mathematical expressions.
- Basic arithmetic operations:
+,-,*,/,% - Parentheses for expression grouping
- Common mathematical functions:
sin,cos,atan,cosh,pow,sqrt,hypot,exp,ln,div_euclid,flooretc... - mathematical constants such as
PI,TAU, andE.
- Basic arithmetic operations:
- Support for variables, operators, and functions.
- Minimal dependencies.
- Provides a simple and easy-to-use API.
Usage
Add the lieval crate to your Cargo.toml file:
[]
= "<version>"
Then, in your Rust code:
use *;
assert_eq!;
assert_eq!;
let mut expr = new.unwrap;
assert_eq!;
// using macro `ex!`
assert_eq!;
You can assign numerical values to variables and evaluate them using Context.
# use *;
#
let mut context = new;
assert_eq!;
assert_eq!;
assert_eq!;
You can use custom functions.
# use *;
#
let mut context = new;
assert_eq!;
assert_eq!;
You can evaluate multiple expressions separated by commas or semicolons.
# use *;
#
assert_eq!;
let mut expr = ex!;
assert_eq!;
assert_eq!;
assert_eq!;
You can efficiently evaluate by precomputing using partial_eval.
# use *;
#
let mut expr = ex!;
expr.set_var
.set_var
.partial_eval
.unwrap;
let mut x = 1.0;
for _ in 0..10
You can perform arithmetic operations between Expr objects.
# use *;
#
let expr1 = new.unwrap;
assert_eq!;
assert_eq!;
// broadcasting
let expr1 = new.unwrap;
let expr2 = new.unwrap;
assert_eq!;
assert_eq!;
// If variables conflict, the variable in the left expression takes precedence,
// so use partial_eval beforehand.
let mut expr1 = new.unwrap;
expr1.set_var.partial_eval.unwrap;
assert_eq!;
API Documentation
Detailed API documentation can be found here.
License
This project is licensed under the MIT license.