scipy.special.loggamma#
- scipy.special.loggamma(z, out=None) = <ufunc 'loggamma'>#
Principal branch of the logarithm of the gamma function.
Defined to be \(\log(\Gamma(x))\) for \(x > 0\) and extended to the complex plane by analytic continuation. The function has a single branch cut on the negative real axis.
Added in version 0.18.0.
- Parameters:
- zarray_like
Values in the complex plane at which to compute
loggamma- outndarray, optional
Output array for computed values of
loggamma
- Returns:
- loggammascalar or ndarray
Values of
loggammaat z.
See also
Notes
It is not generally true that \(\log\Gamma(z) = \log(\Gamma(z))\), though the real parts of the functions do agree. The benefit of not defining
loggammaas \(\log(\Gamma(z))\) is that the latter function has a complicated branch cut structure whereasloggammais analytic except for on the negative real axis.The identities
\[\begin{split}\exp(\log\Gamma(z)) &= \Gamma(z) \\ \log\Gamma(z + 1) &= \log(z) + \log\Gamma(z)\end{split}\]make
loggammauseful for working in complex logspace.On the real line
loggammais related togammalnviaexp(loggamma(x + 0j)) = gammasgn(x)*exp(gammaln(x)), up to rounding error.The implementation here is based on [hare1997].
Array API Standard Support
loggammahas support for Python Array API Standard compatible backends in addition to NumPy. 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
For the NumPy backend, this function supports all NumPy ufunc keyword arguments. Other backends may support
out, but none of the other ufunc kwargs.outis typically supported for CuPy and PyTorch, but not currently in cases where SciPy relies on a generic Array API implementation or, for PyTorch on CPU, falls back to the NumPy backend.outis never supported for JAX because JAX arrays are immutable.loggammadoes not currently supportoutfor the PyTorch backend.See Support for the array API standard for more information.
References
[hare1997]D.E.G. Hare, Computing the Principal Branch of log-Gamma, Journal of Algorithms, Volume 25, Issue 2, November 1997, pages 221-236.
Examples
>>> import numpy as np >>> from scipy.special import loggamma, gamma
>>> z = 1.5 + 2j >>> loggamma(z) np.complex128(-1.4991963725850939+0.7332806816909994j)
Verify \(\exp(\log \Gamma(z)) = \Gamma(z)\):
>>> np.exp(loggamma(z)) np.complex128(0.165915108938991+0.14946347326641998j) >>> gamma(z) np.complex128(0.165915108938991+0.14946347326641998j)
Verify the recurrence \(\log \Gamma(z+1) = \log(z) + \log \Gamma(z)\):
>>> loggamma(z + 1) np.complex128(-0.5829056407109388+1.6605758996926108j) >>> np.log(z) + loggamma(z) np.complex128(-0.5829056407109388+1.6605758996926117j)