hypergeometric.tcc

Go to the documentation of this file.
00001 // Special functions -*- C++ -*-
00002 
00003 // Copyright (C) 2006, 2007, 2008
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 //
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 //
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file tr1/hypergeometric.tcc
00032  *  This is an internal header file, included by other library headers.
00033  *  You should not attempt to use it directly.
00034  */
00035 
00036 //
00037 // ISO C++ 14882 TR1: 5.2  Special functions
00038 //
00039 
00040 // Written by Edward Smith-Rowland based:
00041 //   (1) Handbook of Mathematical Functions,
00042 //       ed. Milton Abramowitz and Irene A. Stegun,
00043 //       Dover Publications,
00044 //       Section 6, pp. 555-566
00045 //   (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
00046 
00047 #ifndef _GLIBCXX_TR1_HYPERGEOMETRIC_TCC
00048 #define _GLIBCXX_TR1_HYPERGEOMETRIC_TCC 1
00049 
00050 namespace std
00051 {
00052 namespace tr1
00053 {
00054 
00055   // [5.2] Special functions
00056 
00057   // Implementation-space details.
00058   namespace __detail
00059   {
00060 
00061     /**
00062      *   @brief This routine returns the confluent hypergeometric function
00063      *          by series expansion.
00064      * 
00065      *   @f[
00066      *     _1F_1(a;c;x) = \frac{\Gamma(c)}{\Gamma(a)}
00067      *                      \sum_{n=0}^{\infty}
00068      *                      \frac{\Gamma(a+n)}{\Gamma(c+n)}
00069      *                      \frac{x^n}{n!}
00070      *   @f]
00071      * 
00072      *   If a and b are integers and a < 0 and either b > 0 or b < a then the
00073      *   series is a polynomial with a finite number of terms.  If b is an integer
00074      *   and b <= 0 the confluent hypergeometric function is undefined.
00075      *
00076      *   @param  __a  The "numerator" parameter.
00077      *   @param  __c  The "denominator" parameter.
00078      *   @param  __x  The argument of the confluent hypergeometric function.
00079      *   @return  The confluent hypergeometric function.
00080      */
00081     template<typename _Tp>
00082     _Tp
00083     __conf_hyperg_series(const _Tp __a, const _Tp __c, const _Tp __x)
00084     {
00085       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00086 
00087       _Tp __term = _Tp(1);
00088       _Tp __Fac = _Tp(1);
00089       const unsigned int __max_iter = 100000;
00090       unsigned int __i;
00091       for (__i = 0; __i < __max_iter; ++__i)
00092         {
00093           __term *= (__a + _Tp(__i)) * __x
00094                   / ((__c + _Tp(__i)) * _Tp(1 + __i));
00095           if (std::abs(__term) < __eps)
00096             {
00097               break;
00098             }
00099           __Fac += __term;
00100         }
00101       if (__i == __max_iter)
00102         std::__throw_runtime_error(__N("Series failed to converge "
00103                                        "in __conf_hyperg_series."));
00104 
00105       return __Fac;
00106     }
00107 
00108 
00109     /**
00110      *  @brief  Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
00111      *          by an iterative procedure described in
00112      *          Luke, Algorithms for the Computation of Mathematical Functions.
00113      *
00114      *  Like the case of the 2F1 rational approximations, these are 
00115      *  probably guaranteed to converge for x < 0, barring gross    
00116      *  numerical instability in the pre-asymptotic regime.         
00117      */
00118     template<typename _Tp>
00119     _Tp
00120     __conf_hyperg_luke(const _Tp __a, const _Tp __c, const _Tp __xin)
00121     {
00122       const _Tp __big = std::pow(std::numeric_limits<_Tp>::max(), _Tp(0.16L));
00123       const int __nmax = 20000;
00124       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00125       const _Tp __x  = -__xin;
00126       const _Tp __x3 = __x * __x * __x;
00127       const _Tp __t0 = __a / __c;
00128       const _Tp __t1 = (__a + _Tp(1)) / (_Tp(2) * __c);
00129       const _Tp __t2 = (__a + _Tp(2)) / (_Tp(2) * (__c + _Tp(1)));
00130       _Tp __F = _Tp(1);
00131       _Tp __prec;
00132 
00133       _Tp __Bnm3 = _Tp(1);
00134       _Tp __Bnm2 = _Tp(1) + __t1 * __x;
00135       _Tp __Bnm1 = _Tp(1) + __t2 * __x * (_Tp(1) + __t1 / _Tp(3) * __x);
00136 
00137       _Tp __Anm3 = _Tp(1);
00138       _Tp __Anm2 = __Bnm2 - __t0 * __x;
00139       _Tp __Anm1 = __Bnm1 - __t0 * (_Tp(1) + __t2 * __x) * __x
00140                  + __t0 * __t1 * (__c / (__c + _Tp(1))) * __x * __x;
00141 
00142       int __n = 3;
00143       while(1)
00144         {
00145           _Tp __npam1 = _Tp(__n - 1) + __a;
00146           _Tp __npcm1 = _Tp(__n - 1) + __c;
00147           _Tp __npam2 = _Tp(__n - 2) + __a;
00148           _Tp __npcm2 = _Tp(__n - 2) + __c;
00149           _Tp __tnm1  = _Tp(2 * __n - 1);
00150           _Tp __tnm3  = _Tp(2 * __n - 3);
00151           _Tp __tnm5  = _Tp(2 * __n - 5);
00152           _Tp __F1 =  (_Tp(__n - 2) - __a) / (_Tp(2) * __tnm3 * __npcm1);
00153           _Tp __F2 =  (_Tp(__n) + __a) * __npam1
00154                    / (_Tp(4) * __tnm1 * __tnm3 * __npcm2 * __npcm1);
00155           _Tp __F3 = -__npam2 * __npam1 * (_Tp(__n - 2) - __a)
00156                    / (_Tp(8) * __tnm3 * __tnm3 * __tnm5
00157                    * (_Tp(__n - 3) + __c) * __npcm2 * __npcm1);
00158           _Tp __E  = -__npam1 * (_Tp(__n - 1) - __c)
00159                    / (_Tp(2) * __tnm3 * __npcm2 * __npcm1);
00160 
00161           _Tp __An = (_Tp(1) + __F1 * __x) * __Anm1
00162                    + (__E + __F2 * __x) * __x * __Anm2 + __F3 * __x3 * __Anm3;
00163           _Tp __Bn = (_Tp(1) + __F1 * __x) * __Bnm1
00164                    + (__E + __F2 * __x) * __x * __Bnm2 + __F3 * __x3 * __Bnm3;
00165           _Tp __r = __An / __Bn;
00166 
00167           __prec = std::abs((__F - __r) / __F);
00168           __F = __r;
00169 
00170           if (__prec < __eps || __n > __nmax)
00171             break;
00172 
00173           if (std::abs(__An) > __big || std::abs(__Bn) > __big)
00174             {
00175               __An   /= __big;
00176               __Bn   /= __big;
00177               __Anm1 /= __big;
00178               __Bnm1 /= __big;
00179               __Anm2 /= __big;
00180               __Bnm2 /= __big;
00181               __Anm3 /= __big;
00182               __Bnm3 /= __big;
00183             }
00184           else if (std::abs(__An) < _Tp(1) / __big
00185                 || std::abs(__Bn) < _Tp(1) / __big)
00186             {
00187               __An   *= __big;
00188               __Bn   *= __big;
00189               __Anm1 *= __big;
00190               __Bnm1 *= __big;
00191               __Anm2 *= __big;
00192               __Bnm2 *= __big;
00193               __Anm3 *= __big;
00194               __Bnm3 *= __big;
00195             }
00196 
00197           ++__n;
00198           __Bnm3 = __Bnm2;
00199           __Bnm2 = __Bnm1;
00200           __Bnm1 = __Bn;
00201           __Anm3 = __Anm2;
00202           __Anm2 = __Anm1;
00203           __Anm1 = __An;
00204         }
00205 
00206       if (__n >= __nmax)
00207         std::__throw_runtime_error(__N("Iteration failed to converge "
00208                                        "in __conf_hyperg_luke."));
00209 
00210       return __F;
00211     }
00212 
00213 
00214     /**
00215      *   @brief  Return the confluent hypogeometric function
00216      *           @f$ _1F_1(a;c;x) @f$.
00217      * 
00218      *   @todo  Handle b == nonpositive integer blowup - return NaN.
00219      *
00220      *   @param  __a  The "numerator" parameter.
00221      *   @param  __c  The "denominator" parameter.
00222      *   @param  __x  The argument of the confluent hypergeometric function.
00223      *   @return  The confluent hypergeometric function.
00224      */
00225     template<typename _Tp>
00226     inline _Tp
00227     __conf_hyperg(const _Tp __a, const _Tp __c, const _Tp __x)
00228     {
00229 #if _GLIBCXX_USE_C99_MATH_TR1
00230       const _Tp __c_nint = std::tr1::nearbyint(__c);
00231 #else
00232       const _Tp __c_nint = static_cast<int>(__c + _Tp(0.5L));
00233 #endif
00234       if (__isnan(__a) || __isnan(__c) || __isnan(__x))
00235         return std::numeric_limits<_Tp>::quiet_NaN();
00236       else if (__c_nint == __c && __c_nint <= 0)
00237         return std::numeric_limits<_Tp>::infinity();
00238       else if (__a == _Tp(0))
00239         return _Tp(1);
00240       else if (__c == __a)
00241         return std::exp(__x);
00242       else if (__x < _Tp(0))
00243         return __conf_hyperg_luke(__a, __c, __x);
00244       else
00245         return __conf_hyperg_series(__a, __c, __x);
00246     }
00247 
00248 
00249     /**
00250      *   @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
00251      *   by series expansion.
00252      * 
00253      *   The hypogeometric function is defined by
00254      *   @f[
00255      *     _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
00256      *                      \sum_{n=0}^{\infty}
00257      *                      \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
00258      *                      \frac{x^n}{n!}
00259      *   @f]
00260      * 
00261      *   This works and it's pretty fast.
00262      *
00263      *   @param  __a  The first "numerator" parameter.
00264      *   @param  __a  The second "numerator" parameter.
00265      *   @param  __c  The "denominator" parameter.
00266      *   @param  __x  The argument of the confluent hypergeometric function.
00267      *   @return  The confluent hypergeometric function.
00268      */
00269     template<typename _Tp>
00270     _Tp
00271     __hyperg_series(const _Tp __a, const _Tp __b,
00272                     const _Tp __c, const _Tp __x)
00273     {
00274       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00275 
00276       _Tp __term = _Tp(1);
00277       _Tp __Fabc = _Tp(1);
00278       const unsigned int __max_iter = 100000;
00279       unsigned int __i;
00280       for (__i = 0; __i < __max_iter; ++__i)
00281         {
00282           __term *= (__a + _Tp(__i)) * (__b + _Tp(__i)) * __x
00283                   / ((__c + _Tp(__i)) * _Tp(1 + __i));
00284           if (std::abs(__term) < __eps)
00285             {
00286               break;
00287             }
00288           __Fabc += __term;
00289         }
00290       if (__i == __max_iter)
00291         std::__throw_runtime_error(__N("Series failed to converge "
00292                                        "in __hyperg_series."));
00293 
00294       return __Fabc;
00295     }
00296 
00297 
00298     /**
00299      *   @brief  Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
00300      *           by an iterative procedure described in
00301      *           Luke, Algorithms for the Computation of Mathematical Functions.
00302      */
00303     template<typename _Tp>
00304     _Tp
00305     __hyperg_luke(const _Tp __a, const _Tp __b, const _Tp __c,
00306                   const _Tp __xin)
00307     {
00308       const _Tp __big = std::pow(std::numeric_limits<_Tp>::max(), _Tp(0.16L));
00309       const int __nmax = 20000;
00310       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00311       const _Tp __x  = -__xin;
00312       const _Tp __x3 = __x * __x * __x;
00313       const _Tp __t0 = __a * __b / __c;
00314       const _Tp __t1 = (__a + _Tp(1)) * (__b + _Tp(1)) / (_Tp(2) * __c);
00315       const _Tp __t2 = (__a + _Tp(2)) * (__b + _Tp(2))
00316                      / (_Tp(2) * (__c + _Tp(1)));
00317 
00318       _Tp __F = _Tp(1);
00319 
00320       _Tp __Bnm3 = _Tp(1);
00321       _Tp __Bnm2 = _Tp(1) + __t1 * __x;
00322       _Tp __Bnm1 = _Tp(1) + __t2 * __x * (_Tp(1) + __t1 / _Tp(3) * __x);
00323 
00324       _Tp __Anm3 = _Tp(1);
00325       _Tp __Anm2 = __Bnm2 - __t0 * __x;
00326       _Tp __Anm1 = __Bnm1 - __t0 * (_Tp(1) + __t2 * __x) * __x
00327                  + __t0 * __t1 * (__c / (__c + _Tp(1))) * __x * __x;
00328 
00329       int __n = 3;
00330       while (1)
00331         {
00332           const _Tp __npam1 = _Tp(__n - 1) + __a;
00333           const _Tp __npbm1 = _Tp(__n - 1) + __b;
00334           const _Tp __npcm1 = _Tp(__n - 1) + __c;
00335           const _Tp __npam2 = _Tp(__n - 2) + __a;
00336           const _Tp __npbm2 = _Tp(__n - 2) + __b;
00337           const _Tp __npcm2 = _Tp(__n - 2) + __c;
00338           const _Tp __tnm1  = _Tp(2 * __n - 1);
00339           const _Tp __tnm3  = _Tp(2 * __n - 3);
00340           const _Tp __tnm5  = _Tp(2 * __n - 5);
00341           const _Tp __n2 = __n * __n;
00342           const _Tp __F1 = (_Tp(3) * __n2 + (__a + __b - _Tp(6)) * __n
00343                          + _Tp(2) - __a * __b - _Tp(2) * (__a + __b))
00344                          / (_Tp(2) * __tnm3 * __npcm1);
00345           const _Tp __F2 = -(_Tp(3) * __n2 - (__a + __b + _Tp(6)) * __n
00346                          + _Tp(2) - __a * __b) * __npam1 * __npbm1
00347                          / (_Tp(4) * __tnm1 * __tnm3 * __npcm2 * __npcm1);
00348           const _Tp __F3 = (__npam2 * __npam1 * __npbm2 * __npbm1
00349                          * (_Tp(__n - 2) - __a) * (_Tp(__n - 2) - __b))
00350                          / (_Tp(8) * __tnm3 * __tnm3 * __tnm5
00351                          * (_Tp(__n - 3) + __c) * __npcm2 * __npcm1);
00352           const _Tp __E  = -__npam1 * __npbm1 * (_Tp(__n - 1) - __c)
00353                          / (_Tp(2) * __tnm3 * __npcm2 * __npcm1);
00354 
00355           _Tp __An = (_Tp(1) + __F1 * __x) * __Anm1
00356                    + (__E + __F2 * __x) * __x * __Anm2 + __F3 * __x3 * __Anm3;
00357           _Tp __Bn = (_Tp(1) + __F1 * __x) * __Bnm1
00358                    + (__E + __F2 * __x) * __x * __Bnm2 + __F3 * __x3 * __Bnm3;
00359           const _Tp __r = __An / __Bn;
00360 
00361           const _Tp __prec = std::abs((__F - __r) / __F);
00362           __F = __r;
00363 
00364           if (__prec < __eps || __n > __nmax)
00365             break;
00366 
00367           if (std::abs(__An) > __big || std::abs(__Bn) > __big)
00368             {
00369               __An   /= __big;
00370               __Bn   /= __big;
00371               __Anm1 /= __big;
00372               __Bnm1 /= __big;
00373               __Anm2 /= __big;
00374               __Bnm2 /= __big;
00375               __Anm3 /= __big;
00376               __Bnm3 /= __big;
00377             }
00378           else if (std::abs(__An) < _Tp(1) / __big
00379                 || std::abs(__Bn) < _Tp(1) / __big)
00380             {
00381               __An   *= __big;
00382               __Bn   *= __big;
00383               __Anm1 *= __big;
00384               __Bnm1 *= __big;
00385               __Anm2 *= __big;
00386               __Bnm2 *= __big;
00387               __Anm3 *= __big;
00388               __Bnm3 *= __big;
00389             }
00390 
00391           ++__n;
00392           __Bnm3 = __Bnm2;
00393           __Bnm2 = __Bnm1;
00394           __Bnm1 = __Bn;
00395           __Anm3 = __Anm2;
00396           __Anm2 = __Anm1;
00397           __Anm1 = __An;
00398         }
00399 
00400       if (__n >= __nmax)
00401         std::__throw_runtime_error(__N("Iteration failed to converge "
00402                                        "in __hyperg_luke."));
00403 
00404       return __F;
00405     }
00406 
00407 
00408     /**
00409      *  @brief  Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$ by the reflection
00410      *          formulae in Abramowitz & Stegun formula 15.3.6 for d = c - a - b not integral
00411      *          and formula 15.3.11 for d = c - a - b integral.
00412      *          This assumes a, b, c != negative integer.
00413      *
00414      *   The hypogeometric function is defined by
00415      *   @f[
00416      *     _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
00417      *                      \sum_{n=0}^{\infty}
00418      *                      \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
00419      *                      \frac{x^n}{n!}
00420      *   @f]
00421      *
00422      *   The reflection formula for nonintegral @f$ d = c - a - b @f$ is:
00423      *   @f[
00424      *     _2F_1(a,b;c;x) = \frac{\Gamma(c)\Gamma(d)}{\Gamma(c-a)\Gamma(c-b)}
00425      *                            _2F_1(a,b;1-d;1-x)
00426      *                    + \frac{\Gamma(c)\Gamma(-d)}{\Gamma(a)\Gamma(b)}
00427      *                            _2F_1(c-a,c-b;1+d;1-x)
00428      *   @f]
00429      *
00430      *   The reflection formula for integral @f$ m = c - a - b @f$ is:
00431      *   @f[
00432      *     _2F_1(a,b;a+b+m;x) = \frac{\Gamma(m)\Gamma(a+b+m)}{\Gamma(a+m)\Gamma(b+m)}
00433      *                        \sum_{k=0}^{m-1} \frac{(m+a)_k(m+b)_k}{k!(1-m)_k}
00434      *                      - 
00435      *   @f]
00436      */
00437     template<typename _Tp>
00438     _Tp
00439     __hyperg_reflect(const _Tp __a, const _Tp __b, const _Tp __c,
00440                      const _Tp __x)
00441     {
00442       const _Tp __d = __c - __a - __b;
00443       const int __intd  = std::floor(__d + _Tp(0.5L));
00444       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00445       const _Tp __toler = _Tp(1000) * __eps;
00446       const _Tp __log_max = std::log(std::numeric_limits<_Tp>::max());
00447       const bool __d_integer = (std::abs(__d - __intd) < __toler);
00448 
00449       if (__d_integer)
00450         {
00451           const _Tp __ln_omx = std::log(_Tp(1) - __x);
00452           const _Tp __ad = std::abs(__d);
00453           _Tp __F1, __F2;
00454 
00455           _Tp __d1, __d2;
00456           if (__d >= _Tp(0))
00457             {
00458               __d1 = __d;
00459               __d2 = _Tp(0);
00460             }
00461           else
00462             {
00463               __d1 = _Tp(0);
00464               __d2 = __d;
00465             }
00466 
00467           const _Tp __lng_c = __log_gamma(__c);
00468 
00469           //  Evaluate F1.
00470           if (__ad < __eps)
00471             {
00472               //  d = c - a - b = 0.
00473               __F1 = _Tp(0);
00474             }
00475           else
00476             {
00477 
00478               bool __ok_d1 = true;
00479               _Tp __lng_ad, __lng_ad1, __lng_bd1;
00480               try
00481                 {
00482                   __lng_ad = __log_gamma(__ad);
00483                   __lng_ad1 = __log_gamma(__a + __d1);
00484                   __lng_bd1 = __log_gamma(__b + __d1);
00485                 }
00486               catch(...)
00487                 {
00488                   __ok_d1 = false;
00489                 }
00490 
00491               if (__ok_d1)
00492                 {
00493                   /* Gamma functions in the denominator are ok.
00494                    * Proceed with evaluation.
00495                    */
00496                   _Tp __sum1 = _Tp(1);
00497                   _Tp __term = _Tp(1);
00498                   _Tp __ln_pre1 = __lng_ad + __lng_c + __d2 * __ln_omx
00499                                 - __lng_ad1 - __lng_bd1;
00500 
00501                   /* Do F1 sum.
00502                    */
00503                   for (int __i = 1; __i < __ad; ++__i)
00504                     {
00505                       const int __j = __i - 1;
00506                       __term *= (__a + __d2 + __j) * (__b + __d2 + __j)
00507                               / (_Tp(1) + __d2 + __j) / __i * (_Tp(1) - __x);
00508                       __sum1 += __term;
00509                     }
00510 
00511                   if (__ln_pre1 > __log_max)
00512                     std::__throw_runtime_error(__N("Overflow of gamma functions "
00513                                                    "in __hyperg_luke."));
00514                   else
00515                     __F1 = std::exp(__ln_pre1) * __sum1;
00516                 }
00517               else
00518                 {
00519                   //  Gamma functions in the denominator were not ok.
00520                   //  So the F1 term is zero.
00521                   __F1 = _Tp(0);
00522                 }
00523             } // end F1 evaluation
00524 
00525           // Evaluate F2.
00526           bool __ok_d2 = true;
00527           _Tp __lng_ad2, __lng_bd2;
00528           try
00529             {
00530               __lng_ad2 = __log_gamma(__a + __d2);
00531               __lng_bd2 = __log_gamma(__b + __d2);
00532             }
00533           catch(...)
00534             {
00535               __ok_d2 = false;
00536             }
00537 
00538           if (__ok_d2)
00539             {
00540               //  Gamma functions in the denominator are ok.
00541               //  Proceed with evaluation.
00542               const int __maxiter = 2000;
00543               const _Tp __psi_1 = -__numeric_constants<_Tp>::__gamma_e();
00544               const _Tp __psi_1pd = __psi(_Tp(1) + __ad);
00545               const _Tp __psi_apd1 = __psi(__a + __d1);
00546               const _Tp __psi_bpd1 = __psi(__b + __d1);
00547 
00548               _Tp __psi_term = __psi_1 + __psi_1pd - __psi_apd1
00549                              - __psi_bpd1 - __ln_omx;
00550               _Tp __fact = _Tp(1);
00551               _Tp __sum2 = __psi_term;
00552               _Tp __ln_pre2 = __lng_c + __d1 * __ln_omx
00553                             - __lng_ad2 - __lng_bd2;
00554 
00555               // Do F2 sum.
00556               int __j;
00557               for (__j = 1; __j < __maxiter; ++__j)
00558                 {
00559                   //  Values for psi functions use recurrence; Abramowitz & Stegun 6.3.5
00560                   const _Tp __term1 = _Tp(1) / _Tp(__j)
00561                                     + _Tp(1) / (__ad + __j);
00562                   const _Tp __term2 = _Tp(1) / (__a + __d1 + _Tp(__j - 1))
00563                                     + _Tp(1) / (__b + __d1 + _Tp(__j - 1));
00564                   __psi_term += __term1 - __term2;
00565                   __fact *= (__a + __d1 + _Tp(__j - 1))
00566                           * (__b + __d1 + _Tp(__j - 1))
00567                           / ((__ad + __j) * __j) * (_Tp(1) - __x);
00568                   const _Tp __delta = __fact * __psi_term;
00569                   __sum2 += __delta;
00570                   if (std::abs(__delta) < __eps * std::abs(__sum2))
00571                     break;
00572                 }
00573               if (__j == __maxiter)
00574                 std::__throw_runtime_error(__N("Sum F2 failed to converge "
00575                                                "in __hyperg_reflect"));
00576 
00577               if (__sum2 == _Tp(0))
00578                 __F2 = _Tp(0);
00579               else
00580                 __F2 = std::exp(__ln_pre2) * __sum2;
00581             }
00582           else
00583             {
00584               // Gamma functions in the denominator not ok.
00585               // So the F2 term is zero.
00586               __F2 = _Tp(0);
00587             } // end F2 evaluation
00588 
00589           const _Tp __sgn_2 = (__intd % 2 == 1 ? -_Tp(1) : _Tp(1));
00590           const _Tp __F = __F1 + __sgn_2 * __F2;
00591 
00592           return __F;
00593         }
00594       else
00595         {
00596           //  d = c - a - b not an integer.
00597 
00598           //  These gamma functions appear in the denominator, so we
00599           //  catch their harmless domain errors and set the terms to zero.
00600           bool __ok1 = true;
00601           _Tp __sgn_g1ca = _Tp(0), __ln_g1ca = _Tp(0);
00602           _Tp __sgn_g1cb = _Tp(0), __ln_g1cb = _Tp(0);
00603           try
00604             {
00605               __sgn_g1ca = __log_gamma_sign(__c - __a);
00606               __ln_g1ca = __log_gamma(__c - __a);
00607               __sgn_g1cb = __log_gamma_sign(__c - __b);
00608               __ln_g1cb = __log_gamma(__c - __b);
00609             }
00610           catch(...)
00611             {
00612               __ok1 = false;
00613             }
00614 
00615           bool __ok2 = true;
00616           _Tp __sgn_g2a = _Tp(0), __ln_g2a = _Tp(0);
00617           _Tp __sgn_g2b = _Tp(0), __ln_g2b = _Tp(0);
00618           try
00619             {
00620               __sgn_g2a = __log_gamma_sign(__a);
00621               __ln_g2a = __log_gamma(__a);
00622               __sgn_g2b = __log_gamma_sign(__b);
00623               __ln_g2b = __log_gamma(__b);
00624             }
00625           catch(...)
00626             {
00627               __ok2 = false;
00628             }
00629 
00630           const _Tp __sgn_gc = __log_gamma_sign(__c);
00631           const _Tp __ln_gc = __log_gamma(__c);
00632           const _Tp __sgn_gd = __log_gamma_sign(__d);
00633           const _Tp __ln_gd = __log_gamma(__d);
00634           const _Tp __sgn_gmd = __log_gamma_sign(-__d);
00635           const _Tp __ln_gmd = __log_gamma(-__d);
00636 
00637           const _Tp __sgn1 = __sgn_gc * __sgn_gd  * __sgn_g1ca * __sgn_g1cb;
00638           const _Tp __sgn2 = __sgn_gc * __sgn_gmd * __sgn_g2a  * __sgn_g2b;
00639 
00640           _Tp __pre1, __pre2;
00641           if (__ok1 && __ok2)
00642             {
00643               _Tp __ln_pre1 = __ln_gc + __ln_gd  - __ln_g1ca - __ln_g1cb;
00644               _Tp __ln_pre2 = __ln_gc + __ln_gmd - __ln_g2a  - __ln_g2b
00645                             + __d * std::log(_Tp(1) - __x);
00646               if (__ln_pre1 < __log_max && __ln_pre2 < __log_max)
00647                 {
00648                   __pre1 = std::exp(__ln_pre1);
00649                   __pre2 = std::exp(__ln_pre2);
00650                   __pre1 *= __sgn1;
00651                   __pre2 *= __sgn2;
00652                 }
00653               else
00654                 {
00655                   std::__throw_runtime_error(__N("Overflow of gamma functions "
00656                                                  "in __hyperg_reflect"));
00657                 }
00658             }
00659           else if (__ok1 && !__ok2)
00660             {
00661               _Tp __ln_pre1 = __ln_gc + __ln_gd - __ln_g1ca - __ln_g1cb;
00662               if (__ln_pre1 < __log_max)
00663                 {
00664                   __pre1 = std::exp(__ln_pre1);
00665                   __pre1 *= __sgn1;
00666                   __pre2 = _Tp(0);
00667                 }
00668               else
00669                 {
00670                   std::__throw_runtime_error(__N("Overflow of gamma functions "
00671                                                  "in __hyperg_reflect"));
00672                 }
00673             }
00674           else if (!__ok1 && __ok2)
00675             {
00676               _Tp __ln_pre2 = __ln_gc + __ln_gmd - __ln_g2a - __ln_g2b
00677                             + __d * std::log(_Tp(1) - __x);
00678               if (__ln_pre2 < __log_max)
00679                 {
00680                   __pre1 = _Tp(0);
00681                   __pre2 = std::exp(__ln_pre2);
00682                   __pre2 *= __sgn2;
00683                 }
00684               else
00685                 {
00686                   std::__throw_runtime_error(__N("Overflow of gamma functions "
00687                                                  "in __hyperg_reflect"));
00688                 }
00689             }
00690           else
00691             {
00692               __pre1 = _Tp(0);
00693               __pre2 = _Tp(0);
00694               std::__throw_runtime_error(__N("Underflow of gamma functions "
00695                                              "in __hyperg_reflect"));
00696             }
00697 
00698           const _Tp __F1 = __hyperg_series(__a, __b, _Tp(1) - __d,
00699                                            _Tp(1) - __x);
00700           const _Tp __F2 = __hyperg_series(__c - __a, __c - __b, _Tp(1) + __d,
00701                                            _Tp(1) - __x);
00702 
00703           const _Tp __F = __pre1 * __F1 + __pre2 * __F2;
00704 
00705           return __F;
00706         }
00707     }
00708 
00709 
00710     /**
00711      *   @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$.
00712      *
00713      *   The hypogeometric function is defined by
00714      *   @f[
00715      *     _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
00716      *                      \sum_{n=0}^{\infty}
00717      *                      \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
00718      *                      \frac{x^n}{n!}
00719      *   @f]
00720      *
00721      *   @param  __a  The first "numerator" parameter.
00722      *   @param  __a  The second "numerator" parameter.
00723      *   @param  __c  The "denominator" parameter.
00724      *   @param  __x  The argument of the confluent hypergeometric function.
00725      *   @return  The confluent hypergeometric function.
00726      */
00727     template<typename _Tp>
00728     inline _Tp
00729     __hyperg(const _Tp __a, const _Tp __b, const _Tp __c, const _Tp __x)
00730     {
00731 #if _GLIBCXX_USE_C99_MATH_TR1
00732       const _Tp __a_nint = std::tr1::nearbyint(__a);
00733       const _Tp __b_nint = std::tr1::nearbyint(__b);
00734       const _Tp __c_nint = std::tr1::nearbyint(__c);
00735 #else
00736       const _Tp __a_nint = static_cast<int>(__a + _Tp(0.5L));
00737       const _Tp __b_nint = static_cast<int>(__b + _Tp(0.5L));
00738       const _Tp __c_nint = static_cast<int>(__c + _Tp(0.5L));
00739 #endif
00740       const _Tp __toler = _Tp(1000) * std::numeric_limits<_Tp>::epsilon();
00741       if (std::abs(__x) >= _Tp(1))
00742         std::__throw_domain_error(__N("Argument outside unit circle "
00743                                       "in __hyperg."));
00744       else if (__isnan(__a) || __isnan(__b)
00745             || __isnan(__c) || __isnan(__x))
00746         return std::numeric_limits<_Tp>::quiet_NaN();
00747       else if (__c_nint == __c && __c_nint <= _Tp(0))
00748         return std::numeric_limits<_Tp>::infinity();
00749       else if (std::abs(__c - __b) < __toler || std::abs(__c - __a) < __toler)
00750         return std::pow(_Tp(1) - __x, __c - __a - __b);
00751       else if (__a >= _Tp(0) && __b >= _Tp(0) && __c >= _Tp(0)
00752             && __x >= _Tp(0) && __x < _Tp(0.995L))
00753         return __hyperg_series(__a, __b, __c, __x);
00754       else if (std::abs(__a) < _Tp(10) && std::abs(__b) < _Tp(10))
00755         {
00756           //  For integer a and b the hypergeometric function is a finite polynomial.
00757           if (__a < _Tp(0)  &&  std::abs(__a - __a_nint) < __toler)
00758             return __hyperg_series(__a_nint, __b, __c, __x);
00759           else if (__b < _Tp(0)  &&  std::abs(__b - __b_nint) < __toler)
00760             return __hyperg_series(__a, __b_nint, __c, __x);
00761           else if (__x < -_Tp(0.25L))
00762             return __hyperg_luke(__a, __b, __c, __x);
00763           else if (__x < _Tp(0.5L))
00764             return __hyperg_series(__a, __b, __c, __x);
00765           else
00766             if (std::abs(__c) > _Tp(10))
00767               return __hyperg_series(__a, __b, __c, __x);
00768             else
00769               return __hyperg_reflect(__a, __b, __c, __x);
00770         }
00771       else
00772         return __hyperg_luke(__a, __b, __c, __x);
00773     }
00774 
00775   } // namespace std::tr1::__detail
00776 }
00777 }
00778 
00779 #endif // _GLIBCXX_TR1_HYPERGEOMETRIC_TCC

Generated on Fri Jan 23 20:12:13 2009 for libstdc++ by  doxygen 1.5.6