Skip to main content

Expr

Enum Expr 

Source
pub enum Expr {
    Const(f64),
    Var(String),
    Add(Box<Expr>, Box<Expr>),
    Mul(Box<Expr>, Box<Expr>),
    Pow(Box<Expr>, Box<Expr>),
    Neg(Box<Expr>),
    Fn(MathFn, Box<Expr>),
}
Expand description

A symbolic expression AST.

Sub is represented as Add(a, Neg(b)) and Div as Mul(a, Pow(b, Const(-1))).

§Examples

let expr = var("x") + constant(1.0);
let mut vars = HashMap::new();
vars.insert("x".to_string(), 2.0);
assert!((expr.eval(&vars).unwrap() - 3.0).abs() < 1e-10);

Variants§

§

Const(f64)

Numeric constant.

§

Var(String)

Named variable.

§

Add(Box<Expr>, Box<Expr>)

Addition: lhs + rhs.

§

Mul(Box<Expr>, Box<Expr>)

Multiplication: lhs * rhs.

§

Pow(Box<Expr>, Box<Expr>)

Exponentiation: base ^ exp.

§

Neg(Box<Expr>)

Negation: -expr.

§

Fn(MathFn, Box<Expr>)

Function application: f(arg).

Implementations§

Source§

impl Expr

Source

pub fn eval(&self, vars: &HashMap<String, f64>) -> Result<f64>

Evaluate the expression given concrete variable bindings.

§Examples
let expr = constant(2.0) * var("x") + constant(1.0);
let vars = HashMap::from([("x".to_string(), 3.0)]);
assert!((expr.eval(&vars).unwrap() - 7.0).abs() < 1e-10);
Source

pub fn substitute(&self, var: &str, replacement: &Expr) -> Expr

Replace every occurrence of var with replacement.

§Examples
let expr = var("x") + constant(1.0);
let replaced = expr.substitute("x", &constant(5.0));
assert!((replaced.eval(&HashMap::new()).unwrap() - 6.0).abs() < 1e-10);
Source

pub fn free_variables(&self) -> HashSet<String>

Collect all free variable names in the expression.

§Examples
let expr = var("x") + var("y") * constant(2.0);
let vars = expr.free_variables();
assert!(vars.contains("x"));
assert!(vars.contains("y"));
assert_eq!(vars.len(), 2);
Source

pub fn is_zero(&self) -> bool

Returns true if the expression is Const(0.0).

§Examples
assert!(constant(0.0).is_zero());
assert!(!constant(1.0).is_zero());
Source

pub fn is_one(&self) -> bool

Returns true if the expression is Const(1.0).

§Examples
assert!(constant(1.0).is_one());
assert!(!constant(2.0).is_one());
Source

pub fn is_const(&self) -> bool

Returns true if the expression is a constant.

§Examples
assert!(constant(3.14).is_const());
assert!(!var("x").is_const());
Source

pub fn as_const(&self) -> Option<f64>

If the expression is a constant, return its value.

§Examples
assert_eq!(constant(42.0).as_const(), Some(42.0));
assert_eq!(var("x").as_const(), None);

Trait Implementations§

Source§

impl Add for Expr

Source§

type Output = Expr

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl Clone for Expr

Source§

fn clone(&self) -> Expr

Returns a duplicate 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 Debug for Expr

Source§

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

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

impl Display for Expr

Source§

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

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

impl Div for Expr

Source§

type Output = Expr

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self

Performs the / operation. Read more
Source§

impl Mul for Expr

Source§

type Output = Expr

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl Neg for Expr

Source§

type Output = Expr

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Expr

Source§

fn eq(&self, other: &Expr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for Expr

Source§

type Output = Expr

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl StructuralPartialEq for Expr

Auto Trait Implementations§

§

impl Freeze for Expr

§

impl RefUnwindSafe for Expr

§

impl Send for Expr

§

impl Sync for Expr

§

impl Unpin for Expr

§

impl UnsafeUnpin for Expr

§

impl UnwindSafe for Expr

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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§

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>,

Source§

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>,

Source§

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.