Skip to main content

Expr

Enum Expr 

Source
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

Expression AST node.

Normally constructed via symbol, constant, and the free-standing math functions (e.g. sin, cos, pow) rather than directly.

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).

Fields

§name: String
§value: f64
§rust_f32: String
§rust_f64: String
§latex: String
§

Func

User-defined function application.

Fields

§name: String

Function name (for display).

§params: Vec<String>

Formal parameter names.

§kind: FuncKind

Function behavior (differentiation, codegen, eval).

§args: Vec<E>

Actual argument expressions.

Implementations§

Source§

impl Expr

Source

pub fn diff(&self, var: impl AsVarName) -> E

Symbolically differentiate this expression with respect to a variable.

Applies the chain rule, product rule, and quotient rule automatically. The result is simplified.

Source§

impl Expr

Source

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.

Source

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

pub fn free_vars(&self) -> BTreeSet<String>

Collect all free (unbound) variable names in the expression.

Returns a sorted set of variable name strings.

Source

pub fn diff_all(&self, vars: &[&str]) -> Vec<E>

Differentiate with respect to multiple variables, returning a vector.

Equivalent to calling Expr::diff for each variable in order.

Source§

impl Expr

Source

pub fn to_latex(&self) -> String

Format the expression as LaTeX math notation.

Produces a string suitable for embedding in LaTeX documents, using \frac, \sqrt, \sin, etc.

Source

pub fn to_rust(&self, float_type: &str) -> String

Generate Rust source code for this expression.

The float_type parameter (e.g. "f64") controls numeric literal suffixes. Pass an empty string to omit type suffixes.

Source§

impl Expr

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl AsRef<Expr> for E

Source§

fn as_ref(&self) -> &Expr

Converts this type into a shared reference of the (usually inferred) input type.
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 Hash for Expr

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. 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 Eq for Expr

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.