MathCore includes a recursive, rule-based simplifier that traverses an expression tree bottom-up and applies a set of algebraic reduction rules at each node. It handles the most common algebraic identities — collapsing constant sub-expressions, eliminating additive and multiplicative identity elements, cancelling self-subtracting symbols, and unwinding double negations — producing a leaner expression tree without changing the mathematical value.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.
MathCore::simplify
expression into an Expr tree and applies the full set of simplification rules recursively. Returns the simplified Expr.
simplify returns an Expr, not a String. Use the Display implementation (format!("{}", result) or println!) to convert it back to a human-readable string.Simplification rules
The simplifier works in a single recursive pass. Each binary or unary node is reduced after its children have already been simplified, so rules compose freely.Constant folding
Numeric sub-expressions are evaluated immediately, collapsing them to a singleExpr::Number.
| Input | Output |
|---|---|
2 + 3 | 5 |
4 - 1 | 3 |
3 * 4 | 12 |
10 / 2 | 5 |
2^8 | 256 |
Additive identity — x + 0 and 0 + x
Adding zero on either side of an expression is eliminated.
Subtractive identity — x - 0
Subtracting zero is a no-op.
Multiplicative zero — 0 * x and x * 0
Any expression multiplied by zero collapses to zero, regardless of the other operand.
Multiplicative identity — 1 * x and x * 1
Multiplying by one on either side is removed.
Division identity — x / 1
Dividing by one is a no-op.
Power rules — x^0 and x^1
Standard exponent identities are reduced at the node level.
Symbol cancellation — x - x
When the same named symbol is subtracted from itself, the result is zero.
This rule matches on identical symbol names only.
x - y is not simplified even if x and y happen to hold the same value at runtime, because simplify operates purely symbolically.Double negation — -(-(x))
Two consecutive negations cancel out, returning the original inner expression.
Full example
Automatic simplification after calculus operations
MathCore applies simplification automatically as a post-processing step when computing derivatives and integrals. This means theExpr returned by MathCore::differentiate and MathCore::integrate has already had obvious identities reduced — you generally do not need to call simplify on their output manually.
Current limitations
The following reductions are not currently supported:- Polynomial expansion —
(x + 1)^2is not expanded tox^2 + 2*x + 1 - Collecting like terms —
x + xis not reduced to2*x - Trigonometric identities —
sin(x)^2 + cos(x)^2is not reduced to1 - Rational simplification —
(x^2 - 1) / (x - 1)is not reduced tox + 1 - Multi-pass canonicalisation — commutativity and associativity reordering are not applied
simplify is best used for cleaning up derivative and integral results and reducing arithmetic constant sub-trees.