Expressions

The Expression type is the foundation of MathHook. Expressions are represented as an enum with variants for different mathematical constructs including numbers, variables, operations, functions, constants, matrices, and relations.

Code Examples

Basic Expression Creation with Macros

Using expr! and symbol! macros for ergonomic expression creation

use mathhook::prelude::*;

let x = symbol!(x);
let y = symbol!(y);

// Basic arithmetic
let sum = expr!(x + y);
let product = expr!(x * y);
let power = expr!(x ^ 2);

// Complex expressions
let quadratic = expr!(a * x ^ 2 + b * x + c);

Canonical Form Normalization

Expressions are automatically normalized to canonical form

use mathhook::prelude::*;

let expr1 = expr!(x + y);
let expr2 = expr!(y + x);

// Both normalized to same form
assert_eq!(expr1, expr2);

// Rationals reduced
let frac = Expression::rational(6, 4);
assert_eq!(frac, Expression::rational(3, 2));

Immutable Operations

All expression operations return new expressions without modifying originals

use mathhook::prelude::*;

let expr = expr!(x + 1);
let doubled = expr.mul(&expr!(2));

// Original unchanged
println!("Original: {}", expr);  // x + 1
println!("Doubled: {}", doubled); // 2*(x + 1)

🔗 Related Topics