pub enum Expr {
Show 28 variants
Sym(String),
Const(f64),
Neg(E),
Add(E, E),
Sub(E, E),
Mul(E, E),
Div(E, E),
Pow(E, E),
Sin(E),
Cos(E),
Tan(E),
Asin(E),
Acos(E),
Atan(E),
Atan2(E, E),
Sinh(E),
Cosh(E),
Tanh(E),
Exp(E),
Ln(E),
Log2(E),
Log10(E),
Sqrt(E),
Abs(E),
Heaviside(E),
Clamp(E, E, E),
NamedConst {
name: String,
value: f64,
rust_f32: String,
rust_f64: String,
latex: String,
},
Func {
name: String,
params: Vec<String>,
kind: FuncKind,
args: Vec<E>,
},
}Expand description
Variants§
Sym(String)
Named symbolic variable.
Const(f64)
Numeric constant.
Neg(E)
Unary negation.
Add(E, E)
Addition.
Sub(E, E)
Subtraction.
Mul(E, E)
Multiplication.
Div(E, E)
Division.
Pow(E, E)
Exponentiation (base^exponent).
Sin(E)
Sine.
Cos(E)
Cosine.
Tan(E)
Tangent.
Asin(E)
Arcsine.
Acos(E)
Arccosine.
Atan(E)
Arctangent.
Atan2(E, E)
Two-argument arctangent (atan2(y, x)).
Sinh(E)
Hyperbolic sine.
Cosh(E)
Hyperbolic cosine.
Tanh(E)
Hyperbolic tangent.
Exp(E)
Exponential (e^x).
Ln(E)
Natural logarithm.
Log2(E)
Base-2 logarithm.
Log10(E)
Base-10 logarithm.
Sqrt(E)
Square root.
Abs(E)
Absolute value.
Heaviside(E)
Heaviside step function: 0 if x < 0, 1 if x >= 0. Derivative is 0.
Clamp(E, E, E)
Clamp value to [lo, hi]. Derivative passes through (= d(val)/dvar).
NamedConst
Named constant (pi, epsilon, e, or user-defined). Survives simplification (unlike Const which may be folded away).
Func
User-defined function application.
Implementations§
Source§impl Expr
impl Expr
Sourcepub fn eval(&self, vars: &HashMap<&str, f64>) -> Result<f64, String>
pub fn eval(&self, vars: &HashMap<&str, f64>) -> Result<f64, String>
Evaluate the expression numerically given variable bindings.
Returns Err if any symbol in the expression is not bound in vars.
Sourcepub fn subs(&self, var: impl AsVarName, replacement: &E) -> E
pub fn subs(&self, var: impl AsVarName, replacement: &E) -> E
Substitute all occurrences of the named variable with replacement.
var can be any [AsVarName] – a &str, a String, or an
E handle wrapping a Sym node. Returns a new expression
with the substitution applied throughout.
Source§impl Expr
impl Expr
Source§impl Expr
impl Expr
Sourcepub fn simplify(&self) -> E
pub fn simplify(&self) -> E
Apply algebraic simplification rules.
Performs constant folding, identity elimination (0+x=x, 1*x=x), like-term collection, power combination, fraction cancellation, and canonical ordering. Iterates until a fixed point is reached.
Sourcepub fn expand(&self) -> E
pub fn expand(&self) -> E
Expand products and integer powers over sums.
Distributes multiplication: (a + b) * c becomes a*c + b*c.
Integer powers up to 8 are expanded: (a + b)^3 becomes the full
multinomial expansion. The result is simplified afterwards.
Sourcepub fn collect(&self, var: impl AsVarName) -> E
pub fn collect(&self, var: impl AsVarName) -> E
Collect like terms containing var by structural match.
var can be any [AsVarName] – a &str, a String, or an
E handle wrapping a Sym node. Groups additive terms
that share var as a factor, summing their coefficients.
For example, a*x + b*x + c becomes (a + b)*x + c.