# modified_bessel_func.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/modified_bessel_func.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.
    00041 //
    00042 // References:
    00043 //   (1) Handbook of Mathematical Functions,
    00044 //       Ed. Milton Abramowitz and Irene A. Stegun,
    00045 //       Dover Publications,
    00046 //       Section 9, pp. 355-434, Section 10 pp. 435-478
    00047 //   (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
    00048 //   (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
    00049 //       W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
    00050 //       2nd ed, pp. 246-249.
    00051
    00052 #ifndef _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC
    00053 #define _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC 1
    00054
    00055 #include "special_function_util.h"
    00056
    00057 namespace std
    00058 {
    00059 namespace tr1
    00060 {
    00061
    00062   // [5.2] Special functions
    00063
    00064   // Implementation-space details.
    00065   namespace __detail
    00066   {
    00067
    00068     /**
    00069      *   @brief  Compute the modified Bessel functions @f$ I_\nu(x) @f$ and
    00070      *           @f$ K_\nu(x) @f$ and their first derivatives
    00071      *           @f$ I'_\nu(x) @f$ and @f$ K'_\nu(x) @f$ respectively.
    00072      *           These four functions are computed together for numerical
    00073      *           stability.
    00074      *
    00075      *   @param  __nu  The order of the Bessel functions.
    00076      *   @param  __x   The argument of the Bessel functions.
    00077      *   @param  __Inu  The output regular modified Bessel function.
    00078      *   @param  __Knu  The output irregular modified Bessel function.
    00079      *   @param  __Ipnu  The output derivative of the regular
    00080      *                   modified Bessel function.
    00081      *   @param  __Kpnu  The output derivative of the irregular
    00082      *                   modified Bessel function.
    00083      */
    00084     template <typename _Tp>
    00085     void
    00086     __bessel_ik(const _Tp __nu, const _Tp __x,
    00087                 _Tp & __Inu, _Tp & __Knu, _Tp & __Ipnu, _Tp & __Kpnu)
    00088     {
    00089       if (__x == _Tp(0))
    00090         {
    00091           if (__nu == _Tp(0))
    00092             {
    00093               __Inu = _Tp(1);
    00094               __Ipnu = _Tp(0);
    00095             }
    00096           else if (__nu == _Tp(1))
    00097             {
    00098               __Inu = _Tp(0);
    00099               __Ipnu = _Tp(0.5L);
    00100             }
    00101           else
    00102             {
    00103               __Inu = _Tp(0);
    00104               __Ipnu = _Tp(0);
    00105             }
    00106           __Knu = std::numeric_limits<_Tp>::infinity();
    00107           __Kpnu = -std::numeric_limits<_Tp>::infinity();
    00108           return;
    00109         }
    00110
    00111       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
    00112       const _Tp __fp_min = _Tp(10) * std::numeric_limits<_Tp>::epsilon();
    00113       const int __max_iter = 15000;
    00114       const _Tp __x_min = _Tp(2);
    00115
    00116       const int __nl = static_cast<int>(__nu + _Tp(0.5L));
    00117
    00118       const _Tp __mu = __nu - __nl;
    00119       const _Tp __mu2 = __mu * __mu;
    00120       const _Tp __xi = _Tp(1) / __x;
    00121       const _Tp __xi2 = _Tp(2) * __xi;
    00122       _Tp __h = __nu * __xi;
    00123       if ( __h < __fp_min )
    00124         __h = __fp_min;
    00125       _Tp __b = __xi2 * __nu;
    00126       _Tp __d = _Tp(0);
    00127       _Tp __c = __h;
    00128       int __i;
    00129       for ( __i = 1; __i <= __max_iter; ++__i )
    00130         {
    00131           __b += __xi2;
    00132           __d = _Tp(1) / (__b + __d);
    00133           __c = __b + _Tp(1) / __c;
    00134           const _Tp __del = __c * __d;
    00135           __h *= __del;
    00136           if (std::abs(__del - _Tp(1)) < __eps)
    00137             break;
    00138         }
    00139       if (__i > __max_iter)
    00140         std::__throw_runtime_error(__N("Argument x too large "
    00141                                        "in __bessel_jn; "
    00142                                        "try asymptotic expansion."));
    00143       _Tp __Inul = __fp_min;
    00144       _Tp __Ipnul = __h * __Inul;
    00145       _Tp __Inul1 = __Inul;
    00146       _Tp __Ipnu1 = __Ipnul;
    00147       _Tp __fact = __nu * __xi;
    00148       for (int __l = __nl; __l >= 1; --__l)
    00149         {
    00150           const _Tp __Inutemp = __fact * __Inul + __Ipnul;
    00151           __fact -= __xi;
    00152           __Ipnul = __fact * __Inutemp + __Inul;
    00153           __Inul = __Inutemp;
    00154         }
    00155       _Tp __f = __Ipnul / __Inul;
    00156       _Tp __Kmu, __Knu1;
    00157       if (__x < __x_min)
    00158         {
    00159           const _Tp __x2 = __x / _Tp(2);
    00160           const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
    00161           const _Tp __fact = (std::abs(__pimu) < __eps
    00162                             ? _Tp(1) : __pimu / std::sin(__pimu));
    00163           _Tp __d = -std::log(__x2);
    00164           _Tp __e = __mu * __d;
    00165           const _Tp __fact2 = (std::abs(__e) < __eps
    00166                             ? _Tp(1) : std::sinh(__e) / __e);
    00167           _Tp __gam1, __gam2, __gampl, __gammi;
    00168           __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
    00169           _Tp __ff = __fact
    00170                    * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
    00171           _Tp __sum = __ff;
    00172           __e = std::exp(__e);
    00173           _Tp __p = __e / (_Tp(2) * __gampl);
    00174           _Tp __q = _Tp(1) / (_Tp(2) * __e * __gammi);
    00175           _Tp __c = _Tp(1);
    00176           __d = __x2 * __x2;
    00177           _Tp __sum1 = __p;
    00178           int __i;
    00179           for (__i = 1; __i <= __max_iter; ++__i)
    00180             {
    00181               __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
    00182               __c *= __d / __i;
    00183               __p /= __i - __mu;
    00184               __q /= __i + __mu;
    00185               const _Tp __del = __c * __ff;
    00186               __sum += __del;
    00187               const _Tp __del1 = __c * (__p - __i * __ff);
    00188               __sum1 += __del1;
    00189               if (std::abs(__del) < __eps * std::abs(__sum))
    00190                 break;
    00191             }
    00192           if (__i > __max_iter)
    00193             std::__throw_runtime_error(__N("Bessel k series failed to converge "
    00194                                            "in __bessel_jn."));
    00195           __Kmu = __sum;
    00196           __Knu1 = __sum1 * __xi2;
    00197         }
    00198       else
    00199         {
    00200           _Tp __b = _Tp(2) * (_Tp(1) + __x);
    00201           _Tp __d = _Tp(1) / __b;
    00202           _Tp __delh = __d;
    00203           _Tp __h = __delh;
    00204           _Tp __q1 = _Tp(0);
    00205           _Tp __q2 = _Tp(1);
    00206           _Tp __a1 = _Tp(0.25L) - __mu2;
    00207           _Tp __q = __c = __a1;
    00208           _Tp __a = -__a1;
    00209           _Tp __s = _Tp(1) + __q * __delh;
    00210           int __i;
    00211           for (__i = 2; __i <= __max_iter; ++__i)
    00212             {
    00213               __a -= 2 * (__i - 1);
    00214               __c = -__a * __c / __i;
    00215               const _Tp __qnew = (__q1 - __b * __q2) / __a;
    00216               __q1 = __q2;
    00217               __q2 = __qnew;
    00218               __q += __c * __qnew;
    00219               __b += _Tp(2);
    00220               __d = _Tp(1) / (__b + __a * __d);
    00221               __delh = (__b * __d - _Tp(1)) * __delh;
    00222               __h += __delh;
    00223               const _Tp __dels = __q * __delh;
    00224               __s += __dels;
    00225               if ( std::abs(__dels / __s) < __eps )
    00226                 break;
    00227             }
    00228           if (__i > __max_iter)
    00229             std::__throw_runtime_error(__N("Steed's method failed "
    00230                                            "in __bessel_jn."));
    00231           __h = __a1 * __h;
    00232           __Kmu = std::sqrt(__numeric_constants<_Tp>::__pi() / (_Tp(2) * __x))
    00233                 * std::exp(-__x) / __s;
    00234           __Knu1 = __Kmu * (__mu + __x + _Tp(0.5L) - __h) * __xi;
    00235         }
    00236
    00237       _Tp __Kpmu = __mu * __xi * __Kmu - __Knu1;
    00238       _Tp __Inumu = __xi / (__f * __Kmu - __Kpmu);
    00239       __Inu = __Inumu * __Inul1 / __Inul;
    00240       __Ipnu = __Inumu * __Ipnu1 / __Inul;
    00241       for ( __i = 1; __i <= __nl; ++__i )
    00242         {
    00243           const _Tp __Knutemp = (__mu + __i) * __xi2 * __Knu1 + __Kmu;
    00244           __Kmu = __Knu1;
    00245           __Knu1 = __Knutemp;
    00246         }
    00247       __Knu = __Kmu;
    00248       __Kpnu = __nu * __xi * __Kmu - __Knu1;
    00249
    00250       return;
    00251     }
    00252
    00253
    00254     /**
    00255      *   @brief  Return the regular modified Bessel function of order
    00256      *           \f$ \nu \f$: \f$ I_{\nu}(x) \f$.
    00257      *
    00258      *   The regular modified cylindrical Bessel function is:
    00259      *   @f[
    00260      *    I_{\nu}(x) = \sum_{k=0}^{\infty}
    00261      *              \frac{(x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
    00262      *   @f]
    00263      *
    00264      *   @param  __nu  The order of the regular modified Bessel function.
    00265      *   @param  __x   The argument of the regular modified Bessel function.
    00266      *   @return  The output regular modified Bessel function.
    00267      */
    00268     template<typename _Tp>
    00269     _Tp
    00270     __cyl_bessel_i(const _Tp __nu, const _Tp __x)
    00271     {
    00272       if (__nu < _Tp(0) || __x < _Tp(0))
    00273         std::__throw_domain_error(__N("Bad argument "
    00274                                       "in __cyl_bessel_i."));
    00275       else if (__isnan(__nu) || __isnan(__x))
    00276         return std::numeric_limits<_Tp>::quiet_NaN();
    00277       else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
    00278         return __cyl_bessel_ij_series(__nu, __x, +_Tp(1), 200);
    00279       else
    00280         {
    00281           _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
    00282           __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
    00283           return __I_nu;
    00284         }
    00285     }
    00286
    00287
    00288     /**
    00289      *   @brief  Return the irregular modified Bessel function
    00290      *           \f$ K_{\nu}(x) \f$ of order \f$ \nu \f$.
    00291      *
    00292      *   The irregular modified Bessel function is defined by:
    00293      *   @f[
    00294      *      K_{\nu}(x) = \frac{\pi}{2}
    00295      *                   \frac{I_{-\nu}(x) - I_{\nu}(x)}{\sin \nu\pi}
    00296      *   @f]
    00297      *   where for integral \f$ \nu = n \f$ a limit is taken:
    00298      *   \f$ lim_{\nu \to n} \f$.
    00299      *
    00300      *   @param  __nu  The order of the irregular modified Bessel function.
    00301      *   @param  __x   The argument of the irregular modified Bessel function.
    00302      *   @return  The output irregular modified Bessel function.
    00303      */
    00304     template<typename _Tp>
    00305     _Tp
    00306     __cyl_bessel_k(const _Tp __nu, const _Tp __x)
    00307     {
    00308       if (__nu < _Tp(0) || __x < _Tp(0))
    00309         std::__throw_domain_error(__N("Bad argument "
    00310                                       "in __cyl_bessel_k."));
    00311       else if (__isnan(__nu) || __isnan(__x))
    00312         return std::numeric_limits<_Tp>::quiet_NaN();
    00313       else
    00314         {
    00315           _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
    00316           __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
    00317           return __K_nu;
    00318         }
    00319     }
    00320
    00321
    00322     /**
    00323      *   @brief  Compute the spherical modified Bessel functions
    00324      *           @f$ i_n(x) @f$ and @f$ k_n(x) @f$ and their first
    00325      *           derivatives @f$ i'_n(x) @f$ and @f$ k'_n(x) @f$
    00326      *           respectively.
    00327      *
    00328      *   @param  __n  The order of the modified spherical Bessel function.
    00329      *   @param  __x  The argument of the modified spherical Bessel function.
    00330      *   @param  __i_n  The output regular modified spherical Bessel function.
    00331      *   @param  __k_n  The output irregular modified spherical
    00332      *                  Bessel function.
    00333      *   @param  __ip_n  The output derivative of the regular modified
    00334      *                   spherical Bessel function.
    00335      *   @param  __kp_n  The output derivative of the irregular modified
    00336      *                   spherical Bessel function.
    00337      */
    00338     template <typename _Tp>
    00339     void
    00340     __sph_bessel_ik(const unsigned int __n, const _Tp __x,
    00341                     _Tp & __i_n, _Tp & __k_n, _Tp & __ip_n, _Tp & __kp_n)
    00342     {
    00343       const _Tp __nu = _Tp(__n) + _Tp(0.5L);
    00344
    00345       _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
    00346       __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
    00347
    00348       const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
    00349                          / std::sqrt(__x);
    00350
    00351       __i_n = __factor * __I_nu;
    00352       __k_n = __factor * __K_nu;
    00353       __ip_n = __factor * __Ip_nu - __i_n / (_Tp(2) * __x);
    00354       __kp_n = __factor * __Kp_nu - __k_n / (_Tp(2) * __x);
    00355
    00356       return;
    00357     }
    00358
    00359
    00360     /**
    00361      *   @brief  Compute the Airy functions
    00362      *           @f$ Ai(x) @f$ and @f$ Bi(x) @f$ and their first
    00363      *           derivatives @f$ Ai'(x) @f$ and @f$ Bi(x) @f$
    00364      *           respectively.
    00365      *
    00366      *   @param  __n  The order of the Airy functions.
    00367      *   @param  __x  The argument of the Airy functions.
    00368      *   @param  __i_n  The output Airy function.
    00369      *   @param  __k_n  The output Airy function.
    00370      *   @param  __ip_n  The output derivative of the Airy function.
    00371      *   @param  __kp_n  The output derivative of the Airy function.
    00372      */
    00373     template <typename _Tp>
    00374     void
    00375     __airy(const _Tp __x,
    00376            _Tp & __Ai, _Tp & __Bi, _Tp & __Aip, _Tp & __Bip)
    00377     {
    00378       const _Tp __absx = std::abs(__x);
    00379       const _Tp __rootx = std::sqrt(__absx);
    00380       const _Tp __z = _Tp(2) * __absx * __rootx / _Tp(3);
    00381
    00382       if (__isnan(__x))
    00383         return std::numeric_limits<_Tp>::quiet_NaN();
    00384       else if (__x > _Tp(0))
    00385         {
    00386           _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
    00387
    00388           __bessel_ik(_Tp(1) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
    00389           __Ai = __rootx * __K_nu
    00390                / (__numeric_constants<_Tp>::__sqrt3()
    00391                 * __numeric_constants<_Tp>::__pi());
    00392           __Bi = __rootx * (__K_nu / __numeric_constants<_Tp>::__pi()
    00393                  + _Tp(2) * __I_nu / __numeric_constants<_Tp>::__sqrt3());
    00394
    00395           __bessel_ik(_Tp(2) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
    00396           __Aip = -__x * __K_nu
    00397                 / (__numeric_constants<_Tp>::__sqrt3()
    00398                  * __numeric_constants<_Tp>::__pi());
    00399           __Bip = __x * (__K_nu / __numeric_constants<_Tp>::__pi()
    00400                       + _Tp(2) * __I_nu
    00401                       / __numeric_constants<_Tp>::__sqrt3());
    00402         }
    00403       else if (__x < _Tp(0))
    00404         {
    00405           _Tp __J_nu, __Jp_nu, __N_nu, __Np_nu;
    00406
    00407           __bessel_jn(_Tp(1) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
    00408           __Ai = __rootx * (__J_nu
    00409                     - __N_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
    00410           __Bi = -__rootx * (__N_nu
    00411                     + __J_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
    00412
    00413           __bessel_jn(_Tp(2) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
    00414           __Aip = __absx * (__N_nu / __numeric_constants<_Tp>::__sqrt3()
    00415                           + __J_nu) / _Tp(2);
    00416           __Bip = __absx * (__J_nu / __numeric_constants<_Tp>::__sqrt3()
    00417                           - __N_nu) / _Tp(2);
    00418         }
    00419       else
    00420         {
    00421           //  Reference:
    00422           //    Abramowitz & Stegun, page 446 section 10.4.4 on Airy functions.
    00423           //  The number is Ai(0) = 3^{-2/3}/\Gamma(2/3).
    00424           __Ai = _Tp(0.35502805388781723926L);
    00425           __Bi = __Ai * __numeric_constants<_Tp>::__sqrt3();
    00426
    00427           //  Reference:
    00428           //    Abramowitz & Stegun, page 446 section 10.4.5 on Airy functions.
    00429           //  The number is Ai'(0) = -3^{-1/3}/\Gamma(1/3).
    00430           __Aip = -_Tp(0.25881940379280679840L);
    00431           __Bip = -__Aip * __numeric_constants<_Tp>::__sqrt3();
    00432         }
    00433
    00434       return;
    00435     }
    00436
    00437   } // namespace std::tr1::__detail
    00438 }
    00439 }
    00440
    00441 #endif // _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC