Trait exmex::prelude::Express[][src]

pub trait Express<'a, T> {
    fn from_str(text: &'a str) -> ExResult<Self>
    where
        <T as FromStr>::Err: Debug,
        T: FromStr,
        Self: Sized
;
fn from_regex(text: &'a str, number_regex: &Regex) -> ExResult<Self>
    where
        <T as FromStr>::Err: Debug,
        T: DataType,
        Self: Sized
;
fn from_pattern(text: &'a str, number_regex_pattern: &str) -> ExResult<Self>
    where
        <T as FromStr>::Err: Debug,
        T: DataType,
        Self: Sized
;
fn eval(&self, vars: &[T]) -> ExResult<T>;
fn partial(self, var_idx: usize) -> ExResult<Self>
    where
        Self: Sized,
        T: DataType + Float,
        <T as FromStr>::Err: Debug
;
fn unparse(&self) -> ExResult<String>;
fn reduce_memory(&mut self);
fn n_vars(&self) -> usize; }
Expand description

Expressions implementing this trait can be evaluated for specific variable values, differentiated partially, and unparsed, i.e., transformed into a string representation.

Required methods

Parses a string into an expression that can be evaluated.

Arguments
  • text - string to be parsed into an expression
Errors

An error is returned if text cannot be parsed.

Use custom number literals defined as regex to create an expression that can be evaluated.

Arguments
  • text - string to be parsed into an expression
  • number_regex - compiled regex whose matches are number literals
Errors

An ExError is returned, if

  • the text cannot be parsed.

Use custom number literals defined as regex patterns to create an expression that can be evaluated.

Arguments
  • text - string to be parsed into an expression
  • number_regex_pattern - regex pattern whose matches are number literals at the beginning of a string. For instance, the regex to match boolean literals is ^(true|false) instead of true|false.
Errors

An ExError is returned, if

  • the argument number_regex_pattern cannot be compiled or
  • the text cannot be parsed.

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

This method computes a new instance that is a partial derivative of self with default operators.

Example
use exmex::prelude::*;

let expr = FlatEx::<f64>::from_str("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);
//             |    
//           The 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 self has been reduce_memoryed, we cannot compute the partial derivative and return an ExError.
  • If you use custom operators this might not work as expected. It could return an ExError if 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.

use exmex::prelude::*;
let flatex = FlatEx::<f64>::from_str("--sin ( z) +  {another var} + 1 + 2")?;
assert_eq!(format!("{}", flatex), "--sin ( z) +  {another var} + 1 + 2");

This function frees some memory. After calling partial memory might be re-allocated.

Returns the number of variables of the expression

Implementors