special
On this page
Special functions of mathematical physics and statistics, over f64
and, where standard, Complex<f64>. The functions are scalar and follow
SciPy's names; f.(x) maps one over an array and apply does it on
all cores. A domain error gives NaN; only the table builders throw
SpecialError. Pure Nabi, so it builds for every target, wasm included.
Modules
notationThe functions under the names of the textbooks:Γ(x),ψ(x),ζ(s),J₀(x),Φ⁻¹(p).
Enums
SpecialErrorSource
pub enum SpecialErrorWhat went wrong in a call that throws.
use special::SpecialError;
let e = try {
_ = try special::roots_legendre(0);
SpecialError::InvalidArgument { what: "" }
} catch e: SpecialError { e };
assert(f"{e}".contains("n >= 1"));
An argument out of its range;
whatnames it, the range and what to pass instead.An iteration did not converge in
iterationssteps.
Trait implementations 2
impl Display for SpecialErrorimpl Error for SpecialError
Traits
ElementarySource
pub trait ElementaryElementary functions on Complex<f64> and Complex<f32>, as
methods. f32 values are computed in f64 and rounded once.
use special::Elementary;
use core::num::Complex;
let z = 1.0 + 2.0i;
assert(z.sin().is_close(3.165778513216168 + 1.959601041421606i));
assert(z.asin().sin().is_close(z));
// The cut of asin runs along the real axis beyond 1; the sign of a
// zero imaginary part picks the side.
assert(Complex::new(2.0, 0.0).asin().im > 0.0 && Complex::new(2.0, -0.0).asin().im < 0.0);
sin z.cos z.tan z.sinh z.cosh z.tanh z, without overflow for large|re|.The principal
asin z; cuts along the real axis outside[-1, 1].The principal
acos z; cuts asasin.The principal
atan z; cuts along the imaginary axis outside[-i, i].The principal
asinh z; cuts along the imaginary axis outside[-i, i].The principal
acosh z(real part >= 0); cut along the real axis left of 1.The principal
atanh z; cuts along the real axis outside[-1, 1].log10 zon the principal branch.log2 zon the principal branch.The principal cube root (
|z|^(1/3) e^(i arg(z) / 3)):cbrtof-8is1 + 1.732i, not-2.e^z - 1, accurate for smallz.ln(1 + z), accurate for smallz.Whether
|self - other| <= max(rel * max(|self|, |other|), abs), the rule off64::is_close;reldefaults to 1e-9 (f32: 2^-20).
Functions
aiSource
pub fn ai(x: f64) -> f64 The Airy function Ai(x), the solution of y'' = x y that decays
as x -> inf. See airy for the accuracy.
assert((special::ai(1.0) - 0.13529241631288141).abs() < 3e-16);
aipSource
pub fn aip(x: f64) -> f64 Ai'(x).
assert((special::aip(1.0) + 0.15914744129679328).abs() < 3e-16);
airySource
pub fn airy(x: f64) -> (f64, f64, f64, f64) (Ai(x), Ai'(x), Bi(x), Bi'(x)) in one call. About 1e-15 relative
for x > -2.5 (Ai underflows past 105, Bi overflows past 104); for
x < -2.5 about 1e-16 absolute times the envelope |x|^(1/4), with
the phase error zeta eps that rounding x itself costs. SciPy:
airy.
let (ai, aip, bi, bip) = special::airy(0.0);
assert((ai - 0.3550280538878172).abs() < 1e-16 && (bip - 0.4482883573538264).abs() < 3e-16);
// The Wronskian Ai Bi' - Ai' Bi = 1 / pi.
let (a, ap, b, bp) = special::airy(-3.7);
assert((a * bp - ap * b - 1.0 / 3.141592653589793).abs() < 1e-15);
airyeSource
pub fn airye(x: f64) -> (f64, f64, f64, f64) airy(x) scaled: Ai and Ai' times e^zeta, Bi and Bi' times
e^-zeta for x > 0 (zeta = 2/3 x^(3/2)), unscaled for x <= 0;
finite where airy under- or overflows. SciPy: airye.
let (eai, _, ebi, _) = special::airye(200.0);
// Ai(x) e^zeta ~ x^(-1/4) / (2 sqrt(pi)).
assert(((eai - 200f64.pow(-0.25) / (2.0 * 3.141592653589793f64.sqrt())) / eai).abs() < 1e-4);
assert(ebi.is_finite());
biSource
pub fn bi(x: f64) -> f64 The Airy function Bi(x), the solution of y'' = x y that grows
as x -> inf.
assert((special::bi(1.0) - 1.2074235949528713).abs() < 3e-16);
bipSource
pub fn bip(x: f64) -> f64 Bi'(x).
assert((special::bip(1.0) - 0.9324359333927756).abs() < 2e-16);
applySource
pub fn apply<const N: usize>(f: fn(f64) -> f64 + Send + Sync, a: Array<f64, N>) -> Array<f64, N> f of every element of a, in parallel above 16384 elements; the
same values as f.(a) (each element is computed alone, so the
split does not change a result).
use core::array::{Array, Vector};
let x: Vector<f64> = Array::from_fn([100000], |i: [usize; 1]| -> f64 { 0.001 * i[0] as f64 });
let y = special::apply(special::erf, x);
assert(y[2000] == special::erf(2.0) && y == special::erf.(x));
hankel1Source
pub fn hankel1(v: f64, x: f64) -> Complex<f64> The Hankel function of the first kind H1_v(x) = J_v(x) + i Y_v(x)
for real v and x > 0 (NaN parts otherwise); one evaluation gives
both parts, with the accuracy of jv and yv. SciPy: hankel1.
let h = special::hankel1(0.5, 1.0);
assert(h.re == special::jv(0.5, 1.0) && h.im == special::yv(0.5, 1.0));
hankel1eSource
pub fn hankel1e(v: f64, x: f64) -> Complex<f64> H1_v(x) e^-ix, which is smooth in x (|H1| ~ sqrt(2 / (pi x))
with the oscillation removed). SciPy: hankel1e.
let h = special::hankel1e(0.5, 100.0);
// H1_1/2(x) = -i sqrt(2 / (pi x)) e^ix.
assert(h.re.abs() < 1e-16 && (h.im + (2.0 / (3.141592653589793 * 100.0)).sqrt()).abs() < 3e-16);
hankel2Source
pub fn hankel2(v: f64, x: f64) -> Complex<f64> The Hankel function of the second kind H2_v(x) = J_v(x) - i Y_v(x).
SciPy: hankel2.
assert(special::hankel2(1.5, 2.0) == special::hankel1(1.5, 2.0).conj());
hankel2eSource
pub fn hankel2e(v: f64, x: f64) -> Complex<f64> H2_v(x) e^ix. SciPy: hankel2e.
assert(special::hankel2e(0.5, 3.0) == special::hankel1e(0.5, 3.0).conj());
i0Source
pub fn i0(x: f64) -> f64 The modified Bessel function of the first kind of order 0, I0(x),
even in x; inf past 713.9. About 2 ulp. SciPy: i0.
assert(special::i0(0.0) == 1.0);
assert((special::i0(1.0) - 1.2660658777520082).abs() < 3e-16);
i0eSource
pub fn i0e(x: f64) -> f64 I0(x) e^-|x|, the exponentially scaled modified Bessel function of
order 0. About 2 ulp. SciPy: i0e.
assert((special::i0e(1000.0) - 0.012617240455891257).abs() < 1e-17);
i1Source
pub fn i1(x: f64) -> f64 The modified Bessel function of the first kind of order 1, I1(x),
odd in x. About 2 ulp. SciPy: i1.
assert((special::i1(1.0) - 0.5651591039924851).abs() < 2e-16);
i1eSource
pub fn i1e(x: f64) -> f64 I1(x) e^-|x|, odd in x. About 2 ulp. SciPy: i1e.
assert((special::i1e(1.0) - 0.2079104153497085).abs() < 3e-16);
ivSource
pub fn iv(v: f64, x: f64) -> f64 The modified Bessel function of the first kind I_v(x) of real
order. Integer v takes x < 0 (I_n(-x) = (-1)^n I_n(x)); other
v give NaN there. inf once the value overflows. About 1e-15
relative (4e-15 in the tests); I_-v = I_v + (2 / pi) sin(v pi) K_v. SciPy: iv.
assert((special::iv(0.5, 1.0) - 0.9376748882454876).abs() < 5e-16);
assert(special::iv(0.0, 2.0) == special::i0(2.0));
iveSource
pub fn ive(v: f64, x: f64) -> f64 I_v(x) e^-|x|, the exponentially scaled iv, finite where iv
overflows. SciPy: ive.
assert((special::ive(0.5, 1000.0) - 0.0126156626101008).abs() < 1e-17);
j0Source
pub fn j0(x: f64) -> f64 The Bessel function of the first kind of order 0, J0(x). About 2
ulp up to 8 (its zeros 2.40 and 5.52 included); past 8 the error is
about 1e-16 absolute, so the relative error grows near the zeros.
SciPy: j0.
assert(special::j0(0.0) == 1.0);
assert((special::j0(1.0) - 0.7651976865579666).abs() < 2e-16);
j1Source
pub fn j1(x: f64) -> f64 The Bessel function of the first kind of order 1, J1(x), odd in
x. About 2 ulp up to 8, about 1e-16 absolute past it. SciPy: j1.
assert((special::j1(1.0) - 0.44005058574493355).abs() < 3e-16);
assert(special::j1(-1.0) == -special::j1(1.0));
jnSource
pub fn jn(n: i32, x: f64) -> f64 The Bessel function of the first kind of integer order, J_n(x):
forward recurrence from J0 and J1 where it is stable (x >= n);
below that the power series for x < 1, else Miller's backward
recurrence. J_-n = (-1)^n J_n, J_n(-x) = (-1)^n J_n(x). About
1e-15 relative off the zeros. SciPy: jn.
assert((special::jn(5, 2.0) - 0.007039629755871685).abs() < 1e-18);
assert(special::jn(-1, 1.0) == -special::j1(1.0));
jvSource
pub fn jv(v: f64, x: f64) -> f64 The Bessel function of the first kind J_v(x) of real order v.
Integer v goes to jn; otherwise x < 0 is NaN. About 1e-15
relative off the zeros for v up to a few hundred (5e-15 in the
tests), absolute 1e-16 near them; negative v by J_-v = cos(v pi) J_v - sin(v pi) Y_v. Complex x is not covered. SciPy: jv.
assert((special::jv(0.5, 1.0) - 0.6713967071418031).abs() < 3e-16);
assert(special::jv(2.0, 1.5) == special::jn(2, 1.5));
jveSource
pub fn jve(v: f64, x: f64) -> f64 jv scaled by e^-|Im x|, which for real x is jv itself (the
name for code ported from SciPy's jve).
assert(special::jve(1.5, 2.0) == special::jv(1.5, 2.0));
k0Source
pub fn k0(x: f64) -> f64 The modified Bessel function of the second kind of order 0, K0(x),
x > 0: inf at 0, NaN below. About 2 ulp. SciPy: k0.
assert((special::k0(1.0) - 0.42102443824070834).abs() < 3e-16);
k0eSource
pub fn k0e(x: f64) -> f64 K0(x) e^x. About 2 ulp. SciPy: k0e.
assert((special::k0e(1000.0) - 0.03962832160075422).abs() < 1e-17);
k1Source
pub fn k1(x: f64) -> f64 The modified Bessel function of the second kind of order 1, K1(x),
x > 0. About 2 ulp. SciPy: k1.
assert((special::k1(1.0) - 0.6019072301972346).abs() < 2e-16);
k1eSource
pub fn k1e(x: f64) -> f64 K1(x) e^x. About 2 ulp. SciPy: k1e.
assert((special::k1e(1.0) - 1.636153486263258).abs() < 4e-16);
knSource
pub fn kn(n: i32, x: f64) -> f64 The modified Bessel function of the second kind of integer order,
K_n(x), x > 0, by forward recurrence from K0 and K1 (stable);
K_-n = K_n. About 1e-15 relative. SciPy: kn.
assert((special::kn(3, 2.0) - 0.6473853909486342).abs() < 1e-15);
kvSource
pub fn kv(v: f64, x: f64) -> f64 The modified Bessel function of the second kind K_v(x) of real
order, x > 0 (NaN below, inf at 0); K_-v = K_v. Integer v
goes to kn. About 1e-15 relative (3e-15 in the tests). SciPy:
kv.
assert((special::kv(0.5, 1.0) - 0.4610685044478946).abs() < 3e-16);
kveSource
pub fn kve(v: f64, x: f64) -> f64 K_v(x) e^x, the exponentially scaled kv. SciPy: kve.
assert((special::kve(0.5, 4.0) - (3.141592653589793 / 8.0).sqrt()).abs() < 3e-16);
spherical_inSource
pub fn spherical_in(n: u32, x: f64, derivative: bool = false) -> f64 The modified spherical Bessel function of the first kind i_n(x) = sqrt(pi / (2x)) I_(n+1/2)(x), or its derivative (i'_n = i_(n-1) - (n + 1) / x i_n). SciPy: spherical_in.
assert((special::spherical_in(0, 2.0) - 2f64.sinh() / 2.0).abs() < 1e-15);
spherical_jnSource
pub fn spherical_jn(n: u32, x: f64, derivative: bool = false) -> f64 The spherical Bessel function of the first kind j_n(x) = sqrt(pi / (2x)) J_(n+1/2)(x), or its derivative with derivative: true (j'_n = j_(n-1) - (n + 1) / x j_n). Upward recurrence from sin x / x
where it is stable (x >= n), jv below it; about 1e-15 relative
off the zeros. SciPy: spherical_jn.
assert((special::spherical_jn(0, 2.0) - 2f64.sin() / 2.0).abs() < 3e-16);
assert((special::spherical_jn(1, 2.0, derivative: true) - 0.01925093843284923).abs() < 3e-16);
spherical_knSource
pub fn spherical_kn(n: u32, x: f64, derivative: bool = false) -> f64 The modified spherical Bessel function of the second kind k_n(x) = sqrt(pi / (2x)) K_(n+1/2)(x) (k_0 = pi / (2x) e^-x), x > 0, or
its derivative (k'_n = -k_(n-1) - (n + 1) / x k_n); upward
recurrence. SciPy: spherical_kn.
assert((special::spherical_kn(0, 1.0) - 1.5707963267948966 * (-1.0f64).exp()).abs() < 3e-16);
spherical_ynSource
pub fn spherical_yn(n: u32, x: f64, derivative: bool = false) -> f64 The spherical Bessel function of the second kind y_n(x), x > 0,
or its derivative; upward recurrence from -cos x / x. SciPy:
spherical_yn.
assert((special::spherical_yn(0, 2.0) + 2f64.cos() / 2.0).abs() < 3e-16);
y0Source
pub fn y0(x: f64) -> f64 The Bessel function of the second kind of order 0, Y0(x), for x > 0: -inf at 0, NaN below. About 1e-16 absolute (relative near the
zeros grows as 1 / Y0). SciPy: y0.
assert((special::y0(1.0) - 0.08825696421567697).abs() < 3e-16);
assert(special::y0(0.0) == f64::NEG_INFINITY);
y1Source
pub fn y1(x: f64) -> f64 The Bessel function of the second kind of order 1, Y1(x), for x > 0: -inf at 0, NaN below. About 1e-16 absolute. SciPy: y1.
assert((special::y1(1.0) + 0.7812128213002887).abs() < 3e-16);
ynSource
pub fn yn(n: i32, x: f64) -> f64 The Bessel function of the second kind of integer order, Y_n(x),
x > 0, by forward recurrence from Y0 and Y1 (always stable for Y);
-inf once it overflows. Y_-n = (-1)^n Y_n. About 1e-15 relative
off the zeros. SciPy: yn.
assert((special::yn(3, 2.0) + 1.1277837768404278).abs() < 1e-15);
yvSource
pub fn yv(v: f64, x: f64) -> f64 The Bessel function of the second kind Y_v(x) of real order, x > 0 (NaN below, -inf at 0 for v >= 0). Integer v goes to yn.
Accuracy as jv. SciPy: yv.
assert((special::yv(0.5, 1.0) + 0.43109886801837607).abs() < 3e-16);
yveSource
pub fn yve(v: f64, x: f64) -> f64 yv scaled by e^-|Im x|: yv itself for real x. SciPy: yve.
assert(special::yve(1.5, 2.0) == special::yv(1.5, 2.0));
betaSource
pub fn beta(a: f64, b: f64) -> f64 The beta function B(a, b) = Gamma(a) Gamma(b) / Gamma(a + b).
use special::beta;
assert((beta(2.0, 3.0) - 1.0 / 12.0).abs() < 1e-17);
betaincSource
pub fn betainc(a: f64, b: f64, x: f64) -> f64 The regularized incomplete beta function I_x(a, b), a, b > 0, 0 <= x <= 1 (NaN otherwise); the CDF of Beta(a, b). Relative error
about 1e-14 for parameters up to 1e5. SciPy: betainc.
use special::betainc;
assert((betainc(2.0, 3.0, 0.4) - 0.5248).abs() < 1e-15);
assert(betainc(1.0, 1.0, 0.25) == 0.25);
betainc_factorSource
pub fn betainc_factor(a: f64, b: f64, x: f64, y: f64) -> f64 x^a y^b / B(a, b) for x + y = 1, the factor in front of the
incomplete beta function. The smaller of x, y is taken as exact
and the larger's log comes from it (ln_1p), so x = 1e-5 does
not lose the digits that 1 - 1e-5 rounds away; for a, b >= 10
the large powers cancel about the mean before the exponential
(DiDonato and Morris 1992). NaN outside a, b > 0, 0 <= x <= 1.
// x^2 y^3 / B(2, 3) = 12 x^2 y^3.
assert((special::betainc_factor(2.0, 3.0, 0.25, 0.75) - 12.0 * 0.0625 * 0.421875).abs() < 1e-15);
betainc_pqSource
pub fn betainc_pq(a: f64, b: f64, x: f64, y: f64) -> (f64, f64) (I_x(a, b), 1 - I_x(a, b)) with y = 1 - x passed exactly (so a
tiny y keeps its digits), each accurate where it is the smaller.
NaN outside a, b > 0, 0 <= x, y.
let (i, c) = special::betainc_pq(2.0, 3.0, 0.4, 0.6);
assert((i - 0.5248).abs() < 1e-15 && (c - 0.4752).abs() < 1e-15);
betainccSource
pub fn betaincc(a: f64, b: f64, x: f64) -> f64 1 - I_x(a, b), accurate where it is small. SciPy: betaincc.
use special::{betainc, betaincc};
assert((betaincc(2.0, 3.0, 0.4) - (1.0 - betainc(2.0, 3.0, 0.4))).abs() < 1e-15);
betainccinvSource
pub fn betainccinv(a: f64, b: f64, q: f64) -> f64 The inverse of betaincc in x: 1 - I_x(a, b) = q, accurate for
tiny q. SciPy: betainccinv.
use special::{betaincc, betainccinv};
let x = betainccinv(2.0, 5.0, 1e-12);
// x is 0.99722: one ulp of x moves the tail by 2e-13 of itself.
assert(((betaincc(2.0, 5.0, x) - 1e-12) / 1e-12).abs() < 1e-11);
betaincinvSource
pub fn betaincinv(a: f64, b: f64, p: f64) -> f64 The inverse of betainc in x: I_x(a, b) = p. About 1e-15
relative. SciPy: betaincinv.
use special::{betainc, betaincinv};
let x = betaincinv(2.0, 5.0, 0.3);
assert((betainc(2.0, 5.0, x) - 0.3).abs() < 1e-15);
lbetaSource
pub fn lbeta(a: f64, b: f64) -> f64 ln B(a, b) = ln Gamma(a) + ln Gamma(b) - ln Gamma(a + b) for `a, b
0` (NaN otherwise), without the cancellation of the plain sum when either is large. About 1e-15 relative.
use special::lbeta;
assert((lbeta(2.0, 3.0) - (1.0 / 12.0).ln()).abs() < 1e-15);
assert((lbeta(1e5, 1e5) + 138633.92706134806).abs() < 1e-10);
lcombSource
pub fn lcomb(n: f64, k: f64) -> f64 ln C(n, k), the log of the binomial coefficient, for real `n >= k
= 0
(throughlbeta, so no overflow atn = 1e300). Seecombfor the value andcomb_exact` for the exact integer.
assert((special::lcomb(10.0, 3.0) - 120f64.ln()).abs() < 1e-14);
binomSource
pub fn binom(n: f64, k: f64) -> f64 The binomial coefficient for real arguments, Gamma(n + 1) / (Gamma(k + 1) Gamma(n - k + 1)), continued through the poles as
the limit (0 where only the denominator has one, the generalized
n (n - 1) ... (n - k + 1) / k! for integer k and any n).
Correctly rounded at integer n and k, about k ulp for integer
k below 1000, about 1e-15 times ln |result| otherwise.
SciPy: binom.
assert(special::binom(5.0, 2.0) == 10.0);
assert((special::binom(0.5, 2.0) + 0.125).abs() < 1e-16);
assert(special::binom(-1.0, 3.0) == -1.0);
combSource
pub fn comb(n: u64, k: u64) -> f64 C(n, k) as f64: exact while below 2^53, correctly rounded up to
the f64 range, from lcomb past the point where the exact integer
would have more than 2000 bits (so then about 1e-13 relative). 0
for k > n. SciPy: comb.
assert(special::comb(52, 5) == 2598960.0);
assert(special::comb(1030, 515) == f64::INFINITY);
comb_exactSource
pub fn comb_exact(n: u64, k: u64) -> int The binomial coefficient C(n, k) as an exact integer (0 for k > n).
assert(special::comb_exact(10, 3) == 120);
assert(special::comb_exact(100, 50) == 100891344545564193334812497256);
factorialSource
pub fn factorial(n: u64) -> f64 n! as f64: exact through 22!, correctly rounded through 170!,
inf past it. SciPy: factorial.
assert(special::factorial(5) == 120.0);
assert(special::factorial(171) == f64::INFINITY);
factorial2Source
pub fn factorial2(n: i64) -> f64 The double factorial n!! as f64, for n >= -1 ((-1)!! = 0!! = 1); exact while it fits 53 bits, correctly rounded up to inf
past 300!!. NaN below -1. SciPy: factorial2.
assert(special::factorial2(7) == 105.0 && special::factorial2(-1) == 1.0);
factorial2_exactSource
pub fn factorial2_exact(n: u64) -> int The double factorial n!! = n (n - 2) (n - 4) ... down to 1 or 2,
as an exact integer; 0!! = 1.
assert(special::factorial2_exact(9) == 945 && special::factorial2_exact(10) == 3840);
factorial_exactSource
pub fn factorial_exact(n: u64) -> int n! as an exact integer. Binary splitting: 100000! (456574
digits) takes a few tens of milliseconds.
assert(special::factorial_exact(25) == 15511210043330985984000000);
assert(f"{special::factorial_exact(100)}".len() == 158);
lfactorialSource
pub fn lfactorial(n: u64) -> f64 ln n!, for any size of n.
assert((special::lfactorial(1000) - 5912.128178488163).abs() < 1e-11);
multicombSource
pub fn multicomb(n: u64, k: u64) -> f64 C(n + k - 1, k): the number of multisets of size k from n
kinds (SciPy's comb(n, k, repetition=True)).
assert(special::multicomb(3, 2) == 6.0);
permSource
pub fn perm(n: u64, k: u64) -> f64 n! / (n - k)! as f64, correctly rounded (0 for k > n, inf past
the f64 range). SciPy: perm.
assert(special::perm(10, 3) == 720.0);
perm_exactSource
pub fn perm_exact(n: u64, k: u64) -> int The number of ordered selections n! / (n - k)! as an exact
integer (0 for k > n).
assert(special::perm_exact(10, 3) == 720);
pochSource
pub fn poch(x: f64, m: f64) -> f64 The Pochhammer symbol (rising factorial) (x)_m = Gamma(x + m) / Gamma(x), for real x and m; x (x + 1) ... (x + m - 1) for
integer m >= 0. SciPy: poch.
assert(special::poch(3.0, 4.0) == 360.0);
assert(special::poch(-3.0, 2.0) == 6.0);
assert((special::poch(0.5, 0.5) - 0.5641895835477563).abs() < 1e-16);
stirling2Source
pub fn stirling2(n: u64, k: u64) -> int The Stirling number of the second kind S(n, k): the number of ways
to split n items into k non-empty sets, exact. SciPy:
stirling2(exact=True).
assert(special::stirling2(5, 2) == 15 && special::stirling2(10, 3) == 9330);
cdigammaSource
pub fn cdigamma(z: Complex<f64>) -> Complex<f64> The digamma function psi(z) for complex z: reflection `psi(1 - z)
- pi cot(pi z)
left of 0, the recurrence up to|z| >= 10and the asymptotic series there (B_2k / 2kthrough k = 9). About 1e-15 relative (7e-16 measured) except near the zeros on the real axis, where the error is 1e-16 absolute; the realdigammaon the real axis.NaNat the poles. SciPy:psi` of a complex argument.
let p = special::cdigamma(1.0i);
assert((p.re - 0.09465032062247697).abs() < 1e-15 && (p.im - 2.0766740474685810).abs() < 1e-15);
cgammaSource
pub fn cgamma(z: Complex<f64>) -> Complex<f64> Gamma(z) for complex z, as exp(clgamma(z)) (the real gamma on
the real axis), so its relative error is the absolute error of ln Gamma: about 5e-15 within 7 of the origin, |ln Gamma(z)| eps past
it (2.5e-14 measured at |z| = 20). NaN at the poles.
SciPy: gamma of a complex argument.
let g = special::cgamma(0.5 + 0.0i);
assert((g.re - 1.7724538509055159).abs() < 1e-15 && g.im == 0.0);
let h = special::cgamma(1.0i);
assert((h.re + 0.15494982830181067).abs() < 5e-15 && (h.im + 0.49801566811835607).abs() < 5e-15);
clgammaSource
pub fn clgamma(z: Complex<f64>) -> Complex<f64> The principal branch of ln Gamma(z) (SciPy's loggamma): equal to
lgamma(x) on the positive real axis and continuous everywhere off
the negative real axis, across which its imaginary part steps by 2
pi. About 1e-15 relative where Stirling's series is used (|Re z| or
|Im z| past 7); nearer 0 the recurrence cancels to about 5e-15
absolute (3e-15 measured), and the Taylor series about 1 and 2 keep
full relative accuracy at those zeros. NaN at the poles 0, -1, -2,
...
let z = special::clgamma(1.0 + 1.0i);
assert((z.re + 0.6509231993018564).abs() < 5e-15 && (z.im + 0.3016403204675332).abs() < 5e-15);
assert(special::clgamma(5.0 + 0.0i).re == 24f64.ln());
crgammaSource
pub fn crgamma(z: Complex<f64>) -> Complex<f64> 1 / Gamma(z) for complex z: 0 at the poles, exp(-clgamma(z))
elsewhere. SciPy: rgamma of a complex argument.
let r = special::crgamma(-2.0 + 0.0i);
assert(r.re == 0.0 && r.im == 0.0);
rgammaSource
pub fn rgamma(x: f64) -> f64 1 / Gamma(x), entire: 0 at the poles 0, -1, -2, ..., no overflow
past 171.6 (it underflows to 0 instead). A few ulp. SciPy: rgamma.
assert(special::rgamma(-3.0) == 0.0 && special::rgamma(4.0) == 1.0 / 6.0);
elliprcSource
pub fn elliprc(x: f64, y: f64) -> f64 R_C(x, y) = R_F(x, y, y), x >= 0, y != 0 (NaN otherwise); the
Cauchy principal value for y < 0. Duplication with the series to
s^7; about 2 ulp. SciPy: elliprc.
// R_C(0, 1/4) = pi, R_C(9/4, 2) = ln 2.
assert((special::elliprc(0.0, 0.25) - 3.141592653589793).abs() < 1e-15);
assert((special::elliprc(2.25, 2.0) - 2f64.ln()).abs() < 4e-16);
elliprdSource
pub fn elliprd(x: f64, y: f64, z: f64) -> f64 R_D(x, y, z) = R_J(x, y, z, z), x, y >= 0 (not both 0), z > 0
(NaN otherwise). Duplication and the fifth-order series; about 3
ulp. SciPy: elliprd.
assert((special::elliprd(0.0, 2.0, 1.0) - 1.7972103521033884).abs() < 1e-15);
elliprfSource
pub fn elliprf(x: f64, y: f64, z: f64) -> f64 R_F(x, y, z), x, y, z >= 0 with at most one of them 0 (inf when
two are, NaN for a negative one). Duplication and the fifth-order
series; about 2 ulp. SciPy: elliprf.
// R_F(0, 1, 2) = K(1/2).
assert((special::elliprf(0.0, 1.0, 2.0) - 1.3110287771460600).abs() < 1e-15);
assert(special::elliprf(1.0, 1.0, 1.0) == 1.0);
elliprgSource
pub fn elliprg(x: f64, y: f64, z: f64) -> f64 R_G(x, y, z), x, y, z >= 0 (NaN otherwise), from 2 R_G = z R_F - (x - z)(y - z) R_D / 3 + sqrt(x y / z) with z the middle argument
(DLMF 19.21.10), so (x - z)(y - z) <= 0 and the three terms add
without cancellation; with a zero argument sqrt(z) E(1 - y / z) / 2
by the complete integral. About 4 ulp. SciPy: elliprg.
// R_G(0, 1, 1) = pi / 4.
assert((special::elliprg(0.0, 1.0, 1.0) - 0.7853981633974483).abs() < 4e-16);
elliprjSource
pub fn elliprj(x: f64, y: f64, z: f64, p: f64) -> f64 R_J(x, y, z, p), x, y, z >= 0 with at most one 0, p != 0 (NaN
otherwise); the Cauchy principal value for p < 0 (Carlson's
formula through R_J at a positive q, R_F and R_C).
Duplication and the fifth-order series; about 4 ulp for p > 0,
with 1 + e_m formed without cancellation so p far below x, y, z
keeps full accuracy; the principal value within about 1e-14, the
error of the cancelling terms of that formula. SciPy: elliprj.
// R_J(x, y, z, z) = R_D(x, y, z).
assert((special::elliprj(0.5, 1.0, 2.0, 2.0) - special::elliprd(0.5, 1.0, 2.0)).abs() < 1e-15);
assert((special::elliprj(2.0, 3.0, 4.0, 5.0) - 0.14297579667156753).abs() < 4e-16);
ellipjSource
pub fn ellipj(u: f64, m: f64) -> (f64, f64, f64, f64) The Jacobi elliptic functions (sn(u | m), cn(u | m), dn(u | m), am(u | m)) in the parameter m = k^2, with sn = sin am, cn = cos am and dn = sqrt(1 - m sn^2). For 0 <= m <= 1 the descending
Landen transformation (Cephes' ellpj; within about 4e-16 absolute of
mpmath for |u| <= 10, growing like |u| eps past it as the
amplitude does); m < 0 by sn(u | m) = sd(u sqrt(1 - m) | m / (m - 1)) / sqrt(1 - m) and m > 1 by sn(u | m) = sn(u sqrt(m) | 1 / m) / sqrt(m). For m > 1 the amplitude is not monotonic and am is the
angle of (cn, sn) in (-pi, pi]. NaN for NaN input. SciPy:
ellipj, which gives NaN outside 0 <= m <= 1.
let (sn, cn, dn, am) = special::ellipj(0.5, 0.3);
assert((sn - 0.4742156227118206).abs() < 4e-16);
assert((sn * sn + cn * cn - 1.0).abs() < 4e-16 && (dn * dn + 0.3 * sn * sn - 1.0).abs() < 4e-16);
assert(am.sin() == sn);
ellipeSource
pub fn ellipe(m: f64) -> f64 The complete elliptic integral of the second kind E(m) = integral_0^(pi/2) sqrt(1 - m sin^2 t) dt, m <= 1 (NaN above).
The arithmetic-geometric mean with the sum of squares (A&S 17.6),
its logarithmic series in 1 - m below 1 - m = 0.1 where that sum
cancels, and E(m) = sqrt(1 - m) E(m / (m - 1)) for m < 0. Within
3 ulp of mpmath on the tests. SciPy: ellipe.
assert(special::ellipe(1.0) == 1.0);
assert((special::ellipe(0.5) - 1.3506438810476755).abs() < 1e-15);
assert((special::ellipe(-1.0) - 1.9100988945138560).abs() < 1e-15);
ellipeincSource
pub fn ellipeinc(phi: f64, m: f64) -> f64 The incomplete elliptic integral of the second kind E(phi | m) = integral_0^phi sqrt(1 - m sin^2 t) dt, for any real amplitude
(E(phi + j pi) = 2 j E(m) + E(phi)) and m <= 1; for m > 1
while m sin^2 phi <= 1. For 0 < m < 1 through DLMF 19.25.10,
whose three terms are all of one sign ((1 - m) s R_F + m (1 - m) s^3 R_D / 3 + m s c / Delta), else through 19.25.9. Within 4 ulp of
mpmath on the tests. SciPy: ellipeinc.
assert((special::ellipeinc(1.0, 0.5) - 0.9273298836244401).abs() < 1e-15);
assert((special::ellipeinc(1.2, 1.0) - 1.2f64.sin()).abs() < 4e-16);
ellipkSource
pub fn ellipk(m: f64) -> f64 The complete elliptic integral of the first kind K(m) = integral_0^(pi/2) dt / sqrt(1 - m sin^2 t), m <= 1 (NaN above,
inf at 1). By the arithmetic-geometric mean, K = pi / (2 agm(1, sqrt(1 - m))); within 2 ulp of mpmath on the tests, m from -1e10
to 1 - 1e-16. Near m = 1 pass 1 - m to ellipkm1 instead, which
keeps the digits 1 - m rounds away. SciPy: ellipk.
assert((special::ellipk(0.0) - 1.5707963267948966).abs() < 4e-16);
assert((special::ellipk(0.5) - 1.8540746773013719).abs() < 1e-15);
assert(special::ellipk(1.0) == f64::INFINITY);
ellipkincSource
pub fn ellipkinc(phi: f64, m: f64) -> f64 The incomplete elliptic integral of the first kind F(phi | m) = integral_0^phi dt / sqrt(1 - m sin^2 t) for any real amplitude
(F(phi + j pi) = 2 j K(m) + F(phi)) and m <= 1; for m > 1
while m sin^2 phi <= 1 (NaN past it). sin(phi) R_F(cos^2 phi, 1 - m sin^2 phi, 1) with 1 - m sin^2 phi formed as cos^2 + (1 - m) sin^2, so m near 1 and phi near pi/2 keep their digits.
Within 4 ulp of mpmath on the tests (plus the rounding of phi
reduced by pi for large amplitudes). SciPy: ellipkinc.
assert(special::ellipkinc(0.7, 0.0) == 0.7);
assert((special::ellipkinc(1.0, 0.5) - 1.0832167728451688).abs() < 1e-15);
// A full half-period gives 2 K.
assert((special::ellipkinc(3.141592653589793, 0.5) - 2.0 * special::ellipk(0.5)).abs() < 1e-15);
ellipkm1Source
pub fn ellipkm1(p: f64) -> f64 K(1 - p), the complete integral of the first kind as a function
of the complementary parameter p = 1 - m >= 0 (NaN below, inf
at 0): full relative accuracy for tiny p, where K grows like
ln(4 / sqrt p). Within 2 ulp. SciPy: ellipkm1.
assert((special::ellipkm1(1e-20) - 24.41214529106035).abs() < 4e-14);
ellippiSource
pub fn ellippi(n: f64, m: f64) -> f64 The complete elliptic integral of the third kind Pi(n | m) = integral_0^(pi/2) dt / ((1 - n sin^2 t) sqrt(1 - m sin^2 t)), m <= 1, n != 1 (inf at n = 1); the Cauchy principal value for n > 1. R_F(0, 1 - m, 1) + n R_J(0, 1 - m, 1, 1 - n) / 3 (DLMF
19.25.2). The sign convention is mpmath's and Boost's (1 - n sin^2).
Within 14 ulp of mpmath on the tests, the principal values within
2e-14. SciPy has no ellippi.
assert((special::ellippi(0.0, 0.5) - special::ellipk(0.5)).abs() < 1e-15);
assert((special::ellippi(0.3, 0.5) - 2.2503768219439467).abs() < 1e-15);
ellippiincSource
pub fn ellippiinc(n: f64, phi: f64, m: f64) -> f64 The incomplete elliptic integral of the third kind Pi(n; phi | m) = integral_0^phi dt / ((1 - n sin^2 t) sqrt(1 - m sin^2 t)), for any
real amplitude when m <= 1 (Pi(n; phi + j pi | m) = 2 j Pi(n | m) + Pi(n; phi | m)), the principal value past the pole n sin^2 t = 1.
s R_F(c^2, Delta^2, 1) + n s^3 R_J(c^2, Delta^2, 1, 1 - n s^2) / 3
(DLMF 19.25.14). Within 14 ulp of mpmath on the tests away from the
pole. mpmath: ellippi(n, phi, m).
assert((special::ellippiinc(0.3, 1.2, 0.5) - 1.5303674742027079).abs() < 1e-15);
assert(special::ellippiinc(0.0, 1.0, 0.5) == special::ellipkinc(1.0, 0.5));
chiSource
pub fn chi(x: f64) -> f64 The hyperbolic cosine integral Chi(x); see shichi.
assert(special::chi(0.5238225713898644).abs() < 1e-16);
ciSource
pub fn ci(x: f64) -> f64 The cosine integral Ci(x); see sici.
assert((special::ci(2.0) - 0.42298082877486500).abs() < 2e-16);
e1Source
pub fn e1(x: f64) -> f64 The exponential integral E1(x) = int_x^inf e^-t / t dt, x > 0
(inf at 0, NaN below: there it is complex, -Ei(-x) - i pi). The
series to 0.8, the continued fraction above; about 3 ulp (7.2e-16
measured). SciPy: exp1.
assert((special::e1(1.0) - 0.21938393439552028).abs() < 1e-16);
assert(special::e1(0.0) == f64::INFINITY);
eiSource
pub fn ei(x: f64) -> f64 The exponential integral Ei(x) = -PV int_-x^inf e^-t / t dt: the
series about its zero 0.37250741... below 2 (full relative accuracy
at the zero), the plain series to 40, the asymptotic series above; -E1(-x) for x < 0.
About 4 ulp (9e-16 measured); -inf at 0, inf past 716.4. SciPy:
expi.
assert((special::ei(1.0) - 1.8951178163559368).abs() < 3e-16);
assert(special::ei(-1.0) == -special::e1(1.0));
expnSource
pub fn expn(n: u32, x: f64) -> f64 The generalized exponential integral E_n(x) = int_1^inf e^-xt / t^n dt for x >= 0: e^-x / x for n = 0, the series with its
digamma term for x <= 1, the continued fraction above (DLMF
8.19.17, bottom up). About 2 ulp (5.1e-16 measured); E_n(0) = 1 / (n - 1) for n >= 2. NaN for x < 0. SciPy: expn.
assert((special::expn(2, 1.0) - 0.14849550677592205).abs() < 1e-16);
assert(special::expn(3, 0.0) == 0.5);
shiSource
pub fn shi(x: f64) -> f64 The hyperbolic sine integral Shi(x); see shichi.
assert(special::shi(0.0) == 0.0);
shichiSource
pub fn shichi(x: f64) -> (f64, f64) The hyperbolic sine and cosine integrals (Shi(x), Chi(x)), Shi(x) = int_0^x sinh t / t dt, `Chi(x) = euler + ln x + int_0^x (cosh t -
- / t dt
: the series to 8 (Chi's about its zero 0.5238..., so it keeps full relative accuracy),(Ei(x) +- E1(x)) / 2past it. About 3 ulp to 8, 7 ulp past it where Ei sets the error (1.5e-15 measured near 30);infpast 716.4. Forx < 0, Shi is odd and Chi is the real part (Chi(|x|)), as SciPy. SciPy:shichi`.
let (s, c) = special::shichi(1.0);
assert((s - 1.0572508753757285).abs() < 3e-16 && (c - 0.83786694098020824).abs() < 2e-16);
siSource
pub fn si(x: f64) -> f64 The sine integral Si(x); see sici.
assert((special::si(2.0) - 1.6054129768026948).abs() < 3e-16);
siciSource
pub fn sici(x: f64) -> (f64, f64) The sine and cosine integrals (Si(x), Ci(x)), Si(x) = int_0^x sin t / t dt, Ci(x) = euler + ln x + int_0^x (cos t - 1) / t dt. The
series to 2 (Ci's about its first zero, so that zero keeps full
relative accuracy), the continued fraction past it: Si about 2 ulp
(4.5e-16 measured), Ci 2 ulp (5.1e-16) except near its zeros past
2, where the error is a few 1e-16 absolute. For x < 0, Si is odd and Ci is the real part of the
complex value (Ci(|x|)), as SciPy; Ci(0) = -inf. SciPy: sici.
let (s, c) = special::sici(1.0);
assert((s - 0.94608307036718301).abs() < 2e-16 && (c - 0.33740392290096813).abs() < 2e-16);
cdawsonSource
pub fn cdawson(z: Complex<f64>) -> Complex<f64> Dawson's integral of a complex argument, D(z) = sqrt(pi) / 2 i (e^-z^2 - w(z)): its Maclaurin series for |z| < 1, the Taylor
series in iy about the real axis for |Im z| < 0.05 (where the
difference cancels), the difference elsewhere. About 1e-15 relative
to |D(z)| (Im D relative 1e-14 within 0.05 of the real axis for
1 < |x| < 6.5). SciPy: dawsn of a complex argument.
let d = special::cdawson(1.0 + 1.0i);
assert((d.re - 0.9903730923223614).abs() < 1e-15 && (d.im + 0.6388730515644433).abs() < 1e-15);
cerfSource
pub fn cerf(z: Complex<f64>) -> Complex<f64> The error function of a complex argument: the series for |z| <= 1, 1 - e^-z^2 w(iz) beyond (odd in z), the real erf on the real
axis and i erfi(y) on the imaginary one. About 1e-15 relative to
|erf z|; near its complex zeros the error is absolute. SciPy:
erf of a complex argument.
let e = special::cerf(1.0 + 1.0i);
assert((e.re - 1.3161512816979476).abs() < 1e-15 && (e.im - 0.19045346923783471).abs() < 1e-15);
cerfcSource
pub fn cerfc(z: Complex<f64>) -> Complex<f64> The complementary error function of a complex argument, e^-z^2 w(iz) for Re z >= 0 and 2 - e^-z^2 w(-iz) left of it; the real
erfc on the real axis. About 1e-15 relative. SciPy: erfc of a
complex argument.
let e = special::cerfc(1.0 + 1.0i);
assert((e.re + 0.31615128169794764).abs() < 1e-15 && (e.im + 0.19045346923783471).abs() < 1e-15);
cerfcxSource
pub fn cerfcx(z: Complex<f64>) -> Complex<f64> erfcx(z) = e^(z^2) erfc(z) = w(iz) for complex z. SciPy: erfcx
of a complex argument.
let e = special::cerfcx(2.0 + 0.0i);
assert((e.re - special::erfcx(2.0)).abs() < 1e-17);
cerfiSource
pub fn cerfi(z: Complex<f64>) -> Complex<f64> erfi(z) = -i erf(iz) for complex z. SciPy: erfi of a complex
argument.
let e = special::cerfi(1.0 + 0.0i);
assert((e.re - special::erfi(1.0)).abs() < 4e-16 && e.im == 0.0);
dawsonSource
pub fn dawson(x: f64) -> f64 Dawson's integral D(x) = e^-x^2 int_0^x e^t^2 dt = sqrt(pi) / 2 Im w(x). About 3 ulp (6.7e-16 measured), and 1 / (2x) to double
precision past 1e8. SciPy: dawsn.
assert((special::dawson(1.0) - 0.53807950691276842).abs() < 2e-16);
assert(special::dawson(-2.0) == -special::dawson(2.0));
erfcxSource
pub fn erfcx(x: f64) -> f64 The scaled complementary error function erfcx(x) = e^(x^2) erfc(x), which falls like 1 / (x sqrt(pi)) where erfc underflows.
About 2 ulp (3.4e-16 measured; the continued fraction from 2, e^(x^2) erfc(x) with the square in two parts below); overflows to inf
below -26.6. SciPy: erfcx.
assert(special::erfcx(0.0) == 1.0);
assert((special::erfcx(30.0) - 0.018795888861416751).abs() < 4e-18);
assert((special::erfcx(1.0) - 0.42758357615580700).abs() < 2e-16);
erfiSource
pub fn erfi(x: f64) -> f64 The imaginary error function erfi(x) = -i erf(ix) = 2 / sqrt(pi) int_0^x e^t^2 dt, as e^(x^2) Im w(x): about 4 ulp (8.5e-16
measured), inf past 26.6. SciPy: erfi.
assert((special::erfi(1.0) - 1.6504257587975428).abs() < 2e-15);
assert(special::erfi(0.0) == 0.0);
wofzSource
pub fn wofz(z: Complex<f64>) -> Complex<f64> The Faddeeva function w(z) = e^-z^2 erfc(-iz), the plasma
dispersion function up to a factor and the Voigt profile Re w.
In the upper half plane about 7e-16 of |w| and 2e-15 in each part
(measured against mpmath over 1000 points, |z| from 1e-6 to 1e4),
except Im w for |Re z| below 0.01 and Im z between 1 and 2, where it
is 1e-14 of Im w but 1e-17 of |w|. In the lower half plane `2 e^-z^2
- w(-z)
, as well conditioned ase^-z^2(about |z|^2 eps), which overflows to infinity pastIm zof about -26. SciPy:wofz`.
let w = special::wofz(1.0 + 1.0i);
assert((w.re - 0.3047442052569126).abs() < 1e-15 && (w.im - 0.20821893820283163).abs() < 1e-15);
assert(special::wofz(0.0i).re == 1.0);
fresnelSource
pub fn fresnel(x: f64) -> (f64, f64) The Fresnel integrals (S(x), C(x)), normalized as SciPy and
Abramowitz and Stegun 7.3: S(x) = int_0^x sin(pi t^2 / 2) dt,
C(x) = int_0^x cos(pi t^2 / 2) dt. Odd in x, both 1/2 at infinity.
About 3 ulp (6.2e-16 measured) up to the largest finite x. SciPy:
fresnel.
let (s, c) = special::fresnel(1.0);
assert((s - 0.43825914739035476).abs() < 4e-16 && (c - 0.7798934003768229).abs() < 4e-16);
assert(special::fresnel(f64::INFINITY) == (0.5, 0.5));
cospiSource
pub fn cospi(x: f64) -> f64 cos(pi x), exact at the integers and half-integers.
assert(special::cospi(0.5) == 0.0 && special::cospi(1.0) == -1.0);
digammaSource
pub fn digamma(x: f64) -> f64 The digamma function psi(x) = Gamma'(x) / Gamma(x). A few ulp for
x > 0, the positive zero 1.46163... included: the Taylor series
about the zero on [0.91, 2.01], recurrence into it below 10, the
asymptotic series above; reflection for x < 0. NaN at the negative integers, -inf at 0.
use special::digamma;
assert((digamma(1.0) + 0.5772156649015329).abs() < 1e-16);
assert((digamma(0.5) + 1.9635100260214235).abs() < 1e-15);
gammaSource
pub fn gamma(x: f64) -> f64 Gamma(x). A few ulp for x > 0 (exact factorials up to 22!);
the reflection formula for x < 0. Overflows to inf past
171.6; NaN at the negative integers, +-inf at +-0.
use special::gamma;
assert(gamma(5.0) == 24.0);
assert((gamma(0.5) - 1.7724538509055159).abs() < 4e-16);
assert((gamma(-1.5) - 2.3632718012073548).abs() < 1e-15);
gammaincSource
pub fn gammainc(a: f64, x: f64) -> f64 The regularized lower incomplete gamma function P(a, x) = gamma(a, x) / Gamma(a), a > 0, x >= 0 (NaN otherwise); the CDF
of Gamma(a, 1). Relative error about 1e-15 for a below 100,
growing like a * eps past it where x is far from a. SciPy:
gammainc.
use special::gammainc;
assert((gammainc(1.0, 2.0) - (1.0 - (-2.0).exp())).abs() < 1e-16);
assert((gammainc(0.5, 1.0) - 0.8427007929497149).abs() < 1e-15);
gammainc_factorSource
pub fn gammainc_factor(a: f64, x: f64) -> f64 x^a e^-x / Gamma(a), the factor in front of both P and Q, so x
times the density of Gamma(a, 1) at x. For a >= 10 through a log1pmx((x - a) / a) and Stirling's correction, so the large
exponents cancel before the exponential: full accuracy at a = 1e6.
assert((special::gammainc_factor(2.0, 3.0) - 9.0 * (-3.0).exp()).abs() < 1e-16);
gammainc_pqSource
pub fn gammainc_pq(a: f64, x: f64) -> (f64, f64) (P(a, x), Q(a, x)) in one evaluation, each with full relative
accuracy where it is the smaller: the series for x < a + 1,
Legendre's continued fraction above, and for a < 1, x < 1.1 the
series for Gamma(a, x) that keeps a small Q accurate (DiDonato and
Morris 1986). a > 0, x >= 0, else both NaN.
let (p, q) = special::gammainc_pq(3.0, 2.0);
assert((p - special::gammainc(3.0, 2.0)).abs() < 1e-16 && (p + q - 1.0).abs() < 1e-16);
gammainccSource
pub fn gammaincc(a: f64, x: f64) -> f64 The regularized upper incomplete gamma function Q(a, x) = 1 - P(a, x), accurate where it is small (the upper tail). SciPy:
gammaincc.
use special::gammaincc;
assert(((gammaincc(1.0, 30.0) - (-30.0).exp()) / (-30.0).exp()).abs() < 1e-15);
gammainccinvSource
pub fn gammainccinv(a: f64, q: f64) -> f64 The inverse of gammaincc in x: Q(a, x) = q, accurate for tiny
q. SciPy: gammainccinv.
use special::{gammaincc, gammainccinv};
let x = gammainccinv(2.0, 1e-20);
assert(((gammaincc(2.0, x) - 1e-20) / 1e-20).abs() < 1e-13);
gammaincinvSource
pub fn gammaincinv(a: f64, p: f64) -> f64 The inverse of gammainc in x: P(a, x) = p. Halley's method from
a Wilson-Hilferty start; about 1e-15 relative. SciPy:
gammaincinv.
use special::{gammainc, gammaincinv};
let x = gammaincinv(3.0, 0.4);
assert((gammainc(3.0, x) - 0.4).abs() < 1e-15);
lgammaSource
pub fn lgamma(x: f64) -> f64 ln |Gamma(x)|. About 2 ulp for x > 0 (full relative accuracy
at the zeros 1 and 2); for x < 0 the reflection formula, with
absolute error near 1e-16 times the result's size. inf at 0 and
the negative integers. Python: math.lgamma.
use special::lgamma;
assert(lgamma(1.0) == 0.0 && lgamma(2.0) == 0.0);
assert((lgamma(0.5) - 0.5723649429247001).abs() < 1e-16);
assert((lgamma(100.0) - 359.1342053695754).abs() < 1e-12);
lgamma1pSource
pub fn lgamma1p(a: f64) -> f64 ln Gamma(1 + a), accurate as a goes to 0 (the Taylor series
about 2 for |a| <= 1/2), where lgamma(1.0 + a) loses the digits
of a that 1 + a rounds away.
assert((special::lgamma1p(1e-10) + 5.772156648192862e-11).abs() < 1e-26);
log1pmxSource
pub fn log1pmx(u: f64) -> f64 ln(1 + u) - u, accurate for small u (the series below 1/4 in
size, ln_1p(u) - u past it).
assert((special::log1pmx(1e-8) + 4.999999966666667e-17).abs() < 1e-31);
sinpiSource
pub fn sinpi(x: f64) -> f64 sin(pi x), exact at the integers and half-integers (0 and +-1
there, never 1e-16), with the argument reduced before the product
with pi so large x keeps full accuracy.
assert(special::sinpi(1.0) == 0.0 && special::sinpi(0.5) == 1.0);
assert((special::sinpi(1e10 + 0.25) - 0.5f64.sqrt()).abs() < 1e-15);
trigammaSource
pub fn trigamma(x: f64) -> f64 The trigamma function psi'(x), x > 0 (NaN otherwise):
recurrence to x >= 10, then the asymptotic series. About 1e-15
relative.
use special::trigamma;
assert((trigamma(1.0) - 1.6449340668482264).abs() < 1e-15);
lambertwSource
pub fn lambertw(x: f64, branch: i32 = 0) -> f64 The Lambert W function: the w with w e^w = x, on the real branch
branch = 0 (the principal one, x >= -1/e, w >= -1) or branch = -1 (-1/e <= x < 0, w <= -1). The series about the branch point
-1/e (as accurate as the rounding of x allows there), Fritsch's
iteration elsewhere: about 1 ulp (2.6e-16 measured). NaN off the real
range of the branch and for any other branch; W_-1(0) = -inf.
SciPy: lambertw(x, k).real.
let w = special::lambertw(1.0);
assert((w - 0.5671432904097838).abs() < 1e-16);
assert((special::lambertw(-0.1, -1) + 3.577152063957297).abs() < 1e-15);
assert(special::lambertw(-0.36787944117144233) == -1.0);
assoc_legendre_pSource
pub fn assoc_legendre_p(n: u32, m: i32, x: f64) -> f64 The associated Legendre function P_n^m(x) of integer degree n
and order m, with the Condon-Shortley phase: P_n^m(x) = (-1)^m (1 - x^2)^(m/2) d^m P_n(x) / dx^m for m >= 0 and P_n^-m = (-1)^m (n - m)! / (n + m)! P_n^m. 0 for |m| > n, NaN for |x| > 1. The upward
recurrence in n from P_m^m, rescaled so no intermediate
overflows: within about 4 n ulp of mpmath on the tests (the
recurrence's rounding), up to n = 1000. SciPy: assoc_legendre_p
(and lpmv for integer degree).
// P_2^1(x) = -3 x sqrt(1 - x^2).
let x = 0.6;
assert((special::assoc_legendre_p(2, 1, x) + 3.0 * x * 0.8).abs() < 1e-15);
assert(special::assoc_legendre_p(3, 0, 1.0) == 1.0);
assert(special::assoc_legendre_p(2, 3, 0.5) == 0.0);
lpmvSource
pub fn lpmv(m: i32, v: f64, x: f64) -> f64 The associated Legendre function P_v^m(x) (Ferrers' function on
the cut, with the Condon-Shortley phase) of integer order m and
real degree v, -1 <= x <= 1 (NaN outside). An integer v goes to
assoc_legendre_p (with P_-v-1 = P_v); otherwise for x > -0.3 the
series of DLMF 14.3.1 in (1 - x) / 2, else that of 14.3.11 in
x^2 for degrees below 2, the recurrence in the degree from there,
and P_v^-m = (-1)^m Gamma(v - m + 1) / Gamma(v + m + 1) P_v^m.
Within 3e-14 relative of mpmath on the tests away from the zeros;
the series converge slowly as x nears -1, where P_v^m is singular
(NaN at -1). SciPy: lpmv.
assert((special::lpmv(0, 0.5, 0.3) - 0.7009385309696551).abs() < 1e-15);
assert(special::lpmv(1, 2.0, 0.6) == special::assoc_legendre_p(2, 1, 0.6));
sph_harmSource
pub fn sph_harm(n: u32, m: i32, theta: f64, phi: f64) -> Complex<f64> The spherical harmonic Y_n^m(theta, phi) = sph_legendre_p(n, m, theta) e^(i m phi) of degree n and order m, with theta the
polar angle (from the z axis) and phi the azimuth: SciPy's
sph_harm_y and mpmath's spherharm argument order and the
Condon-Shortley phase, orthonormal on the sphere. 0 for |m| > n.
Within about n ulp of mpmath on the tests.
let y = special::sph_harm(1, 1, 0.5, 0.0);
// Y_1^1 = -sqrt(3 / (8 pi)) sin(theta) e^(i phi).
assert((y.re + (3.0 / (8.0 * 3.141592653589793)).sqrt() * 0.5f64.sin()).abs() < 4e-16 && y.im == 0.0);
sph_legendre_pSource
pub fn sph_legendre_p(n: u32, m: i32, theta: f64) -> f64 The normalized associated Legendre function sqrt((2n + 1) / (4 pi) (n - m)! / (n + m)!) P_n^m(cos theta), the theta part of sph_harm
(Condon-Shortley phase; (-1)^m times the |m| value for m < 0; 0
for |m| > n). By the normalized recurrence, carried with a power
of 2^256 so a high degree at any angle neither overflows nor
underflows on the way; within about n ulp of mpmath on the tests,
n^2 ulp within 0.1 of a pole, where the rounding of cos theta
dominates. SciPy: sph_legendre_p.
// Y_0^0 = 1 / sqrt(4 pi).
assert(special::sph_legendre_p(0, 0, 1.3) == 0.28209479177387814);
assert((special::sph_legendre_p(1, 0, 0.0) - 0.4886025119029199).abs() < 4e-16);
expitSource
pub fn expit(x: f64) -> f64 The logistic sigmoid 1 / (1 + e^-x), without overflow for any x
and with full relative accuracy in the lower tail (expit(-700) is
9.86e-305). SciPy: expit.
assert(special::expit(0.0) == 0.5);
assert(special::expit(800.0) == 1.0 && special::expit(-800.0) == 0.0);
log1mexpSource
pub fn log1mexp(a: f64) -> f64 ln(1 - e^-a) for a >= 0 (NaN below, -inf at 0): ln(-expm1(-a))
up to ln 2 and ln_1p(-e^-a) past it, the split of Maechler (2012)
that keeps both ends accurate.
assert((special::log1mexp(1e-20) - 1e-20f64.ln()).abs() < 1e-14);
assert(special::log1mexp(40.0) == -(-40.0).exp());
log1pexpSource
pub fn log1pexp(x: f64) -> f64 ln(1 + e^x) (the softplus), exact to rounding for every x: e^x
below -37, ln_1p(e^x) to 18, x + e^-x to 33.3 and x past it
(Maechler 2012). SciPy: log1p(exp(x)) without its overflow.
assert(special::log1pexp(1000.0) == 1000.0);
assert(special::log1pexp(-50.0) == (-50.0).exp());
assert((special::log1pexp(0.0) - 2f64.ln()).abs() < 1e-16);
log_expitSource
pub fn log_expit(x: f64) -> f64 ln(expit(x)) = -log1pexp(-x), the log of the sigmoid without its
underflow (log_expit(-1000) is -1000). SciPy: log_expit.
assert(special::log_expit(-1000.0) == -1000.0);
log_softmaxSource
pub fn log_softmax<const N: usize>(a: Array<f64, N>, axis: usize = N - 1) throws (SpecialError) -> Array<f64, N> The log of the softmax, x_i - logsumexp(x), along axis (the last
by default), accurate where the softmax underflows. SciPy:
log_softmax.
use core::array::Vector;
let x: Vector<f64> = [0, 1000];
assert((try special::log_softmax(x))[0] == -1000.0);
logaddexpSource
pub fn logaddexp(a: f64, b: f64) -> f64 ln(e^a + e^b) without overflow. NumPy: logaddexp.
assert(special::logaddexp(1000.0, 1000.0) == 1000.0 + 2f64.ln());
logitSource
pub fn logit(p: f64) -> f64 The logit ln(p / (1 - p)), the inverse of expit, for 0 <= p <= 1 (NaN outside, -inf / inf at the ends). Through 2 atanh(2p - 1) near 1/2 and ln p - ln_1p(-p) below 1/4, so both ends keep their
digits. SciPy: logit.
assert(special::logit(0.5) == 0.0);
assert((special::logit(special::expit(-3.0)) + 3.0).abs() < 1e-15);
logsumexpSource
pub fn logsumexp<const N: usize>(a: Array<f64, N>) -> f64 ln sum_i e^(x_i) over every element of a (any rank), shifted by
the largest so nothing overflows: within a few ulp of the exact
value plus n eps from the sum. -inf for an empty array or one of
-infs, NaN if any element is NaN. SciPy: logsumexp.
use core::array::Vector;
let x: Vector<f64> = [1000, 1000, 1000];
assert((special::logsumexp(x) - (1000.0 + 3f64.ln())).abs() < 1e-12);
logsumexp_axisSource
pub fn logsumexp_axis<const N: usize>(a: Array<f64, N>, axis: usize = N - 1) throws (SpecialError) -> Array<f64, { N - 1 }> logsumexp of each lane along axis (the last by default), giving an array of one rank less. Throws InvalidArgument for
an axis past the rank.
use core::array::Matrix;
let m: Matrix<f64> = [[0, 0], [1, 1]];
let r = try special::logsumexp_axis(m, 1);
assert((r[1] - (1.0 + 2f64.ln())).abs() < 1e-15);
softmaxSource
pub fn softmax<const N: usize>(a: Array<f64, N>, axis: usize = N - 1) throws (SpecialError) -> Array<f64, N> The softmax e^(x_i) / sum_j e^(x_j) of each lane along axis
(the last by default), each lane
shifted by its largest value so nothing overflows; each value within
2 ulp, the lanes sum to 1 within a few ulp. Throws
InvalidArgument for an axis past the rank. SciPy: softmax.
use core::array::{Matrix, Vector};
let x: Vector<f64> = [1000, 1000];
assert((try special::softmax(x)).to_list() == [0.5, 0.5]);
let m: Matrix<f64> = [[1, 1], [0, 2]];
let s = try special::softmax(m, 0);
assert((s[0, 0] - special::expit(1.0)).abs() < 1e-16);
erfSource
pub fn erf(x: f64) -> f64 The error function erf(x) = 2 / sqrt(pi) * integral_0^x e^-t^2 dt.
About 2 ulp everywhere. Python: math.erf.
use special::erf;
assert(erf(0.0) == 0.0);
assert((erf(0.5) - 0.5204998778130465).abs() < 1e-16);
assert(erf(-1.0) == -erf(1.0));
erfcSource
pub fn erfc(x: f64) -> f64 The complementary error function erfc(x) = 1 - erf(x), with full
relative accuracy in the upper tail (about 2 ulp; subnormal past 26.6, 0 past 27.3).
Python: math.erfc.
use special::erfc;
assert((erfc(1.0) - 0.15729920705028513).abs() < 3e-17);
assert(((erfc(10.0) - 2.088487583762545e-45) / 2.088487583762545e-45).abs() < 1e-14);
erfcinvSource
pub fn erfcinv(q: f64) -> f64 The inverse complementary error function: erfc(erfcinv(q)) = q for
0 < q < 2, accurate for tiny q.
use special::{erfc, erfcinv};
let x = erfcinv(1e-100);
// erfc changes by 2x = 30 relative per unit of x, so one ulp of x
// is 5e-14 here.
assert(((erfc(x) - 1e-100) / 1e-100).abs() < 1e-13);
erfinvSource
pub fn erfinv(y: f64) -> f64 The inverse error function: erf(erfinv(y)) = y for -1 < y < 1;
+-inf at +-1, NaN outside. From the normal quantile, refined by
one or two Halley steps on erf; about 2 ulp.
use special::{erf, erfinv};
assert(erfinv(0.0) == 0.0);
assert((erf(erfinv(0.3)) - 0.3).abs() < 1e-16);
ndtrSource
pub fn ndtr(x: f64) -> f64 The standard normal CDF Phi(x), with full relative accuracy in
both tails (0 below -38.5). SciPy: special.ndtr.
use special::ndtr;
assert(ndtr(0.0) == 0.5);
assert((ndtr(1.96) - 0.9750021048517795).abs() < 1e-16);
assert(((ndtr(-10.0) - 7.619853024160527e-24) / 7.619853024160527e-24).abs() < 1e-14);
ndtriSource
pub fn ndtri(p: f64) -> f64 The standard normal quantile Phi^-1(p), 0 <= p <= 1 (NaN
otherwise): Wichura's AS 241, about 1e-16 relative. -inf / inf
at 0 / 1. SciPy: special.ndtri.
use special::ndtri;
assert(ndtri(0.5) == 0.0);
assert((ndtri(0.975) - 1.959963984540054).abs() < 1e-15);
ndtri_pqSource
pub fn ndtri_pq(p: f64, q: f64) -> f64 The normal quantile from both p and q = 1 - p, so an upper-tail
probability below 1e-16 keeps its precision (ndtri(1 - 1e-20) is
inf; ndtri_pq(1.0, 1e-20) is 9.26...).
assert((special::ndtri_pq(1.0, 1e-20) - 9.262340089798408).abs() < 1e-14);
eval_chebytSource
pub fn eval_chebyt(n: u32, x: f64) -> f64 The Chebyshev polynomial of the first kind T_n(x), any real x:
the recurrence T_(k+1) = 2x T_k - T_(k-1) up to degree 20, cos(n acos x) on [-1, 1] and +-cosh(n acosh |x|) outside past it.
Within about 4 ulp of mpmath at small degree, n ulp in absolute
error at large. SciPy: eval_chebyt.
assert(special::eval_chebyt(3, 0.5) == -1.0);
assert((special::eval_chebyt(100, 0.3) - (100.0 * 0.3f64.acos()).cos()).abs() < 1e-15);
eval_chebyuSource
pub fn eval_chebyu(n: u32, x: f64) -> f64 The Chebyshev polynomial of the second kind U_n(x), any real x:
the recurrence up to degree 20, sin((n + 1) t) / sin t with x = cos t on (-1, 1) past it (and (n + 1) (+-1)^n at the ends), the
hyperbolic form outside. SciPy: eval_chebyu.
// U_2(x) = 4 x^2 - 1.
assert(special::eval_chebyu(2, 0.5) == 0.0);
assert(special::eval_chebyu(50, 1.0) == 51.0);
eval_gegenbauerSource
pub fn eval_gegenbauer(n: u32, alpha: f64, x: f64) -> f64 The Gegenbauer (ultraspherical) polynomial C_n^(alpha)(x): `(k +
- C_(k+1) = 2 (k + alpha) x C_k - (k + 2 alpha - 1) C_(k-1)
,C_1 = 2 alpha x; identically 0 forn >= 1atalpha = 0, as the standard normalization gives (useeval_chebytfor its limit). Any realalphaandx. SciPy:eval_gegenbauer`.
// C_n^(1/2) = P_n, C_n^(1) = U_n.
assert((special::eval_gegenbauer(4, 0.5, 0.3) - special::eval_legendre(4, 0.3)).abs() < 4e-16);
assert((special::eval_gegenbauer(5, 1.0, 0.3) - special::eval_chebyu(5, 0.3)).abs() < 4e-16);
eval_genlaguerreSource
pub fn eval_genlaguerre(n: u32, alpha: f64, x: f64) -> f64 The generalized Laguerre polynomial L_n^(alpha)(x): (k + 1) L_(k+1) = (2k + 1 + alpha - x) L_k - (k + alpha) L_(k-1). Any real
alpha and x; within about n ulp of mpmath on the tests away
from the zeros. SciPy: eval_genlaguerre.
// L_1^(a)(x) = 1 + a - x.
assert(special::eval_genlaguerre(1, 0.5, 2.0) == -0.5);
eval_hermiteSource
pub fn eval_hermite(n: u32, x: f64) -> f64 The physicists' Hermite polynomial H_n(x) (weight e^-x^2):
H_(k+1) = 2x H_k - 2k H_(k-1). Within about n ulp of mpmath on
the tests. SciPy: eval_hermite.
// H_3(x) = 8 x^3 - 12 x.
assert(special::eval_hermite(3, 2.0) == 40.0);
eval_hermitenormSource
pub fn eval_hermitenorm(n: u32, x: f64) -> f64 The probabilists' Hermite polynomial He_n(x) (weight e^(-x^2 / 2)): He_(k+1) = x He_k - k He_(k-1). SciPy: eval_hermitenorm.
// He_3(x) = x^3 - 3 x.
assert(special::eval_hermitenorm(3, 2.0) == 2.0);
eval_jacobiSource
pub fn eval_jacobi(n: u32, alpha: f64, beta: f64, x: f64) -> f64 The Jacobi polynomial P_n^(alpha, beta)(x) by the recurrence of
DLMF 18.9.2, any real parameters and x (NaN where the recurrence
divides by zero, alpha + beta a negative integer at least -2n).
Within about n ulp of mpmath on the tests. SciPy: eval_jacobi.
// P_1^(a, b)(x) = (a + 1) + (a + b + 2) (x - 1) / 2.
assert(special::eval_jacobi(1, 1.0, 2.0, 0.5) == 0.75);
assert((special::eval_jacobi(6, 0.0, 0.0, 0.4) - special::eval_legendre(6, 0.4)).abs() < 4e-16);
eval_laguerreSource
pub fn eval_laguerre(n: u32, x: f64) -> f64 The Laguerre polynomial L_n(x) = L_n^(0)(x). SciPy:
eval_laguerre.
// L_2(x) = (x^2 - 4x + 2) / 2.
assert(special::eval_laguerre(2, 1.0) == -0.5);
eval_legendreSource
pub fn eval_legendre(n: u32, x: f64) -> f64 The Legendre polynomial P_n(x), any real x. (k + 1) P_(k+1) = (2k + 1) x P_k - k P_(k-1); within about n ulp of mpmath on the
tests on [-1, 1]. SciPy: eval_legendre.
// P_2(x) = (3 x^2 - 1) / 2.
assert(special::eval_legendre(2, 0.5) == -0.125);
assert(special::eval_legendre(7, 1.0) == 1.0);
roots_chebytSource
pub fn roots_chebyt(n: usize) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The Gauss-Chebyshev rule of the first kind (weight 1 / sqrt(1 - x^2) on (-1, 1)): nodes cos((2k - 1) pi / (2n)), weights pi / n,
closed form. Throws for n = 0. SciPy: roots_chebyt.
let (x, w) = try special::roots_chebyt(2);
assert((x[1] - 0.5f64.sqrt()).abs() < 4e-16 && w[0] == 3.141592653589793 / 2.0);
roots_chebyuSource
pub fn roots_chebyu(n: usize) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The Gauss-Chebyshev rule of the second kind (weight sqrt(1 - x^2)):
nodes cos(k pi / (n + 1)), weights pi / (n + 1) sin^2(k pi / (n + 1)), closed form. Throws for n = 0. SciPy: roots_chebyu.
let (x, w) = try special::roots_chebyu(1);
assert(x[0] == 0.0 && (w[0] - 3.141592653589793 / 2.0).abs() < 4e-16);
roots_gegenbauerSource
pub fn roots_gegenbauer(n: usize, alpha: f64) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The Gauss-Gegenbauer rule for the weight (1 - x^2)^(alpha - 1/2) on
(-1, 1), alpha > -1/2 (the Jacobi rule with both parameters `alpha
- 1/2
;alpha = 0is the Chebyshev rule of the first kind). ThrowsInvalidArgumentforn = 0oralpha <= -1/2. SciPy:roots_gegenbauer`.
let (x, w) = try special::roots_gegenbauer(3, 0.5);
let (y, v) = try special::roots_legendre(3);
assert((x[0] - y[0]).abs() < 4e-16 && (w[0] - v[0]).abs() < 4e-16);
roots_genlaguerreSource
pub fn roots_genlaguerre(n: usize, alpha: f64) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The generalized Gauss-Laguerre rule for the weight x^alpha e^-x on
(0, inf), alpha > -1 (the weights sum to Gamma(alpha + 1)).
Golub-Welsch, then Newton and the Christoffel weights; nodes within
4 ulp of mpmath's on the tests (10 ulp for the smallest node on a
target without fused multiply-add, as wasm32: the recurrence cancels
there), weights within about 1e-14 relative (the last ones underflow
for large n). Throws InvalidArgument for n = 0 or alpha <= -1.
SciPy: roots_genlaguerre.
let (x, w) = try special::roots_genlaguerre(1, 2.0);
// One node at the mean alpha + 1, weight Gamma(3) = 2.
assert(x[0] == 3.0 && (w[0] - 2.0).abs() < 1e-15);
roots_hermiteSource
pub fn roots_hermite(n: usize) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The Gauss-Hermite rule for the weight e^-x^2 on the real line
(physicists' Hermite polynomials; the weights sum to sqrt(pi)).
Golub-Welsch, then Newton and the Christoffel weights; nodes within
4 ulp of mpmath's on the tests, weights within about 1e-14 relative
(those past about n = 1500 underflow to 0, as their values do).
Throws for n = 0. SciPy: roots_hermite.
let (x, w) = try special::roots_hermite(2);
assert((x[1] - 0.5f64.sqrt()).abs() < 4e-16);
assert((w[0] - 3.141592653589793f64.sqrt() / 2.0).abs() < 4e-16);
roots_hermitenormSource
pub fn roots_hermitenorm(n: usize) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The Gauss-Hermite rule for the weight e^(-x^2 / 2) (probabilists'
Hermite polynomials; the weights sum to sqrt(2 pi)). Accuracy as
roots_hermite. Throws for n = 0. SciPy: roots_hermitenorm.
let (x, w) = try special::roots_hermitenorm(2);
assert(x[1] == 1.0 && (w[0] - (2.0 * 3.141592653589793).sqrt() / 2.0).abs() < 1e-15);
roots_jacobiSource
pub fn roots_jacobi(n: usize, alpha: f64, beta: f64) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The Gauss-Jacobi rule for the weight (1 - x)^alpha (1 + x)^beta on
(-1, 1), alpha, beta > -1 (the weights sum to 2^(alpha + beta + 1) B(alpha + 1, beta + 1)). Golub-Welsch, then Newton and the
Christoffel weights; nodes within 4 ulp, weights within 1e-14
relative of mpmath on the tests.
Throws InvalidArgument for n = 0 or a parameter <= -1. SciPy:
roots_jacobi.
let (x, w) = try special::roots_jacobi(1, 1.0, 0.0);
// One node at the weight's mean -1/3, weight 2.
assert((x[0] + 1.0 / 3.0).abs() < 4e-16 && (w[0] - 2.0).abs() < 1e-15);
roots_laguerreSource
pub fn roots_laguerre(n: usize) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The Gauss-Laguerre rule for the weight e^-x on (0, inf): the
weights sum to 1. Throws for n = 0. SciPy: roots_laguerre.
let (x, w) = try special::roots_laguerre(2);
assert((x[0] - (2.0 - 2f64.sqrt())).abs() < 4e-16);
assert((w[0] + w[1] - 1.0).abs() < 4e-16);
roots_legendreSource
pub fn roots_legendre(n: usize) throws (SpecialError) -> (Vector<f64>, Vector<f64>) The Gauss-Legendre rule on [-1, 1] (weight 1): (nodes, weights),
nodes rising. No eigenproblem: starts from Tricomi's asymptotic
nodes, x_k = (1 - 1/(8 n^2) + 1/(8 n^3)) cos((4k - 1) pi / (4n + 2)), then Newton and the Christoffel weights: O(n^2). Nodes within 4
ulp, weights within 1e-14 relative (about 30 ulp at the ends of a
large rule), exactly symmetric.
Throws InvalidArgument for n = 0. SciPy: roots_legendre.
let (x, w) = try special::roots_legendre(3);
assert((x[2] - 0.6f64.sqrt()).abs() < 4e-16 && x[1] == 0.0);
assert((w[0] - 5.0 / 9.0).abs() < 4e-16 && (w[1] - 8.0 / 9.0).abs() < 4e-16);
polygammaSource
pub fn polygamma(n: u32, x: f64) -> f64 The polygamma function psi^(n)(x), the n-th derivative of the
digamma function: digamma for n = 0, trigamma for n = 1 and
x > 0, and (-1)^(n+1) n! zeta(n + 1, x) otherwise, the Hurwitz
sum running over the shifted terms for x < 0. About 5 ulp for x > 0 (1.1e-15 measured; logs added where n! or the sum would
overflow alone), absolute accuracy for x < 0 near the zeros. At the poles 0, -1, -2, ...:
+inf for odd n (both sides go up), NaN for even n. SciPy:
polygamma.
// psi''(1) = -2 zeta(3).
assert((special::polygamma(2, 1.0) + 2.4041138063191885).abs() < 1e-15);
assert(special::polygamma(1, 1.0) == special::trigamma(1.0));
assert(special::polygamma(1, -2.0) == f64::INFINITY);
struve_hSource
pub fn struve_h(v: f64, x: f64) -> f64 The Struve function H_v(x) for real v > -3/2 (NaN otherwise) and
x >= 0, or any x for integer v. About 1e-15 relative off its
zeros (the series in double-double below x = 40; the large-x
expansion on top of yv past it). SciPy: struve.
assert((special::struve_h(0.0, 1.0) - 0.5686566270482879).abs() < 3e-16);
// H_1/2(x) = sqrt(2 / (pi x)) (1 - cos x).
let x = 30.0;
assert((special::struve_h(0.5, x) - (2.0 / (3.141592653589793 * x)).sqrt() * (1.0 - x.cos())).abs() < 1e-15);
struve_lSource
pub fn struve_l(v: f64, x: f64) -> f64 The modified Struve function L_v(x) for real v > -3/2 and x >= 0, or any x for integer v; inf once it overflows (past about
713). The positive series in double-double below x = 30, I_-v
less the large-x expansion past it; about 1e-15 relative. SciPy:
modstruve.
assert((special::struve_l(0.0, 1.0) - 0.7102431859378909).abs() < 2e-16);
czetaSource
pub fn czeta(s: Complex<f64>) -> Complex<f64> The Riemann zeta function of a complex argument: Euler-Maclaurin
with n >= 10 + |Im s| terms (and 0.6 (|s| + 30)) for Re s >= 1/2 and the functional
equation below. About 1e-15 relative for Re s >= 2; in and left of
the critical strip the terms cancel, to about 1e-14 of |zeta(s)|
for |Im s| up to 40 (3e-14 measured), more past it. The cost grows
like |Im s|. inf at s = 1. SciPy has no complex zeta; mpmath: zeta.
let z = special::czeta(0.5 + 14.134725141734693i);
assert(z.abs() < 1e-14);
let w = special::czeta(2.0 + 0.0i);
assert((w.re - 1.6449340668482264).abs() < 3e-16);
hurwitzSource
pub fn hurwitz(s: f64, q: f64) -> f64 The Hurwitz zeta function zeta(s, q) = sum_{k >= 0} (q + k)^-s for
s > 1 (inf at s = 1, NaN below, as SciPy). q > 0 keeps every
term positive: about 3 ulp (6.2e-16 measured, Euler-Maclaurin). For q <= 0 the sum
runs over the shifted terms when s is an integer (inf when q
is a pole, the terms of mixed sign then cost absolute accuracy) and
is NaN otherwise. SciPy: zeta(s, q).
assert((special::hurwitz(2.0, 1.0) - special::zeta(2.0)).abs() < 3e-16);
// zeta(s, 1/2) = (2^s - 1) zeta(s).
assert((special::hurwitz(3.0, 0.5) - 7.0 * special::zeta(3.0)).abs() < 2e-15);
zetaSource
pub fn zeta(s: f64) -> f64 The Riemann zeta function zeta(s) = sum_{k >= 1} k^-s, continued to
every real s: inf at the pole s = 1, exactly 0 at the negative
even integers, -1/2 at 0. About 2 ulp for s > -1/2 (the
Stieltjes series on (-1/2, 3), Euler-Maclaurin past it); below, the
functional equation, whose powers of 2 pi cost about |s| eps / 20
(MEAS); overflows to +-inf below about -260. SciPy: zeta(s).
assert((special::zeta(2.0) - 1.6449340668482264).abs() < 3e-16);
assert(special::zeta(0.0) == -0.5 && special::zeta(-2.0) == 0.0);
assert((special::zeta(-1.0) + 1.0 / 12.0).abs() < 1e-17);
assert(special::zeta(1.0) == f64::INFINITY);
zetacSource
pub fn zetac(s: f64) -> f64 zeta(s) - 1, with full relative accuracy as it falls to 0 for
large s (zetac(100) is 7.9e-31). SciPy: zetac.
assert(((special::zetac(60.0) - 8.673617380119934e-19) / 8.673617380119934e-19).abs() < 1e-15);
assert(special::zetac(0.0) == -1.5);