scipy.special.psi#

scipy.special.psi(z, out=None) = <ufunc 'psi'>#

The digamma function.

The logarithmic derivative of the gamma function evaluated at z.

\[\psi(z) = \frac{d}{dz}\log\Gamma(z) = \frac{\Gamma'(z)}{\Gamma(z)}\]
Parameters:
zarray_like

Real or complex argument.

outndarray, optional

Array for the computed values of psi.

Returns:
digammascalar or ndarray

Computed values of psi.

Notes

For large values not close to the negative real axis, psi is computed using the asymptotic series (5.11.2) from [1]. For small arguments not close to the negative real axis, the recurrence relation (5.5.2) from [1] is used until the argument is large enough to use the asymptotic series. For values close to the negative real axis, the reflection formula (5.5.4) from [1] is used first. Note that psi has a family of zeros on the negative real axis which occur between the poles at nonpositive integers. Around the zeros the reflection formula suffers from cancellation and the implementation loses precision. The sole positive zero and the first negative zero, however, are handled separately by precomputing series expansions using [2], so the function should maintain full accuracy around the origin.

Array API Standard Support

psi has 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. out is 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. out is never supported for JAX because JAX arrays are immutable.

See Support for the array API standard for more information.

References

[1] (1,2,3)

NIST Digital Library of Mathematical Functions https://dlmf.nist.gov/5

[2]

Fredrik Johansson and others. “mpmath: a Python library for arbitrary-precision floating-point arithmetic” (Version 0.19) http://mpmath.org/

Examples

>>> from scipy.special import psi
>>> z = 3 + 4j
>>> psi(z)
(1.55035981733341+1.0105022091860445j)

Verify psi(z) = psi(z + 1) - 1/z:

>>> psi(z + 1) - 1/z
(1.55035981733341+1.0105022091860445j)