Struct exmex::FlatEx [−][src]
Expand description
This is the core data type representing a flattened expression and the result of
parsing a string. We use flattened expressions to make efficient evaluation possible.
Simplified, a flat expression consists of a SmallVec
of nodes and a SmallVec of operators that are applied
to the nodes in an order following operator priorities.
You create an expression with the parse function or one of its
variants, namely parse_with_default_ops and parse_with_number_pattern.
use exmex::{parse_with_default_ops};
// create an expression by parsing a string
let expr = parse_with_default_ops::<f32>("sin(1+y)*x")?;
assert!((expr.eval(&[1.5, 2.0])? - (1.0 + 2.0 as f32).sin() * 1.5).abs() < 1e-6);The second argument &[1.5, 2.0] in the call of eval specifies the
variable values in the alphabetical order of the variable names.
In this example, we want to evaluate the expression for the varibale values x=2.0 and y=1.5.
Variables in the string to-be-parsed are all substrings that are no numbers, no
operators, and no parentheses.
Implementations
Evaluates an expression with the given variable values and returns the computed result.
Arguments
vars- Values of the variables of the expression; the n-th value corresponds to the n-th variable in alphabetical order. Thereby, only the first occurrence of the variable in the string is relevant. If an expression has been created by partial derivation, the variables always coincide with those of the antiderivatives even in cases where variables are irrelevant such as(x)'=1.
Errors
If the number of variables in the parsed expression are different from the length of
the variable slice, we return an ExParseError.
This method computes a FlatEx instance that is a partial derivative of self with default operators
as shown in the following example.
use exmex::{parse_with_default_ops};
let expr = parse_with_default_ops::<f64>("sin(1+y^2)*x")?;
let dexpr_dx = expr.clone().partial(0)?;
let dexpr_dy = expr.partial(1)?;
assert!((dexpr_dx.eval(&[9e5, 2.0])? - (5.0 as f64).sin()).abs() < 1e-12);
// |
// This partial derivative dexpr_dx does depend on x. Still, it expects
// the same number of parameters as the corresponding
// antiderivative. Hence, you can pass any number for x.
assert!((dexpr_dy.eval(&[2.5, 2.0])? - 10.0 * (5.0 as f64).cos()).abs() < 1e-12);Arguments
var_idx- variable with respect to which the partial derivative is computed
Errors
- If
selfhas beenclear_deepexed, we cannot compute the partial derivative and return anExParseError. - If you use none-default operators this might not work as expected. It could return an
ExParseErrorif an operator is not found or compute a wrong result if an operator is defined in an un-expected way.
Creates an expression string that corresponds to the FlatEx instance. This is
not necessarily the input string. More precisely,
- variables are put between curly braces,
- spaces outside of curly brackets are ignored,
- parentheses can be different from the input, and
- expressions are compiled as shown in the following example.
use exmex::parse_with_default_ops;
let flatex = parse_with_default_ops::<f64>("--sin ( z) + {another var} + 1 + 2")?;
assert_eq!(format!("{}", flatex), "-(-(sin({z})))+{another var}+3.0");Usually, a FlatEx instance keeps a nested, deep structure of the expression
that is not necessary for evaluation. This functions removes the deep expression
to reduce memory consumption. The methods partial and
unparse as well as the implementation of the
Display trait will stop working after calling this function.
Trait Implementations
Deserialize this value from the given Serde deserializer. Read more
The expression is displayed as a string created by unparse.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
Auto Trait Implementations
impl<'a, T> RefUnwindSafe for FlatEx<'a, T> where
T: RefUnwindSafe,
impl<'a, T> UnwindSafe for FlatEx<'a, T> where
T: RefUnwindSafe + UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more