# Computation of hypergeometric function in R

I'm having tremendous difficulty evaluating 2f1(a,b;c;z) with the hypergeo package in R. In my case, values of a,b,c are always positive real numbers. Even so, the hypergeo function is incredibly sensitive to their values. I am not looking for extreme precision; I can use Excel to get a rough estimation of the Guass hypergeometric that is fine for my purposes.

Any suggestions for an implementation in R that will give a fast, error-free, if not super accurate Gaussian hypergeometric computation of positive real numbers with a wide range of values? Thanks.

Edit: it seems there is far more code for this in MATLAB than R. Any thoughts as to why?

-
 i would have thought a good approach would be to find the largest term in the hypergeometric sum and truncate around this term. this way you can factor out the largest term and work with terms less than 1 in the sum. you could also use laplace method on the integral representation (suitably parameterised to avoid boundary issues). – probabilityislogic Aug 1 '12 at 2:16

Unless you need to evaluate the Gauss hypergeometric function for complex values of the parameters or the variable, it is better to use Robin Hankin's gsl package.

Based on my experience I also recommend to only evaluate the Gauss hypergeometric function for a value of the variable lying in $[0,1]$, and use a transformation formula for values in $]-\infty, 0]$.

library(gsl)
Gauss2F1 <- function(a,b,c,x){
if(x>=0 & x<1){
hyperg_2F1(a,b,c,x)
}else{
hyperg_2F1(c-a,b,c,1-1/(1-x))/(1-x)^b
}
}


### Update

Here is my alternative implementation with the gmp package (at least, for fun)

-
Thanks for a simple, clean solution to a not so simple problem! There isn't a lot of documentation on gauss hypergeometric functions for R so this approach is valuable. – benrolls Aug 1 '12 at 13:39
@benrolls I have experienced the Gauss hypergeometric function with the hypergeo package, the gsl package, another method implemented by myself, and Mathematica too. I guarantee that the solution I give above is very performant, I have never find a bug when using it. – Stéphane Laurent Aug 1 '12 at 14:31