happydpc / exmex

Math parser and evaluator in Rust, capable of partial differentiation, allows the use of custom operators.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crate API example workflow license

Exmex

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, and Debug.

Parts of Exmex' functionality are accessible from Python via Mexpress.

Installation

Add

[dependencies]
# ...
exmex = "0.12.0"

to your Cargo.toml 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.

Basic Usage

To simply evaluate a string there is

use exmex;
let result = exmex::eval_str("sin(73)")?;

To create an expression with variables that represents a mathematical function you can use any string that does not define an operator and matches r"[a-zA-Zα-ωΑ-Ω_]+[a-zA-Zα-ωΑ-Ω_0-9]*" as in

use exmex::prelude::*;
let expr = exmex::parse::<f64>("2*β^3-4/τ")?;

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., β=5.3 and τ=0.5 you can use

let result = expr.eval(&[5.3, 0.5])?;
assert!((result - 289.75399999999996).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>::from_str("!(a|b)")?;
let result = expr.eval(&[0, 1])?;
assert_eq!(result, u32::MAX - 1);

More involved examples of data types are

Expressions of type FlatEx have a lifetime parameter since they avoid string copies. There is also the type OwnedFlatEx that does not need a lifetime parameter, see the docs.

Partial Differentiation

To compute partial derivatives of expressions with floating point numbers, you can use the method partial. The result 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);

Mixing Data Types in one Expression with the Feature value

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, or a float. This way, it is possible to use booleans, ints, and floats 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);

Serialization and Deserialization

To use serde activate the feature serde.

Documentation

More documentation and examples including integer data types and boolean literals can be found for the latest release under docs.rs/exmex/ or generated via

cargo doc

Benchmarks v0.12.0

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 Win10 machine with an i7-10850H 2.7 GHz processor in micro-seconds, i.e., smaller means better. Criterion-based benchmarks can be executed via

cargo bench --bench benchmark -- --noplot --sample-size 10 --nresamples 20

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 5.88 4.51 19.36 21.11 more than mathematical expressions
Exmex f64 0.27 0.5 0.57 0.53
Exmex Val 0.77 1.13 1.87 1.73 multiple data types in one expression possible
Fasteval 1.19 1.46 1.59 1.6 only f64, supports a faster, unsafe mode
Meval 0.65 0.66 0.82 1.01 only f64, no custom operators
Rsc 4.88 8.21 13.32 24.28

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 35.94
Exmex f64 48.52
Exmex Val 55.24
Fasteval 18.42
Meval 17.99
Rsc 20.50

Exmex parsing can be made faster by passing only the relevant operators.

The crates Mexprp and Asciimath did not run without errors on Win10. 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.

License

You as library user can select between MIT and Apache 2.0.

About

Math parser and evaluator in Rust, capable of partial differentiation, allows the use of custom operators.

License:Apache License 2.0


Languages

Language:Rust 100.0%