3 * Confluent hypergeometric function
9 * float a, b, x, y, hypergf();
11 * y = hypergf( a, b, x );
17 * Computes the confluent hypergeometric function
21 * F ( a,b;x ) = 1 + ---- + --------- + ...
24 * Many higher transcendental functions are special cases of
27 * As is evident from the formula, b must not be a negative
28 * integer or zero unless a is an integer with 0 >= a > b.
30 * The routine attempts both a direct summation of the series
31 * and an asymptotic expansion. In each case error due to
32 * roundoff, cancellation, and nonconvergence is estimated.
33 * The result with smaller estimated error is returned.
39 * Tested at random points (a, b, x), all three variables
40 * ranging from 0 to 30.
42 * arithmetic domain # trials peak rms
43 * IEEE 0,5 10000 6.6e-7 1.3e-7
44 * IEEE 0,30 30000 1.1e-5 6.5e-7
46 * Larger errors can be observed when b is near a negative
47 * integer or zero. Certain combinations of arguments yield
48 * serious cancellation error in the power series summation
49 * and also are not in the region of near convergence of the
50 * asymptotic series. An error message is printed if the
51 * self-estimated relative error is greater than 1.0e-3.
59 Cephes Math Library Release 2.1: November, 1988
60 Copyright 1984, 1987, 1988 by Stephen L. Moshier
61 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
66 extern float MAXNUMF, MACHEPF;
68 #define fabsf(x) ( (x) < 0 ? -(x) : (x) )
72 float hyp2f0f(float, float, float, int, float *);
73 static float hy1f1af(float, float, float, float *);
74 static float hy1f1pf(float, float, float, float *);
75 float logf(float), gammaf(float), lgamf(float);
77 float expf(), hyp2f0f();
78 float logf(), gammaf(), lgamf();
79 static float hy1f1pf(), hy1f1af();
83 float hypergf( float aa, float bb, float xx )
85 float hypergf( aa, bb, xx)
89 float a, b, x, asum, psum, acanc, pcanc, temp;
95 /* See if a Kummer transformation will help */
97 if( fabsf(temp) < 0.001 * fabsf(a) )
98 return( expf(x) * hypergf( temp, b, -x ) );
100 psum = hy1f1pf( a, b, x, &pcanc );
105 /* try asymptotic series */
107 asum = hy1f1af( a, b, x, &acanc );
110 /* Pick the result with less estimated error */
120 mtherr( "hyperg", PLOSS );
128 /* Power series summation for confluent hypergeometric function */
132 static float hy1f1pf( float aa, float bb, float xx, float *err )
134 static float hy1f1pf( aa, bb, xx, err )
139 float a, b, x, n, a0, sum, t, u, temp;
140 float an, bn, maxt, pcanc;
145 /* set up for power series summation */
157 if( bn == 0 ) /* check bn first since if both */
159 mtherr( "hypergf", SING );
160 return( MAXNUMF ); /* an and bn are zero it is */
162 if( an == 0 ) /* a singularity */
166 u = x * ( an / (bn * n) );
168 /* check for blowup */
170 if( (temp > 1.0 ) && (maxt > (MAXNUMF/temp)) )
172 pcanc = 1.0; /* estimate 100% error */
182 if( (maxt/fabsf(sum)) > 1.0e17 )
195 /* estimate error due to roundoff and cancellation */
198 maxt *= MACHEPF; /* this way avoids multiply overflow */
199 pcanc = fabsf( MACHEPF * n + maxt );
210 /* asymptotic formula for hypergeometric function:
214 * | (b) ( -------- 2f0( a, 1+a-b, -1/x )
221 * + -------- 2f0( b-a, 1-a, 1/x ) )
227 static float hy1f1af( float aa, float bb, float xx, float *err )
229 static float hy1f1af( aa, bb, xx, err )
234 float a, b, x, h1, h2, t, u, temp, acanc, asum, err1, err2;
245 temp = logf( fabsf(x) );
246 t = x + temp * (a-b);
256 h1 = hyp2f0f( a, a-b+1, -1.0/x, 1, &err1 );
258 temp = expf(u) / gammaf(b-a);
262 h2 = hyp2f0f( b-a, 1.0-a, 1.0/x, 2, &err2 );
265 temp = expf(t) / gammaf(a);
267 temp = expf( t - lgamf(a) );
277 acanc = fabsf(err1) + fabsf(err2);
284 acanc *= fabsf(temp);
289 acanc /= fabsf(asum);
291 acanc *= 30.0; /* fudge factor, since error of asymptotic formula
292 * often seems this much larger than advertised */
304 float hyp2f0f(float aa, float bb, float xx, int type, float *err)
306 float hyp2f0f( aa, bb, xx, type, err )
308 int type; /* determines what converging factor to use */
312 float a, b, x, a0, alast, t, tlast, maxt;
313 float n, an, bn, u, sum, temp;
335 u = an * (bn * x / n);
337 /* check for blowup */
339 if( (temp > 1.0 ) && (maxt > (MAXNUMF/temp)) )
345 /* terminating condition for asymptotic series */
350 sum += alast; /* the sum is one term behind */
362 while( t > MACHEPF );
365 pdone: /* series converged! */
367 /* estimate error due to roundoff and cancellation */
368 *err = fabsf( MACHEPF * (n + maxt) );
373 ndone: /* series did not converge */
375 /* The following "Converging factors" are supposed to improve accuracy,
376 * but do not actually seem to accomplish very much. */
381 switch( type ) /* "type" given as subroutine argument */
384 alast *= ( 0.5 + (0.125 + 0.25*b - 0.5*a + 0.25*x - 0.25*n)/x );
388 alast *= 2.0/3.0 - b + 2.0*a + x - n;
395 /* estimate error due to roundoff, cancellation, and nonconvergence */
396 *err = MACHEPF * (n + maxt) + fabsf( a0 );
403 /* series blew up: */
406 mtherr( "hypergf", TLOSS );