Files to compute the Gauss hypergeometric function 2F1 in C (using VC++ 2005 Express) in the complex plane for complex parameters, free for download and with no warranty. 

In a posting posting at http://www.math.utexas.edu/pipermail/maxima/2006/000126.html Fateman cites Gosper with an interesting approach to compute the hypergeometric function 2F1.

I used that to produce a compiled version (double precision only), which can be achieved from Excel for example. The approach however can be used within Maple as well.

The idea is, that with his way the linear transformations given in Abramowitz & Stegun "Handbook of  Mathematical Functions" need to be applied only once to cover the whole complex plane: up to 1 transformation one can use either Gosper's recursion or already lives in the unit circle with radius 1/2 or somewhat larger (where the hypergeometric series already converges quite fast).

Most troubles are given for exceptional parameters, where the linear transformations in Abramowitz & Stegun are not simply applicable and would involve expensive (and less exact) series in 4 Digamma functions (given in A&S of course). However choosing some appropriate presentation (which can be found by reading the proof in Lebedev's book and massaging them in Maple) one can treat this by numerical differentiating w.r.t. parameters. And choosing enough points one can take a stepsize large enough to keep errors small.

Note that in any of the subsequent codes the parameters should be of modest size (say below 12 or similar or roughly 'such that n! can be done by multiplication'): already Taylor series around 0 will have bad errors beyond that for small variables and even for polynomial cases one can not expect too much for unhumble degrees. Even if evaluating series and recursions with more care sooner or later one would have to use extented precision (I want to guess).

And the code can not cover all variants for functions, which can be written as 2F1, but want their own implementations (certainly the elementary ones).

Doing all that Maple is of real help, it would be a mess to make all things by hand. And of course it is the very tool for testing (but here: not for generating the actual code).

I enclose a short overview (as sheet), test sheets and the complete C(++) source as VC2005 project for the different versions.

Feed back is welcome for both more testing and porting it to a non-proprietary compiler (say: Dev-C++).

hypergeometric_2F1_using_a_recipe_of_Gosper.mws.pdf (60 kB)
hypergeometric_2F1_using_a_recipe_of_Gosper.mws.zip (15 kB)
   Short overview as pdf or zipped Maple worksheet.

Variant: Snow's method (Nov 2009)
This is a totally different quadratic transformation and solution. I found it through scanning an article of Peter Potts, he refers to
Chester Snow (though I think he did eleborate it himself, at least essentials, but assigns it to him). Complexity / Simplicity is like
Gosper's method and results are comparable in errors or exactness (in my implementation, which is like in the 'slim' version),
but not always identical. So consider it as a possible cross check. Not bad to have one.

Here is a note for the approach, since it goes a different route: using Snow's series.pdf  (286 kB)

hyp2f1_slim_snow_sources.zip (16 kB)
    Sources (for VC project).

hyp2f1_slim_snow.mws.pdf (68 kB)
hyp2f1_slim_snow.mws.zip (18 kB)
    Detailled tests using a Maple sheet (given as pdf). As the others it contains some ranting about using the differential equation
    for testing exactness and looks at the branching point as well. Test cases are the same.
       

Version 2.0 slim (Nov 2009)
This is a slim version of version 2.0 (no regularized hypergeometric though). Essentially I have reduced 'everything' by applying
Cauchy's formula. Results seem to be just as good or bad as version 2.0. So what ...

hyp2f1_sources_v2.0_slim.zip (11 kB)
    Sources (for VC project).

Version 2.0 (Nov 2009)
I  removed some coding errors and improved the behaviour, if the parameters do not exactly satisfy the nasty integer relations,
but are only close to that: in that case Cauchy's formula is used.  The (reciprocal) Gamma function has been improved as well.
Approaching the unit circle as been changed (superseding parts of 'recipe of Gosper', see the code comments), but I have not
documented it in detail ... 
Also contains the regularized 2F1 = 2F1(a,b,c, z)/Gamma(c).

At least here is a note, how Cauchy's formula is applied: regularization_of_2F1_and_Cauchy_formula.pdf  (24 kB)

hyp2f1_sources_v2.0.zip (19 kB)
    Sources (for VC project).

test_hyp2F1_v2.0.mws.pdf (68 kB)
test_hyp2F1_v2.0.mws.zip (18 kB)
    Detailled tests using a Maple sheet (given as pdf as well), as always that considers Forrey's real tests as well.    .

Version 1.1 (2009, outdated)
I  removed some coding errors and improved the behaviour, if the parameters do not exactly satisfy the nasty integer relations,
but are only close to that: in that case Cauchy's formula is used.  The (reciprocal) Gamma function has been improved as well.

Also contains the regularized
2F1 = 2F1(a,b,c, z)/Gamma(c).

hyp2f1_sources_v1.1.zip (19 kB)
    Sources (for VC project).

test_hyp2F1_v1.1.mws.pdf (68 kB)
test_hyp2F1_v1.1.mws.zip (17 kB)
    Detailled tests using a Maple sheet (given as pdf as well).

Version 1.0 (2007, outdated)

hyp2f1_sources_and_examples.zip (113 kB)
    Sources (for VC project), a compiled DLL and an Excel sheet for usage from VBA.

test_hyp2F1.mws.pdf (68 kB)
test_hyp2F1.mws.zip (14 kB)
    Detailled tests using a Maple sheet (given as pdf as well to read it anyway).


   up 


feedback is welcome & if you want to drop me any comment ... 


 

Nov 2009: uploaded several  improved versions and variants

22 Apr 2007: uploaded
 

This software is provided "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.