mrpy.base.special.gamma¶
-
mrpy.base.special.
gamma
(z)¶ Gamma function.
Note
This is exactly as defined by mpmath, but modified to take
array_like
arguments, and return results with type float.Notes
Computes the gamma function, Gamma(x). The gamma function is a shifted version of the ordinary factorial, satisfying Gamma(n) = (n-1)! for integers n > 0. More generally, it is defined by
\[\Gamma(x) = \int_0^{\infty} t^{x-1} e^{-t}\, dt\]for any real or complex x with Re(x) > 0 and for Re(x) < 0 by analytic continuation.
Examples
Basic values and limits:
>>> from mpmath import * >>> mp.dps = 15; mp.pretty = True >>> for k in range(1, 6): ... print("%s %s" % (k, gamma(k))) ... 1 1.0 2 1.0 3 2.0 4 6.0 5 24.0 >>> gamma(inf) +inf >>> gamma(0) Traceback (most recent call last): ... ValueError: gamma function pole
The gamma function of a half-integer is a rational multiple of sqrt{pi}:
>>> gamma(0.5), sqrt(pi) (1.77245385090552, 1.77245385090552) >>> gamma(1.5), sqrt(pi)/2 (0.886226925452758, 0.886226925452758)
We can check the integral definition:
>>> gamma(3.5) 3.32335097044784 >>> quad(lambda t: t**2.5*exp(-t), [0,inf]) 3.32335097044784
gamma()
supports arbitrary-precision evaluation and complex arguments:>>> mp.dps = 50 >>> gamma(sqrt(3)) 0.91510229697308632046045539308226554038315280564184 >>> mp.dps = 25 >>> gamma(2j) (0.009902440080927490985955066 - 0.07595200133501806872408048j)
Arguments can also be large. Note that the gamma function grows very quickly:
>>> mp.dps = 15 >>> gamma(10**20) 1.9328495143101e+1956570551809674817225