Special Functions
Numra provides implementations of the classical special functions commonly
needed in scientific computing, physics, and engineering. All functions are
generic over S: Scalar, working with both f32 and f64.
use numra::special::{ // Gamma family gamma, lgamma, digamma, beta, gammainc, gammaincc, betainc, // Error functions erf, erfc, erfinv, erfcinv, // Bessel functions besselj, bessely, besseli, besselk, // Elliptic integrals ellipk, ellipe, ellipf, // Airy functions airy_ai, airy_bi, // Hypergeometric functions hyp1f1, hyp2f1, // Orthogonal polynomials legendre_p, hermite_h, laguerre_l, chebyshev_t, legendre_plm, // Zeta functions zeta, hurwitz_zeta, // Miscellaneous dawson, fresnel_s, fresnel_c, mittag_leffler, mittag_leffler_1,};Gamma Family
Section titled “Gamma Family”The gamma function and related functions are foundational to combinatorics, probability distributions, and many areas of mathematical physics.
Gamma Function
Section titled “Gamma Function”Generalizes the factorial: for positive integers.
use numra::special::gamma;
assert!((gamma(5.0_f64) - 24.0).abs() < 1e-10); // Gamma(5) = 4! = 24assert!((gamma(0.5_f64) - std::f64::consts::PI.sqrt()).abs() < 1e-10); // Gamma(1/2) = sqrt(pi)Log-Gamma
Section titled “Log-Gamma”lgamma(z) computes , avoiding overflow for large arguments
where itself would exceed floating-point range.
use numra::special::lgamma;
let val = lgamma(100.0_f64); // ln(99!) -- too large for Gamma directlyDigamma (Psi) Function
Section titled “Digamma (Psi) Function”Used in maximum likelihood estimation, Bayesian inference, and as a building block for the polygamma functions.
use numra::special::digamma;
let val = digamma(1.0_f64); // psi(1) = -gamma (Euler-Mascheroni constant)Beta Function
Section titled “Beta Function”use numra::special::beta;
let val = beta(2.0_f64, 3.0); // B(2,3) = 1*2 / 4! * 3! = 1/12Incomplete Gamma Functions
Section titled “Incomplete Gamma Functions”The regularized lower and upper incomplete gamma functions:
Essential for chi-squared distributions and Poisson statistics.
use numra::special::{gammainc, gammaincc};
let p = gammainc(1.0_f64, 1.0); // P(1, 1) = 1 - e^(-1)let q = gammaincc(1.0_f64, 1.0); // Q(1, 1) = e^(-1)assert!((p + q - 1.0).abs() < 1e-12);Regularized Incomplete Beta
Section titled “Regularized Incomplete Beta”The CDF of the beta distribution and a building block for the F-distribution and Student’s t-distribution.
use numra::special::betainc;
let val = betainc(2.0_f64, 3.0, 0.5); // I_{0.5}(2, 3)Error Functions
Section titled “Error Functions”The error function and its relatives arise in probability, statistics, and solutions of the diffusion equation.
erf and erfc
Section titled “erf and erfc”The relationship to the normal distribution CDF is: .
use numra::special::{erf, erfc};
assert!((erf(0.0_f64)).abs() < 1e-15); // erf(0) = 0assert!((erf(1e10_f64) - 1.0).abs() < 1e-15); // erf(inf) = 1assert!((erfc(0.0_f64) - 1.0).abs() < 1e-15); // erfc(0) = 1Inverse Error Functions
Section titled “Inverse Error Functions”erfinv(p) returns the value such that .
Uses a rational approximation with Newton refinement.
use numra::special::{erfinv, erfcinv};
let x = erfinv(0.5_f64);assert!((erf(x) - 0.5).abs() < 1e-12);
let x = erfcinv(0.1_f64);assert!((erfc(x) - 0.1).abs() < 1e-12);Use cases: Generating normally-distributed random variates from uniform ones, computing confidence intervals, quantile functions.
Bessel Functions
Section titled “Bessel Functions”Bessel functions are solutions to Bessel’s differential equation:
They arise in problems with cylindrical symmetry: heat conduction in cylinders, vibrations of circular membranes, electromagnetic waveguides, and diffraction.
First and Second Kind
Section titled “First and Second Kind”use numra::special::{besselj, bessely};
// J_0(0) = 1, J_n(0) = 0 for n > 0assert!((besselj(0.0_f64, 0.0) - 1.0).abs() < 1e-12);assert!(besselj(1.0_f64, 0.0).abs() < 1e-12);
// Y_0(x) diverges as x -> 0let y0 = bessely(0.0_f64, 1.0);Modified Bessel Functions
Section titled “Modified Bessel Functions”and are solutions to the modified Bessel equation and arise in problems with exponential rather than oscillatory behavior.
use numra::special::{besseli, besselk};
let i0 = besseli(0.0_f64, 1.0); // I_0(1)let k0 = besselk(0.0_f64, 1.0); // K_0(1)Use cases: Signal processing (Kaiser window), statistical distributions (von Mises), heat conduction, elastic vibrations.
Elliptic Integrals
Section titled “Elliptic Integrals”Elliptic integrals arise in computing arc lengths of ellipses, periods of pendulums, and in many problems in celestial mechanics and electrostatics.
Complete Elliptic Integrals
Section titled “Complete Elliptic Integrals”where the parameter is the squared elliptic modulus ().
use numra::special::{ellipk, ellipe};
// K(0) = E(0) = pi/2assert!((ellipk(0.0_f64) - std::f64::consts::FRAC_PI_2).abs() < 1e-12);assert!((ellipe(0.0_f64) - std::f64::consts::FRAC_PI_2).abs() < 1e-12);
// K(m) -> infinity as m -> 1let k_near_1 = ellipk(0.999_f64);Incomplete Elliptic Integral of the First Kind
Section titled “Incomplete Elliptic Integral of the First Kind”use numra::special::ellipf;
let val = ellipf(1.0_f64, 0.5); // F(1.0, 0.5)Use cases: Pendulum period beyond small-angle approximation, conformal mapping, geodesics on an ellipsoid.
Airy Functions
Section titled “Airy Functions”Solutions to Airy’s equation :
Ai(x) decays exponentially for and oscillates for . Bi(x) grows exponentially for .
use numra::special::{airy_ai, airy_bi};
let ai0 = airy_ai(0.0_f64); // Ai(0) = 1 / (3^{2/3} * Gamma(2/3))let bi0 = airy_bi(0.0_f64); // Bi(0) = 1 / (3^{1/6} * Gamma(2/3))Use cases: Quantum mechanics (WKB turning points), optics (diffraction near caustics), fluid dynamics (Stokes flow near boundaries).
Hypergeometric Functions
Section titled “Hypergeometric Functions”The generalized hypergeometric functions unify many special functions.
Confluent Hypergeometric —
Section titled “Confluent Hypergeometric — {}_1F_1”Also known as Kummer’s function .
use numra::special::hyp1f1;
// 1F1(1; 1; z) = e^zlet val = hyp1f1(1.0_f64, 1.0, 1.0);assert!((val - std::f64::consts::E).abs() < 1e-8);Gauss Hypergeometric —
Section titled “Gauss Hypergeometric — {}_2F_1”Generalizes many classical functions: Legendre polynomials, Chebyshev polynomials, the incomplete beta function, and more.
use numra::special::hyp2f1;
let val = hyp2f1(1.0_f64, 1.0, 2.0, 0.5); // -ln(1-z)/z at z=0.5 = 2*ln(2)Use cases: Angular momentum coupling in quantum mechanics, solutions to Fuchsian differential equations, statistical distributions.
Orthogonal Polynomials
Section titled “Orthogonal Polynomials”Classical orthogonal polynomial families, essential for spectral methods, Gaussian quadrature, and expansion in orthogonal bases.
Legendre Polynomials
Section titled “Legendre Polynomials”are orthogonal on with weight 1:
use numra::special::legendre_p;
assert!((legendre_p(0, 0.5_f64) - 1.0).abs() < 1e-12); // P_0(x) = 1assert!((legendre_p(1, 0.5_f64) - 0.5).abs() < 1e-12); // P_1(x) = xassert!((legendre_p(2, 0.5_f64) - (-0.125)).abs() < 1e-12); // P_2(x) = (3x^2-1)/2Associated Legendre Functions
Section titled “Associated Legendre Functions”for spherical harmonics:
use numra::special::legendre_plm;
let val = legendre_plm(2, 1, 0.5_f64); // P_2^1(0.5)Hermite Polynomials
Section titled “Hermite Polynomials”are orthogonal on with weight :
use numra::special::hermite_h;
assert!((hermite_h(0, 1.0_f64) - 1.0).abs() < 1e-12); // H_0 = 1assert!((hermite_h(1, 1.0_f64) - 2.0).abs() < 1e-12); // H_1 = 2xassert!((hermite_h(2, 1.0_f64) - 2.0).abs() < 1e-12); // H_2 = 4x^2 - 2Use cases: Quantum harmonic oscillator wavefunctions, Gauss-Hermite quadrature, probability theory.
Laguerre Polynomials
Section titled “Laguerre Polynomials”are orthogonal on with weight :
use numra::special::laguerre_l;
assert!((laguerre_l(0, 1.0_f64) - 1.0).abs() < 1e-12); // L_0 = 1assert!((laguerre_l(1, 1.0_f64) - 0.0).abs() < 1e-12); // L_1 = 1-xassert!((laguerre_l(2, 1.0_f64) - (-0.5)).abs() < 1e-12); // L_2 = (x^2-4x+2)/2Use cases: Hydrogen atom radial wavefunctions, Gauss-Laguerre quadrature.
Chebyshev Polynomials
Section titled “Chebyshev Polynomials”are orthogonal on with weight :
use numra::special::chebyshev_t;
assert!((chebyshev_t(0, 0.5_f64) - 1.0).abs() < 1e-12); // T_0 = 1assert!((chebyshev_t(1, 0.5_f64) - 0.5).abs() < 1e-12); // T_1 = xassert!((chebyshev_t(2, 0.5_f64) - (-0.5)).abs() < 1e-12); // T_2 = 2x^2 - 1Use cases: Spectral methods, function approximation (Chebyshev expansion), filter design, numerical analysis.
Zeta Functions
Section titled “Zeta Functions”Riemann Zeta Function
Section titled “Riemann Zeta Function”Extended to the complex plane via analytic continuation.
use numra::special::zeta;
assert!((zeta(2.0_f64) - std::f64::consts::PI.powi(2) / 6.0).abs() < 1e-10);Hurwitz Zeta Function
Section titled “Hurwitz Zeta Function”Generalizes the Riemann zeta ().
use numra::special::hurwitz_zeta;
let val = hurwitz_zeta(2.0_f64, 1.0); // should equal zeta(2)Use cases: Number theory, quantum field theory (Casimir effect), thermodynamics (Bose-Einstein and Fermi-Dirac integrals).
Miscellaneous Functions
Section titled “Miscellaneous Functions”Dawson’s Integral
Section titled “Dawson’s Integral”Related to the imaginary error function. Used in plasma physics and spectroscopy (Voigt profile computation).
use numra::special::dawson;
let val = dawson(1.0_f64);Fresnel Integrals
Section titled “Fresnel Integrals”Used in optics (near-field diffraction) and road/railway design (clothoid curves).
use numra::special::{fresnel_s, fresnel_c};
let s = fresnel_s(1.0_f64);let c = fresnel_c(1.0_f64);Mittag-Leffler Function
Section titled “Mittag-Leffler Function”Generalizes the exponential function: . The Mittag-Leffler function is the natural generalization of the exponential for fractional calculus.
use numra::special::{mittag_leffler, mittag_leffler_1};
// E_{1,1}(z) = e^zlet val = mittag_leffler(1.0_f64, 1.0, 1.0);assert!((val - std::f64::consts::E).abs() < 1e-8);
// Shorthand: E_{alpha}(z) = E_{alpha, 1}(z)let val = mittag_leffler_1(0.5_f64, -1.0);Use cases: Solutions of fractional differential equations, viscoelastic material models, anomalous diffusion, relaxation processes in disordered media.
Function Family Summary
Section titled “Function Family Summary”| Family | Functions | Typical Use Cases |
|---|---|---|
| Gamma | gamma, lgamma, digamma, beta, gammainc, gammaincc, betainc | Statistics, combinatorics, distribution CDFs |
| Error | erf, erfc, erfinv, erfcinv | Normal distribution, diffusion, heat equation |
| Bessel | besselj, bessely, besseli, besselk | Cylindrical symmetry, wave propagation, signal processing |
| Elliptic | ellipk, ellipe, ellipf | Pendulum, celestial mechanics, electrostatics |
| Airy | airy_ai, airy_bi | Quantum mechanics, optics, WKB approximation |
| Hypergeometric | hyp1f1, hyp2f1 | General framework unifying many special functions |
| Polynomials | legendre_p, hermite_h, laguerre_l, chebyshev_t, legendre_plm | Spectral methods, quantum mechanics, quadrature |
| Zeta | zeta, hurwitz_zeta | Number theory, quantum field theory, thermodynamics |
| Misc | dawson, fresnel_s, fresnel_c, mittag_leffler, mittag_leffler_1 | Spectroscopy, optics, fractional calculus |