Special Functions

Gamma Functions

gamma(x)

Computes the Gamma function \(\Gamma(x)\), subject to \(x\) not being a negative integer or zero. That is, it is computing

\[\Gamma(x) = \int_0^{\infty}{ t^{x-1} e^{-t} dt}\]

The value is computed using the real Lanczos method. The maximum value of \(x\) such that \(\Gamma(x)\) is not considered an overflow is 171.

>>> gamma(5)
= 24
>>> gamma(5.5)
= 52.34277778455351
lngamma(x)

Compute the logarithm of the Gamma function, \(\log(\Gamma(x))\), subject to \(x\) not being a negative integer or zero. For \(x < 0\) the real part of \(\log(\Gamma(x))\) is returned, which is equivalent to \(\log(|\Gamma(x)|)\). The function is computed using the real Lanczos method.

>>> lngamma(5)
= 3.178053830347945
>>> gamma(5.5)
= 3.957813967618717
gammastar(x)

Compute the regulated Gamma function \(\Gamma^*(x)\) for \(x > 0\). The regulated gamma function is given by,

\[\Gamma^*(x) = \frac{\Gamma(x)}{\sqrt{2\pi}x^{x-1/2} e^{-x}}\]
>>> gammastar(5)
= 1.016783985827808
>>> gammastar(5.5)
= 1.015250086943625
gammainv(x)

Compute the reciprocal of the Gamma function \(1/\Gamma(x)\) using the real Lanczos method.

>>> gammainv(5)
= 1/24
>>> gammainv(5.5)
= 0.01910483245876
fact(x)

Computes the factorial \(x!\). The factorial is related to the Gamma function by \(x! = \Gamma(x+1)\). The maximum value of \(x\) such that \(x!\) is not considered an overflow is \(170\).

>>> fact(5)
= 120
>>> 5!
= 120
>>> fact(5.5)
= 287.8852778150443
>>> 5.5!
= 287.8852778150443
dfact(x)

Computes the double factorial \(x!! = x(x-2)(x-4)\dots\). The maximum value of \(x\) such that \(x!!\) is not considered an overflow is \(297\).

>>> dfact(10)
= 3840
>>> 10!!
= 3840
lnfact(x)

Computes the logarithm of the factorial of \(x\), \(\log(x!)\). This algorithm is faster than computing \(\log(x!)\) explicitly.

>>> lnfact(5)
= 4.787491742782046
lndfact(x)

Computes the logarithm of the double factorial of \(x\), \(\log(x!!)\). This algorithm is faster than computing \(\log(x!!)\) explicitly.

>>> lndfact(10)
= 8.253227645581772
choose(n, r)

Computes the combinatorial factor \({n \choose r} = \frac{n!}{r!(n-r)!}\). Aliases: comb

>>> choose(10, 3)
= 120
>>> comb(10, 3)
= 120
permute(n, r)

Computes the permutation factor \(n^{(r)} = \frac{n!}{(n-r)!}\). Aliases: perm

>>> permute(10, 3)
= 720
>>> perm(10, 3)
= 720
lnpermute(n, r)

Computes the logarithm of the permutation factor \(\ln(n^{(r)}) = \ln(n!) - \ln((n-r)!)\). Aliases: lmperm

>>> lnpermute(10, 3)
= 6.579251212010101
>>> lnperm(10, 3)
= 6.579251212010101
taylorcoeff(n, x)

Computes the Taylor coefficient \(\frac{x^n}{n!}\) for \(x \ge 0, n \ge 0\)

>>> taylorcoeff(3, 4)
= 32/3

Pochhammer Symbol

poch(a, x)

Compute the Pochhammer symbol \((a)_x = \frac{\Gamma(a + x)}{\Gamma(a)}\). The Pochhammer symbol is also known as the Apell symbol and sometimes written as \((a,x)\). When \(a\) and \(a + x\) are negative integers or zero, the limiting value of the ratio is returned.

>>> poch(3, 4)
= 360
lnpoch(a, x)

Computes the logarithm of the Pochhammer symbol, \(\log((a)_x) = \log(\frac{\Gamma(a + x)}{\Gamma(a)})\).

>>> lnpoch(3, 4)
= 5.886104031450156
pochrel(a, x)

Computes the relative Pochhammer symbol \(\frac{(a)_x - 1}{x}\) where \((a)_x = \frac{\Gamma(a + x)}{\Gamma(a)}\).

>>> pochrel(3, 4)
= 359/4

Incomplete Gamma Functions

gamma_inc(a, x)

These functions compute the unnormalized incomplete Gamma Function

\[\Gamma(a,x) = \int_x^\infty t^{(a-1)} \exp(-t) dt\]

for a real and \(x \ge 0\). Aliases: gammainc

>>> gamma_inc(3, 4)
= 0.4762066111070874
>>> gammainc(3, 4)
= 0.4762066111070874
gamma_inc_Q(a, x)

These functions compute the normalized incomplete Gamma Function

\[Q(a,x) = \frac{1}{\Gamma(a)} \int_x^\infty t^{(a-1)} \exp(-t) dt\]

for a real and \(a > 0, x \ge 0\). Aliases: gammaincq

>>> gamma_inc_Q(3, 4)
= 0.2381033055535437
>>> gammaincq(3, 4)
= 0.2381033055535437
gamma_inc_P(a, x)

These functions compute the complementary normalized incomplete Gamma Function

\[P(a, x) = 1 - Q(a,x) = 1 - \frac{1}{\Gamma(a)} \int_x^\infty t^{(a-1)} \exp(-t) dt\]

for a real and \(a > 0, x \ge 0\). Aliases: gammaincp

>>> gamma_inc_P(3, 4)
= 0.7618966944464536
>>> gammaincp(3, 4)
= 0.7618966944464536

Beta Functions

Beta(a, b)

Computes the Beta Function, \(B(a,b) = \frac{\Gamma(a)\Gamma(b)}{\Gamma(a+b)}\) subject to \(a\) and \(b\) not being negative integers.

>>> Beta(3, 4)
= 1/60
lnBeta(a, b)

Computes the logarithm of the Beta Function, \(\ln(B(a,b)) = \ln(\Gamma(a)) + \ln(\Gamma(b)) - \ln(\Gamma(a+b))\) subject to \(a\) and \(b\) not being negative integers.

>>> lnBeta(10, 15)
= -16.79168073529216

Incomplete Beta Function

Betainc(a, b, x)

Computes the normalized incomplete Beta function \(I_x(a,b) = \frac{B_x(a,b)}{B(a,b)}\) where

\[B_x(a,b) = \int_0^x t^{a-1} (1-t)^{b-1} dt\]

for \(0 \le x \le 1\). For \(a > 0, b > 0\) the value is computed using a continued fraction expansion. For all other values it is computed using the relation

\[I_x(a,b,x) = \frac{1}{a} x^a {}_2F_1(a,1-b,a+1,x)/B(a,b)\]
>>> Betainc(1, 2, 0.5)
= 3/4