hypergeometric.tcc

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

Generated on Tue Apr 21 13:13:28 2009 for libstdc++ by  doxygen 1.5.8