不完整的伽马函数不接受复数值输入

Med*_*ata 5 python scipy gamma-function

我正在尝试计算像这篇文章中定义的上不完全伽马函数。如果我使用

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))
Run Code Online (Sandbox Code Playgroud)

哪里z是一个复杂的向量我得到一个错误

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''
Run Code Online (Sandbox Code Playgroud)

是否有替代函数来进行计算?当我尝试使用 WolframAlpha 的Gamma函数执行此操作时,似乎没有问题。

小智 4

当 SciPy 不足以处理棘手的特殊函数时,mpmath通常会发挥作用。比较

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

与Wolfram Alpha相同。

用Python编写,比SciPy慢;它也不是矢量化的。所以用你的数据它会像

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

0请注意,我避免作为参数传递(它是一个极点)。的返回类型mpmath.gammainc是它自己的mpc对象类型,但它可以像上面一样转换回 NumPy。