pub enum Expression {
BinOp(BinOp, Box<Expression>, Box<Expression>),
Constant(f32),
Variable(String),
}
Variants§
Implementations§
Source§impl Expression
impl Expression
Sourcepub fn parse(s: &str) -> Result<Self, ParserError>
pub fn parse(s: &str) -> Result<Self, ParserError>
Sourcepub fn constant(val: f32) -> Self
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);
Sourcepub fn variable(name: &str) -> Self
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);
Sourcepub fn addition(e1: Expression, e2: Expression) -> Self
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);
Sourcepub fn subtraction(e1: Expression, e2: Expression) -> Self
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);
Sourcepub fn product(e1: Expression, e2: Expression) -> Self
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);
Sourcepub fn division(e1: Expression, e2: Expression) -> Self
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);
Sourcepub fn eval(&self) -> Result<f32, EvalError>
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
Sourcepub fn eval_with_context(&self, ctx: &Context) -> Result<f32, EvalError>
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.
Sourcepub fn derivative(&self, deriv_var: &str) -> Self
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");
Sourcepub fn constant_propagation(&self) -> Result<Self, EvalError>
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
impl Add for Expression
Source§impl Clone for Expression
impl Clone for Expression
Source§fn clone(&self) -> Expression
fn clone(&self) -> Expression
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more