Python 中不完整的伽马函数



scipy.special.gammainc不能为第一个参数取负值。还有其他可以在 python 中实现的吗?我肯定可以进行手动集成,但我想知道是否已经存在好的替代方案。

Correct result: 1 - Gamma[-1,1] = 0.85
Use Scipy: scipy.special.gammainc(-1, 1) = 0

谢谢。

每当我需要特殊函数并且我不太关心性能时,我通常会使用 mpmath。 (尽管在许多情况下它的性能还是相当不错的。

例如:

>>> import mpmath
>>> mpmath.gammainc(-1,1)
mpf('0.14849550677592205')
>>> 1-mpmath.gammainc(-1,1)
mpf('0.85150449322407795')
>>> mpmath.mp.dps = 50 # arbitrary precision!
>>> 1-mpmath.gammainc(-1,1)
mpf('0.85150449322407795208164000529866078158523616237514084')

我只是遇到了同样的问题,最终在 a<0 时使用了函数的递归关系。http://en.wikipedia.org/wiki/Incomplete_gamma_function#Properties

另请注意,scipy 函数 gammainc 和 gammaincc 给出正则化形式 Gamma(a,x)/Gamma(a)

> 在 2021 年仍然是一个问题,他们在 scipy 中仍然没有改进这一点。 尤其令人沮丧的是,scipy甚至没有提供上下不完整伽马函数的非正则化版本。 我最终还使用了 mpmath ,它使用自己的数据类型(这里mpf mpmath 浮动 - 支持任意精度)。 为了快速为上下不完整的Gamma函数准备一些东西,该函数适用于numpy数组,并且其行为类似于人们从评估这些积分时所期望的那样,我想出了以下内容:

import numpy as np
from mpmath import gammainc
"""
In both functinos below a is a float and z is a numpy.array.
"""
def gammainc_up(a,z):
    return np.asarray([gammainc(a, zi, regularized=False)
                       for zi in z]).astype(float)
def gammainc_low(a,z):
    return np.asarray([gamainc(a, 0, zi, regularized=False)
                       for zi in z]).astype(float)

再次注意,这是针对非正则化函数(DLMF 中的等式 8.2.1 和 8.2.2),正则化函数(等式 8.2.3 和 8.2.4)可以通过设置关键字 regularized=Truempmath中得到

最新更新