Exmex is an extendable mathematical expression parser and evaluator. Ease of use, flexibility, and efficient evaluations are its main design goals. Exmex can parse mathematical expressions possibly containing variables and operators. On the one hand, it comes with a list of default operators for floating point values. For differentiable default operators, Exmex can compute partial derivatives. On the other hand, users can define their own operators and work with different data types such as float, integer, bool, or other types that implement Clone
, FromStr
, Debug
, Default
.
Parts of Exmex' functionality are accessible from Python via Mexpress.
Run
cargo add exmex
in your project's directory for the latest relase. If you want to use the newest version of Exmex, add
[dependencies]
# ...
exmex = { git = "https://github.com/bertiqwerty/exmex.git", branch = "main" }
to your Cargo.toml
.
To simply evaluate a string there is
let result = exmex::eval_str::<f64>("e^(2*π-τ)")?;
assert!((result - 1.0).abs() < 1e-12);
where π
/PI
, τ
/TAU
, and Euler's number E
/e
are available as constants.
To create an expression with variables that represents a mathematical function you can use any string that does not define an operator or constant and matches r"[a-zA-Zα-ωΑ-Ω_]+[a-zA-Zα-ωΑ-Ω_0-9]*"
as in
use exmex::prelude::*;
let expr = exmex::parse::<f64>("2*x^3-4/y")?;
The wildcard-import from prelude
makes only the expression-trait Express
and its implementation FlatEx
, a flattened expression, accessible. To use variables, you do not need to use a context or tell the parser explicitly what variables are. To evaluate the function at, e.g., x=2.0
and y=4.0
you can use
let result = expr.eval(&[2.0, 4.0])?;
assert!((result - 15.0).abs() < 1e-12);
The order of the variables' values passed for evaluation has to match the alphabetical order of the variable names.
Besides predefined operators for floats, you can implement custom operators and use their factory type as generic argument as shown in the following example.
use exmex::prelude::*;
use exmex::{BinOp, MakeOperators, Operator};
ops_factory!(
BitwiseOpsFactory,
u32,
Operator::make_bin(
"|",
BinOp {
apply: |a, b| a | b,
prio: 0,
is_commutative: true,
}
),
Operator::make_unary("!", |a| !a)
);
let expr = FlatEx::<_, BitwiseOpsFactory>::parse("!(a|b)")?;
let result = expr.eval(&[0, 1])?;
assert_eq!(result, u32::MAX - 1);
More involved examples of data types are
- operators as operands as used for day 19 of Advent of Code 2020 and
- the type
Val
that can be activated with the featurevalue
, see below.
To compute partial derivatives of expressions with floating point numbers, you can use the method partial
after activating the Exmex-feature partial
in the Cargo.toml
via
[dependencies]
exmex = { ..., features = ["partial"] }
The result of the method partial
is again an expression.
use exmex::prelude::*;
let expr = exmex::parse::<f64>("y*x^2")?;
// d_x
let dexpr_dx = expr.partial(0)?;
assert_eq!(format!("{}", dexpr_dx), "({x}*2.0)*{y}");
// d_xy
let ddexpr_dxy = dexpr_dx.partial(1)?;
assert_eq!(format!("{}", ddexpr_dxy), "{x}*2.0");
let result = ddexpr_dxy.eval(&[2.0, f64::MAX])?;
assert!((result - 4.0).abs() < 1e-12);
// d_xyx
let dddexpr_dxyx = ddexpr_dxy.partial(0)?;
assert_eq!(format!("{}", dddexpr_dxyx), "2.0");
let result = dddexpr_dxyx.eval(&[f64::MAX, f64::MAX])?;
assert!((result - 2.0).abs() < 1e-12);
// all in one
let dddexpr_dxyx_iter = expr.partial_iter([0, 1, 0].iter())?;
assert_eq!(format!("{}", dddexpr_dxyx_iter), "2.0");
let result = dddexpr_dxyx_iter.eval(&[f64::MAX, f64::MAX])?;
assert!((result - 2.0).abs() < 1e-12);
After activating the Exmex-feature value
one can use expressions with data of type Val
, inspired by the type Value
from the crate Evalexpr. An instance of Val
can contain a boolean, an int, a float, or a vector of floats. This way, it is possible to use booleans, ints, floats, and vectors in the same expression. Further, Exmex provides in terms of ValOpsFactory
a pre-defined set of operators for Val
. See the following example of a Python-like if
-else
-operator.
use exmex::{Express, Val};
let expr = exmex::parse_val::<i32, f64>("0 if b < c else 1.2")?;
let res = expr.eval(&[Val::Float(34.0), Val::Int(21)])?.to_float()?;
assert!((res - 1.2).abs() < 1e-12);
See the Val
-docs for an example containing vectors.
If both partial
and value
are activated, partial derivatives can be computed for expressions of types such
as FlatExVal<i32, f64>
. This is currently not supported for vectors.
use exmex::{Differentiate, Express, Val};
let expr = exmex::parse_val::<i32, f64>("3*x if x > 1 else x^2")?;
let deri = expr.partial(0)?;
let res = deri.eval(&[Val::Float(1.0)])?.to_float()?;
assert!((res - 2.0).abs() < 1e-12);
let res = deri.eval(&[Val::Float(7.0)])?.to_float()?;
assert!((res - 3.0).abs() < 1e-12);
To use serde
activate the feature serde
.
More features and examples including integer data types and boolean literals can be found for the documentation release under docs.rs/exmex/ or generated via
cargo doc --all-features --no-deps
Exmex was created with flexibility (e.g., use your own operators, literals, and types), ergonomics (e.g., just finds variables), and evaluation speed in mind. On the other hand, Exmex is slower than the other crates during parsing. However, evaluation might be more performance critical depending on the application.
The expressions used to compare Exmex with other creates are:
sin: "sin(x)+sin(y)+sin(z)",
power: "x^2+y*y+z^z",
nested: "x*0.02*sin(-(3*(2*sin(x-1/(sin(y*5)+(5.0-1/z))))))",
compile: "x*0.2*5/4+x*2*4*1*1*1*1*1*1*1+7*sin(y)-z/sin(3.0/2/(1-x*4*1*1*1*1))",
The following table shows mean runtimes of 5-evaluation-runs with increasing x
-values on a Macbook Pro M1 Max in micro-seconds, i.e., smaller means better. Criterion-based benchmarks can be executed via
cargo bench --all-features --bench benchmark -- --noplot --sample-size 10 --nresamples 10
to compute the results. Reported is the best result over multiple invocations. More about taking the minimum run-time for benchmarking can be found below.
sin | power | nested | compile | comment | |
---|---|---|---|---|---|
Evalexpr | 3.9 | 3.23 | 7.84 | 11.06 | more than mathematical expressions |
Exmex f64 |
0.14 | 0.18 | 0.5 | 0.3 | can compute partial derivatives |
Exmex uncompiled f64 |
0.14 | 0.18 | 0.5 | 0.66 | can compute partial derivatives |
Exmex Val |
0.57 | 0.45 | 1.14 | 0.94 | multiple data types in one expression possible |
Fasteval | 0.68 | 0.78 | 1.19 | 1.03 | only f64 , supports a faster, unsafe mode |
Meval | 0.42 | 0.43 | 0.79 | 0.99 | only f64 , no custom operators |
Rsc | 4.52 | 5.0 | 10.47 | 19.8 |
Note that we also tried the optimization flag --emit=asm
which did not change the results qualitatively. Benchmarks for parsing all expressions again in μs on the aforementioned machine are shown in the following.
all expressions | |
---|---|
Evalexpr | 28.95 |
Exmex f64 |
23.45 |
Exmex uncompiled f64 |
20.00 |
Exmex Val |
33.32 |
Fasteval | 13.04 |
Meval | 15.00 |
Rsc | 14.64 |
Exmex parsing can be made faster by passing only the relevant operators.
The crates Mexprp and Asciimath did not run without errors on Win10. However, I have not tried to use them in a while. More details about the benchmarking can be found in the source file.
Note that Criterion does not provide the option to simply report the minimum runtime. A talk by Andrei Alexandrescu explains why I think taking the minimum is a good idea in many cases. See also bheisler/criterion.rs#485.
You as library user can select between MIT and Apache 2.0.