Mathematical Constants
MathHook provides built-in mathematical constants with exact symbolic representation and high-precision numerical evaluation. Constants include π, e, i (imaginary unit), golden ratio (φ), Euler-Mascheroni constant (γ), infinity, and undefined values. All constants preserve mathematical exactness throughout symbolic computations.
📐
Mathematical Definition
Fundamental Constants:
- Pi:
(ratio of circle's circumference to diameter)
- Euler's Number:
- Imaginary Unit:
- Golden Ratio:
- Euler-Mascheroni:
Code Examples
Using Pi in Expressions
Pi constant for exact trigonometric and geometric calculations
use mathhook::prelude::*;
let pi = Expression::pi();
let r = symbol!(r);
// Circle area: A = πr²
let area = expr!(pi * r^2);
// Circumference: C = 2πr
let circumference = expr!(2*pi*r);
// Exact trigonometric values
assert_eq!(expr!(sin(pi)), expr!(0));
assert_eq!(expr!(cos(pi)), expr!(-1));
assert_eq!(expr!(sin(pi/2)), expr!(1));
Euler's Number (e) and Natural Logarithm
Using e for exponential growth and logarithmic identities
use mathhook::prelude::*;
let e = Expression::e();
let x = symbol!(x);
// Exponential growth
let growth = expr!(e^(r*t));
// Logarithm identities
assert_eq!(expr!(ln(e)), expr!(1));
assert_eq!(expr!(ln(e^x)).simplify(), x);
assert_eq!(expr!(e^(ln(x))).simplify(), x);
// Derivative property: d/dx(e^x) = e^x
let exp_x = expr!(e^x);
assert_eq!(exp_x.derivative(&x, 1), exp_x);
Imaginary Unit (i) and Euler's Identity
Complex numbers and the most beautiful equation in mathematics
use mathhook::prelude::*;
let i = Expression::i();
// Complex numbers
let z = expr!(3 + 4*i);
let magnitude = expr!(sqrt((3^2) + (4^2)));
assert_eq!(magnitude.simplify(), expr!(5));
// Euler's identity: e^(iπ) + 1 = 0
let euler_identity = expr!(e^(i*pi) + 1);
assert_eq!(euler_identity.simplify(), expr!(0));
// Powers of i
assert_eq!(expr!(i^2).simplify(), expr!(-1));
assert_eq!(expr!(i^3).simplify(), expr!(-i));
assert_eq!(expr!(i^4).simplify(), expr!(1));
Golden Ratio and Fibonacci Connection
Golden ratio in geometry and its relationship to Fibonacci sequence
use mathhook::prelude::*;
let phi = Expression::golden_ratio();
// Exact form: φ = (1 + √5) / 2
let phi_exact = expr!((1 + sqrt(5)) / 2);
assert_eq!(phi.simplify(), phi_exact.simplify());
// Golden ratio property: φ² = φ + 1
assert_eq!(expr!(phi^2).simplify(), expr!(phi + 1));
// Fibonacci limit: lim F(n+1)/F(n) = φ
// φ ≈ 1.618033988749895
Infinity and Undefined Values
Working with unbounded limits and indeterminate forms
use mathhook::prelude::*;
let inf = Expression::infinity();
let neg_inf = Expression::negative_infinity();
// Defined operations
assert_eq!(expr!(infinity + 1), expr!(infinity));
assert_eq!(expr!(infinity * 2), expr!(infinity));
assert_eq!(expr!(1 / infinity).simplify(), expr!(0));
// Limits
let x = symbol!(x);
let limit = expr!(lim(1/x, x, infinity));
assert_eq!(limit.simplify(), expr!(0));
// Undefined forms (indeterminate)
// infinity - infinity → Undefined
// infinity / infinity → Undefined
// 0 * infinity → Undefined
Real-World Physics: Harmonic Motion
Using π and e in simple harmonic oscillator equations
use mathhook::prelude::*;
// Simple harmonic oscillator: x(t) = A*cos(ω*t + φ)
let t = symbol!(t);
let A = expr!(2);
let omega = expr!(pi);
let phi = expr!(pi/4);
let position = expr!(A * cos(omega*t + phi));
let velocity = position.derivative(&t, 1);
let acceleration = velocity.derivative(&t, 1);
// Verify: a = -ω²x
assert_eq!(acceleration, expr!(-(omega^2) * position));