Medulla Oblongata
Medulla Oblongata

Reputation: 3961

Incomplete gamma function does not accept complex value inputs

I'm trying to compute the upper incomplete gamma function defined like in this post. If I use

from scipy.special import gamma,gammainc
from numpy import linspace

a = 0
z = (2+3j)*np.linspace(0,10)
gamma(a)*(1-gammainc(a,z))

where z is a complex vector I get an error

TypeError: ufunc 'gammainc' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Is there an alternative function to do the calculation? There doesn't seem to be a problem when I try to do this with WolframAlpha's Gamma function.

Upvotes: 6

Views: 1286

Answers (1)

user6655984
user6655984

Reputation:

When SciPy is not enough for tricky special functions, mpmath usually comes to the rescue. Compare

>>> mpmath.gammainc(0, 2+3j)
mpc(real='-0.024826207944199364', imag='0.020316674911044622')

and the same from Wolfram Alpha.

Being written in Python, it is slower than SciPy; it is also not vectorized. So with your data it would work like

import mpmath
result = np.array([mpmath.gammainc(w) for w in z[1:]], dtype=np.complex)

Note that I avoid passing 0 as the argument (it's a pole). The return type of mpmath.gammainc is its own mpc object type, but it can be converted back to NumPy as above.

Upvotes: 5

Related Questions