3 * Confluent hypergeometric function
9 * double a, b, x, y, hyperg();
11 * y = hyperg( 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 * DEC 0,30 2000 1.2e-15 1.3e-16
45 21800 max = 1.4200E-14 rms = 1.0841E-15 ave = -5.3640E-17
47 25500 max = 1.2759e-14 rms = 3.7155e-16 ave = 1.5384e-18
48 * IEEE 0,30 30000 1.8e-14 1.1e-15
50 * Larger errors can be observed when b is near a negative
51 * integer or zero. Certain combinations of arguments yield
52 * serious cancellation error in the power series summation
53 * and also are not in the region of near convergence of the
54 * asymptotic series. An error message is printed if the
55 * self-estimated relative error is greater than 1.0e-12.
63 Cephes Math Library Release 2.8: June, 2000
64 Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
70 extern double exp ( double );
71 extern double log ( double );
72 extern double gamma ( double );
73 extern double lgam ( double );
74 extern double fabs ( double );
75 double hyp2f0 ( double, double, double, int, double * );
76 static double hy1f1p(double, double, double, double *);
77 static double hy1f1a(double, double, double, double *);
78 double hyperg (double, double, double);
80 double exp(), log(), gamma(), lgam(), fabs(), hyp2f0();
81 static double hy1f1p();
82 static double hy1f1a();
85 extern double MAXNUM, MACHEP;
87 double hyperg( a, b, x)
90 double asum, psum, acanc, pcanc, temp;
92 /* See if a Kummer transformation will help */
94 if( fabs(temp) < 0.001 * fabs(a) )
95 return( exp(x) * hyperg( temp, b, -x ) );
98 psum = hy1f1p( a, b, x, &pcanc );
103 /* try asymptotic series */
105 asum = hy1f1a( a, b, x, &acanc );
108 /* Pick the result with less estimated error */
117 if( pcanc > 1.0e-12 )
118 mtherr( "hyperg", PLOSS );
126 /* Power series summation for confluent hypergeometric function */
129 static double hy1f1p( a, b, x, err )
133 double n, a0, sum, t, u, temp;
134 double an, bn, maxt, pcanc;
137 /* set up for power series summation */
149 if( bn == 0 ) /* check bn first since if both */
151 mtherr( "hyperg", SING );
152 return( MAXNUM ); /* an and bn are zero it is */
154 if( an == 0 ) /* a singularity */
158 u = x * ( an / (bn * n) );
160 /* check for blowup */
162 if( (temp > 1.0 ) && (maxt > (MAXNUM/temp)) )
164 pcanc = 1.0; /* estimate 100% error */
174 if( (maxt/fabs(sum)) > 1.0e17 )
187 /* estimate error due to roundoff and cancellation */
190 maxt *= MACHEP; /* this way avoids multiply overflow */
191 pcanc = fabs( MACHEP * n + maxt );
202 /* asymptotic formula for hypergeometric function:
206 * | (b) ( -------- 2f0( a, 1+a-b, -1/x )
213 * + -------- 2f0( b-a, 1-a, 1/x ) )
218 static double hy1f1a( a, b, x, err )
222 double h1, h2, t, u, temp, acanc, asum, err1, err2;
230 temp = log( fabs(x) );
231 t = x + temp * (a-b);
241 h1 = hyp2f0( a, a-b+1, -1.0/x, 1, &err1 );
243 temp = exp(u) / gamma(b-a);
247 h2 = hyp2f0( b-a, 1.0-a, 1.0/x, 2, &err2 );
250 temp = exp(t) / gamma(a);
252 temp = exp( t - lgam(a) );
262 acanc = fabs(err1) + fabs(err2);
276 acanc *= 30.0; /* fudge factor, since error of asymptotic formula
277 * often seems this much larger than advertised */
288 double hyp2f0( a, b, x, type, err )
290 int type; /* determines what converging factor to use */
293 double a0, alast, t, tlast, maxt;
294 double n, an, bn, u, sum, temp;
313 u = an * (bn * x / n);
315 /* check for blowup */
317 if( (temp > 1.0 ) && (maxt > (MAXNUM/temp)) )
323 /* terminating condition for asymptotic series */
328 sum += alast; /* the sum is one term behind */
343 pdone: /* series converged! */
345 /* estimate error due to roundoff and cancellation */
346 *err = fabs( MACHEP * (n + maxt) );
351 ndone: /* series did not converge */
353 /* The following "Converging factors" are supposed to improve accuracy,
354 * but do not actually seem to accomplish very much. */
359 switch( type ) /* "type" given as subroutine argument */
362 alast *= ( 0.5 + (0.125 + 0.25*b - 0.5*a + 0.25*x - 0.25*n)/x );
366 alast *= 2.0/3.0 - b + 2.0*a + x - n;
373 /* estimate error due to roundoff, cancellation, and nonconvergence */
374 *err = MACHEP * (n + maxt) + fabs ( a0 );
381 /* series blew up: */
384 mtherr( "hyperg", TLOSS );