scipy.special.gammaln
- scipy.special.gammaln(x, out=None) = <ufunc 'gammaln'>
Logarithm of the absolute value of the gamma function.
Defined as
Logarithm of the absolute value of the gamma function.
Defined as
where is the gamma function. For more details on the gamma function, see [dlmf].
Real argument
Optional output array for the function results
Values of the log of the absolute value of gamma
It is the same function as the Python standard library function
math.lgamma.
When used in conjunction with gammasgn, this function is useful
for working in logspace on the real axis without having to deal
with complex numbers via the relation exp(gammaln(x)) =
gammasgn(x) * gamma(x).
Array API Standard Support
gammaln has experimental support for Python Array API Standard compatible
backends in addition to NumPy. Please consider testing these features
by setting an environment variable SCIPY_ARRAY_API=1 and providing
CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following
combinations of backend and device (or other capability) are supported.
Library | CPU | GPU |
|---|---|---|
NumPy | ✅ | n/a |
CuPy | n/a | ✅ |
PyTorch | ✅ | ✅ |
JAX | ✅ | ✅ |
Dask | ✅ | n/a |
See Support for the array API standard for more information.
NIST Digital Library of Mathematical Functions https://dlmf.nist.gov/5
>>> import numpy as np
>>> import scipy.special as sc
It has two positive zeros.
>>> sc.gammaln([1, 2])
array([0., 0.])
It has poles at nonpositive integers.
>>> sc.gammaln([0, -1, -2, -3, -4])
array([inf, inf, inf, inf, inf])
It asymptotically approaches x * log(x) (Stirling’s formula).
>>> x = np.array([1e10, 1e20, 1e40, 1e80])
>>> sc.gammaln(x)
array([2.20258509e+11, 4.50517019e+21, 9.11034037e+41, 1.83206807e+82])
>>> x * np.log(x)
array([2.30258509e+11, 4.60517019e+21, 9.21034037e+41, 1.84206807e+82])