MathVerse Complex
Complex number arithmetic, analysis, special functions, and matrix algebra —
generic over any RealFull type (f64 by default, with f32 support).
Features
- Generic
Complex<T>— one implementation overf64(default),f32, or any [RealFull] type;C32/C64convenience aliases - Full arithmetic —
Add/Sub/Mul/Div/Neg, overflow-safe division (Smith's algorithm), scalar operations,Index-style accessors - Transcendental functions — trigonometric, hyperbolic, inverse trigonometric,
logarithmic, principal powers and roots (
0⁰ = 1convention) - Python parity —
cmath/numpy-named methods:phase,to_polar/polar,rect,is_close,real()/imag() - Complex analysis — contour integration, Cauchy integral/derivative formulas, complex-step derivative, residues, Laurent series, conformal mappings
- Special functions — gamma, digamma, zeta, polylogarithm, erf/erfc, Bessel, Airy, Fresnel, exponential integral over the complex domain
- Complex matrix algebra — LU decomposition, solve, inverse, determinant, QR decomposition, eigenvalues (Wilkinson-shift QR), matrix exp/ln
- FFT — radix-2 Cooley–Tukey
fft/ifftwith round-trip identity - Polynomial roots — Durand–Kerner (Weierstrass) method for all complex roots
- Mandelbrot — iteration and smooth-coloring helpers
- No external runtime dependencies (only
mathverse-core)
Module Overview
| Module | Purpose |
|---|---|
[Complex] (root) |
Core type, arithmetic, transcendental functions, Mandelbrot |
[analysis] |
Contour integrals, Cauchy formulas, residues, conformal mappings |
[special_functions] |
Gamma, zeta, polylog, erf, Bessel, Airy, Fresnel |
[matrix] |
Complex matrix algebra, decompositions, eigenvalues |
[fft] |
Radix-2 fast Fourier transform |
[polynomial] |
Polynomial evaluation and root finding |
Installation
[]
= "0.2"
Quick Start
use Complex;
Per-Module Documentation
Complex — Core Type
Construction
let a = new; // 1 + 2i
let b = real; // 5 + 0i
let c = i; // 0 + 1i
let d = polar; // 0 + 2i (r·e^(iθ))
let e: Complex = 3.0.into; // From<f64>
let f: Complex = .into; // From<(f64, f64)>
Properties
| Method | Formula |
|---|---|
norm() |
√(re² + im²) |
norm_sq() |
re² + im² |
arg() / phase() |
atan2(im, re) ∈ (-π, π] |
to_polar() / polar() |
(norm, arg) — cmath.polar parity |
rect(r, θ) |
r·(cos θ + i·sin θ) — cmath.rect parity |
conjugate() |
re − im·i |
signum() |
z / |z| (unit magnitude) |
is_zero() |
re == 0 && im == 0 |
is_close(other, rel, abs) |
cmath.isclose semantics |
re / im fields |
Direct component access (like numpy.real(z) / numpy.imag(z)) |
Functions
| Method | Formula / Notes |
|---|---|
sqrt() |
Algebraic formula (Numerical Recipes §5.4), full precision near the real axis; branch cut on (−∞, 0] |
exp() |
ez = ere(cos im + i·sin im) |
ln() |
Principal: ln|z| + i·arg(z) |
pow(p) |
e^(p·ln z), principal branch; 0⁰ = 1, 0^p = 0 for Re(p) > 0 |
sin/cos/tan |
Complex trigonometric functions |
sinh/cosh/tanh |
Complex hyperbolic functions |
asin/acos/atan |
Inverse trigonometric functions |
acosh/asinh/atanh |
Inverse hyperbolic functions (documented branch cuts) |
Arithmetic
Implements Add, Sub, Mul, Div, Neg, plus scalar Complex<T> ± T,
Complex<T> · T, Complex<T> / T:
let z1 = new;
let z2 = new;
let sum = z1 + z2; // (4, 6)
let prod = z1 * z2; // (-5, 10)
let quot = z1 / z2; // (0.44, 0.08)
Division uses Smith's overflow-safe algorithm: (1e300 + 1e300i) / (1e300 + 1e300i)
is exactly 1 + 0i, where the naive a·d/|d|² formula overflows to NaN.
fft — Fast Fourier Transform
use ;
// 1024-sample sine wave → spectrum → round-trip
let n = 1024;
let signal: =
.map
.collect;
let spectrum = fft;
let back = ifft;
assert!;
Input length must be a power of two (asserted).
polynomial — Root Finding
use ;
// z² + 1 = 0 → ±i
let roots = polynomial_roots;
assert!;
Coefficients are lowest-order first: coeffs[0] + coeffs[1]·z + coeffs[2]·z² + ….
Uses the Durand–Kerner (Weierstrass) iteration, globally convergent for any
polynomial degree.
analysis — Complex Analysis
use ;
derivative/derivative_complex_step/derivative_cauchy— three ways to differentiate an analytic functioncontour_integral_circle,cauchy_integral_formula,cauchy_derivative_formularesidue_simple_pole/residue_pole_order_nis_analytic— Cauchy–Riemann checklaurent_series_coefficients,mobius_transform,schwarz_christoffel,argument_principle,rouches_theorem
special_functions — Complex Special Functions
use ComplexSpecialFunctions;
let g = gamma; // Γ(5) = 24
let z = zeta; // ζ(2) ≈ π²/6
let e = erf; // erf(1) ≈ 0.8427
let j0 = bessel_j; // J₀(1) ≈ 0.7652
Lanczos gamma with reflection; Euler–Maclaurin zeta with the functional equation
for Re(s) < 0; erf/erfc switch to an optimally-truncated asymptotic expansion
for |z| ≥ 3; Bessel series with early-exit convergence detection.
matrix — Complex Matrix Algebra
use ;
let mut a = new;
a.set;
a.set;
a.set;
a.set;
let b = vec!;
let x = a.solve.unwrap; // x ≈ [1.0, 1.0]
Supports LU decomposition (relative pivot threshold), solve, inverse, determinant, power, matrix exp/ln, QR decomposition, Hermitian/unitary checks, and eigenvalues via the QR algorithm with Wilkinson shifts:
let e = a.eigenvalues?; // Vec<Complex>
use linalg;
let ev = eig?; // scipy.linalg.eig parity
Dimension mismatches and out-of-bounds access return MathResult/Option
instead of panicking (add, mul, try_get, try_set); get/set follow the
slice-indexing convention for hot paths.
Testing & Benchmarks
Rust Version & Safety
- Minimum supported Rust version: 1.87 (workspace policy)
#![forbid(unsafe_code)]— nounsafeanywhere- Division by zero,
ln(0), etc. yieldNaN/infcomponents mirroring std float semantics — never panic
Contributing
See CONTRIBUTING.md.
License
MIT OR Apache-2.0 — see LICENSE-MIT and LICENSE.md (Apache-2.0).