Skip to main content

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

MathCore::simplify

pub fn simplify(expression: &str) -> Result<Expr, MathError>
Parses expression into an Expr tree and applies the full set of simplification rules recursively. Returns the simplified Expr.
use mathcore::MathCore;

let result = MathCore::simplify("1 * x").unwrap();
println!("{}", result); // x
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 single Expr::Number.
InputOutput
2 + 35
4 - 13
3 * 412
10 / 25
2^8256
MathCore::simplify("2 + 3").unwrap();  // 5

Additive identity — x + 0 and 0 + x

Adding zero on either side of an expression is eliminated.
MathCore::simplify("x + 0").unwrap(); // x
MathCore::simplify("0 + x").unwrap(); // x

Subtractive identity — x - 0

Subtracting zero is a no-op.
MathCore::simplify("x - 0").unwrap(); // x

Multiplicative zero — 0 * x and x * 0

Any expression multiplied by zero collapses to zero, regardless of the other operand.
MathCore::simplify("0 * x").unwrap(); // 0

Multiplicative identity — 1 * x and x * 1

Multiplying by one on either side is removed.
MathCore::simplify("1 * x").unwrap(); // x

Division identity — x / 1

Dividing by one is a no-op.
MathCore::simplify("x / 1").unwrap(); // x

Power rules — x^0 and x^1

Standard exponent identities are reduced at the node level.
MathCore::simplify("x^0").unwrap(); // 1
MathCore::simplify("x^1").unwrap(); // x

Symbol cancellation — x - x

When the same named symbol is subtracted from itself, the result is zero.
MathCore::simplify("x - x").unwrap(); // 0
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.
// -(-(x)) → x
let simplified = MathCore::simplify("-(-(x))").unwrap(); // x

Full example

use mathcore::MathCore;

// Constant folding
let r = MathCore::simplify("2 + 3").unwrap();
println!("{}", r); // 5

// Multiplicative zero
let r = MathCore::simplify("0 * x").unwrap();
println!("{}", r); // 0

// Multiplicative identity
let r = MathCore::simplify("1 * x").unwrap();
println!("{}", r); // x

// Power rules
let r = MathCore::simplify("x^0").unwrap();
println!("{}", r); // 1

let r = MathCore::simplify("x^1").unwrap();
println!("{}", r); // x

// Symbol cancellation
let r = MathCore::simplify("x - x").unwrap();
println!("{}", r); // 0

Automatic simplification after calculus operations

MathCore applies simplification automatically as a post-processing step when computing derivatives and integrals. This means the Expr 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.
// Differentiate x^1 → result is already simplified to 1, not 1 * x^0
let d = MathCore::differentiate("x", "x").unwrap();
println!("{}", d); // 1
If you are building expression trees programmatically using Expr variants directly, call MathCore::simplify on the final tree before displaying or evaluating it to ensure a clean, minimal form.

Current limitations

The simplifier performs a single bottom-up pass and does not iterate until a fixed point. Some expressions may require you to call simplify more than once to reach the fully reduced form, though in practice a single call handles the common cases.
The following reductions are not currently supported:
  • Polynomial expansion(x + 1)^2 is not expanded to x^2 + 2*x + 1
  • Collecting like termsx + x is not reduced to 2*x
  • Trigonometric identitiessin(x)^2 + cos(x)^2 is not reduced to 1
  • Rational simplification(x^2 - 1) / (x - 1) is not reduced to x + 1
  • Multi-pass canonicalisation — commutativity and associativity reordering are not applied
These capabilities are planned for future releases. For the current scope, simplify is best used for cleaning up derivative and integral results and reducing arithmetic constant sub-trees.

Build docs developers (and LLMs) love