In R, incomplete gamma function with complex input?

1.3k views Asked by At

Incomplete gamma functions can be calculated in R with pgamma, or with gamma_inc_Q from library(gsl), or with gammainc from library(expint). However, all of these functions take only real input.

I need an implementation of the incomplete gamma function which will take complex input. Specifically, I have an integer for the first argument, and a complex number for the second argument (the limit in the integral).

This function is well-defined for complex inputs (see Wikipedia), and I've been calculating it in Mathematica. It doesn't seem to be built into R though, and I don't see it in any libraries.

So, can anyone suggest a shorter path to doing these calculations, than looking up an algorithm, implementing it in C, and writing an R interface?

(If I do have to implement it myself, here's the only algorithm for complex inputs that I've found: Kostlan & Gokhman 1987)

1

There are 1 answers

0
Stéphane Laurent On

Here is an implementation, assuming you want the lower incomplete gamma function. I've compared a couple of values with Wolfram and they match.

library(CharFun)

incgamma <- function(s,z){
  z^s * exp(-z) * hypergeom1F1(z, 1, s+1) / s
}

Perhaps the evaluation fails for a large s.


EDIT

Looks like CharFun has been removed from CRAN. You can use IncGamma in HypergeoMat:

> library(HypergeoMat)
> IncGamma(m=50, 2+2i, 5-6i)
[1] 0.3841221+0.3348439i

The result is the same on Wolfram.