How to compute Gauss hypergeometric function 2F1(a,b;c;z)?

I am looking for C++ source code to compute Gauss hypergeometric function 2F1(a,b;c;z)?
http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric2F1/

Can anyone assist?

Thanks in advance.
Note convergence restrictions: abs(x) < 1 and c not a negative integer or zero.

If you want complex z then replace type by complex<double> and use analytic continuation if you stray outside the range abs(z) < 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
using namespace std;

// Note convergence restrictions: abs(x) < 1 and c not a negative integer or zero

double hypergeometric( double a, double b, double c, double x )
{
   const double TOLERANCE = 1.0e-10;
   double term = a * b * x / c;
   double value = 1.0 + term;
   int n = 1;

   while ( abs( term ) > TOLERANCE )
   {
      a++, b++, c++, n++;
      term *= a * b * x / c / n;
      value += term;
   }

   return value;
}


//======================================================================

int main()
{
   double a = 2.0, b = 3.0, c = 4.0;
   double x = 0.5;
   cout << "F( " << a << ", " << b << "; " << c << ", " << x << " ) = " << hypergeometric( a, b, c, x ) << '\n';
}


F( 2, 3; 4, 0.5 ) = 2.72894

https://www.wolframalpha.com/input/?i=Hypergeometric2F1%5B2,3,4,0.5%5D

Last edited on
Topic archived. No new replies allowed.