Search code examples
pythonscipy

incomplete gamma function in python?


the scipy.special.gammainc can not take negative values for the first argument. Are there any other implementations that could in python? I can do a manual integration for sure but I'd like to know if there are good alternatives that already exist.

Correct result: 1 - Gamma[-1,1] = 0.85

Use Scipy: scipy.special.gammainc(-1, 1) = 0

Thanks.


Solution

  • I typically reach for mpmath whenever I need special functions and I'm not too concerned about performance. (Although its performance in many cases is pretty good anyway.)

    For example:

    >>> 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')