Exmex is a fast, simple, and extendable mathematical expression evaluator with the ability to compute partial derivatives of expressions.
The following snippet shows how to evaluate a string.
# use Error;
#
For floats, we have a list of predifined operators containing
^, *, /, +, -, sin, cos, tan, exp, log, and log2. The full list is
defined in DefaultOpsFactory. Library users can also create their
own operators as shown below in the section about extendability.
Variables
To define variables we can use strings that are not in the list of operators as shown in the following expression.
Additionally, variables should consist only of letters, numbers, and underscores. More precisely, they need to fit the
regular expression
r"^[a-zA-Z_]+[a-zA-Z_0-9]*".
Variables' values are passed as slices to eval.
# use Error;
#
The n-th number in the slice corresponds to the n-th variable. Thereby, the
alphatical order of the variables is relevant. In this example, we have y=3.7 and z=2.5.
If variables are between curly brackets, they can have arbitrary names, e.g.,
{456/549*(}, {x}, and confusingly even {x+y} are valid variable names as shown in the following.
# use Error;
#
The value returned by parse implements the Express trait
and is an instance of the struct FlatEx.
Extendability
How to use custom operators as well as custom data types of the operands even with non-numeric literals is described in the following sub-sections.
Custom Operators
Operators are instances of the struct
Operator. An operator's representation in the string-to-be-parsed is defined in the field
repr. Further, operator instances define a binary and a unary operator, since an operator
representation can correspond to both such as - or +. Note that we expect a unary operator to be always
on the left of a number.
To make serialization via serde possible, operators need to be created by factories as
shown in the following.
# use Error;
#
To extend an existing list of operators, the macro ops_factory is not
sufficient. In this case one has to create a factory struct and implement the
MakeOperators trait with a little boilerplate code.
# use Error;
#
Custom Data Types of Numbers
You can use any type that implements Copy and
FromStr. In case the representation of your data type in the
string does not match the number regex r"\.?[0-9]+(\.[0-9]+)?", you have to pass a
suitable regex and use the function
from_pattern instead of
from_str. Here is an example for bool.
# use Error;
#
Partial Derivatives
For default operators, expressions can be transformed into their partial derivatives
again represented by expressions. To this end, there exists the method partial.
# use Error;
#
Owned Expression
You cannot return all expression types from a function without a lifetime parameter.
For instance, expressions that are instances of FlatEx keep &strs instead of
Strings of variable or operator names to make faster parsing possible.
# use Error;
#
If you are willing to pay the price of roughly doubled parsing times, you can
obtain an expression that is an instance of OwnedFlatEx and owns
its strings. Evaluation times should be comparable. However, a lifetime parameter is
not needed anymore as shown in the following.
# use Error;
#
Priorities and Parentheses
In Exmex-land, unary operators always have higher priority than binary operators, e.g.,
-2^2=4 instead of -2^2=-4. Moreover, we are not too strict regarding parentheses.
For instance
# use Error;
#
If you want to be on the safe side, we suggest using parentheses.
Display
An instance of FlatEx can be displayed as string. Note that this
unparsed string does not necessarily coincide with the original
string, since, e.g., curly brackets are added and expressions are compiled.
# use Error;
#
Serialization and Deserialization
To use serde you can activate the feature serde.
The implementation un-parses and re-parses the whole expression.
Deserialize and
Serialize are implemented for
both, FlatEx and OwnedFlatEx.
Unicode
Unicode input strings are currently not supported 😕 but might be added in the future 😀.