Symbols and Numbers

Symbols represent mathematical variables (x, y, θ, etc.) using efficient string interning. Numbers support integers, rationals, floats, and complex numbers with exact symbolic representation for precise mathematical computation.

Code Examples

Symbol Creation and Equality

Creating symbols with string interning for O(1) equality checks

use mathhook::prelude::*;

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

// O(1) pointer comparison
assert_eq!(x1, x2);
assert_ne!(x1, y);

Exact Rational Arithmetic

Using rationals for exact fractional computation

use mathhook::prelude::*;

// Exact: 1/3
let third = Expression::rational(1, 3);
let result = expr!(3 * third);
assert_eq!(result, Expression::integer(1));

// Auto-reduction: 6/4 = 3/2
let frac = Expression::rational(6, 4);
assert_eq!(frac, Expression::rational(3, 2));

Complex Numbers

Working with complex numbers and imaginary unit

use mathhook::prelude::*;

// 3 + 4i
let z = Expression::complex(
    Expression::integer(3),
    Expression::integer(4)
);

// Magnitude: |z| = sqrt(3^2 + 4^2) = 5
let magnitude = expr!(sqrt((3^2) + (4^2)));
assert_eq!(magnitude.simplify(), Expression::integer(5));

🔗 Related Topics