Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
Expr is the core algebraic data type of MathCore. Every parsed expression, every intermediate computation, and every final result is represented as an Expr tree. The variants cover the full range of constructs the library can work with — literal numbers (real and complex), symbolic variables, binary and unary operations, named function calls, and first-class representations of unevaluated derivatives and integrals. Because Expr derives Clone and Debug, trees can be inspected, cloned, and pattern-matched freely.
pub enum Expr
Variants
Number(f64)
A real floating-point constant. This is the most common leaf node produced by evaluation.
Complex(Complex64)
A complex number, using num_complex::Complex64 (Complex<f64>). Produced when operations result in a complex value.
Display impl formats this as "re+imi" or "re-|im|i" depending on the sign of the imaginary part.
Symbol(String)
A named variable or constant that has not yet been bound to a value.
Binary { op: BinaryOp, left: Box<Expr>, right: Box<Expr> }
A binary operation applied to two sub-expressions. Both sub-expressions are heap-allocated to allow recursion.
Unary { op: UnaryOp, expr: Box<Expr> }
A unary operation applied to a single sub-expression.
Function { name: String, args: Vec<Expr> }
A named function call with zero or more arguments. Built-in functions include sin, cos, tan, exp, ln, sqrt, and others recognized by the parser and engine.
Derivative { expr: Box<Expr>, var: String, order: u32 }
An unevaluated derivative of expr with respect to var of the given order. Produced when the parser encounters derivative notation; the calculus subsystem evaluates this into a concrete Expr.
Integral { expr: Box<Expr>, var: String, lower: Option<Box<Expr>>, upper: Option<Box<Expr>> }
An unevaluated integral. When lower and upper are both None the integral is indefinite; when both are Some it is definite. The Display impl renders ∫ expr dvar for indefinite and ∫[lower,upper] expr dvar for definite integrals.
pub enum BinaryOp
BinaryOp is Copy, so it can be used freely in pattern-matching arms without cloning.
| Variant | Symbol | Example expression |
|---|---|---|
Add | + | x + 1 |
Subtract | - | x - 1 |
Multiply | * | 2 * x |
Divide | / | x / 2 |
Power | ^ | x ^ 3 |
Modulo | % | x % 2 |
pub enum UnaryOp
| Variant | Display format | Example |
|---|---|---|
Negate | -(expr) | -(x) |
Factorial | expr! | 5! |
Abs | |expr| | |x| |
impl Expr — helper methods
zero() -> Expr
Expr::Number(0.0). Convenience constructor for the additive identity.
one() -> Expr
Expr::Number(1.0). Convenience constructor for the multiplicative identity.
is_zero(&self) -> bool
true when self is Expr::Number(n) and n.abs() < f64::EPSILON.
Only matches the
Number variant. A Binary expression that would evaluate
to zero is not considered zero by this method — use simplify first.is_one(&self) -> bool
true when self is Expr::Number(n) and (n - 1.0).abs() < f64::EPSILON.
is_constant(&self) -> bool
true when the expression contains no Symbol nodes — i.e. it can be evaluated without any variable bindings. Number and Complex leaves are always constant. Binary, Unary, and Function nodes are constant when all their sub-expressions are constant.
contains_var(&self, var: &str) -> bool
true when the expression tree contains a Symbol whose name equals var. Recursively checks Binary, Unary, Function, Derivative, and Integral nodes.
degree(&self, var: &str) -> u32
var:
- A bare
Symbol(var)has degree 1. expr ^ Number(n)whereexprcontainsvarhas degreen as u32.- For
Multiplythe degrees of both sides are summed. - For
Add/Subtractthe maximum of the two sides is returned. - All other cases (constants, unrelated symbols, functions) return 0.
impl Display for Expr
Expr implements std::fmt::Display, so any expression can be formatted with {} or converted to a String via .to_string(). The formatting rules are:
| Variant | Format |
|---|---|
Number | Rust’s default f64 format |
Complex | re+imi or re-|im|i |
Symbol | The symbol name verbatim |
Binary | (left op right) |
Unary | -(expr), expr!, or |expr| |
Function | name(arg0, arg1, …) |
Derivative | d^order/dvar^order(expr) |
Integral | ∫ expr dvar or ∫[l,u] expr dvar |
pub struct Context
Context provides a scoped environment for variable bindings and custom function definitions. It is used internally by the Engine and can be constructed directly for advanced use-cases.
CustomFunction is a type alias for Rc<dyn Fn(&[Expr]) -> Result<Expr, MathError>>.
Context::new
Context::with_defaults
| Name | Value |
|---|---|
pi | std::f64::consts::PI (π) |
e | std::f64::consts::E (e) |
tau | std::f64::consts::TAU (2π) |
set_var
value can be any Expr, including symbolic expressions.
The variable name. Must match the
Symbol name used in expressions.The value to bind. Typically
Expr::Number(…) for a numeric constant or an
Expr::Symbol / Expr::Binary for a symbolic alias.get_var
Expr or None if it is not defined.
The variable name to look up.
Some(&Expr) if the variable is bound, None otherwise.
