Struct exmex::FlatEx

source ·
pub struct FlatEx<T, OF = FloatOpsFactory<T>, LM = NumberMatcher>
where T: Debug + Clone, OF: MakeOperators<T>, LM: MatchLiteral,
{ /* private fields */ }
Expand description

Flattened expressions 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.

Creation of expressions is possible with the function parse which is equivalent to FlatEx::parse.

use exmex::prelude::*;

// create an expression by parsing a string
let expr = FlatEx::<f32>::parse("sin(1+y)*x")?;
assert!((expr.eval(&[1.5, 2.0])? - (1.0 + 2.0 as f32).sin() * 1.5).abs() < 1e-6);

The 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.

Implementations§

source§

impl<T, OF, LMF> FlatEx<T, OF, LMF>
where T: DataType, OF: MakeOperators<T>, LMF: MatchLiteral,

source

pub fn new( nodes: SmallVec<[FlatNode<T>; 32]>, ops: SmallVec<[FlatOp<T>; 32]>, prio_indices: SmallVec<[usize; 32]>, var_names: SmallVec<[String; 16]>, text: String ) -> Self

source

pub fn compile(&mut self)

Executes calculations that can trivially be executed, e.g., multiplies two numbers that need to be multiplied anyway.

source

pub fn parse_wo_compile(text: &str) -> ExResult<Self>
where T: DataType, <T as FromStr>::Err: Debug,

Parses into an expression without compilation. Allow slightly faster direct evaluation of strings.

source

pub fn var_indices_ordered(&self) -> SmallVec<[usize; 16]>

Returns the indices of the variables in the order of their occurrence during the operations

source

pub fn eval_vec(&self, vars: Vec<T>) -> ExResult<T>

Consumes vector for evaluation, possibly useful for large value types.

source

pub fn eval_iter(&self, vars: impl Iterator<Item = T>) -> ExResult<T>

Collects iterator into SmallVec.

Trait Implementations§

source§

impl<'a, T, OF, LM> Calculate<'a, T> for FlatEx<T, OF, LM>
where T: DataType, OF: MakeOperators<T> + Debug, LM: MatchLiteral + Debug, <T as FromStr>::Err: Debug,

source§

fn operate_unary(self, repr: &'a str) -> ExResult<Self>

Applies a unary operator. Read more
source§

fn operate_binary(self, other: Self, repr: &'a str) -> ExResult<Self>

Applies a binary operator. Read more
source§

fn subs<F>(self, sub: &mut F) -> ExResult<Self>
where F: FnMut(&str) -> Option<Self>,

Substitutes a variable with another expression. Read more
source§

fn from_num(x: T) -> Self

Create an expression that contains exactly one number. Read more
source§

impl<T, OF, LM> Clone for FlatEx<T, OF, LM>
where T: Debug + Clone + Clone, OF: MakeOperators<T> + Clone, LM: MatchLiteral + Clone,

source§

fn clone(&self) -> FlatEx<T, OF, LM>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T, OF, LM> Debug for FlatEx<T, OF, LM>
where T: Debug + Clone + Debug, OF: MakeOperators<T> + Debug, LM: MatchLiteral + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de, T: DataType + 'de, OF: MakeOperators<T>, LMF: MatchLiteral> Deserialize<'de> for FlatEx<T, OF, LMF>
where <T as FromStr>::Err: Debug,

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a, T, OF, LM> Differentiate<'a, T> for FlatEx<T, OF, LM>
where T: DiffDataType, OF: MakeOperators<T> + Debug, LM: MatchLiteral + Debug, <T as FromStr>::Err: Debug,

source§

fn partial(self, var_idx: usize) -> ExResult<Self>

feature = "partial" - This method computes a new expression that is the partial derivative of self with default operators. Read more
source§

fn partial_relaxed( self, var_idx: usize, missing_op_mode: MissingOpMode ) -> ExResult<Self>

Like Differentiate::partial. The only difference is that in case there is no differentation defined for a binary operator this will not necessarily throw an error depending on missing_op_mode, see MissingOpMode.
source§

fn partial_nth(self, var_idx: usize, n: usize) -> ExResult<Self>

feature = "partial" - Computes the nth partial derivative with respect to one variable Read more
source§

fn partial_nth_relaxed( self, var_idx: usize, n: usize, missing_op_mode: MissingOpMode ) -> ExResult<Self>

Like Differentiate::partial_nth. The only difference is that in case there is no differentation defined for a binary operator this will not necessarily throw an error depending on missing_op_mode, see MissingOpMode.
source§

fn partial_iter<I>(self, var_idxs: I) -> ExResult<Self>
where I: Iterator<Item = usize> + Clone,

feature = "partial" - Computes a chain of partial derivatives with respect to the variables passed as iterator Read more
source§

fn partial_iter_relaxed<I>( self, var_idxs: I, missing_op_mode: MissingOpMode ) -> ExResult<Self>
where I: Iterator<Item = usize> + Clone,

Like Differentiate::partial_iter. The only difference is that in case there is no differentation defined for a binary this will not necessarily throw an error depending on missing_op_mode, see MissingOpMode.
source§

impl<T, OF, LMF> Display for FlatEx<T, OF, LMF>
where T: DataType, OF: MakeOperators<T>, LMF: MatchLiteral, <T as FromStr>::Err: Debug,

The expression is displayed as a string created by unparse.

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, T, OF, LM> Express<'a, T> for FlatEx<T, OF, LM>
where T: DataType, OF: MakeOperators<T>, LM: MatchLiteral, <T as FromStr>::Err: Debug,

§

type LiteralMatcher = LM

§

type OperatorFactory = OF

source§

fn eval(&self, vars: &[T]) -> ExResult<T>

Evaluates an expression with the given variable values and returns the computed result. Read more
source§

fn eval_relaxed(&self, vars: &[T]) -> ExResult<T>

Evaluates an expression with the given variable values and returns the computed result. If more variables are passed than necessary the unnecessary ones are ignored. Read more
source§

fn unparse(&self) -> &str

Creates an expression string that corresponds to the FlatEx instance. Read more
source§

fn var_names(&self) -> &[String]

Returns the variables of the expression
source§

fn to_deepex(self) -> ExResult<DeepEx<'a, T, OF, LM>>
where Self: Sized, T: DataType, <T as FromStr>::Err: Debug,

Conversion to a deep expression necessary for computations with expressions
source§

fn from_deepex(deepex: DeepEx<'_, T, OF, LM>) -> ExResult<Self>
where Self: Sized, T: DataType, <T as FromStr>::Err: Debug,

source§

fn parse(text: &'a str) -> ExResult<Self>
where Self: Sized,

source§

impl<T, OF, LM> Ord for FlatEx<T, OF, LM>
where T: Debug + Clone + Ord, OF: MakeOperators<T> + Ord, LM: MatchLiteral + Ord,

source§

fn cmp(&self, other: &FlatEx<T, OF, LM>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T, OF, LM> PartialEq for FlatEx<T, OF, LM>

source§

fn eq(&self, other: &FlatEx<T, OF, LM>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, OF, LM> PartialOrd for FlatEx<T, OF, LM>

source§

fn partial_cmp(&self, other: &FlatEx<T, OF, LM>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: DataType, OF: MakeOperators<T>, LMF: MatchLiteral> Serialize for FlatEx<T, OF, LMF>
where <T as FromStr>::Err: Debug,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T, OF, LM> Eq for FlatEx<T, OF, LM>
where T: Debug + Clone + Eq, OF: MakeOperators<T> + Eq, LM: MatchLiteral + Eq,

source§

impl<T, OF, LM> StructuralEq for FlatEx<T, OF, LM>
where T: Debug + Clone, OF: MakeOperators<T>, LM: MatchLiteral,

source§

impl<T, OF, LM> StructuralPartialEq for FlatEx<T, OF, LM>
where T: Debug + Clone, OF: MakeOperators<T>, LM: MatchLiteral,

Auto Trait Implementations§

§

impl<T, OF, LM> RefUnwindSafe for FlatEx<T, OF, LM>

§

impl<T, OF, LM> Send for FlatEx<T, OF, LM>
where LM: Send, OF: Send, T: Send,

§

impl<T, OF, LM> Sync for FlatEx<T, OF, LM>
where LM: Sync, OF: Sync, T: Sync,

§

impl<T, OF, LM> Unpin for FlatEx<T, OF, LM>
where LM: Unpin, OF: Unpin, T: Unpin,

§

impl<T, OF, LM> UnwindSafe for FlatEx<T, OF, LM>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,