3 * Gauss hypergeometric function F
9 * double a, b, c, x, y, hyp2f1();
11 * y = hyp2f1( a, b, c, x );
17 * hyp2f1( a, b, c, x ) = F ( a, b; c; x )
21 * - a(a+1)...(a+k) b(b+1)...(b+k) k+1
22 * = 1 + > ----------------------------- x .
23 * - c(c+1)...(c+k) (k+1)!
27 * Tests and escapes for negative integer a, b, or c
28 * Linear transformation if c - a or c - b negative integer
29 * Special case c = a or c = b
30 * Linear transformation for x near +1
31 * Transformation for x < -0.5
32 * Psi function expansion if x > 0.5 and c - a - b integer
33 * Conditionally, a recurrence on c to make c-a-b > 0
35 * |x| > 1 is rejected.
37 * The parameters a, b, c are considered to be integer
38 * valued if they are within 1.0e-14 of the nearest integer
39 * (1.0e-13 for IEEE arithmetic).
44 * Relative error (-1 < x < 1):
45 * arithmetic domain # trials peak rms
46 * IEEE -1,7 230000 1.2e-11 5.2e-14
48 * Several special cases also tested with a, b, c in
53 * A "partial loss of precision" message is printed if
54 * the internally estimated relative error exceeds 1^-12.
55 * A "singularity" message is printed on overflow or
56 * in cases not addressed (such as x < -1).
63 Cephes Math Library Release 2.8: June, 2000
64 Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier
90 #define ETHRESH 1.0e-12
93 extern double fabs ( double );
94 extern double pow ( double, double );
95 extern double round ( double );
96 extern double gamma ( double );
97 extern double log ( double );
98 extern double exp ( double );
99 extern double psi ( double );
100 static double hyt2f1(double, double, double, double, double *);
101 static double hys2f1(double, double, double, double, double *);
102 double hyp2f1(double, double, double, double);
104 double fabs(), pow(), round(), gamma(), log(), exp(), psi();
105 static double hyt2f1();
106 static double hys2f1();
109 extern double MAXNUM, MACHEP;
111 double hyp2f1( a, b, c, x )
115 double p, q, r, s, y, ax;
116 double ia, ib, ic, id, err;
123 ia = round(a); /* nearest integer to a */
128 if( fabs(a-ia) < EPS ) /* a is a negative integer */
134 if( fabs(b-ib) < EPS ) /* b is a negative integer */
140 if( fabs(b-c) < EPS ) /* b = c */
142 y = pow( s, -a ); /* s to the -a power */
145 if( fabs(a-c) < EPS ) /* a = c */
147 y = pow( s, -b ); /* s to the -b power */
156 ic = round(c); /* nearest integer to c */
157 if( fabs(c-ic) < EPS ) /* c is a negative integer */
159 /* check if termination before explosion */
160 if( (flag & 1) && (ia > ic) )
162 if( (flag & 2) && (ib > ic) )
168 if( flag ) /* function is a polynomial */
171 if( ax > 1.0 ) /* series diverges */
175 ia = round(p); /* nearest integer to c-a */
176 if( (ia <= 0.0) && (fabs(p-ia) < EPS) ) /* negative int c - a */
180 ib = round(r); /* nearest integer to c-b */
181 if( (ib <= 0.0) && (fabs(r-ib) < EPS) ) /* negative int c - b */
185 id = round(d); /* nearest integer to d */
188 /* Thanks to Christian Burger <BURGER@DMRHRZ11.HRZ.Uni-Marburg.DE>
189 * for reporting a bug here. */
190 if( fabs(ax-1.0) < EPS ) /* |x| == 1.0 */
194 if( flag & 12 ) /* negative int c-a or c-b */
203 y = gamma(c)*gamma(d)/(gamma(p)*gamma(r));
212 /* Conditionally make d > 0 by recurrence on c
217 /* Try the power series first */
218 y = hyt2f1( a, b, c, x, &err );
221 /* Apply the recurrence if power series fails */
225 d2 = hyp2f1(a,b,e,x);
226 d1 = hyp2f1(a,b,e+1.0,x);
228 for( i=0; i<aid; i++ )
231 y = (e*(r-(2.0*e-q)*x)*d2 + (e-a)*(e-b)*x*d1)/(e*r*s);
241 goto hypf; /* negative integer c-a or c-b */
244 y = hyt2f1( a, b, c, x, &err );
250 mtherr( "hyp2f1", PLOSS );
251 /* printf( "Estimated err = %.2e\n", err ); */
255 /* The transformation for c-a or c-b negative integer
259 y = pow( s, d ) * hys2f1( c-a, c-b, c, x, &err );
264 mtherr( "hyp2f1", OVERFLOW );
273 /* Apply transformations for |x| near 1
274 * then call the power series
276 static double hyt2f1( a, b, c, x, loss )
280 double p, q, r, s, t, y, d, err, err1;
281 double ax, id, d1, d2, e, y1;
289 y = pow( s, -a ) * hys2f1( a, c-b, c, -x/s, &err );
292 y = pow( s, -b ) * hys2f1( c-a, b, c, -x/s, &err );
298 id = round(d); /* nearest integer to d */
302 if( fabs(d-id) > EPS ) /* test for integer c-a-b */
304 /* Try the power series first */
305 y = hys2f1( a, b, c, x, &err );
308 /* If power series fails, then apply AMS55 #15.3.6 */
309 q = hys2f1( a, b, 1.0-d, s, &err );
310 q *= gamma(d) /(gamma(c-a) * gamma(c-b));
311 r = pow(s,d) * hys2f1( c-a, c-b, d+1.0, s, &err1 );
312 r *= gamma(-d)/(gamma(a) * gamma(b));
315 q = fabs(q); /* estimate cancellation error */
319 err += err1 + (MACHEP*r)/y;
326 /* Psi function expansion, AMS55 #15.3.10, #15.3.11, #15.3.12 */
345 y = psi(1.0) + psi(1.0+e) - psi(a+d1) - psi(b+d1) - ax;
348 p = (a+d1) * (b+d1) * s / gamma(e+2.0); /* Poch for t=1 */
352 r = psi(1.0+t) + psi(1.0+t+e) - psi(a+t+d1)
356 p *= s * (a+t+d1) / (t+1.0);
357 p *= (b+t+d1) / (t+1.0+e);
360 while( fabs(q/y) > EPS );
365 y *= gamma(c)/(gamma(a)*gamma(b));
376 for( i=1; i<aid; i++ )
379 p *= s * (a+t+d2) * (b+t+d2) / r;
386 y1 *= gamma(e) * p / (gamma(a+d1) * gamma(b+d1));
388 y *= p / (gamma(a+d2) * gamma(b+d2));
392 q = pow( s, id ); /* s to the id power */
405 /* Use defining power series if no special cases */
406 y = hys2f1( a, b, c, x, &err );
417 /* Defining power series expansion of Gauss hypergeometric function */
419 static double hys2f1( a, b, c, x, loss )
421 double *loss; /* estimates loss of significance */
423 double f, g, h, k, m, s, u, umax;
442 u = u * ((f+k) * (g+k) * x / ((h+k) * m));
444 k = fabs(u); /* remember largest term summed */
448 if( ++i > 10000 ) /* should never happen */
454 while( fabs(u/s) > MACHEP );
456 /* return estimated relative error */
457 *loss = (MACHEP*umax)/fabs(s) + (MACHEP*i);