Math.NET Numerics
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

316 lines
6.8 KiB

(*** hide ***)
#I "../../out/lib/net40"
#r "MathNet.Numerics.dll"
#r "MathNet.Numerics.FSharp.dll"
(**
Special Functions
=================
Factorial
---------
`Factorial(x)`
$$$
x \mapsto x! = \prod_{k=1}^{x} k = \Gamma(x+1)
`FactorialLn(x)`
$$$
x \mapsto \ln x! = \ln\Gamma(x+1)
`Binomial(n,k)`
Binomial Coefficient
$$$
\binom{n}{k} = \mathrm{C}_n^k = \frac{n!}{k! (n-k)!}
`BinomialLn(n,k)`
$$$
\ln \binom{n}{k} = \ln n! - \ln k! - \ln(n-k)!
`Multinomial(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!}
Code Sample:
[lang=csharp]
double x = SpecialFunctions.Factorial(14); // 87178291200.0
double y = SpecialFunctions.Factorial(31); // 8.2228386541779224E+33
Gamma-related functions
-----------------------
#### Gamma
`SpecialFunctions.Gamma(a)`
$$$
\Gamma(a) = \int_0^\infty t^{a-1} e^{-t}\,\mathrm{d}t
`SpecialFunctions.GammaLn(a)`
$$$
\ln\Gamma(a)
#### Incomplete Gamma
`SpecialFunctions.GammaLowerIncomplete(a,x)`
Lower incomplete Gamma function (unregularized).
$$$
\gamma(a,x) = \int_0^x t^{a-1} e^{-t}\,\mathrm{d}t
`SpecialFunctions.GammaUpperIncomplete(a,x)`
Upper incomplete Gamma function (unregularized).
$$$
\Gamma(a,x) = \int_x^\infty t^{a-1} e^{-t}\,\mathrm{d}t
#### Regularized Gamma
`SpecialFunctions.GammaLowerRegularized(a,x)`
Lower regularized incomplete Gamma function.
$$$
\mathrm{P}(a,x) = \frac{\gamma(a,x)}{\Gamma(a)}
`SpecialFunctions.GammaUpperRegularized(a,x)`
Upper regularized incomplete Gamma function.
$$$
\mathrm{Q}(a,x) = \frac{\Gamma(a,x)}{\Gamma(a)}
`SpecialFunctions.GammaLowerRegularizedInv(a, y)`
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
`SpecialFunctions.DiGamma(x)`
$$$
\psi(x) = \frac{\mathrm{d}}{\mathrm{d}x}\ln\Gamma(x)
`SpecialFunctions.DiGammaInv(p)`
Inverse $x$ of the DiGamma function, such that $\psi(x) = p$.
$$$
\psi^{-1}(p)
Euler Beta-related functions
----------------------------
#### Euler Beta
`SpecialFunctions.Beta(a,b)`
$$$
\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)}
`SpecialFunctions.BetaLn(a,b)`
$$$
\ln\mathrm{B}(a,b) = \Gamma(a) + \Gamma(b) - \Gamma(a+b)
#### Incomplete Beta
`SpecialFunctions.BetaIncomplete(a,b,x)`
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
`SpecialFunctions.BetaRegularized(a,b,x)`
Lower incomplete regularized Beta function.
$$$
\mathrm{I}_x(a,b) = \frac{\mathrm{B}(a,b,x)}{\mathrm{B}(a,b)}
Error functions
---------------
#### Error Function
`SpecialFunctions.Erf(x)`
$$$
\mathrm{erf}(x) = \frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\,\mathrm{d}t
`SpecialFunctions.ErfInv(z)`
Inverse $x$ of the Error function, such that $\mathrm{erf}(x) = z$.
$$$
z \mapsto \mathrm{erf}^{-1}(z)
#### Complementary Error function.
`SpecialFunctions.Erfc(x)`
$$$
\mathrm{erfc}(x) = 1-\mathrm{erf}(x) = \frac{2}{\sqrt{\pi}}\int_x^\infty e^{-t^2}\,\mathrm{d}t
`SpecialFunctions.ErfcInv(z)`
Inverse $x$ of the complementary Error function, such that $\mathrm{erfc}(x) = z$.
$$$
z \mapsto \mathrm{erfc}^{-1}(z)
Code Sample:
[lang=csharp]
double erf = SpecialFunctions.Erf(0.9); // 0.7969082124
Sigmoid: Logistic function
--------------------------
`SpecialFunctions.Logistic(x)`
$$$
x \mapsto \frac{1}{1+e^{-x}}
`SpecialFunctions.Logit(y)`
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
----------------
`SpecialFunctions.Harmonic(t)`
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)
`SpecialFunctions.GeneralHarmonic(n, m)`
Generalized harmonic number of order n of m.
$$$
\mathrm{H}_{n,m} = \sum_{k=1}^{n}\frac{1}{k^m}
Bessel and Struve Functions
---------------------------
#### Modified 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:
$$$
\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}
`SpecialFunctions.BesselI0(x)`
Modified or hyperbolic Bessel function of the first kind, order 0.
$$$
x \mapsto \mathrm{I}_0(x)
`SpecialFunctions.BesselI1(x)`
Modified or hyperbolic Bessel function of the first kind, order 1.
$$$
x \mapsto \mathrm{I}_1(x)
`SpecialFunctions.BesselK0(x)`
Modified or hyperbolic Bessel function of the second kind, order 0.
$$$
x \mapsto \mathrm{K}_0(x)
`SpecialFunctions.BesselK0e(x)`
Exponentionally scaled modified Bessel function of the second kind, order 0.
$$$
x \mapsto e^x\mathrm{K}_0(x)
`SpecialFunctions.BesselK1(x)`
Modified or hyperbolic Bessel function of the second kind, order 1.
$$$
x \mapsto \mathrm{K}_1(x)
`SpecialFunctions.BesselK1e(x)`
Exponentionally scaled modified Bessel function of the second kind, order 1.
$$$
x \mapsto e^x\mathrm{K}_1(x)
#### Modified Struve functions
Struve functions are solutions $y(x)$ of the non-homogenous 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:
$$$
\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}
`SpecialFunctions.StruveL0(x)`
Modified Struve function of order 0.
$$$
x \mapsto \mathrm{L}_0(x)
`SpecialFunctions.StruveL1(x)`
Modified Struve function of order 1.
$$$
x \mapsto \mathrm{L}_1(x)
#### Misc
`SpecialFunctions.BesselI0MStruveL0(x)`
Difference between the Bessel $I_0$ and the Struve $L_0$ functions.
$$$
x \mapsto I_0(x) - L_0(x)
`SpecialFunctions.BesselI1MStruveL1(x)`
Difference between the Bessel $I_1$ and the Struve $L_1$ functions.
$$$
x \mapsto I_1(x) - L_1(x)
Numeric Stability
-----------------
`SpecialFunctions.ExponentialMinusOne(power)`
$\exp x-1$ is a typical case where a subtraction can lead to low 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
`SpecialFunctions.Hypotenuse(a, b)`
$$$
(a,b) \mapsto \sqrt{a^2 + b^2}
*)