Math.NET Numerics | Math.NET Project | GitHub


Special Functions

All the following special functions are available in the static SpecialFunctions class:

Factorial

\[x \mapsto x! = \prod_{k=1}^{x} k = \Gamma(x+1)\]

Code Sample:

double x = SpecialFunctions.Factorial(14); // 87178291200.0
double y = SpecialFunctions.Factorial(31); // 8.2228386541779224E+33

\[x \mapsto \ln x! = \ln\Gamma(x+1)\]

Binomial Coefficient

\[\binom{n}{k} = \mathrm{C}_n^k = \frac{n!}{k! (n-k)!}\]

\[\ln \binom{n}{k} = \ln n! - \ln k! - \ln(n-k)!\]

Multinomial Coefficient

\[\binom{n}{k_1,k_2,\dots,k_r} = \frac{n!}{k_1! k_2! \cdots k_r!} = \frac{n!}{\prod_{i=1}^{r}k_i!}\]

Exponential Integral

Generalized Exponential Integral

\[E_n(x) = \int_1^\infty t^{-n} e^{-xt}\,\mathrm{d}t\]

Gamma functions

Gamma

\[\Gamma(a) = \int_0^\infty t^{a-1} e^{-t}\,\mathrm{d}t\]

\[\ln\Gamma(a)\]

Incomplete Gamma

Lower incomplete Gamma function, unregularized.

\[\gamma(a,x) = \int_0^x t^{a-1} e^{-t}\,\mathrm{d}t\]

Upper incomplete Gamma function, unregularized.

\[\Gamma(a,x) = \int_x^\infty t^{a-1} e^{-t}\,\mathrm{d}t\]

Regularized Gamma

Lower regularized incomplete Gamma function.

\[\mathrm{P}(a,x) = \frac{\gamma(a,x)}{\Gamma(a)}\]

Upper regularized incomplete Gamma function.

\[\mathrm{Q}(a,x) = \frac{\Gamma(a,x)}{\Gamma(a)}\]

Inverse \(x\) of the lower regularized Gamma function, such that \(\mathrm{P}(a,x) = y\).

\[\mathrm{P}^{-1}(a,y)\]

Psi: Derivative of Logarithmic Gamma

\[\psi(x) = \frac{\mathrm{d}}{\mathrm{d}x}\ln\Gamma(x)\]

Inverse \(x\) of the DiGamma function, such that \(\psi(x) = p\).

\[\psi^{-1}(p)\]

Euler Beta functions

Euler Beta

\[\mathrm{B}(a,b) = \int_0^1 t^{a-1} (1-t)^{b-1}\,\mathrm{d}t = \frac{\Gamma(a)\Gamma(b)}{\Gamma(a+b)}\]

\[\ln\mathrm{B}(a,b) = \ln\Gamma(a) + \ln\Gamma(b) - \ln\Gamma(a+b)\]

Incomplete Beta

Lower incomplete Beta function (unregularized).

\[\mathrm{B}_x(a,b) = \int_0^x t^{a-1} (1-t)^{b-1}\,\mathrm{d}t\]

Regularized Beta

Lower incomplete regularized Beta function.

\[\mathrm{I}_x(a,b) = \frac{\mathrm{B}(a,b,x)}{\mathrm{B}(a,b)}\]

Error functions

Error Function

\[\mathrm{erf}(x) = \frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\,\mathrm{d}t\]

Inverse \(x\) of the Error function, such that \(\mathrm{erf}(x) = z\).

\[z \mapsto \mathrm{erf}^{-1}(z)\]

Complementary Error function.

\[\mathrm{erfc}(x) = 1-\mathrm{erf}(x) = \frac{2}{\sqrt{\pi}}\int_x^\infty e^{-t^2}\,\mathrm{d}t\]

Inverse \(x\) of the complementary Error function, such that \(\mathrm{erfc}(x) = z\).

\[z \mapsto \mathrm{erfc}^{-1}(z)\]

Code Sample:

double erf = SpecialFunctions.Erf(0.9); // 0.7969082124

Sigmoid: Logistic function

\[x \mapsto \frac{1}{1+e^{-x}}\]

Inverse of the Logistic function, for \(y\) between 0 and 1 (where the function is real-valued).

\[y \mapsto \ln \frac{y}{1-y}\]

Harmonic Numbers

The n-th Harmonic number is the sum of the reciprocals of the first n natural numbers. With \(\gamma\) as the Euler-Mascheroni constant and the DiGamma function:

\[\mathrm{H}_n = \sum_{k=1}^{n}\frac{1}{k} = \gamma - \psi(n+1)\]

Generalized harmonic number of order n of m.

\[\mathrm{H}_{n,m} = \sum_{k=1}^{n}\frac{1}{k^m}\]

Bessel and Struve Functions

Bessel functions

Bessel functions are canonical solutions \(y(x)\) of Bessel's differential equation

\[x^2\frac{\mathrm{d}^2y}{\mathrm{d}x^2}+x\frac{\mathrm{d}y}{\mathrm{d}x}+(x^2-\alpha^2)y = 0\]

Modified Bessel functions

Modified Bessel's equation:

\[x^2\frac{\mathrm{d}^2y}{\mathrm{d}x^2}+x\frac{\mathrm{d}y}{\mathrm{d}x}-(x^2+\alpha^2)y = 0\]

Modified Bessel functions:

\[\begin{align} \mathrm{I}_\alpha(x) &= \imath^{-\alpha}\mathrm{J}_\alpha(\imath x) = \sum_{m=0}^\infty \frac{1}{m!\Gamma(m+\alpha+1)}\left(\frac{x}{2}\right)^{2m+\alpha} \\ \mathrm{K}_\alpha(x) &= \frac{\pi}{2} \frac{\mathrm{I}_{-\alpha}(x)-\mathrm{I}_\alpha(x)}{\sin(\alpha\pi)} \end{align}\]

Modified or hyperbolic Bessel function of the first kind, order 0.

\[x \mapsto \mathrm{I}_0(x)\]

Modified or hyperbolic Bessel function of the first kind, order 1.

\[x \mapsto \mathrm{I}_1(x)\]

Modified or hyperbolic Bessel function of the second kind, order 0.

\[x \mapsto \mathrm{K}_0(x)\]

Exponentionally scaled modified Bessel function of the second kind, order 0.

\[x \mapsto e^x\mathrm{K}_0(x)\]

Modified or hyperbolic Bessel function of the second kind, order 1.

\[x \mapsto \mathrm{K}_1(x)\]

Exponentially scaled modified Bessel function of the second kind, order 1.

\[x \mapsto e^x\mathrm{K}_1(x)\]

Struve functions

Struve functions are solutions \(y(x)\) of the non-homogeneous Bessel's differential equation

\[x^2\frac{\mathrm{d}^2y}{\mathrm{d}x^2}+x\frac{\mathrm{d}y}{\mathrm{d}x}+(x^2-\alpha^2)y = \frac{4(\frac{x}{2})^{\alpha+1}}{\sqrt{\pi}\Gamma(\alpha+\frac{1}{2})}\]

Modified Struve functions

Modified equation:

\[x^2\frac{\mathrm{d}^2y}{\mathrm{d}x^2}+x\frac{\mathrm{d}y}{\mathrm{d}x}-(x^2+\alpha^2)y = \frac{4(\frac{x}{2})^{\alpha+1}}{\sqrt{\pi}\Gamma(\alpha+\frac{1}{2})}\]

Modified Struve functions:

\[\mathrm{L}_\alpha(x) = \left(\frac{x}{2}\right)^{\alpha+1}\sum_{k=0}^\infty \frac{1}{\Gamma(\frac{3}{2}+k)\Gamma(\frac{3}{2}+k+\alpha)}\left(\frac{x}{2}\right)^{2k}\]

Modified Struve function of order 0.

\[x \mapsto \mathrm{L}_0(x)\]

Modified Struve function of order 1.

\[x \mapsto \mathrm{L}_1(x)\]

Misc

Difference between the Bessel \(I_0\) and the Struve \(L_0\) functions.

\[x \mapsto I_0(x) - L_0(x)\]

Difference between the Bessel \(I_1\) and the Struve \(L_1\) functions.

\[x \mapsto I_1(x) - L_1(x)\]

Numeric Stability

\(\exp x-1\) is a typical case where a subtraction can be fatal for accuracy. For example, at \(10^{-13}\) the naive expression is 0.08% off, at \(10^{-15}\) roughly 11% and at \(10^{-18}\) it just returns 0.

\[x \mapsto e^x - 1\]

\[(a,b) \mapsto \sqrt{a^2 + b^2}\]

Trigonometry

The Trig class provides the complete set of fundamental trigonometric functions for both real and complex arguments.