Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / tr1 / hypergeometric.tcc
blob5e6902f98ed149600aa14fd9a8089dcfda61c9be
1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file tr1/hypergeometric.tcc
32  *  This is an internal header file, included by other library headers.
33  *  You should not attempt to use it directly.
34  */
37 // ISO C++ 14882 TR1: 5.2  Special functions
40 // Written by Edward Smith-Rowland based:
41 //   (1) Handbook of Mathematical Functions,
42 //       ed. Milton Abramowitz and Irene A. Stegun,
43 //       Dover Publications,
44 //       Section 6, pp. 555-566
45 //   (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
47 #ifndef _GLIBCXX_TR1_HYPERGEOMETRIC_TCC
48 #define _GLIBCXX_TR1_HYPERGEOMETRIC_TCC 1
50 namespace std
52 namespace tr1
55   // [5.2] Special functions
57   // Implementation-space details.
58   namespace __detail
59   {
61     /**
62      *   @brief This routine returns the confluent hypergeometric function
63      *          by series expansion.
64      * 
65      *   @f[
66      *     _1F_1(a;c;x) = \frac{\Gamma(c)}{\Gamma(a)}
67      *                      \sum_{n=0}^{\infty}
68      *                      \frac{\Gamma(a+n)}{\Gamma(c+n)}
69      *                      \frac{x^n}{n!}
70      *   @f]
71      * 
72      *   If a and b are integers and a < 0 and either b > 0 or b < a then the
73      *   series is a polynomial with a finite number of terms.  If b is an integer
74      *   and b <= 0 the confluent hypergeometric function is undefined.
75      *
76      *   @param  __a  The "numerator" parameter.
77      *   @param  __c  The "denominator" parameter.
78      *   @param  __x  The argument of the confluent hypergeometric function.
79      *   @return  The confluent hypergeometric function.
80      */
81     template<typename _Tp>
82     _Tp
83     __conf_hyperg_series(const _Tp __a, const _Tp __c, const _Tp __x)
84     {
85       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
87       _Tp __term = _Tp(1);
88       _Tp __Fac = _Tp(1);
89       const unsigned int __max_iter = 100000;
90       unsigned int __i;
91       for (__i = 0; __i < __max_iter; ++__i)
92         {
93           __term *= (__a + _Tp(__i)) * __x
94                   / ((__c + _Tp(__i)) * _Tp(1 + __i));
95           if (std::abs(__term) < __eps)
96             {
97               break;
98             }
99           __Fac += __term;
100         }
101       if (__i == __max_iter)
102         std::__throw_runtime_error(__N("Series failed to converge "
103                                        "in __conf_hyperg_series."));
105       return __Fac;
106     }
109     /**
110      *  @brief  Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
111      *          by an iterative procedure described in
112      *          Luke, Algorithms for the Computation of Mathematical Functions.
113      *
114      *  Like the case of the 2F1 rational approximations, these are 
115      *  probably guaranteed to converge for x < 0, barring gross    
116      *  numerical instability in the pre-asymptotic regime.         
117      */
118     template<typename _Tp>
119     _Tp
120     __conf_hyperg_luke(const _Tp __a, const _Tp __c, const _Tp __xin)
121     {
122       const _Tp __big = std::pow(std::numeric_limits<_Tp>::max(), _Tp(0.16L));
123       const int __nmax = 20000;
124       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
125       const _Tp __x  = -__xin;
126       const _Tp __x3 = __x * __x * __x;
127       const _Tp __t0 = __a / __c;
128       const _Tp __t1 = (__a + _Tp(1)) / (_Tp(2) * __c);
129       const _Tp __t2 = (__a + _Tp(2)) / (_Tp(2) * (__c + _Tp(1)));
130       _Tp __F = _Tp(1);
131       _Tp __prec;
133       _Tp __Bnm3 = _Tp(1);
134       _Tp __Bnm2 = _Tp(1) + __t1 * __x;
135       _Tp __Bnm1 = _Tp(1) + __t2 * __x * (_Tp(1) + __t1 / _Tp(3) * __x);
137       _Tp __Anm3 = _Tp(1);
138       _Tp __Anm2 = __Bnm2 - __t0 * __x;
139       _Tp __Anm1 = __Bnm1 - __t0 * (_Tp(1) + __t2 * __x) * __x
140                  + __t0 * __t1 * (__c / (__c + _Tp(1))) * __x * __x;
142       int __n = 3;
143       while(1)
144         {
145           _Tp __npam1 = _Tp(__n - 1) + __a;
146           _Tp __npcm1 = _Tp(__n - 1) + __c;
147           _Tp __npam2 = _Tp(__n - 2) + __a;
148           _Tp __npcm2 = _Tp(__n - 2) + __c;
149           _Tp __tnm1  = _Tp(2 * __n - 1);
150           _Tp __tnm3  = _Tp(2 * __n - 3);
151           _Tp __tnm5  = _Tp(2 * __n - 5);
152           _Tp __F1 =  (_Tp(__n - 2) - __a) / (_Tp(2) * __tnm3 * __npcm1);
153           _Tp __F2 =  (_Tp(__n) + __a) * __npam1
154                    / (_Tp(4) * __tnm1 * __tnm3 * __npcm2 * __npcm1);
155           _Tp __F3 = -__npam2 * __npam1 * (_Tp(__n - 2) - __a)
156                    / (_Tp(8) * __tnm3 * __tnm3 * __tnm5
157                    * (_Tp(__n - 3) + __c) * __npcm2 * __npcm1);
158           _Tp __E  = -__npam1 * (_Tp(__n - 1) - __c)
159                    / (_Tp(2) * __tnm3 * __npcm2 * __npcm1);
161           _Tp __An = (_Tp(1) + __F1 * __x) * __Anm1
162                    + (__E + __F2 * __x) * __x * __Anm2 + __F3 * __x3 * __Anm3;
163           _Tp __Bn = (_Tp(1) + __F1 * __x) * __Bnm1
164                    + (__E + __F2 * __x) * __x * __Bnm2 + __F3 * __x3 * __Bnm3;
165           _Tp __r = __An / __Bn;
167           __prec = std::abs((__F - __r) / __F);
168           __F = __r;
170           if (__prec < __eps || __n > __nmax)
171             break;
173           if (std::abs(__An) > __big || std::abs(__Bn) > __big)
174             {
175               __An   /= __big;
176               __Bn   /= __big;
177               __Anm1 /= __big;
178               __Bnm1 /= __big;
179               __Anm2 /= __big;
180               __Bnm2 /= __big;
181               __Anm3 /= __big;
182               __Bnm3 /= __big;
183             }
184           else if (std::abs(__An) < _Tp(1) / __big
185                 || std::abs(__Bn) < _Tp(1) / __big)
186             {
187               __An   *= __big;
188               __Bn   *= __big;
189               __Anm1 *= __big;
190               __Bnm1 *= __big;
191               __Anm2 *= __big;
192               __Bnm2 *= __big;
193               __Anm3 *= __big;
194               __Bnm3 *= __big;
195             }
197           ++__n;
198           __Bnm3 = __Bnm2;
199           __Bnm2 = __Bnm1;
200           __Bnm1 = __Bn;
201           __Anm3 = __Anm2;
202           __Anm2 = __Anm1;
203           __Anm1 = __An;
204         }
206       if (__n >= __nmax)
207         std::__throw_runtime_error(__N("Iteration failed to converge "
208                                        "in __conf_hyperg_luke."));
210       return __F;
211     }
214     /**
215      *   @brief  Return the confluent hypogeometric function
216      *           @f$ _1F_1(a;c;x) @f$.
217      * 
218      *   @todo  Handle b == nonpositive integer blowup - return NaN.
219      *
220      *   @param  __a  The "numerator" parameter.
221      *   @param  __c  The "denominator" parameter.
222      *   @param  __x  The argument of the confluent hypergeometric function.
223      *   @return  The confluent hypergeometric function.
224      */
225     template<typename _Tp>
226     inline _Tp
227     __conf_hyperg(const _Tp __a, const _Tp __c, const _Tp __x)
228     {
229 #if _GLIBCXX_USE_C99_MATH_TR1
230       const _Tp __c_nint = std::tr1::nearbyint(__c);
231 #else
232       const _Tp __c_nint = static_cast<int>(__c + _Tp(0.5L));
233 #endif
234       if (__isnan(__a) || __isnan(__c) || __isnan(__x))
235         return std::numeric_limits<_Tp>::quiet_NaN();
236       else if (__c_nint == __c && __c_nint <= 0)
237         return std::numeric_limits<_Tp>::infinity();
238       else if (__a == _Tp(0))
239         return _Tp(1);
240       else if (__c == __a)
241         return std::exp(__x);
242       else if (__x < _Tp(0))
243         return __conf_hyperg_luke(__a, __c, __x);
244       else
245         return __conf_hyperg_series(__a, __c, __x);
246     }
249     /**
250      *   @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
251      *   by series expansion.
252      * 
253      *   The hypogeometric function is defined by
254      *   @f[
255      *     _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
256      *                      \sum_{n=0}^{\infty}
257      *                      \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
258      *                      \frac{x^n}{n!}
259      *   @f]
260      * 
261      *   This works and it's pretty fast.
262      *
263      *   @param  __a  The first "numerator" parameter.
264      *   @param  __a  The second "numerator" parameter.
265      *   @param  __c  The "denominator" parameter.
266      *   @param  __x  The argument of the confluent hypergeometric function.
267      *   @return  The confluent hypergeometric function.
268      */
269     template<typename _Tp>
270     _Tp
271     __hyperg_series(const _Tp __a, const _Tp __b,
272                     const _Tp __c, const _Tp __x)
273     {
274       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
276       _Tp __term = _Tp(1);
277       _Tp __Fabc = _Tp(1);
278       const unsigned int __max_iter = 100000;
279       unsigned int __i;
280       for (__i = 0; __i < __max_iter; ++__i)
281         {
282           __term *= (__a + _Tp(__i)) * (__b + _Tp(__i)) * __x
283                   / ((__c + _Tp(__i)) * _Tp(1 + __i));
284           if (std::abs(__term) < __eps)
285             {
286               break;
287             }
288           __Fabc += __term;
289         }
290       if (__i == __max_iter)
291         std::__throw_runtime_error(__N("Series failed to converge "
292                                        "in __hyperg_series."));
294       return __Fabc;
295     }
298     /**
299      *   @brief  Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
300      *           by an iterative procedure described in
301      *           Luke, Algorithms for the Computation of Mathematical Functions.
302      */
303     template<typename _Tp>
304     _Tp
305     __hyperg_luke(const _Tp __a, const _Tp __b, const _Tp __c,
306                   const _Tp __xin)
307     {
308       const _Tp __big = std::pow(std::numeric_limits<_Tp>::max(), _Tp(0.16L));
309       const int __nmax = 20000;
310       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
311       const _Tp __x  = -__xin;
312       const _Tp __x3 = __x * __x * __x;
313       const _Tp __t0 = __a * __b / __c;
314       const _Tp __t1 = (__a + _Tp(1)) * (__b + _Tp(1)) / (_Tp(2) * __c);
315       const _Tp __t2 = (__a + _Tp(2)) * (__b + _Tp(2))
316                      / (_Tp(2) * (__c + _Tp(1)));
318       _Tp __F = _Tp(1);
320       _Tp __Bnm3 = _Tp(1);
321       _Tp __Bnm2 = _Tp(1) + __t1 * __x;
322       _Tp __Bnm1 = _Tp(1) + __t2 * __x * (_Tp(1) + __t1 / _Tp(3) * __x);
324       _Tp __Anm3 = _Tp(1);
325       _Tp __Anm2 = __Bnm2 - __t0 * __x;
326       _Tp __Anm1 = __Bnm1 - __t0 * (_Tp(1) + __t2 * __x) * __x
327                  + __t0 * __t1 * (__c / (__c + _Tp(1))) * __x * __x;
329       int __n = 3;
330       while (1)
331         {
332           const _Tp __npam1 = _Tp(__n - 1) + __a;
333           const _Tp __npbm1 = _Tp(__n - 1) + __b;
334           const _Tp __npcm1 = _Tp(__n - 1) + __c;
335           const _Tp __npam2 = _Tp(__n - 2) + __a;
336           const _Tp __npbm2 = _Tp(__n - 2) + __b;
337           const _Tp __npcm2 = _Tp(__n - 2) + __c;
338           const _Tp __tnm1  = _Tp(2 * __n - 1);
339           const _Tp __tnm3  = _Tp(2 * __n - 3);
340           const _Tp __tnm5  = _Tp(2 * __n - 5);
341           const _Tp __n2 = __n * __n;
342           const _Tp __F1 = (_Tp(3) * __n2 + (__a + __b - _Tp(6)) * __n
343                          + _Tp(2) - __a * __b - _Tp(2) * (__a + __b))
344                          / (_Tp(2) * __tnm3 * __npcm1);
345           const _Tp __F2 = -(_Tp(3) * __n2 - (__a + __b + _Tp(6)) * __n
346                          + _Tp(2) - __a * __b) * __npam1 * __npbm1
347                          / (_Tp(4) * __tnm1 * __tnm3 * __npcm2 * __npcm1);
348           const _Tp __F3 = (__npam2 * __npam1 * __npbm2 * __npbm1
349                          * (_Tp(__n - 2) - __a) * (_Tp(__n - 2) - __b))
350                          / (_Tp(8) * __tnm3 * __tnm3 * __tnm5
351                          * (_Tp(__n - 3) + __c) * __npcm2 * __npcm1);
352           const _Tp __E  = -__npam1 * __npbm1 * (_Tp(__n - 1) - __c)
353                          / (_Tp(2) * __tnm3 * __npcm2 * __npcm1);
355           _Tp __An = (_Tp(1) + __F1 * __x) * __Anm1
356                    + (__E + __F2 * __x) * __x * __Anm2 + __F3 * __x3 * __Anm3;
357           _Tp __Bn = (_Tp(1) + __F1 * __x) * __Bnm1
358                    + (__E + __F2 * __x) * __x * __Bnm2 + __F3 * __x3 * __Bnm3;
359           const _Tp __r = __An / __Bn;
361           const _Tp __prec = std::abs((__F - __r) / __F);
362           __F = __r;
364           if (__prec < __eps || __n > __nmax)
365             break;
367           if (std::abs(__An) > __big || std::abs(__Bn) > __big)
368             {
369               __An   /= __big;
370               __Bn   /= __big;
371               __Anm1 /= __big;
372               __Bnm1 /= __big;
373               __Anm2 /= __big;
374               __Bnm2 /= __big;
375               __Anm3 /= __big;
376               __Bnm3 /= __big;
377             }
378           else if (std::abs(__An) < _Tp(1) / __big
379                 || std::abs(__Bn) < _Tp(1) / __big)
380             {
381               __An   *= __big;
382               __Bn   *= __big;
383               __Anm1 *= __big;
384               __Bnm1 *= __big;
385               __Anm2 *= __big;
386               __Bnm2 *= __big;
387               __Anm3 *= __big;
388               __Bnm3 *= __big;
389             }
391           ++__n;
392           __Bnm3 = __Bnm2;
393           __Bnm2 = __Bnm1;
394           __Bnm1 = __Bn;
395           __Anm3 = __Anm2;
396           __Anm2 = __Anm1;
397           __Anm1 = __An;
398         }
400       if (__n >= __nmax)
401         std::__throw_runtime_error(__N("Iteration failed to converge "
402                                        "in __hyperg_luke."));
404       return __F;
405     }
408     /**
409      *  @brief  Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$ by the reflection
410      *          formulae in Abramowitz & Stegun formula 15.3.6 for d = c - a - b not integral
411      *          and formula 15.3.11 for d = c - a - b integral.
412      *          This assumes a, b, c != negative integer.
413      *
414      *   The hypogeometric function is defined by
415      *   @f[
416      *     _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
417      *                      \sum_{n=0}^{\infty}
418      *                      \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
419      *                      \frac{x^n}{n!}
420      *   @f]
421      *
422      *   The reflection formula for nonintegral @f$ d = c - a - b @f$ is:
423      *   @f[
424      *     _2F_1(a,b;c;x) = \frac{\Gamma(c)\Gamma(d)}{\Gamma(c-a)\Gamma(c-b)}
425      *                            _2F_1(a,b;1-d;1-x)
426      *                    + \frac{\Gamma(c)\Gamma(-d)}{\Gamma(a)\Gamma(b)}
427      *                            _2F_1(c-a,c-b;1+d;1-x)
428      *   @f]
429      *
430      *   The reflection formula for integral @f$ m = c - a - b @f$ is:
431      *   @f[
432      *     _2F_1(a,b;a+b+m;x) = \frac{\Gamma(m)\Gamma(a+b+m)}{\Gamma(a+m)\Gamma(b+m)}
433      *                        \sum_{k=0}^{m-1} \frac{(m+a)_k(m+b)_k}{k!(1-m)_k}
434      *                      - 
435      *   @f]
436      */
437     template<typename _Tp>
438     _Tp
439     __hyperg_reflect(const _Tp __a, const _Tp __b, const _Tp __c,
440                      const _Tp __x)
441     {
442       const _Tp __d = __c - __a - __b;
443       const int __intd  = std::floor(__d + _Tp(0.5L));
444       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
445       const _Tp __toler = _Tp(1000) * __eps;
446       const _Tp __log_max = std::log(std::numeric_limits<_Tp>::max());
447       const bool __d_integer = (std::abs(__d - __intd) < __toler);
449       if (__d_integer)
450         {
451           const _Tp __ln_omx = std::log(_Tp(1) - __x);
452           const _Tp __ad = std::abs(__d);
453           _Tp __F1, __F2;
455           _Tp __d1, __d2;
456           if (__d >= _Tp(0))
457             {
458               __d1 = __d;
459               __d2 = _Tp(0);
460             }
461           else
462             {
463               __d1 = _Tp(0);
464               __d2 = __d;
465             }
467           const _Tp __lng_c = __log_gamma(__c);
469           //  Evaluate F1.
470           if (__ad < __eps)
471             {
472               //  d = c - a - b = 0.
473               __F1 = _Tp(0);
474             }
475           else
476             {
478               bool __ok_d1 = true;
479               _Tp __lng_ad, __lng_ad1, __lng_bd1;
480               try
481                 {
482                   __lng_ad = __log_gamma(__ad);
483                   __lng_ad1 = __log_gamma(__a + __d1);
484                   __lng_bd1 = __log_gamma(__b + __d1);
485                 }
486               catch(...)
487                 {
488                   __ok_d1 = false;
489                 }
491               if (__ok_d1)
492                 {
493                   /* Gamma functions in the denominator are ok.
494                    * Proceed with evaluation.
495                    */
496                   _Tp __sum1 = _Tp(1);
497                   _Tp __term = _Tp(1);
498                   _Tp __ln_pre1 = __lng_ad + __lng_c + __d2 * __ln_omx
499                                 - __lng_ad1 - __lng_bd1;
501                   /* Do F1 sum.
502                    */
503                   for (int __i = 1; __i < __ad; ++__i)
504                     {
505                       const int __j = __i - 1;
506                       __term *= (__a + __d2 + __j) * (__b + __d2 + __j)
507                               / (_Tp(1) + __d2 + __j) / __i * (_Tp(1) - __x);
508                       __sum1 += __term;
509                     }
511                   if (__ln_pre1 > __log_max)
512                     std::__throw_runtime_error(__N("Overflow of gamma functions "
513                                                    "in __hyperg_luke."));
514                   else
515                     __F1 = std::exp(__ln_pre1) * __sum1;
516                 }
517               else
518                 {
519                   //  Gamma functions in the denominator were not ok.
520                   //  So the F1 term is zero.
521                   __F1 = _Tp(0);
522                 }
523             } // end F1 evaluation
525           // Evaluate F2.
526           bool __ok_d2 = true;
527           _Tp __lng_ad2, __lng_bd2;
528           try
529             {
530               __lng_ad2 = __log_gamma(__a + __d2);
531               __lng_bd2 = __log_gamma(__b + __d2);
532             }
533           catch(...)
534             {
535               __ok_d2 = false;
536             }
538           if (__ok_d2)
539             {
540               //  Gamma functions in the denominator are ok.
541               //  Proceed with evaluation.
542               const int __maxiter = 2000;
543               const _Tp __psi_1 = -__numeric_constants<_Tp>::__gamma_e();
544               const _Tp __psi_1pd = __psi(_Tp(1) + __ad);
545               const _Tp __psi_apd1 = __psi(__a + __d1);
546               const _Tp __psi_bpd1 = __psi(__b + __d1);
548               _Tp __psi_term = __psi_1 + __psi_1pd - __psi_apd1
549                              - __psi_bpd1 - __ln_omx;
550               _Tp __fact = _Tp(1);
551               _Tp __sum2 = __psi_term;
552               _Tp __ln_pre2 = __lng_c + __d1 * __ln_omx
553                             - __lng_ad2 - __lng_bd2;
555               // Do F2 sum.
556               int __j;
557               for (__j = 1; __j < __maxiter; ++__j)
558                 {
559                   //  Values for psi functions use recurrence; Abramowitz & Stegun 6.3.5
560                   const _Tp __term1 = _Tp(1) / _Tp(__j)
561                                     + _Tp(1) / (__ad + __j);
562                   const _Tp __term2 = _Tp(1) / (__a + __d1 + _Tp(__j - 1))
563                                     + _Tp(1) / (__b + __d1 + _Tp(__j - 1));
564                   __psi_term += __term1 - __term2;
565                   __fact *= (__a + __d1 + _Tp(__j - 1))
566                           * (__b + __d1 + _Tp(__j - 1))
567                           / ((__ad + __j) * __j) * (_Tp(1) - __x);
568                   const _Tp __delta = __fact * __psi_term;
569                   __sum2 += __delta;
570                   if (std::abs(__delta) < __eps * std::abs(__sum2))
571                     break;
572                 }
573               if (__j == __maxiter)
574                 std::__throw_runtime_error(__N("Sum F2 failed to converge "
575                                                "in __hyperg_reflect"));
577               if (__sum2 == _Tp(0))
578                 __F2 = _Tp(0);
579               else
580                 __F2 = std::exp(__ln_pre2) * __sum2;
581             }
582           else
583             {
584               // Gamma functions in the denominator not ok.
585               // So the F2 term is zero.
586               __F2 = _Tp(0);
587             } // end F2 evaluation
589           const _Tp __sgn_2 = (__intd % 2 == 1 ? -_Tp(1) : _Tp(1));
590           const _Tp __F = __F1 + __sgn_2 * __F2;
592           return __F;
593         }
594       else
595         {
596           //  d = c - a - b not an integer.
598           //  These gamma functions appear in the denominator, so we
599           //  catch their harmless domain errors and set the terms to zero.
600           bool __ok1 = true;
601           _Tp __sgn_g1ca = _Tp(0), __ln_g1ca = _Tp(0);
602           _Tp __sgn_g1cb = _Tp(0), __ln_g1cb = _Tp(0);
603           try
604             {
605               __sgn_g1ca = __log_gamma_sign(__c - __a);
606               __ln_g1ca = __log_gamma(__c - __a);
607               __sgn_g1cb = __log_gamma_sign(__c - __b);
608               __ln_g1cb = __log_gamma(__c - __b);
609             }
610           catch(...)
611             {
612               __ok1 = false;
613             }
615           bool __ok2 = true;
616           _Tp __sgn_g2a = _Tp(0), __ln_g2a = _Tp(0);
617           _Tp __sgn_g2b = _Tp(0), __ln_g2b = _Tp(0);
618           try
619             {
620               __sgn_g2a = __log_gamma_sign(__a);
621               __ln_g2a = __log_gamma(__a);
622               __sgn_g2b = __log_gamma_sign(__b);
623               __ln_g2b = __log_gamma(__b);
624             }
625           catch(...)
626             {
627               __ok2 = false;
628             }
630           const _Tp __sgn_gc = __log_gamma_sign(__c);
631           const _Tp __ln_gc = __log_gamma(__c);
632           const _Tp __sgn_gd = __log_gamma_sign(__d);
633           const _Tp __ln_gd = __log_gamma(__d);
634           const _Tp __sgn_gmd = __log_gamma_sign(-__d);
635           const _Tp __ln_gmd = __log_gamma(-__d);
637           const _Tp __sgn1 = __sgn_gc * __sgn_gd  * __sgn_g1ca * __sgn_g1cb;
638           const _Tp __sgn2 = __sgn_gc * __sgn_gmd * __sgn_g2a  * __sgn_g2b;
640           _Tp __pre1, __pre2;
641           if (__ok1 && __ok2)
642             {
643               _Tp __ln_pre1 = __ln_gc + __ln_gd  - __ln_g1ca - __ln_g1cb;
644               _Tp __ln_pre2 = __ln_gc + __ln_gmd - __ln_g2a  - __ln_g2b
645                             + __d * std::log(_Tp(1) - __x);
646               if (__ln_pre1 < __log_max && __ln_pre2 < __log_max)
647                 {
648                   __pre1 = std::exp(__ln_pre1);
649                   __pre2 = std::exp(__ln_pre2);
650                   __pre1 *= __sgn1;
651                   __pre2 *= __sgn2;
652                 }
653               else
654                 {
655                   std::__throw_runtime_error(__N("Overflow of gamma functions "
656                                                  "in __hyperg_reflect"));
657                 }
658             }
659           else if (__ok1 && !__ok2)
660             {
661               _Tp __ln_pre1 = __ln_gc + __ln_gd - __ln_g1ca - __ln_g1cb;
662               if (__ln_pre1 < __log_max)
663                 {
664                   __pre1 = std::exp(__ln_pre1);
665                   __pre1 *= __sgn1;
666                   __pre2 = _Tp(0);
667                 }
668               else
669                 {
670                   std::__throw_runtime_error(__N("Overflow of gamma functions "
671                                                  "in __hyperg_reflect"));
672                 }
673             }
674           else if (!__ok1 && __ok2)
675             {
676               _Tp __ln_pre2 = __ln_gc + __ln_gmd - __ln_g2a - __ln_g2b
677                             + __d * std::log(_Tp(1) - __x);
678               if (__ln_pre2 < __log_max)
679                 {
680                   __pre1 = _Tp(0);
681                   __pre2 = std::exp(__ln_pre2);
682                   __pre2 *= __sgn2;
683                 }
684               else
685                 {
686                   std::__throw_runtime_error(__N("Overflow of gamma functions "
687                                                  "in __hyperg_reflect"));
688                 }
689             }
690           else
691             {
692               __pre1 = _Tp(0);
693               __pre2 = _Tp(0);
694               std::__throw_runtime_error(__N("Underflow of gamma functions "
695                                              "in __hyperg_reflect"));
696             }
698           const _Tp __F1 = __hyperg_series(__a, __b, _Tp(1) - __d,
699                                            _Tp(1) - __x);
700           const _Tp __F2 = __hyperg_series(__c - __a, __c - __b, _Tp(1) + __d,
701                                            _Tp(1) - __x);
703           const _Tp __F = __pre1 * __F1 + __pre2 * __F2;
705           return __F;
706         }
707     }
710     /**
711      *   @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$.
712      *
713      *   The hypogeometric function is defined by
714      *   @f[
715      *     _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
716      *                      \sum_{n=0}^{\infty}
717      *                      \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
718      *                      \frac{x^n}{n!}
719      *   @f]
720      *
721      *   @param  __a  The first "numerator" parameter.
722      *   @param  __a  The second "numerator" parameter.
723      *   @param  __c  The "denominator" parameter.
724      *   @param  __x  The argument of the confluent hypergeometric function.
725      *   @return  The confluent hypergeometric function.
726      */
727     template<typename _Tp>
728     inline _Tp
729     __hyperg(const _Tp __a, const _Tp __b, const _Tp __c, const _Tp __x)
730     {
731 #if _GLIBCXX_USE_C99_MATH_TR1
732       const _Tp __a_nint = std::tr1::nearbyint(__a);
733       const _Tp __b_nint = std::tr1::nearbyint(__b);
734       const _Tp __c_nint = std::tr1::nearbyint(__c);
735 #else
736       const _Tp __a_nint = static_cast<int>(__a + _Tp(0.5L));
737       const _Tp __b_nint = static_cast<int>(__b + _Tp(0.5L));
738       const _Tp __c_nint = static_cast<int>(__c + _Tp(0.5L));
739 #endif
740       const _Tp __toler = _Tp(1000) * std::numeric_limits<_Tp>::epsilon();
741       if (std::abs(__x) >= _Tp(1))
742         std::__throw_domain_error(__N("Argument outside unit circle "
743                                       "in __hyperg."));
744       else if (__isnan(__a) || __isnan(__b)
745             || __isnan(__c) || __isnan(__x))
746         return std::numeric_limits<_Tp>::quiet_NaN();
747       else if (__c_nint == __c && __c_nint <= _Tp(0))
748         return std::numeric_limits<_Tp>::infinity();
749       else if (std::abs(__c - __b) < __toler || std::abs(__c - __a) < __toler)
750         return std::pow(_Tp(1) - __x, __c - __a - __b);
751       else if (__a >= _Tp(0) && __b >= _Tp(0) && __c >= _Tp(0)
752             && __x >= _Tp(0) && __x < _Tp(0.995L))
753         return __hyperg_series(__a, __b, __c, __x);
754       else if (std::abs(__a) < _Tp(10) && std::abs(__b) < _Tp(10))
755         {
756           //  For integer a and b the hypergeometric function is a finite polynomial.
757           if (__a < _Tp(0)  &&  std::abs(__a - __a_nint) < __toler)
758             return __hyperg_series(__a_nint, __b, __c, __x);
759           else if (__b < _Tp(0)  &&  std::abs(__b - __b_nint) < __toler)
760             return __hyperg_series(__a, __b_nint, __c, __x);
761           else if (__x < -_Tp(0.25L))
762             return __hyperg_luke(__a, __b, __c, __x);
763           else if (__x < _Tp(0.5L))
764             return __hyperg_series(__a, __b, __c, __x);
765           else
766             if (std::abs(__c) > _Tp(10))
767               return __hyperg_series(__a, __b, __c, __x);
768             else
769               return __hyperg_reflect(__a, __b, __c, __x);
770         }
771       else
772         return __hyperg_luke(__a, __b, __c, __x);
773     }
775   } // namespace std::tr1::__detail
779 #endif // _GLIBCXX_TR1_HYPERGEOMETRIC_TCC