Trait exmex::Calculate

source ·
pub trait Calculate<'a, T>: Express<'a, T>
where T: DataType, <T as FromStr>::Err: Debug, Self: Sized,
{ // Provided methods fn operate_unary(self, repr: &'a str) -> ExResult<Self> { ... } fn operate_binary(self, other: Self, repr: &'a str) -> ExResult<Self> { ... } fn subs<F>(self, sub: &mut F) -> ExResult<Self> where F: FnMut(&str) -> Option<Self> { ... } fn from_num(x: T) -> Self { ... } }
Expand description

Calculation with expression such as application of operators or substitution

Provided Methods§

source

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

Applies a unary operator.

Arguments
  • repr - representation of the binary operator
Errors
  • The passed string can be found in the list of operator representations.
source

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

Applies a binary operator.

Arguments
  • other - other expression to be used in the in the binary operation
  • repr - representation of the binary operator
Errors
  • The passed string can be found in the list of operator representations.
source

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

Substitutes a variable with another expression.

Arguments
  • sub - function that assigns to each variable name optionally a new expression
Example
use exmex::{DeepEx, prelude::*};
use std::f64;
let expr = DeepEx::<f64>::parse("-z/x + 2^7 + E")?;
let mut sub = |var: &str| match var {
    "z" => Some(DeepEx::<f64>::parse("2*y").unwrap()),
    _ => None,
};
let substituted = expr.subs(&mut sub)?;
assert_eq!(substituted.var_names(), ["x", "y"]);
let reference = 2.0f64.powi(7) + f64::consts::E + 1.0;
assert_eq!(substituted.eval(&[-4.0, 2.0])?, reference);
source

fn from_num(x: T) -> Self

Create an expression that contains exactly one number.

Arguments
  • x - number the expression will represent

Object Safety§

This trait is not object safe.

Implementors§

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§

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