pub struct FlatEx<T, OF = FloatOpsFactory<T>, LM = NumberMatcher>{ /* 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>
impl<T, OF, LMF> FlatEx<T, OF, LMF>
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
sourcepub fn compile(&mut self)
pub fn compile(&mut self)
Executes calculations that can trivially be executed, e.g., multiplies two numbers that need to be multiplied anyway.
sourcepub fn parse_wo_compile(text: &str) -> ExResult<Self>
pub fn parse_wo_compile(text: &str) -> ExResult<Self>
Parses into an expression without compilation. Allow slightly faster direct evaluation of strings.
sourcepub fn var_indices_ordered(&self) -> SmallVec<[usize; 16]>
pub fn var_indices_ordered(&self) -> SmallVec<[usize; 16]>
Returns the indices of the variables in the order of their occurrence during the operations
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,
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§impl<'de, T: DataType + 'de, OF: MakeOperators<T>, LMF: MatchLiteral> Deserialize<'de> for FlatEx<T, OF, LMF>
impl<'de, T: DataType + 'de, OF: MakeOperators<T>, LMF: MatchLiteral> Deserialize<'de> for FlatEx<T, OF, LMF>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
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,
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>
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 moresource§fn partial_relaxed(
self,
var_idx: usize,
missing_op_mode: MissingOpMode,
) -> ExResult<Self>
fn partial_relaxed( self, var_idx: usize, missing_op_mode: MissingOpMode, ) -> ExResult<Self>
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>
fn partial_nth(self, var_idx: usize, n: usize) -> ExResult<Self>
feature = "partial"
- Computes the nth partial derivative with respect to one variable Read moresource§fn partial_nth_relaxed(
self,
var_idx: usize,
n: usize,
missing_op_mode: MissingOpMode,
) -> ExResult<Self>
fn partial_nth_relaxed( self, var_idx: usize, n: usize, missing_op_mode: MissingOpMode, ) -> ExResult<Self>
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>
fn partial_iter<I>(self, var_idxs: I) -> ExResult<Self>
feature = "partial"
- Computes a chain of partial derivatives with respect to the variables passed as iterator Read moresource§fn partial_iter_relaxed<I>(
self,
var_idxs: I,
missing_op_mode: MissingOpMode,
) -> ExResult<Self>
fn partial_iter_relaxed<I>( self, var_idxs: I, missing_op_mode: MissingOpMode, ) -> ExResult<Self>
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>
impl<T, OF, LMF> Display for FlatEx<T, OF, LMF>
The expression is displayed as a string created by unparse
.
source§impl<'a, T, OF, LM> Express<'a, T> for FlatEx<T, OF, LM>
impl<'a, T, OF, LM> Express<'a, T> for FlatEx<T, OF, LM>
type LiteralMatcher = LM
type OperatorFactory = OF
source§fn eval(&self, vars: &[T]) -> ExResult<T>
fn eval(&self, vars: &[T]) -> ExResult<T>
source§fn eval_relaxed(&self, vars: &[T]) -> ExResult<T>
fn eval_relaxed(&self, vars: &[T]) -> ExResult<T>
source§fn unparse(&self) -> &str
fn unparse(&self) -> &str
FlatEx
instance. Read moresource§fn to_deepex(self) -> ExResult<DeepEx<'a, T, OF, LM>>
fn to_deepex(self) -> ExResult<DeepEx<'a, T, OF, LM>>
fn from_deepex(deepex: DeepEx<'_, T, OF, LM>) -> ExResult<Self>
fn parse(text: &'a str) -> ExResult<Self>where
Self: Sized,
source§fn binary_reprs(&self) -> SmallVec<[String; 16]>
fn binary_reprs(&self) -> SmallVec<[String; 16]>
source§impl<T, OF, LM> Ord for FlatEx<T, OF, LM>
impl<T, OF, LM> Ord for FlatEx<T, OF, LM>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<T, OF, LM> PartialOrd for FlatEx<T, OF, LM>where
T: Debug + Clone + PartialOrd,
OF: MakeOperators<T> + PartialOrd,
LM: MatchLiteral + PartialOrd,
impl<T, OF, LM> PartialOrd for FlatEx<T, OF, LM>where
T: Debug + Clone + PartialOrd,
OF: MakeOperators<T> + PartialOrd,
LM: MatchLiteral + PartialOrd,
source§impl<T: DataType, OF: MakeOperators<T>, LMF: MatchLiteral> Serialize for FlatEx<T, OF, LMF>
impl<T: DataType, OF: MakeOperators<T>, LMF: MatchLiteral> Serialize for FlatEx<T, OF, LMF>
impl<T, OF, LM> Eq for FlatEx<T, OF, LM>
impl<T, OF, LM> StructuralPartialEq for FlatEx<T, OF, LM>
Auto Trait Implementations§
impl<T, OF, LM> Freeze for FlatEx<T, OF, LM>where
T: Freeze,
impl<T, OF, LM> RefUnwindSafe for FlatEx<T, OF, LM>
impl<T, OF, LM> Send for FlatEx<T, OF, LM>
impl<T, OF, LM> Sync for FlatEx<T, OF, LM>
impl<T, OF, LM> Unpin for FlatEx<T, OF, LM>
impl<T, OF, LM> UnwindSafe for FlatEx<T, OF, LM>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)