mini\Util\Math\Expr final class

Documentation

Expression tree with lazy evaluation

Stores expressions as a tree structure for optimization opportunities. Arithmetic operations build up the expression tree lazily; call eval() to compute.

Precision model:

  • Basic arithmetic (+, -, *, /, %) uses arbitrary precision via Decimal/BigInt
  • Integer powers use exact binary exponentiation
  • Transcendental functions (exp, ln) and fractional powers use high-precision iterative algorithms (Taylor series, Newton-Raphson) with configurable scale

Usage: $expr = new Expr('/', 10, 3); $result = $expr->eval(maxScale: 10); // Decimal: 3.3333333333

$expr = Expr::parse('(10 + 5) * 2'); $result = $expr->eval(); // Decimal: 30

$expr = Expr::val(10)->divide(3)->add(1); $result = $expr->eval(maxScale: 4); // Decimal: 4.3333

Inheritance

Implements: mini\Util\Math\NumberInterface IteratorAggregate Stringable Traversable

Properties (3)

public readonly string $op
public readonly mini\Util\Math\NumberInterface|self|string|int|float $operand
public readonly mini\Util\Math\NumberInterface|self|string|int|float|null $other

Methods (53)

Create an expression node

Create expression from a single value (fluent API entry point)

e (Euler's number) - base of natural logarithm

√2 (Pythagoras' constant)

ln(2) - natural logarithm of 2

ln(10) - natural logarithm of 10

φ (phi) - golden ratio (1 + √5) / 2

π (pi) - ratio of circumference to diameter

γ (gamma) - Euler-Mascheroni constant

G - Catalan's constant

ζ(2) = π²/6 - Basel problem

Parse an infix expression string

Evaluate the expression to a Decimal

Recursively evaluate an expression node

Evaluate power with structural pattern matching

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Absolute value

Square root: √x = x^(1/2)

Natural exponential: e^x

Natural logarithm: ln(x)

Scale is unknown until evaluated

Evaluates and returns normalized string representation

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Iterate in RPN (postfix) order

Documentation missing

Documentation missing

Check if scalar value equals expected integer

Structural pattern matching on expression tree

Check if this Expr represents the ratio p/q (handles reducible ratios)

Extract integer value from scalar as BigInt, or null if not an integer

Get rational number representation of this expression

Try to get exact rational representation without approximation

Convert a scalar value to rational Expr

Convert Decimal to exact rational (always possible)

Convert float to exact rational (only for dyadic rationals)

Find best rational approximation using continued fractions

Normalize input to Expr or NumberInterface

Convert value to Decimal for evaluation

Source

src/Util/Math/Expr.php:27-1008