Overview
Special functions, double precision. SciPy's names where SciPy has the function; NaN outside the domain, like SciPy.
| Family | Functions |
|---|---|
| Gamma | gamma, gammaSign, lgamma, digamma, trigamma, polygamma, beta, lbeta, stirlingCorrection |
| Incomplete | gammaincP/gammaincQ (+ gammaincPInv/gammaincQInv), betainc/betaincc (+ betaincinv/betainccinv) |
| Error function | erf, erfc, erfcx, erfinv, erfcinv, ndtr, ndtri |
| Bessel (real order) | besselJ, besselY, besselI, besselK, scaled besselIe/besselKe, sphericalJ/sphericalY |
| Airy | airy(x) → Ai, Ai′, Bi, Bi′ |
| Elliptic | Carlson carlsonRF/RD/RJ/RC; ellipk, ellipkm1, ellipe, ellipkinc, ellipeinc, ellippi, ellippiinc, Jacobi ellipj (parameter m = k^{2}) |
| Legendre / SH | legendreP, assocLegendreP (Condon–Shortley phase, = SciPy lpmv), complex sphericalHarmonic (= SciPy sph_harm_y), real realSphericalHarmonic / realShBasis (riptide_math Sh9 convention), ShRotation |
| Zeta | zeta (all real s), hurwitzZeta |
| Hypergeometric | hyp2f1 (real z < 1), hyp1f1 |
| Helpers | log1p, expm1, log1pmx, sinpi, cospi (raya:math has none of them) |
Spherical harmonics and rendering
realShBasis(lmax, x, y, z, out) writes every real SH up to band lmax for a unit direction (index l^{2} + l + m) without allocating; bands 0–2 equal riptide_math's Sh9.basis. ShRotation.fromMatrix(lmax, r) / ShRotation.fromQuat(lmax, q) builds the block-diagonal rotation by the Ivanic–Ruedenberg recurrence: if f has coefficients c then f(R^{T}\omega) has apply(c). applyToShRgb(sh) rotates a probe's 27 coefficients in place (an environment rotated with the sky, a probe moved with its object). Tested exactly against evaluating the rotated function, to band 8, and each block is orthogonal to 1e-12.
Accuracy
Unit: \mathrm{eps} = 2^{-52} relative error. Oscillatory functions (J, Y, Airy for x < 0, Legendre, spherical harmonics, sn/cn, {}_{2}F_{1}, {}_{1}F_{1}) are measured relative to the local amplitude, the usual practice near zeros. Functions that are exponentially small or large (marked ‡) are measured per unit of \max(1, |\ln f|): the exponent itself is rounded, and the function's condition number in its parameters is that large too (for example, \operatorname{Ai}(28) moves by ~150 ulps when x moves by one ulp). The bounds the test enforces are 1.5× the measured maxima.
| Function | Points | Max error (eps) | Notes | ||
|---|---|---|---|---|---|
| gamma | 974 | 2.7 | x in (-20, 171.6) | ||
| lgamma | 1100 | 1.5 | x in (1e-8, 1e5) and negative; absolute (\times 1) next to its negative-axis roots | ||
| digamma | 800 | 5.2 | absolute for x < 0 (cancellation by reflection next to its negative roots) | ||
| trigamma / polygamma(3) | 400 / 200 | 2.5 / 1.8 | |||
| beta / lbeta | 500 | 2.5 / 2.5 | ‡ beta; a, b in (1e-3, 1e4) | ||
| gammaincP / gammaincQ | 1350 | 5.0 / 9.2 | ‡; a in (0.01, 3000), \frac{x}{a} in (0.01, 20) | ||
| gammaincPInv | 342 | 8.2 | |||
| betainc / betaincc | 840 | 12.9 / 16.2 | ‡; a, b in (0.1, 300) | ||
| betaincinv | 299 | 7.8 | |||
| erf / erfc / erfcx | 650 / 600 / 450 | 1.0 / 2.0 / 1.5 | erfc to 27 (5e-319) | ||
| erfinv / erfcinv | 450 / 400 | 1.0 / 1.3 | erfcinv to 1e-300 | ||
| J_{v} / Y_{v} | 480 | 12.0 / 23.7 | v in {0, 0.3, 1, 2.5, 5, 10, 20, 50}, x in (1e-3, 1e3); amplitude-relative | ||
| J_{v}, v < 0 | 90 | 13.7 | |||
| I_{v} / K_{v} / I_{v} e^{-x} / K_{v} e^{x} | 462–480 | 2.1 / 3.4 / 3.2 / 6.1 | ‡ | ||
| Ai / Bi / Ai′ / Bi′ | 400 | 13.9 / 12.0 / 27.7 / 27.5 | ‡ for x > 0; amplitude-relative for x < 0 (phase \zeta = \tfrac{2}{3} | x | ^\{\frac{3}{2}\} is rounded) |
| ellipk / ellipe / ellipkm1 | 350 / 350 / 100 | 2.1 / 2.1 / 2.6 | m from -5 to 1 - 1e-15 | ||
| ellipkinc / ellipeinc | 400 | 3.4 / 2.7 | |||
| ellippi / ellippiinc | 200 / 300 | 3.8 / 4.9 | n < 1 | ||
| ellipj sn / cn / dn | 300 | 6.6 / 8.8 / 11.9 | u | ≤ 10 | |
| legendreP | 280 | 55.4 | n up to 100: the recurrence's error grows like n | ||
| assocLegendreP | 288 | 10.4 | l \le 20 | ||
| sphericalHarmonic re / im | 180 | 21.9 / 38.3 | l \le 15 | ||
| zeta / hurwitzZeta | 370 / 300 | 7.5 / 3.2 | s in (-40, 60) | ||
| hyp2f1 | 400 | 88.8 | a, b in (-3, 3), c in (0.2, 5), z in (-5, 0.95); the connection formulas cancel | ||
| hyp1f1 | 300 | 16.6 | x in (-30, 30) |
Identities tested besides: Bessel and Airy Wronskians, the Legendre relation for K and E, \operatorname{sn}^{2} + \operatorname{cn}^{2} = 1, \zeta special values, closed forms of {}_{2}F_{1} and {}_{1}F_{1}, inverse round trips, SH orthonormality and rotation.
Methods and sources
Written from the published algorithms; no code was copied.
- Gamma: \frac{1}{\Gamma(1 + x)} Taylor series near the origin, exact downward/upward shifts (each x \pm k is exact), Stirling with the x^{x/2 - 1/4} squaring trick above 13 (as Cephes
gamma.cdoes), reflection below -20. \ln\Gamma near 1 and 2 by their \zeta-series. \beta with a Fast2Sum correction for the rounding of a + b, and log1p forms of Stirling for large or unequal arguments. - Incomplete gamma/beta: series and continued fractions (DLMF 8.11, 8.17.22) with Temme-style log1pmx prefixes (DiDonato and Morris; Boost.Math's
regularised_gamma_prefixidea), a direct small-a form of Q, both tails of I without cancellation; inverses by safeguarded Halley iteration from Wilson–Hilferty / A&S 26.5.22 starts. - erf: Taylor series below \frac{1}{2}, Chebyshev series of erfcx, \exp(-x^{2}) from a Veltkamp split; inverses by Halley from Winitzki's approximation.
- Bessel: Temme's series and Steed's continued fractions with CF1 and order recurrences (Temme \frac{1975}{1976}; Barnett et al. 1974; the scheme of Boost.Math
bessel_jy/bessel_ik), Hankel's expansion with a cancellation guard, and Hankel at orders \mu, \mu + 1 plus forward recurrence below the turning point; positive power series where they win. - Elliptic: Carlson's duplication algorithms (Carlson 1995) and E = 2R_{G} arranged without cancellation as m \to 1; Jacobi functions by the AGM (A&S 16.4, the scheme of Cephes
ellpj). - Zeta: Euler–Maclaurin (the method of Cephes
zeta.c), Borwein's \eta algorithm on [0, 1), the functional equation. - Spherical harmonics: orthonormalised associated-Legendre recurrences; Ivanic and Ruedenberg, J. Phys. Chem. 100 (1996) 6342 and 102 (1998) 9099.
Licences of the works consulted: Boost.Math (BSL-1.0), Cephes (Moshier, free use), SciPy and mpmath (BSD-3).
Functions and constants
Functions
({double ai, double aip, double bi, double bip}) airy(double x)Airy functions Ai, Ai', Bi, Bi' at real x (DLMF 9.6: Bessel functions of order \frac{1}{3} and \frac{2}{3} for |x| \ge 1, the Maclaurin series inside).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
final r = airy(1.0);
print('Ai(1) = ${r.ai.toStringAsFixed(12)}');
print('Ai\'(1) = ${r.aip.toStringAsFixed(12)}');
print('Bi(1) = ${r.bi.toStringAsFixed(12)}');
print('Bi\'(1) = ${r.bip.toStringAsFixed(12)}');
}Ai(1) = 0.135292416313 Ai'(1) = -0.159147441297 Bi(1) = 1.207423594953 Bi'(1) = 0.932435933393
double besselJ(double v, double x)Bessel function of the first kind J_{v}(x) for real v and x (x < 0 only for integer v).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('J0(1) = ${besselJ(0, 1)}');
print('J1(2.5) = ${besselJ(1, 2.5)}');
print('Y0(1) = ${besselY(0, 1)}');
for (final n in [0, 1, 2]) {'label': 'J$n', 'points': [for (var i = 0; i <= 100; i++) [i / 5, besselJ(n.toDouble(), i / 5)]]}
]})}');
}J0(1) = 0.7651976865579664 J1(2.5) = 0.49709410246427405 Y0(1) = 0.08825696421567694
double besselY(double v, double x)Bessel function of the second kind Y_{v}(x), x > 0.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('Y0(1) = ${besselY(0, 1)}');
print('Y1(2.5) = ${besselY(1, 2.5)}');
print('Y0.5(1) = ${besselY(0.5, 1)} (half-integer order)');
print('Y0 near 0 dives to −∞: Y0(0.01) = ${besselY(0, 0.01)}');
final xs = [for (var i = 0; i <= 120; i++) 0.5 + i * 19.5 / 120];
{'label': 'Y0', 'points': [for (final x in xs) [x, besselY(0, x)]]},
{'label': 'Y1', 'points': [for (final x in xs) [x, besselY(1, x)]]},
]})}');
}Y0(1) = 0.08825696421567694 Y1(2.5) = 0.14591813796678596 Y0.5(1) = -0.43109886801837594 (half-integer order) Y0 near 0 dives to −∞: Y0(0.01) = -3.005455637083646
double besselI(double v, double x)Modified Bessel function of the first kind I_{v}(x) (x < 0 only for integer v).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('I0(1) = ${besselI(0, 1)}');
print('K0(1) = ${besselK(0, 1)}');
print('I0(50) scaled by e^-50: ${besselIe(0, 50)}');
}I0(1) = 1.2660658777520082 K0(1) = 0.42102443824070834 I0(50) scaled by e^-50: 0.05656162664745418
double besselIe(double v, double x)Exponentially scaled I_{v}(x) e^{-|x|}.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
// I_v grows like e^x / sqrt(2πx); the scaled form stays finite.
print('I0(50) = ${besselI(0, 50)}');
print('I0e(50) = ${besselIe(0, 50)}');
print('I0e(800) = ${besselIe(0, 800)} (I0(800) itself overflows)');
final xs = [for (var i = 0; i <= 100; i++) i * 20 / 100];
{'label': 'I0e', 'points': [for (final x in xs) [x, besselIe(0, x)]]},
{'label': 'I1e', 'points': [for (final x in xs) [x, besselIe(1, x)]]},
]})}');
}I0(50) = 293255378384933520000.0 I0e(50) = 0.05656162664745418 I0e(800) = 0.014106945005869172 (I0(800) itself overflows)
double besselK(double v, double x)Modified Bessel function of the second kind K_{v}(x), x > 0.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('K0(1) = ${besselK(0, 1)}');
print('K1(1) = ${besselK(1, 1)}');
print('K0.5(2) = ${besselK(0.5, 2)} (= √(π/4)·e^−2 = ${0.886226925452758 * 0.1353352832366127})');
final xs = [for (var i = 0; i <= 80; i++) 0.2 + i * 3.8 / 80];
{'label': 'K0', 'points': [for (final x in xs) [x, besselK(0, x)]]},
{'label': 'K1', 'points': [for (final x in xs) [x, besselK(1, x)]]},
{'label': 'K2', 'points': [for (final x in xs) [x, besselK(2, x)]]},
]})}');
}K0(1) = 0.42102443824070834 K1(1) = 0.6019072301972346 K0.5(2) = 0.11993777196806145 (= √(π/4)·e^−2 = 0.11993777196806146)
double besselKe(double v, double x)Exponentially scaled K_{v}(x) e^{x}.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('K0(30) = ${besselK(0, 30)}');
print('K0e(30) = ${besselKe(0, 30)}');
print('K0e(1000) = ${besselKe(0, 1000)} (K0(1000) underflows to ${besselK(0, 1000)})');
final xs = [for (var i = 0; i <= 100; i++) 0.5 + i * 19.5 / 100];
{'label': 'K0e', 'points': [for (final x in xs) [x, besselKe(0, x)]]},
{'label': 'K1e', 'points': [for (final x in xs) [x, besselKe(1, x)]]},
]})}');
}K0(30) = 2.1324774964630566e-14 K0e(30) = 0.22788666561625373 K0e(1000) = 0.03962832160075422 (K0(1000) underflows to 0.0)
double sphericalJ(int n, double x)Spherical Bessel j_{n}(x) = \sqrt{\frac{\pi}{2x}} J_{n + 1/2}(x).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('j0(1) = ${sphericalJ(0, 1)} (sin 1 / 1)');
print('j1(2) = ${sphericalJ(1, 2)}');
print('j0(0) = ${sphericalJ(0, 0)}, j3(0) = ${sphericalJ(3, 0)}');
final xs = [for (var i = 0; i <= 120; i++) i * 15 / 120];
{'label': 'j0', 'points': [for (final x in xs) [x, sphericalJ(0, x)]]},
{'label': 'j1', 'points': [for (final x in xs) [x, sphericalJ(1, x)]]},
{'label': 'j2', 'points': [for (final x in xs) [x, sphericalJ(2, x)]]},
]})}');
}j0(1) = 0.8414709848078966 (sin 1 / 1) j1(2) = 0.43539777497999155 j0(0) = 1.0, j3(0) = 0.0
double sphericalY(int n, double x)Spherical Bessel y_{n}(x) = \sqrt{\frac{\pi}{2x}} Y_{n + 1/2}(x).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('y0(1) = ${sphericalY(0, 1)} (−cos 1 / 1)');
print('y1(2) = ${sphericalY(1, 2)}');
final xs = [for (var i = 0; i <= 100; i++) 1 + i * 14 / 100];
{'label': 'y0', 'points': [for (final x in xs) [x, sphericalY(0, x)]]},
{'label': 'y1', 'points': [for (final x in xs) [x, sphericalY(1, x)]]},
]})}');
}y0(1) = -0.5403023058681394 (−cos 1 / 1) y1(2) = -0.35061200427605527
double carlsonRC(double x, double y)Carlson's degenerate integral R_{C}(x, y) (Cauchy principal value for y < 0).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
// R_C(x, y) = atan(√((x − y)/y)) / √(x − y) for x > y; R_C(0, 1/4) = π.
print('R_C(0, 0.25) = ${carlsonRC(0, 0.25)} (π = ${math.pi})');
print('R_C(2, 1) = ${carlsonRC(2, 1)} (ln(1+√2) = ${math.log(1 + math.sqrt2)})');
print('R_C(0.25, −2) = ${carlsonRC(0.25, -2)} (principal value)');
}R_C(0, 0.25) = 3.141592653589793 (π = 3.141592653589793) R_C(2, 1) = 0.8813735870195432 (ln(1+√2) = 0.8813735870195429) R_C(0.25, −2) = 0.2310490601866484 (principal value)
double carlsonRF(double x, double y, double z)Carlson's symmetric integral of the first kind R_{F}(x, y, z).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
// K(m) = R_F(0, 1 − m, 1).
final m = 0.5;
print('R_F(0, 1 − m, 1) = ${carlsonRF(0, 1 - m, 1)}');
print('ellipk(m) = ${ellipk(m)}');
print('R_F(1, 2, 0) = ${carlsonRF(1, 2, 0)} (symmetric: R_F(0, 2, 1) = ${carlsonRF(0, 2, 1)})');
}R_F(0, 1 − m, 1) = 1.854074677301372 ellipk(m) = 1.854074677301372 R_F(1, 2, 0) = 1.31102877714606 (symmetric: R_F(0, 2, 1) = 1.31102877714606)
double carlsonRD(double x, double y, double z)Carlson's symmetric integral of the second kind R_{D}(x, y, z) = R_{J}(x, y, z, z).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
// E(m) = R_F(0, 1−m, 1) − m/3 · R_D(0, 1−m, 1).
final m = 0.5;
final e = carlsonRF(0, 1 - m, 1) - m / 3 * carlsonRD(0, 1 - m, 1);
print('R_D(0, 0.5, 1) = ${carlsonRD(0, 0.5, 1)}');
print('E(0.5) from R_F, R_D = $e');
print('ellipe(0.5) = ${ellipe(m)}');
print('R_D(x, y, z) = R_J(x, y, z, z): ${carlsonRJ(0, 0.5, 1, 1)}');
}R_D(0, 0.5, 1) = 3.020584777522178 E(0.5) from R_F, R_D = 1.3506438810476757 ellipe(0.5) = 1.3506438810476755 R_D(x, y, z) = R_J(x, y, z, z): 3.020584777522178
double carlsonRJ(double x, double y, double z, double p)Carlson's symmetric integral of the third kind R_{J}(x, y, z, p), p > 0.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
// Π(n | m) = R_F(0, 1−m, 1) + n/3 · R_J(0, 1−m, 1, 1−n).
const n = 0.3, m = 0.5;
final pi = carlsonRF(0, 1 - m, 1) + n / 3 * carlsonRJ(0, 1 - m, 1, 1 - n);
print('R_J(0, 0.5, 1, 0.7) = ${carlsonRJ(0, 1 - m, 1, 1 - n)}');
print('Π(0.3 | 0.5) from R_F, R_J = $pi');
print('ellippi(0.3, 0.5) = ${ellippi(n, m)}');
}R_J(0, 0.5, 1, 0.7) = 3.963021446425748 Π(0.3 | 0.5) from R_F, R_J = 2.250376821943947 ellippi(0.3, 0.5) = 2.250376821943947
double ellipk(double m)Complete elliptic integral of the first kind K(m), m < 1 (SciPy ellipk).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math';
void main() {
// The period of a pendulum released at 60°: T = 4 K(m) √(L/g), m = sin²(θ/2).
final m = pow(sin(pi / 6), 2).toDouble();
print('K(m) = ${ellipk(m)}, E(m) = ${ellipe(m)}');
print('period / small-angle period = ${(2 / pi * ellipk(m)).toStringAsFixed(6)}');
}K(m) = 1.6857503548125963, E(m) = 1.4674622093394274 period / small-angle period = 1.073182
double ellipkm1(double p)K(1 - p), accurate for p near 0 (SciPy ellipkm1).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
// Near m = 1, K(m) has a log singularity; ellipkm1(p) = K(1 − p) keeps the digits that 1 − p loses.
for (final p in [1e-3, 1e-8, 1e-15, 1e-30]) {
print('ellipkm1($p) = ${ellipkm1(p)}');
}
print('ellipk(1 − 1e-30) = ${ellipk(1 - 1e-30)} (1 − 1e-30 rounds to 1)');
}ellipkm1(0.001) = 4.841132560550298 ellipkm1(1e-8) = 10.59663475708766 ellipkm1(1e-15) = 18.65568255857524 ellipkm1(1e-30) = 35.92507075603058 ellipk(1 − 1e-30) = Infinity (1 − 1e-30 rounds to 1)
double ellipe(double m)Complete elliptic integral of the second kind E(m), m \le 1 (SciPy ellipe).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('E(0) = ${ellipe(0)} (π/2)');
print('E(1) = ${ellipe(1)}');
// Perimeter of an ellipse with semi-axes a ≥ b: 4a·E(1 − b²/a²).
const a = 2.0, b = 1.0;
print('perimeter of a 2×1 ellipse = ${4 * a * ellipe(1 - b * b / (a * a))}');
print('circle check: ${4 * ellipe(0)} vs 2π = ${2 * math.pi}');
final xs = [for (var i = 0; i <= 100; i++) -2 + i * 2.99 / 100];
{'label': 'E(m)', 'points': [for (final x in xs) [x, ellipe(x)]]},
{'label': 'K(m)', 'points': [for (final x in xs) [x, ellipk(x)]]},
]})}');
}E(0) = 1.5707963267948968 (π/2) E(1) = 1.0 perimeter of a 2×1 ellipse = 9.688448220547675 circle check: 6.283185307179587 vs 2π = 6.283185307179586
double ellipkinc(double phi, double m)Incomplete elliptic integral of the first kind F(\varphi \mid m) (SciPy ellipkinc).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('F(π/2 | 0.5) = ${ellipkinc(math.pi / 2, 0.5)}');
print('K(0.5) = ${ellipk(0.5)}');
print('F(φ | 0) = φ: F(1 | 0) = ${ellipkinc(1, 0)}');
final xs = [for (var i = 0; i <= 60; i++) i * 3.1416 / 60];
{'label': 'm = 0', 'points': [for (final x in xs) [x, ellipkinc(x, 0)]]},
{'label': 'm = 0.5', 'points': [for (final x in xs) [x, ellipkinc(x, 0.5)]]},
{'label': 'm = 0.9', 'points': [for (final x in xs) [x, ellipkinc(x, 0.9)]]},
]})}');
}F(π/2 | 0.5) = 1.854074677301372 K(0.5) = 1.854074677301372 F(φ | 0) = φ: F(1 | 0) = 1.0
double ellipeinc(double phi, double m)Incomplete elliptic integral of the second kind E(\varphi \mid m) (SciPy ellipeinc).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('E(π/2 | 0.5) = ${ellipeinc(math.pi / 2, 0.5)} (= ellipe(0.5) = ${ellipe(0.5)})');
// Arc length of the ellipse x = 2 cos t, y = sin t from t = 0 to π/4 (parameter m = 1 − b²/a²).
print('arc length 0 → π/4 (a = 2, b = 1): ${2 * (ellipeinc(math.pi / 2, 0.75) - ellipeinc(math.pi / 4, 0.75))}');
final xs = [for (var i = 0; i <= 60; i++) i * 3.1416 / 60];
{'label': 'm = 0', 'points': [for (final x in xs) [x, ellipeinc(x, 0)]]},
{'label': 'm = 0.5', 'points': [for (final x in xs) [x, ellipeinc(x, 0.5)]]},
{'label': 'm = 1', 'points': [for (final x in xs) [x, ellipeinc(x, 1)]]},
]})}');
}E(π/2 | 0.5) = 1.350643881047675 (= ellipe(0.5) = 1.3506438810476755) arc length 0 → π/4 (a = 2, b = 1): 0.9656637442222276
double ellippi(double n, double m)Complete elliptic integral of the third kind \Pi(n \mid m), n < 1, m < 1.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('Π(0 | 0.5) = ${ellippi(0, 0.5)} (= K(0.5) = ${ellipk(0.5)})');
print('Π(0.3 | 0.5) = ${ellippi(0.3, 0.5)}');
print('Π(−0.5 | 0.5) = ${ellippi(-0.5, 0.5)}');
final xs = [for (var i = 0; i <= 80; i++) -3 + i * 3.9 / 80];
{'label': 'Π(n | 0.5)', 'points': [for (final x in xs) [x, ellippi(x, 0.5)]]},
]})}');
}Π(0 | 0.5) = 1.854074677301372 (= K(0.5) = 1.854074677301372) Π(0.3 | 0.5) = 2.250376821943947 Π(−0.5 | 0.5) = 1.4878469926687985
double ellippiinc(double n, double phi, double m)Incomplete elliptic integral of the third kind \Pi(n; \varphi \mid m) (DLMF 19.25.14; n \sin^{2}\varphi < 1).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('Π(0.3; π/2 | 0.5) = ${ellippiinc(0.3, math.pi / 2, 0.5)}');
print('Π(0.3 | 0.5) = ${ellippi(0.3, 0.5)}');
print('n = 0 gives F: ${ellippiinc(0, 1, 0.5)} vs ${ellipkinc(1, 0.5)}');
}Π(0.3; π/2 | 0.5) = 2.250376821943947 Π(0.3 | 0.5) = 2.250376821943947 n = 0 gives F: 1.0832167728451687 vs 1.0832167728451687
({double sn, double cn, double dn, double ph}) ellipj(double u, double m)Jacobi elliptic functions sn, cn, dn and the amplitude \varphi at u for 0 \le m \le 1 (SciPy ellipj), by the arithmetic–geometric mean and descending Landen transformation (Abramowitz and Stegun 16.4).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
final r = ellipj(0.75, 0.5);
print('sn = ${r.sn}, cn = ${r.cn}, dn = ${r.dn}, φ = ${r.ph}');
print('sn² + cn² = ${r.sn * r.sn + r.cn * r.cn}');
print('dn² + m·sn² = ${r.dn * r.dn + 0.5 * r.sn * r.sn}');
// Period 4K(m).
final k = ellipk(0.5);
for (final f in ['sn', 'cn', 'dn'])
{'label': f, 'points': [for (var i = 0; i <= 80; i++) [4 * k * i / 80, switch (f) { 'sn' => ellipj(4 * k * i / 80, 0.5).sn, 'cn' => ellipj(4 * k * i / 80, 0.5).cn, _ => ellipj(4 * k * i / 80, 0.5).dn }]]}
]})}');
}sn = 0.6585147441299967, cn = 0.7525678253575587, dn = 0.884974104639058, φ = 0.7188434684431806 sn² + cn² = 1.0 dn² + m·sn² = 1.0
double erf(double x)The error function \operatorname{erf}(x).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
for (final x in [0.0, 0.5, 1.0, 2.0]) {
print('erf($x) = ${erf(x)}, erfc($x) = ${erfc(x)}');
}
print('erfinv(0.5) = ${erfinv(0.5)}');
{'label': 'erf', 'points': [for (var i = -30; i <= 30; i++) [i / 10, erf(i / 10)]]},
{'label': 'erfc', 'points': [for (var i = -30; i <= 30; i++) [i / 10, erfc(i / 10)]]},
]})}');
}erf(0.0) = 0.0, erfc(0.0) = 1.0 erf(0.5) = 0.5204998778130465, erfc(0.5) = 0.4795001221869535 erf(1.0) = 0.8427007929497148, erfc(1.0) = 0.15729920705028516 erf(2.0) = 0.9953222650189527, erfc(2.0) = 0.004677734981047265 erfinv(0.5) = 0.4769362762044699
double erfc(double x)The complementary error function \operatorname{erfc}(x) = 1 - \operatorname{erf}(x).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('erfc(0) = ${erfc(0)}');
print('erfc(3) = ${erfc(3)}');
print('erfc(10) = ${erfc(10)} (1 − erf(10) = ${1 - erf(10)})');
print('erfc(−2) = ${erfc(-2)} (= 2 − erfc(2))');
final xs = [for (var i = 0; i <= 60; i++) -3 + i * 6 / 60];
{'label': 'erfc', 'points': [for (final x in xs) [x, erfc(x)]]},
]})}');
}erfc(0) = 1.0 erfc(3) = 0.000022090496998585445 erfc(10) = 2.088487583762545e-45 (1 − erf(10) = 0.0) erfc(−2) = 1.9953222650189528 (= 2 − erfc(2))
double erfcx(double x)The scaled complementary error function \operatorname{erfcx}(x) = e^{x^{2}} \operatorname{erfc}(x).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('erfcx(1) = ${erfcx(1)}');
print('erfcx(30) = ${erfcx(30)} (≈ 1/(x√π) = ${1 / (30 * math.sqrt(math.pi))})');
print('naive e^(x²)·erfc(x) at 30: ${math.exp(900) * erfc(30)}');
final xs = [for (var i = 0; i <= 70; i++) -1 + i * 7 / 70];
{'label': 'erfcx', 'points': [for (final x in xs) [x, erfcx(x)]]},
]})}');
}erfcx(1) = 0.42758357615580705 erfcx(30) = 0.01879588886141675 (≈ 1/(x√π) = 0.01880631945159188) naive e^(x²)·erfc(x) at 30: NaN
double erfinv(double y)Inverse error function: \operatorname{erf}(\operatorname{erfinv}(y)) = y for -1 \le y \le 1.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
for (final y in [0.0, 0.5, 0.9, 0.999999]) {
final x = erfinv(y);
print('erfinv($y) = $x, erf back = ${erf(x)}');
}
print('erfinv(1) = ${erfinv(1)}');
final xs = [for (var i = 0; i <= 80; i++) -0.995 + i * 1.99 / 80];
{'label': 'erfinv', 'points': [for (final x in xs) [x, erfinv(x)]]},
]})}');
}erfinv(0.0) = 0.0, erf back = 0.0 erfinv(0.5) = 0.4769362762044699, erf back = 0.5 erfinv(0.9) = 1.1630871536766743, erf back = 0.9 erfinv(0.999999) = 3.458910737275499, erf back = 0.999999 erfinv(1) = Infinity
double erfcinv(double q)Inverse complementary error function: \operatorname{erfc}(\operatorname{erfcinv}(q)) = q for 0 \le q \le 2.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('erfcinv(1) = ${erfcinv(1)}');
// Tiny tails keep full precision: erfinv(1 − q) would lose them.
for (final q in [1e-5, 1e-20, 1e-100]) {
final x = erfcinv(q);
print('erfcinv($q) = $x, erfc back = ${erfc(x)}');
}
}erfcinv(1) = 0.0 erfcinv(0.00001) = 3.123413274340875, erfc back = 0.000009999999999999994 erfcinv(1e-20) = 6.601580622355143, erfc back = 9.999999999999974e-21 erfcinv(1e-100) = 15.065574702592645, erfc back = 1.0000000000000152e-100
double ndtr(double x)The standard normal CDF \Phi(x) (SciPy ndtr).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
// The standard normal cumulative distribution and its inverse.
print('P(Z < 1.96) = ${ndtr(1.96)}');
print('ndtri(0.975) = ${ndtri(0.975)}');
}P(Z < 1.96) = 0.9750021048517795 ndtri(0.975) = 1.959963984540054
double ndtri(double p)The standard normal quantile \Phi^{-1}(p) (SciPy ndtri).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('ndtri(0.5) = ${ndtri(0.5)}');
print('ndtri(0.975) = ${ndtri(0.975)} (the 95% two-sided z)');
print('ndtri(1e-10) = ${ndtri(1e-10)}, ndtr back = ${ndtr(ndtri(1e-10))}');
final xs = [for (var i = 0; i <= 99; i++) 0.005 + i * 0.99 / 99];
{'label': 'ndtri', 'points': [for (final x in xs) [x, ndtri(x)]]},
]})}');
}ndtri(0.5) = -0.0 ndtri(0.975) = 1.959963984540054 (the 95% two-sided z) ndtri(1e-10) = -6.361340902404057, ndtr back = 9.999999999999934e-11
double stirlingCorrection(double x)Stirling correction \operatorname{lgamma}(x) - ((x - \frac{1}{2}) \ln x - x + \frac{\ln(2\pi)}{2}) for x \ge 10.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
for (final x in [10.0, 100.0, 1e6]) {
final c = stirlingCorrection(x);
print('x = $x: correction $c, 1/(12x) = ${1 / (12 * x)}');
}
// lgamma = Stirling's formula + correction.
const x = 20.0;
print('lgamma(20) = ${lgamma(x)}, Stirling + correction = ${(x - 0.5) * math.log(x) - x + 0.5 * math.log(2 * math.pi) + stirlingCorrection(x)}');
}x = 10.0: correction 0.00833056343336287, 1/(12x) = 0.008333333333333333 x = 100.0: correction 0.0008333305556349147, 1/(12x) = 0.0008333333333333334 x = 1000000.0: correction 8.333333333333055e-8, 1/(12x) = 8.333333333333334e-8 lgamma(20) = 39.339884187199495, Stirling + correction = 39.339884187199495
double gamma(double x)The gamma function \Gamma(x) for real x. NaN at non-positive integers, \pm\infty at \pm0.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('gamma(5) = ${gamma(5)} (4! = 24)');
print('gamma(0.5)² = ${gamma(0.5) * gamma(0.5)} (π)');
print('gamma(-1.5) = ${gamma(-1.5)}');
print('lgamma(100) = ${lgamma(100)}');
final pts = [for (var i = 0; i <= 60; i++) 0.1 + i * 0.075];
{'label': 'Γ(x)', 'points': [for (final x in pts) [x, gamma(x)]]}
]})}');
}gamma(5) = 24.0 (4! = 24) gamma(0.5)² = 3.1415926535897936 (π) gamma(-1.5) = 2.3632718012073544 lgamma(100) = 359.1342053695754
double gammaSign(double x)Sign of \Gamma(x) (+1 or -1; +1 for NaN inputs).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
for (final x in [2.5, -0.5, -1.5, -2.5, -3.5]) {
print('Γ($x) = ${gamma(x)}, gammaSign = ${gammaSign(x)}');
}
}Γ(2.5) = 1.329340388179137, gammaSign = 1.0 Γ(-0.5) = -3.544907701811032, gammaSign = -1.0 Γ(-1.5) = 2.3632718012073544, gammaSign = 1.0 Γ(-2.5) = -0.9453087204829419, gammaSign = -1.0 Γ(-3.5) = 0.2700882058522691, gammaSign = 1.0
double lgamma(double x)\log|\Gamma(x)|. +\infty at non-positive integers. Relative accuracy is lost only next to its roots on the negative axis.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
// ln Γ(n + 1) = ln n!: no overflow even where n! is astronomically large.
for (final n in [10, 100, 1000]) {
print('ln($n!) = ${lgamma(n + 1.0)}');
}
}ln(10!) = 15.104412573075516 ln(100!) = 363.73937555556347 ln(1000!) = 5912.128178488164
double digamma(double x)The digamma function \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}. NaN at non-positive integers (-\infty at 0, as SciPy).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('digamma(1) = ${digamma(1)} (−γ, Euler–Mascheroni)');
print('digamma(2) − digamma(1) = ${digamma(2) - digamma(1)}');
print('trigamma(1) = ${trigamma(1)} (π²/6)');
}digamma(1) = -0.5772156649015329 (−γ, Euler–Mascheroni) digamma(2) − digamma(1) = 1.0 trigamma(1) = 1.6449340668482264 (π²/6)
double trigamma(double x)The trigamma function \psi'(x).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('ψ\'(1) = ${trigamma(1)} (π²/6 = ${math.pi * math.pi / 6})');
print('ψ\'(0.5) = ${trigamma(0.5)} (π²/2 = ${math.pi * math.pi / 2})');
print('ψ\'(100) = ${trigamma(100)} (≈ 1/x)');
final xs = [for (var i = 0; i <= 80; i++) 0.3 + i * 5.7 / 80];
{'label': 'trigamma', 'points': [for (final x in xs) [x, trigamma(x)]]},
]})}');
}ψ'(1) = 1.6449340668482264 (π²/6 = 1.6449340668482264) ψ'(0.5) = 4.93480220054468 (π²/2 = 4.934802200544679) ψ'(100) = 0.010050166663333571 (≈ 1/x)
double polygamma(int n, double x)The polygamma function \psi^{(n)}(x) for n \ge 0 and x > 0 (n = 0, 1 for any real x): (-1)^{n + 1} n! \zeta(n + 1, x).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('ψ⁽⁰⁾(1) = ${polygamma(0, 1)} (digamma(1) = ${digamma(1)})');
print('ψ⁽¹⁾(1) = ${polygamma(1, 1)}');
print('ψ⁽²⁾(1) = ${polygamma(2, 1)} (−2ζ(3) = ${-2 * zeta(3)})');
print('ψ⁽³⁾(2) = ${polygamma(3, 2)}');
final xs = [for (var i = 0; i <= 80; i++) 0.5 + i * 4.5 / 80];
{'label': 'n = 1', 'points': [for (final x in xs) [x, polygamma(1, x)]]},
{'label': 'n = 2', 'points': [for (final x in xs) [x, polygamma(2, x)]]},
{'label': 'n = 3', 'points': [for (final x in xs) [x, polygamma(3, x)]]},
]})}');
}ψ⁽⁰⁾(1) = -0.5772156649015329 (digamma(1) = -0.5772156649015329) ψ⁽¹⁾(1) = 1.6449340668482264 ψ⁽²⁾(1) = -2.4041138063191885 (−2ζ(3) = -2.4041138063191885) ψ⁽³⁾(2) = 0.4939394022668291
double beta(double a, double b)The beta function B(a, b) = \Gamma(a) \Gamma(b) / \Gamma(a + b), for a, b > 0.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('beta(2, 3) = ${beta(2, 3)} (1/12)');
print('beta(0.5, 0.5) = ${beta(0.5, 0.5)} (π)');
print('regularised incomplete beta I_0.5(2, 2) = ${betainc(2, 2, 0.5)}');
}beta(2, 3) = 0.08333333333333333 (1/12) beta(0.5, 0.5) = 3.1415926535897936 (π) regularised incomplete beta I_0.5(2, 2) = 0.5000000000000002
double lbeta(double a, double b)\log B(a, b) for a, b > 0, without cancellation for large or very unequal arguments.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('lbeta(2, 3) = ${lbeta(2, 3)} (ln(1/12) = ${math.log(1 / 12)})');
print('lbeta(1e4, 1e4) = ${lbeta(1e4, 1e4)}');
print('lbeta(1e-3, 1e6) = ${lbeta(1e-3, 1e6)} (very unequal)');
print('beta(1e4, 1e4) underflows: ${beta(1e4, 1e4)}');
}lbeta(2, 3) = -2.4849066497880004 (ln(1/12) = -2.4849066497880004) lbeta(1e4, 1e4) = -13866.28325676141 lbeta(1e-3, 1e6) = 6.893363375325389 (very unequal) beta(1e4, 1e4) underflows: 0.0
double hyp2f1(double a, double b, double c, double z)Gauss hypergeometric function {}_{2}F_{1}(a, b; c; z) for real z < 1 (and z = 1 when c - a - b > 0): the power series for 0 \le z \le \frac{1}{2}, Pfaff's transformation for z < 0 and the 1 - z connection formula (DLMF 15.8.4) for \frac{1}{2} < z < 1 when c - a - b is not an integer (the series otherwise). Polynomials (a or b a non-positive integer) are summed exactly.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math';
void main() {
// 2F1(1, 1; 2; x) = −ln(1 − x)/x.
const x = 0.5;
print('2F1(1, 1; 2; 0.5) = ${hyp2f1(1, 1, 2, x)}');
print('−ln(1 − x)/x = ${-log(1 - x) / x}');
}2F1(1, 1; 2; 0.5) = 1.3862943611198901 −ln(1 − x)/x = 1.3862943611198906
double hyp1f1(double a, double b, double x)Confluent hypergeometric function {}_{1}F_{1}(a; b; x) (Kummer's M) for real x: the series, with Kummer's transformation e^{x} {}_{1}F_{1}(b - a; b; -x) for x < 0.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('₁F₁(1; 1; 2) = ${hyp1f1(1, 1, 2)} (e² = ${math.exp(2)})');
print('₁F₁(1; 2; 1) = ${hyp1f1(1, 2, 1)} ((e − 1) = ${math.e - 1})');
print('₁F₁(−2; 1; x) is a Laguerre polynomial: at 3 = ${hyp1f1(-2, 1, 3)}');
final xs = [for (var i = 0; i <= 60; i++) -3 + i * 6 / 60];
{'label': 'a = −2', 'points': [for (final x in xs) [x, hyp1f1(-2, 1.5, x)]]},
{'label': 'a = 0.5', 'points': [for (final x in xs) [x, hyp1f1(0.5, 1.5, x)]]},
{'label': 'a = 2', 'points': [for (final x in xs) [x, hyp1f1(2, 1.5, x)]]},
]})}');
}₁F₁(1; 1; 2) = 7.389056098930649 (e² = 7.38905609893065) ₁F₁(1; 2; 1) = 1.7182818284590455 ((e − 1) = 1.718281828459045) ₁F₁(−2; 1; x) is a Laguerre polynomial: at 3 = -0.5
double gammaincP(double a, double x)The regularized lower incomplete gamma function P(a, x) = \gamma(a, x) / \Gamma(a) (SciPy gammainc), a > 0, x \ge 0.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
// The regularised lower incomplete gamma P(a, x): for a = 1 it is 1 − e^(−x).
print('P(1, 2) = ${gammaincP(1, 2)}');
print('Q(1, 2) = ${gammaincQ(1, 2)}');
print('P(3, 2) = ${gammaincP(3, 2)}');
}P(1, 2) = 0.8646647167633873 Q(1, 2) = 0.1353352832366127 P(3, 2) = 0.3233235838169366
double gammaincQ(double a, double x)The regularized upper incomplete gamma function Q(a, x) = 1 - P(a, x) (SciPy gammaincc).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('Q(1, 1) = ${gammaincQ(1, 1)} (e^−1)');
print('P + Q = ${gammaincP(3, 2) + gammaincQ(3, 2)}');
// Chi-square survival: P(χ²_k > x) = Q(k/2, x/2).
print('P(χ²₅ > 11.07) = ${gammaincQ(2.5, 11.07 / 2)}');
final xs = [for (var i = 0; i <= 80; i++) i * 12 / 80];
{'label': 'a = 1', 'points': [for (final x in xs) [x, gammaincQ(1, x)]]},
{'label': 'a = 3', 'points': [for (final x in xs) [x, gammaincQ(3, x)]]},
{'label': 'a = 6', 'points': [for (final x in xs) [x, gammaincQ(6, x)]]},
]})}');
}Q(1, 1) = 0.3678794411714422 (e^−1) P + Q = 1.0 P(χ²₅ > 11.07) = 0.050009618622405445
double gammaincPInv(double a, double p)Inverse of gammaincP in x (SciPy gammaincinv).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
final x = gammaincPInv(3, 0.5);
print('x with P(3, x) = 0.5: $x');
print('P(3, x) back = ${gammaincP(3, x)}');
// Chi-square 95th percentile with 4 degrees of freedom: 2 · P⁻¹(2, 0.95).
print('χ²₄ 95th percentile = ${2 * gammaincPInv(2, 0.95)}');
}x with P(3, x) = 0.5: 2.674060313723561 P(3, x) back = 0.5000000000000001 χ²₄ 95th percentile = 9.487729036781154
double gammaincQInv(double a, double q)Inverse of gammaincQ in x (SciPy gammainccinv).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
final x = gammaincQInv(2.5, 1e-12);
print('x with Q(2.5, x) = 1e-12: $x');
print('Q back = ${gammaincQ(2.5, x)}');
print('Q⁻¹(1, e^−1) = ${gammaincQInv(1, 0.36787944117144233)} (1)');
}x with Q(2.5, x) = 1e-12: 32.61931810668392 Q back = 1.0000000000000026e-12 Q⁻¹(1, e^−1) = 0.9999999999999998 (1)
double betainc(double a, double b, double x)The regularized incomplete beta function I_{x}(a, b) (SciPy betainc), a, b > 0, 0 \le x \le 1.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('I_0.5(2, 2) = ${betainc(2, 2, 0.5)} (symmetric)');
print('I_0.3(1, 1) = ${betainc(1, 1, 0.3)} (uniform cdf)');
// Binomial tail: P(X ≤ 3), X ~ Bin(10, 0.5) = I_0.5(7, 4).
print('P(Bin(10, ½) ≤ 3) = ${betainc(7, 4, 0.5)}');
final xs = [for (var i = 0; i <= 80; i++) i * 1 / 80];
{'label': '(0.5, 0.5)', 'points': [for (final x in xs) [x, betainc(0.5, 0.5, x)]]},
{'label': '(2, 2)', 'points': [for (final x in xs) [x, betainc(2, 2, x)]]},
{'label': '(2, 5)', 'points': [for (final x in xs) [x, betainc(2, 5, x)]]},
]})}');
}I_0.5(2, 2) = 0.5000000000000002 (symmetric) I_0.3(1, 1) = 0.3 (uniform cdf) P(Bin(10, ½) ≤ 3) = 0.17187499999999994
double betaincc(double a, double b, double x)1 - I_{x}(a, b) (SciPy betaincc).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('1 − I_0.9(2, 5) = ${betaincc(2, 5, 0.9)}');
print('subtracting: ${1 - betainc(2, 5, 0.9)}');
print('tiny tail I_c at 0.999 (a=2, b=50): ${betaincc(2, 50, 0.999)}');
}1 − I_0.9(2, 5) = 0.00005499999999999995 subtracting: 0.00005500000000002725 tiny tail I_c at 0.999 (a=2, b=50): 5.095000000000224e-149
double betaincinv(double a, double b, double p)Inverse of betainc in x (SciPy betaincinv).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
final x = betaincinv(2, 5, 0.5);
print('median of Beta(2, 5): $x');
print('I_x(2, 5) back = ${betainc(2, 5, x)}');
print('uniform: betaincinv(1, 1, 0.3) = ${betaincinv(1, 1, 0.3)}');
}median of Beta(2, 5): 0.26444998329566005 I_x(2, 5) back = 0.5 uniform: betaincinv(1, 1, 0.3) = 0.3
double betainccinv(double a, double b, double q)Inverse of betaincc in x (SciPy betainccinv).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
final x = betainccinv(2, 5, 0.05);
print('x with 1 − I_x(2, 5) = 0.05: $x (95th percentile of Beta(2, 5))');
print('betaincc back = ${betaincc(2, 5, x)}');
}x with 1 − I_x(2, 5) = 0.05: 0.5818034092520259 (95th percentile of Beta(2, 5)) betaincc back = 0.050000000000000024
double legendreP(int n, double x)Legendre polynomial P_{n}(x) by the three-term recurrence.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
for (var n = 0; n <= 4; n++) {
print('P$n(0.5) = ${legendreP(n, 0.5)}');
}
for (var n = 1; n <= 4; n++) {'label': 'P$n', 'points': [for (var i = -20; i <= 20; i++) [i / 20, legendreP(n, i / 20)]]}
]})}');
}P0(0.5) = 1.0 P1(0.5) = 0.5 P2(0.5) = -0.125 P3(0.5) = -0.4375 P4(0.5) = -0.2890625
double assocLegendreP(int l, int m, double x)Associated Legendre function P_{l}^{m}(x) for |x| \le 1, with the Condon–Shortley phase (-1)^{m} (SciPy $\operatorname{lpmv}(m, l, x)$ and DLMF 14.7.10 convention). Negative m uses P_{l}^{-m} = (-1)^{m} (l-m)!/(l + m)! P_{l}^{m}.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
const x = 0.5;
print('P_1^1(0.5) = ${assocLegendreP(1, 1, x)} (−√(1 − x²) = ${-math.sqrt(1 - x * x)})');
print('P_2^1(0.5) = ${assocLegendreP(2, 1, x)} (−3x√(1 − x²))');
print('P_3^0(0.5) = ${assocLegendreP(3, 0, x)} (= legendreP(3, x) = ${legendreP(3, x)})');
final xs = [for (var i = 0; i <= 80; i++) -1 + i * 2 / 80];
{'label': 'm = 0', 'points': [for (final x in xs) [x, assocLegendreP(3, 0, x)]]},
{'label': 'm = 1', 'points': [for (final x in xs) [x, assocLegendreP(3, 1, x)]]},
{'label': 'm = 2', 'points': [for (final x in xs) [x, assocLegendreP(3, 2, x)]]},
]})}');
}P_1^1(0.5) = -0.8660254037844386 (−√(1 − x²) = -0.8660254037844386) P_2^1(0.5) = -1.299038105676658 (−3x√(1 − x²)) P_3^0(0.5) = -0.4375 (= legendreP(3, x) = -0.4375)
(double, double) sphericalHarmonic(int l, int m, double theta, double phi)Complex spherical harmonic Y_{l}^{m}(\theta, \varphi) = N_{l}^{m} P_{l}^{m}(\cos \theta) e^{im\varphi} (\theta polar, \varphi azimuth; Condon–Shortley phase, as SciPy sph_harm_y(l, m, θ, φ)). Returns (re, im).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
final (re, im) = sphericalHarmonic(0, 0, 0.3, 1.2);
print('Y_0^0 = $re + ${im}i (1/√(4π) = ${1 / math.sqrt(4 * math.pi)})');
final (r1, i1) = sphericalHarmonic(1, 1, math.pi / 2, math.pi / 4);
print('Y_1^1(π/2, π/4) = $r1 + ${i1}i');
print('Y_2^0 at the pole = ${sphericalHarmonic(2, 0, 0, 0).$1}');
final xs = [for (var i = 0; i <= 60; i++) i * 3.1416 / 60];
{'label': 'm = 0', 'points': [for (final x in xs) [x, sphericalHarmonic(2, 0, x, 0).$1]]},
{'label': 'm = 1', 'points': [for (final x in xs) [x, sphericalHarmonic(2, 1, x, 0).$1]]},
{'label': 'm = 2', 'points': [for (final x in xs) [x, sphericalHarmonic(2, 2, x, 0).$1]]},
]})}');
}Y_0^0 = 0.28209479177387814 + 0.0i (1/√(4π) = 0.28209479177387814) Y_1^1(π/2, π/4) = -0.24430125595145996 + -0.2443012559514599i Y_2^0 at the pole = 0.6307831305050399
double realSphericalHarmonic(int l, int m, double theta, double phi)Real spherical harmonic Y_{lm} (no Condon–Shortley phase) at polar angle \theta and azimuth \varphi: \sqrt{2} N P_{l}^{m} \cos m\varphi for m > 0, N P_{l}^{0} for m = 0, \sqrt{2} N P_{l}^{|m|} sin |m|\varphi for m < 0. The convention of riptide_math's Sh9 (Y_{1},-1 \propto y, Y_{10} \propto z, Y_{11} \propto x), indexed l^{2} + l + m.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('Y_00 = ${realSphericalHarmonic(0, 0, 1, 2)}');
// Band 1: Y_1,−1 ∝ y, Y_10 ∝ z, Y_11 ∝ x.
for (final m in [-1, 0, 1]) {
print('Y_1,$m(θ = π/2, φ = 0) = ${realSphericalHarmonic(1, m, math.pi / 2, 0)}');
}
final xs = [for (var i = 0; i <= 60; i++) i * 3.1416 / 60];
{'label': 'm = -2', 'points': [for (final x in xs) [x, realSphericalHarmonic(2, -2, x, math.pi / 4)]]},
{'label': 'm = -1', 'points': [for (final x in xs) [x, realSphericalHarmonic(2, -1, x, math.pi / 4)]]},
{'label': 'm = 0', 'points': [for (final x in xs) [x, realSphericalHarmonic(2, 0, x, math.pi / 4)]]},
{'label': 'm = 1', 'points': [for (final x in xs) [x, realSphericalHarmonic(2, 1, x, math.pi / 4)]]},
{'label': 'm = 2', 'points': [for (final x in xs) [x, realSphericalHarmonic(2, 2, x, math.pi / 4)]]},
]})}');
}Y_00 = 0.28209479177387814 Y_1,-1(θ = π/2, φ = 0) = 0.0 Y_1,0(θ = π/2, φ = 0) = 2.991827511286337e-17 Y_1,1(θ = π/2, φ = 0) = 0.4886025119029199
void realShBasis(int lmax, double x, double y, double z, Float64List out)All real SH values up to band lmax for the unit direction (x, y, z), into out ((lmax+1)² values, index l^{2} + l + m). Allocation-free.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:typed_data';
void main() {
const lmax = 2;
final out = Float64List((lmax + 1) * (lmax + 1));
realShBasis(lmax, 0, 0, 1, out); // straight up
print('basis at +z: ${[for (final v in out) v.toStringAsFixed(4)]}');
realShBasis(lmax, 1, 0, 0, out);
print('basis at +x: ${[for (final v in out) v.toStringAsFixed(4)]}');
['dir', for (var i = 0; i < 9; i++) 'Y$i'],
for (final d in [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) ['xyz'[d.indexOf(1.0)], ...(() { realShBasis(lmax, d[0], d[1], d[2], out); return [for (final v in out) double.parse(v.toStringAsFixed(4))]; })()]
]})}');
}basis at +z: [0.2821, 0.0000, 0.4886, 0.0000, 0.0000, 0.0000, 0.6308, 0.0000, 0.0000] basis at +x: [0.2821, 0.0000, 0.0000, 0.4886, 0.0000, 0.0000, -0.3154, 0.0000, 0.5463]
double log1p(double x)\log(1 + x) accurate for small x (Goldberg's correction: the rounding of 1 + x is divided back out).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math';
void main() {
const x = 1e-12;
print('log1p(1e-12) = ${log1p(x)}');
print('log(1 + 1e-12) = ${log(1 + x)}');
print('expm1(1e-12) = ${expm1(x)}');
}log1p(1e-12) = 9.999999999995e-13 log(1 + 1e-12) = 1.000088900581841e-12 expm1(1e-12) = 1.0000000000005e-12
double expm1(double x)\exp(x) - 1 accurate for small x (Kahan's correction).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
const x = 1e-10;
print('expm1(1e-10) = ${expm1(x)}');
print('exp(1e-10) − 1 = ${math.exp(x) - 1} (loses digits)');
print('expm1(1) = ${expm1(1)} (e − 1)');
print('expm1(−50) = ${expm1(-50)}');
}expm1(1e-10) = 1.00000000005e-10 exp(1e-10) − 1 = 1.000000082740371e-10 (loses digits) expm1(1) = 1.718281828459045 (e − 1) expm1(−50) = -1.0
double log1pmx(double x)\log(1 + x) - x, without cancellation for small x.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
for (final x in [1e-3, 1e-6, 0.5]) {
print('log1pmx($x) = ${log1pmx(x)}, naive = ${math.log(1 + x) - x}');
}
print('≈ −x²/2 for small x: ${-1e-6 * 1e-6 / 2}');
}log1pmx(0.001) = -4.996669164668331e-7, naive = -4.996669165768103e-7 log1pmx(0.000001) = -4.999996666669167e-13, naive = -5.000819331931524e-13 log1pmx(0.5) = -0.09453489189183556, naive = -0.09453489189183562 ≈ −x²/2 for small x: -5e-13
double sinpi(double x)\sin(\pi x) with exact argument reduction (exact zeros at integers).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math';
void main() {
print('sinpi(1) = ${sinpi(1)} vs sin(π) = ${sin(pi)}');
print('cospi(0.5) = ${cospi(0.5)} vs cos(π/2) = ${cos(pi / 2)}');
}sinpi(1) = -0.0 vs sin(π) = 1.2246467991473532e-16 cospi(0.5) = 0.0 vs cos(π/2) = 6.123233995736766e-17
double cospi(double x)\cos(\pi x) with exact argument reduction.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
print('cospi(0.5) = ${cospi(0.5)} (exactly 0)');
print('cos(π·0.5) = ${math.cos(math.pi * 0.5)}');
print('cospi(1e15 + 1) = ${cospi(1e15 + 1)}');
print('cospi(1/3) = ${cospi(1 / 3)}');
final xs = [for (var i = 0; i <= 80; i++) -2 + i * 4 / 80];
{'label': 'cospi', 'points': [for (final x in xs) [x, cospi(x)]]},
{'label': 'sinpi', 'points': [for (final x in xs) [x, sinpi(x)]]},
]})}');
}cospi(0.5) = 0.0 (exactly 0) cos(π·0.5) = 6.123233995736766e-17 cospi(1e15 + 1) = -1.0 cospi(1/3) = 0.5
double hurwitzZeta(double s, double a)Hurwitz zeta \zeta(s, a) = \sum_{k \ge 0} (k + a)^{-s} for a > 0 and s \ne 1, by Euler–Maclaurin summation (the method of Cephes zeta.c and of SciPy's zeta(x, q), written here from the formula). Valid as an analytic continuation for s < 1.
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('ζ(2, 1) = ${hurwitzZeta(2, 1)} (= ζ(2) = ${zeta(2)})');
print('ζ(2, 0.5) = ${hurwitzZeta(2, 0.5)} (3ζ(2) = ${3 * zeta(2)})');
print('ζ(3, 2) = ${hurwitzZeta(3, 2)} (ζ(3) − 1 = ${zeta(3) - 1})');
final xs = [for (var i = 0; i <= 80; i++) 0.3 + i * 3.7 / 80];
{'label': 's = 2', 'points': [for (final x in xs) [x, hurwitzZeta(2, x)]]},
{'label': 's = 3', 'points': [for (final x in xs) [x, hurwitzZeta(3, x)]]},
{'label': 's = 4', 'points': [for (final x in xs) [x, hurwitzZeta(4, x)]]},
]})}');
}ζ(2, 1) = 1.6449340668482262 (= ζ(2) = 1.6449340668482262) ζ(2, 0.5) = 4.93480220054468 (3ζ(2) = 4.934802200544679) ζ(3, 2) = 0.2020569031595943 (ζ(3) − 1 = 0.20205690315959424)
double zeta(double s)Riemann zeta \zeta(s) for real s (the functional equation for s < 0).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
void main() {
print('zeta(2) = ${zeta(2)} (π²/6 = 1.6449340668482264)');
print('zeta(4) = ${zeta(4)} (π⁴/90)');
print('zeta(3) = ${zeta(3)} (Apéry\'s constant)');
print('hurwitzZeta(2, 2) = ${hurwitzZeta(2, 2)} (zeta(2) − 1)');
}zeta(2) = 1.6449340668482262 (π²/6 = 1.6449340668482264) zeta(4) = 1.0823232337111384 (π⁴/90) zeta(3) = 1.2020569031595942 (Apéry's constant) hurwitzZeta(2, 2) = 0.6449340668482264 (zeta(2) − 1)
Classes
ShRotation
final class ShRotation
Rotation matrices for real spherical harmonics, band by band up to lmax, built from a 3 \times 3 rotation by the recurrence of Ivanic and Ruedenberg (J. Phys. Chem. 100, 1996; erratum 102, 1998). If f has coefficients c, then the rotated function g(\omega) = f(R^{T}\omega) has coefficients apply(c).
Constructors
factory ShRotation.fromMatrix(int lmax, List<double> r)From a row-major 3 \times 3 rotation matrix r (r[3 \cdot i + j] = R_{ij}, acting on column vectors).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:typed_data';
void main() {
// 90° about z: x → y.
final rot = ShRotation.fromMatrix(1, [0, -1, 0, 1, 0, 0, 0, 0, 1]);
final coeffs = Float64List(4), out = Float64List(4);
realShBasis(1, 1, 0, 0, coeffs); // a lobe pointing along +x
rot.apply(coeffs, out);
final want = Float64List(4);
realShBasis(1, 0, 1, 0, want);
print('rotated +x lobe: ${[for (final v in out) v.toStringAsFixed(4)]}');
print('+y lobe: ${[for (final v in want) v.toStringAsFixed(4)]}');
}rotated +x lobe: [0.2821, 0.4886, 0.0000, 0.0000] +y lobe: [0.2821, 0.4886, 0.0000, 0.0000]
factory ShRotation.fromQuat(int lmax, Quat q)From a unit quaternion (riptide_math Quat).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'package:riptide_math/riptide_math.ray' show Quat, Vec3;
import 'raya:math' as math;
import 'raya:typed_data';
void main() {
final q = Quat.axisAngle(Vec3(0, 0, 1), math.pi / 2);
final rot = ShRotation.fromQuat(2, q);
final c = Float64List(9), out = Float64List(9), want = Float64List(9);
realShBasis(2, 1, 0, 0, c);
rot.apply(c, out);
realShBasis(2, 0, 1, 0, want);
var err = 0.0;
for (var i = 0; i < 9; i++) err = math.max(err, (out[i] - want[i]).abs());
print('band sizes: ${[for (final b in rot.bands) b.length]}');
print('max |rotated − expected| = ${err < 1e-6 ? '< 1e-6' : err}');
}band sizes: [1, 9, 25] max |rotated − expected| = < 1e-6
Properties
final int lmaxfinal List<Float64List> bandsBandsl is the (2l + 1) \times (2l + 1) row-major block, rows and columns ordered m = -l \cdots l.
Methods
void apply(List<double> coeffs, List<double> out)\text{out} = M \cdot \text{coeffs} for (\text{lmax} + 1)^{2} coefficients (out may not alias coeffs).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
void main() {
// Rotate a band-0..2 signal by 180° about z and compare with sampling the rotated direction.
final rot = ShRotation.fromMatrix(2, [-1, 0, 0, 0, -1, 0, 0, 0, 1]);
final c = Float64List(9), out = Float64List(9), want = Float64List(9);
final s = 1 / math.sqrt(3);
realShBasis(2, s, s, s, c);
rot.apply(c, out);
realShBasis(2, -s, -s, s, want);
print('rotated: ${[for (final v in out) v.toStringAsFixed(3)]}');
print('target: ${[for (final v in want) v.toStringAsFixed(3)]}');
{'label': 'before', 'style': 'points', 'points': [for (var i = 0; i < 9; i++) [i, c[i]]]},
{'label': 'after', 'style': 'points', 'points': [for (var i = 0; i < 9; i++) [i, out[i]]]},
]})}');
}rotated: [0.282, -0.282, 0.282, -0.282, 0.364, -0.364, 0.000, -0.364, 0.000] target: [0.282, -0.282, 0.282, -0.282, 0.364, -0.364, 0.000, -0.364, 0.000]
void applyToShRgb(ShRgb sh)Rotates the 27 RGB coefficients of a riptide_math ShRgb in place (needs lmax ≥ 2).
import 'package:riptide_numerics_special/riptide_numerics_special.ray';
import 'raya:convert';
import 'package:riptide_math/riptide_math.ray' show Quat, Vec3, ShRgb;
import 'raya:math' as math;
void main() {
// Light arriving from +x; rotate the environment 90° about z so it arrives from +y.
final sh = ShRgb();
sh.addSample(Vec3(1, 0, 0), Vec3(1, 1, 1), 4 * math.pi);
final before = sh.irradiance(Vec3(0, 1, 0)).x;
ShRotation.fromQuat(2, Quat.axisAngle(Vec3(0, 0, 1), math.pi / 2)).applyToShRgb(sh);
print('irradiance at +y before: ${before.toStringAsFixed(3)}');
print('irradiance at +y after: ${sh.irradiance(Vec3(0, 1, 0)).x.toStringAsFixed(3)}');
print('irradiance at +x after: ${sh.irradiance(Vec3(1, 0, 0)).x.toStringAsFixed(3)}');
}irradiance at +y before: 1.178 irradiance at +y after: 13.352 irradiance at +x after: 1.178