The gamma function is a logarithmic, convex, smooth function operating on complex numbers, which interpolates the factorial function for all nonnegative integers. It is not defined at zero or any negative integer. This is the most common special function and is widely used in many different applications, either by itself or as the main ingredient in the definition of many other functions. The gamma function is used in diverse fields such as quantum physics, astrophysics, statistics, and fluid dynamics.
The gamma function is defined by the improper integral, as follows:
Evaluation of gamma at integer values gives shifted factorials, and that is precisely how the factorials are coded in SciPy.
The scipy.special
module has algorithms to obtain a fast evaluation of the gamma function at any permissible value. It also contains routines to perform evaluation of the most common compositions of the gamma functions appearing in the literature: gammaln
for the natural logarithm of the absolute value of gamma, rgamma
for the value one over gamma, beta
for quotients, and betaln
for the natural logarithm of the latter. We also have implementations of the logarithm of its derivative (psi
).
An obvious application of gamma functions is the ability to perform computations that are virtually impossible for a computer if approached in a direct way. For instance, in statistical applications we often work with ratios of factorials. If these factorials are too large for the precision of a computer, we resort to expressions involving their logarithms instead. Even then, computing ln(a! / b!) can prove to be an impossible task (try, for example, with a = 10**15 and b = a - 10**10). An elegant solution uses the digamma function psi
by an application of the mean value theorem on the ln(gamma(x))
function. With proper estimation, we obtain the excellent approximation (for this case of choice of a and b):
Let's take a look at the following code snippet:
>>> import scipy.special >>> 10**10*scipy.special.psi(10**15)
The output is as follows:
345387763949.10681
18.118.0.145