Enum Expression

Source
pub enum Expression {
    BinOp(BinOp, Box<Expression>, Box<Expression>),
    Constant(f32),
    Variable(String),
}

Variants§

§

BinOp(BinOp, Box<Expression>, Box<Expression>)

§

Constant(f32)

§

Variable(String)

Implementations§

Source§

impl Expression

Source

pub fn parse(s: &str) -> Result<Self, ParserError>

Parses an expression from a string.

§Examples

Basic usage;

use math_engine::expression::Expression;

let expr = Expression::parse("1.0 + x").unwrap();
§Errors

A ParserError is returned by the parser if the string could not be parsed properly.

Source

pub fn constant(val: f32) -> Self

Creates a new constant from a floating point value.

§Examples

Basic usage:

use math_engine::expression::Expression;

let expr = Expression::constant(2.0);
let eval = expr.eval().unwrap();

assert_eq!(eval, 2.0);
Source

pub fn variable(name: &str) -> Self

Creates a variable.

§Examples

Basic usage:

use math_engine::context::Context;
use math_engine::expression::Expression;

let expr = Expression::variable("x");
let ctx = Context::new().with_variable("x", 32.0);
let eval = expr.eval_with_context(&ctx).unwrap();

assert_eq!(eval, 32.0);
Source

pub fn addition(e1: Expression, e2: Expression) -> Self

Creates a new binary operation which sums two sub-expressions

§Examples

Basic usage:

use math_engine::expression::Expression;

let expr = Expression::addition(
    Expression::constant(2.0),
    Expression::Constant(3.0)
);
let eval = expr.eval().unwrap();

assert_eq!(eval, 5.0);
Source

pub fn subtraction(e1: Expression, e2: Expression) -> Self

Creates a new binary operation which subtracts two sub-expressions

§Examples

Basic usage:

use math_engine::expression::Expression;

let expr = Expression::subtraction(
    Expression::constant(2.0),
    Expression::Constant(3.0)
);
let eval = expr.eval().unwrap();

assert_eq!(eval, -1.0);
Source

pub fn product(e1: Expression, e2: Expression) -> Self

Creates a new binary operation which multiplies two sub-expressions

§Examples

Basic usage:

use math_engine::expression::Expression;

let expr = Expression::product(
    Expression::constant(2.0),
    Expression::Constant(3.0)
);
let eval = expr.eval().unwrap();

assert_eq!(eval, 6.0);
Source

pub fn division(e1: Expression, e2: Expression) -> Self

Creates a new binary operation which divides two sub-expressions

§Examples

Basic usage:

use math_engine::expression::Expression;

let expr = Expression::division(
    Expression::constant(3.0),
    Expression::Constant(2.0)
);
let eval = expr.eval().unwrap();

assert_eq!(eval, 1.5);
Source

pub fn eval(&self) -> Result<f32, EvalError>

Evaluates the expression into a floating point value without a context.

As of now, floating point value is the only supported evaluation. Please note that it is therefore subject to approximations due to some values not being representable.

§Examples
use math_engine::context::Context;
use math_engine::expression::Expression;

// Expression is (1 - 5) + (2 * (4 + 6))
let expr = Expression::addition(
    Expression::subtraction(
        Expression::constant(1.0),
        Expression::constant(5.0)
    ),
    Expression::product(
        Expression::constant(2.0),
        Expression::addition(
            Expression::constant(4.0),
            Expression::constant(6.0)
        )
    )
);
let eval = expr.eval().unwrap();

assert_eq!(eval, 16.0);
§Errors

If any intermediary result is not a number of is infinity, an error is returned. If the expression contains a variable, an error is returned

Source

pub fn eval_with_context(&self, ctx: &Context) -> Result<f32, EvalError>

Evaluates the expression into a floating point value with a given context.

As of now, floating point value is the only supported evaluation. Please note that it is therefore subject to approximations due to some values not being representable.

§Examples
use math_engine::context::Context;
use math_engine::expression::Expression;

// Expression is (1 / (1 + x))
let expr = Expression::division(
    Expression::constant(1.0),
    Expression::addition(
        Expression::constant(1.0),
        Expression::variable("x"),
    )
);
let ctx = Context::new().with_variable("x", 2.0);
let eval = expr.eval_with_context(&ctx).unwrap();

assert_eq!(eval, 1.0/3.0);
§Errors

If any intermediary result is not a number of is infinity, an error is returned. If the expression contains a variable but the context does not define all the variables, an error is returned.

Source

pub fn derivative(&self, deriv_var: &str) -> Self

Calculates the derivative of an expression.

§Examples

Basic usage:

use math_engine::expression::Expression;
use std::str::FromStr;

//Represents y + 2x
let expr = Expression::from_str("1.0 * y + 2.0 * x");

//Represents y + 2
let deri = expr.derivative("x");
Source

pub fn constant_propagation(&self) -> Result<Self, EvalError>

Simplifies the expression by applying constant propagation.

§Examples

Basic usage:

use math_engine::expression::Expression;

let expr = Expression::parse("1.0 * y + 0.0 * x + 2.0 / 3.0").unwrap();

//Represents "y + 0.66666..."
let simp = expr.constant_propagation().unwrap()
§Errors

An EvalError (DivisionByZero) can be returned if the partial evaluation of the expression revealed a division by zero.

Trait Implementations§

Source§

impl Add for Expression

Source§

type Output = Expression

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Expression

Source§

fn clone(&self) -> Expression

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 Debug for Expression

Source§

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

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

impl Display for Expression

Source§

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

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

impl Div for Expression

Source§

type Output = Expression

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl FromStr for Expression

Source§

type Err = ParserError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Mul for Expression

Source§

type Output = Expression

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Sub for Expression

Source§

type Output = Expression

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

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.