pub enum Expr {
Show 17 variants
Const(f64),
Var(VarRef),
Add(Box<Expr>, Box<Expr>),
Mul(Box<Expr>, Box<Expr>),
Sub(Box<Expr>, Box<Expr>),
Div(Box<Expr>, Box<Expr>),
Abs(Box<Expr>),
Pow(Box<Expr>, i64),
PowFloat(Box<Expr>, f64),
PowExpr(Box<Expr>, Box<Expr>),
Exp(Box<Expr>),
Ln(Box<Expr>),
Sqrt(Box<Expr>),
Sin(Box<Expr>),
Cos(Box<Expr>),
Neg(Box<Expr>),
Cached(Box<Expr>, Option<f64>),
}Expand description
An expression tree node representing mathematical operations.
This enum represents different types of mathematical expressions that can be:
- JIT compiled into machine code using Cranelift
- Symbolically differentiated to compute derivatives
- Simplified using algebraic rules
- Modified by inserting replacement expressions
The expression tree is built recursively using Box
Variants§
Const(f64)
A constant floating point value
Var(VarRef)
A reference to a variable
Add(Box<Expr>, Box<Expr>)
Addition of two expressions
Mul(Box<Expr>, Box<Expr>)
Multiplication of two expressions
Sub(Box<Expr>, Box<Expr>)
Subtraction of two expressions
Div(Box<Expr>, Box<Expr>)
Division of two expressions
Abs(Box<Expr>)
Absolute value of an expression
Pow(Box<Expr>, i64)
Exponentiation of an expression by an integer constant
PowFloat(Box<Expr>, f64)
Exponentiation of an expression by a floating point constant
PowExpr(Box<Expr>, Box<Expr>)
Exponentiation of an expression by another expression
Exp(Box<Expr>)
Exponential function of an expression
Ln(Box<Expr>)
Natural logarithm of an expression
Sqrt(Box<Expr>)
Square root of an expression
Sin(Box<Expr>)
Sine of an expression (argument in radians)
Cos(Box<Expr>)
Cosine of an expression (argument in radians)
Neg(Box<Expr>)
Negation of an expression
Cached(Box<Expr>, Option<f64>)
Cached expression with optional pre-computed value
Implementations§
Source§impl Expr
impl Expr
Sourcepub fn pre_evaluate(&self, var_cache: &mut HashMap<String, f64>) -> Box<Expr>
pub fn pre_evaluate(&self, var_cache: &mut HashMap<String, f64>) -> Box<Expr>
Pre-evaluates constants and caches variable loads for improved performance
Sourcepub fn derivative(&self, with_respect_to: &str) -> Box<Expr>
pub fn derivative(&self, with_respect_to: &str) -> Box<Expr>
Computes the symbolic derivative of this expression with respect to a variable.
Recursively applies the rules of differentiation to build a new expression tree representing the derivative. The rules implemented are:
- d/dx(c) = 0 for constants
- d/dx(x) = 1 for the variable we’re differentiating with respect to
- d/dx(y) = 0 for other variables
- Sum rule: d/dx(f + g) = df/dx + dg/dx
- Product rule: d/dx(f * g) = f * dg/dx + g * df/dx
- Quotient rule: d/dx(f/g) = (g * df/dx - f * dg/dx) / g^2
- Chain rule for abs: d/dx|f| = f/|f| * df/dx
- Power rule: d/dx(f^n) = n * f^(n-1) * df/dx
- Chain rule for exp: d/dx(e^f) = e^f * df/dx
- Chain rule for ln: d/dx(ln(f)) = 1/f * df/dx
- Chain rule for sqrt: d/dx(sqrt(f)) = 1/(2*sqrt(f)) * df/dx
- Negation: d/dx(-f) = -(df/dx)
§Arguments
with_respect_to- The name of the variable to differentiate with respect to
§Returns
A new expression tree representing the derivative
Sourcepub fn simplify(&self) -> Box<Expr>
pub fn simplify(&self) -> Box<Expr>
Simplifies the expression by folding constants and applying basic algebraic rules.
This method performs several types of algebraic simplifications:
§Constant Folding
- Evaluates constant expressions: 2 + 3 → 5
- Simplifies operations with special constants: x * 0 → 0
§Identity Rules
- Additive identity: x + 0 → x
- Multiplicative identity: x * 1 → x
- Division identity: x / 1 → x
- Division by self: x / x → 1
§Exponent Rules
- Zero exponent: x^0 → 1 (except when x = 0)
- First power: x^1 → x
- Nested exponents: (x^a)^b → x^(a*b)
§Special Function Simplification
- Absolute value: |-3| → 3, ||x|| → |x|
- Double negation: -(-x) → x
- Evaluates constant special functions: ln(1) → 0
§Expression Caching
- Caches repeated subexpressions to avoid redundant computation
- Preserves existing cached values
§Returns
A new simplified expression tree
Sourcepub fn insert<F>(&self, predicate: F, replacement: &Expr) -> Box<Expr>
pub fn insert<F>(&self, predicate: F, replacement: &Expr) -> Box<Expr>
Inserts an expression by replacing nodes that match a predicate.
Recursively traverses the expression tree and replaces any nodes that match the given predicate with the replacement expression. This allows for targeted modifications of the expression tree.
§Arguments
predicate- A closure that determines which nodes to replacereplacement- The expression to insert where the predicate matches
§Returns
A new expression tree with the replacements applied
Sourcepub fn flatten(&self) -> FlattenedExpr
pub fn flatten(&self) -> FlattenedExpr
Converts expression tree to flattened linear operations for efficient evaluation.
This optimization eliminates:
- Tree traversal overhead
- Function call overhead
- Memory allocation in hot path
- Variable lookup overhead
The result is a linear sequence of stack-based operations that can be executed with minimal overhead.
Sourcepub fn codegen_flattened(
&self,
builder: &mut FunctionBuilder<'_>,
module: &mut dyn Module,
) -> Result<Value, EquationError>
pub fn codegen_flattened( &self, builder: &mut FunctionBuilder<'_>, module: &mut dyn Module, ) -> Result<Value, EquationError>
Generates ultra-optimized linear code from flattened operations.
This eliminates all function call overhead by generating a single linear sequence of optimal instructions with direct register allocation.
Trait Implementations§
Source§impl Display for Expr
Implements string formatting for expressions.
impl Display for Expr
Implements string formatting for expressions.
This implementation converts expressions to their standard mathematical notation:
- Constants are formatted as numbers
- Variables are formatted as their names
- Binary operations (+,-,*,/) are wrapped in parentheses
- Functions (exp, ln, sqrt) use function call notation
- Absolute value uses |x| notation
- Exponents use ^
- Negation uses - prefix
- Cached expressions display their underlying expression
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 UnwindSafe for Expr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more