bessel_function.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/bessel_function.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.
00036 //
00037 // References:
00038 //   (1) Handbook of Mathematical Functions,
00039 //       ed. Milton Abramowitz and Irene A. Stegun,
00040 //       Dover Publications,
00041 //       Section 9, pp. 355-434, Section 10 pp. 435-478
00042 //   (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
00043 //   (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
00044 //       W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
00045 //       2nd ed, pp. 240-245
00046 
00047 #ifndef _GLIBCXX_TR1_BESSEL_FUNCTION_TCC
00048 #define _GLIBCXX_TR1_BESSEL_FUNCTION_TCC 1
00049 
00050 #include "special_function_util.h"
00051 
00052 namespace std
00053 {
00054 namespace tr1
00055 {
00056 
00057   // [5.2] Special functions
00058 
00059   // Implementation-space details.
00060   namespace __detail
00061   {
00062 
00063     /**
00064      *   @brief Compute the gamma functions required by the Temme series
00065      *          expansions of @f$ N_\nu(x) @f$ and @f$ K_\nu(x) @f$.
00066      *   @f[
00067      *     \Gamma_1 = \frac{1}{2\mu}
00068      *                [\frac{1}{\Gamma(1 - \mu)} - \frac{1}{\Gamma(1 + \mu)}]
00069      *   @f]
00070      *   and
00071      *   @f[
00072      *     \Gamma_2 = \frac{1}{2}
00073      *                [\frac{1}{\Gamma(1 - \mu)} + \frac{1}{\Gamma(1 + \mu)}]
00074      *   @f]
00075      *   where @f$ -1/2 <= \mu <= 1/2 @f$ is @f$ \mu = \nu - N @f$ and @f$ N @f$.
00076      *   is the nearest integer to @f$ \nu @f$.
00077      *   The values of \f$ \Gamma(1 + \mu) \f$ and \f$ \Gamma(1 - \mu) \f$
00078      *   are returned as well.
00079      * 
00080      *   The accuracy requirements on this are exquisite.
00081      *
00082      *   @param __mu     The input parameter of the gamma functions.
00083      *   @param __gam1   The output function \f$ \Gamma_1(\mu) \f$
00084      *   @param __gam2   The output function \f$ \Gamma_2(\mu) \f$
00085      *   @param __gampl  The output function \f$ \Gamma(1 + \mu) \f$
00086      *   @param __gammi  The output function \f$ \Gamma(1 - \mu) \f$
00087      */
00088     template <typename _Tp>
00089     void
00090     __gamma_temme(const _Tp __mu,
00091                    _Tp & __gam1, _Tp & __gam2, _Tp & __gampl, _Tp & __gammi)
00092     {
00093 #if _GLIBCXX_USE_C99_MATH_TR1
00094       __gampl = _Tp(1) / std::tr1::tgamma(_Tp(1) + __mu);
00095       __gammi = _Tp(1) / std::tr1::tgamma(_Tp(1) - __mu);
00096 #else
00097       __gampl = _Tp(1) / __gamma(_Tp(1) + __mu);
00098       __gammi = _Tp(1) / __gamma(_Tp(1) - __mu);
00099 #endif
00100 
00101       if (std::abs(__mu) < std::numeric_limits<_Tp>::epsilon())
00102         __gam1 = -_Tp(__numeric_constants<_Tp>::__gamma_e());
00103       else
00104         __gam1 = (__gammi - __gampl) / (_Tp(2) * __mu);
00105 
00106       __gam2 = (__gammi + __gampl) / (_Tp(2));
00107 
00108       return;
00109     }
00110 
00111 
00112     /**
00113      *   @brief  Compute the Bessel @f$ J_\nu(x) @f$ and Neumann
00114      *           @f$ N_\nu(x) @f$ functions and their first derivatives
00115      *           @f$ J'_\nu(x) @f$ and @f$ N'_\nu(x) @f$ respectively.
00116      *           These four functions are computed together for numerical
00117      *           stability.
00118      *
00119      *   @param  __nu  The order of the Bessel functions.
00120      *   @param  __x   The argument of the Bessel functions.
00121      *   @param  __Jnu  The output Bessel function of the first kind.
00122      *   @param  __Nnu  The output Neumann function (Bessel function of the second kind).
00123      *   @param  __Jpnu  The output derivative of the Bessel function of the first kind.
00124      *   @param  __Npnu  The output derivative of the Neumann function.
00125      */
00126     template <typename _Tp>
00127     void
00128     __bessel_jn(const _Tp __nu, const _Tp __x,
00129                 _Tp & __Jnu, _Tp & __Nnu, _Tp & __Jpnu, _Tp & __Npnu)
00130     {
00131       if (__x == _Tp(0))
00132         {
00133           if (__nu == _Tp(0))
00134             {
00135               __Jnu = _Tp(1);
00136               __Jpnu = _Tp(0);
00137             }
00138           else if (__nu == _Tp(1))
00139             {
00140               __Jnu = _Tp(0);
00141               __Jpnu = _Tp(0.5L);
00142             }
00143           else
00144             {
00145               __Jnu = _Tp(0);
00146               __Jpnu = _Tp(0);
00147             }
00148           __Nnu = -std::numeric_limits<_Tp>::infinity();
00149           __Npnu = std::numeric_limits<_Tp>::infinity();
00150           return;
00151         }
00152 
00153       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00154       //  When the multiplier is N i.e.
00155       //  fp_min = N * min()
00156       //  Then J_0 and N_0 tank at x = 8 * N (J_0 = 0 and N_0 = nan)!
00157       //const _Tp __fp_min = _Tp(20) * std::numeric_limits<_Tp>::min();
00158       const _Tp __fp_min = std::sqrt(std::numeric_limits<_Tp>::min());
00159       const int __max_iter = 15000;
00160       const _Tp __x_min = _Tp(2);
00161 
00162       const int __nl = (__x < __x_min
00163                     ? static_cast<int>(__nu + _Tp(0.5L))
00164                     : std::max(0, static_cast<int>(__nu - __x + _Tp(1.5L))));
00165 
00166       const _Tp __mu = __nu - __nl;
00167       const _Tp __mu2 = __mu * __mu;
00168       const _Tp __xi = _Tp(1) / __x;
00169       const _Tp __xi2 = _Tp(2) * __xi;
00170       _Tp __w = __xi2 / __numeric_constants<_Tp>::__pi();
00171       int __isign = 1;
00172       _Tp __h = __nu * __xi;
00173       if (__h < __fp_min)
00174         __h = __fp_min;
00175       _Tp __b = __xi2 * __nu;
00176       _Tp __d = _Tp(0);
00177       _Tp __c = __h;
00178       int __i;
00179       for (__i = 1; __i <= __max_iter; ++__i)
00180         {
00181           __b += __xi2;
00182           __d = __b - __d;
00183           if (std::abs(__d) < __fp_min)
00184             __d = __fp_min;
00185           __c = __b - _Tp(1) / __c;
00186           if (std::abs(__c) < __fp_min)
00187             __c = __fp_min;
00188           __d = _Tp(1) / __d;
00189           const _Tp __del = __c * __d;
00190           __h *= __del;
00191           if (__d < _Tp(0))
00192             __isign = -__isign;
00193           if (std::abs(__del - _Tp(1)) < __eps)
00194             break;
00195         }
00196       if (__i > __max_iter)
00197         std::__throw_runtime_error(__N("Argument x too large in __bessel_jn; "
00198                                        "try asymptotic expansion."));
00199       _Tp __Jnul = __isign * __fp_min;
00200       _Tp __Jpnul = __h * __Jnul;
00201       _Tp __Jnul1 = __Jnul;
00202       _Tp __Jpnu1 = __Jpnul;
00203       _Tp __fact = __nu * __xi;
00204       for ( int __l = __nl; __l >= 1; --__l )
00205         {
00206           const _Tp __Jnutemp = __fact * __Jnul + __Jpnul;
00207           __fact -= __xi;
00208           __Jpnul = __fact * __Jnutemp - __Jnul;
00209           __Jnul = __Jnutemp;
00210         }
00211       if (__Jnul == _Tp(0))
00212         __Jnul = __eps;
00213       _Tp __f= __Jpnul / __Jnul;
00214       _Tp __Nmu, __Nnu1, __Npmu, __Jmu;
00215       if (__x < __x_min)
00216         {
00217           const _Tp __x2 = __x / _Tp(2);
00218           const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
00219           _Tp __fact = (std::abs(__pimu) < __eps
00220                       ? _Tp(1) : __pimu / std::sin(__pimu));
00221           _Tp __d = -std::log(__x2);
00222           _Tp __e = __mu * __d;
00223           _Tp __fact2 = (std::abs(__e) < __eps
00224                        ? _Tp(1) : std::sinh(__e) / __e);
00225           _Tp __gam1, __gam2, __gampl, __gammi;
00226           __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
00227           _Tp __ff = (_Tp(2) / __numeric_constants<_Tp>::__pi())
00228                    * __fact * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
00229           __e = std::exp(__e);
00230           _Tp __p = __e / (__numeric_constants<_Tp>::__pi() * __gampl);
00231           _Tp __q = _Tp(1) / (__e * __numeric_constants<_Tp>::__pi() * __gammi);
00232           const _Tp __pimu2 = __pimu / _Tp(2);
00233           _Tp __fact3 = (std::abs(__pimu2) < __eps
00234                        ? _Tp(1) : std::sin(__pimu2) / __pimu2 );
00235           _Tp __r = __numeric_constants<_Tp>::__pi() * __pimu2 * __fact3 * __fact3;
00236           _Tp __c = _Tp(1);
00237           __d = -__x2 * __x2;
00238           _Tp __sum = __ff + __r * __q;
00239           _Tp __sum1 = __p;
00240           for (__i = 1; __i <= __max_iter; ++__i)
00241             {
00242               __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
00243               __c *= __d / _Tp(__i);
00244               __p /= _Tp(__i) - __mu;
00245               __q /= _Tp(__i) + __mu;
00246               const _Tp __del = __c * (__ff + __r * __q);
00247               __sum += __del; 
00248               const _Tp __del1 = __c * __p - __i * __del;
00249               __sum1 += __del1;
00250               if ( std::abs(__del) < __eps * (_Tp(1) + std::abs(__sum)) )
00251                 break;
00252             }
00253           if ( __i > __max_iter )
00254             std::__throw_runtime_error(__N("Bessel y series failed to converge "
00255                                            "in __bessel_jn."));
00256           __Nmu = -__sum;
00257           __Nnu1 = -__sum1 * __xi2;
00258           __Npmu = __mu * __xi * __Nmu - __Nnu1;
00259           __Jmu = __w / (__Npmu - __f * __Nmu);
00260         }
00261       else
00262         {
00263           _Tp __a = _Tp(0.25L) - __mu2;
00264           _Tp __q = _Tp(1);
00265           _Tp __p = -__xi / _Tp(2);
00266           _Tp __br = _Tp(2) * __x;
00267           _Tp __bi = _Tp(2);
00268           _Tp __fact = __a * __xi / (__p * __p + __q * __q);
00269           _Tp __cr = __br + __q * __fact;
00270           _Tp __ci = __bi + __p * __fact;
00271           _Tp __den = __br * __br + __bi * __bi;
00272           _Tp __dr = __br / __den;
00273           _Tp __di = -__bi / __den;
00274           _Tp __dlr = __cr * __dr - __ci * __di;
00275           _Tp __dli = __cr * __di + __ci * __dr;
00276           _Tp __temp = __p * __dlr - __q * __dli;
00277           __q = __p * __dli + __q * __dlr;
00278           __p = __temp;
00279           int __i;
00280           for (__i = 2; __i <= __max_iter; ++__i)
00281             {
00282               __a += _Tp(2 * (__i - 1));
00283               __bi += _Tp(2);
00284               __dr = __a * __dr + __br;
00285               __di = __a * __di + __bi;
00286               if (std::abs(__dr) + std::abs(__di) < __fp_min)
00287                 __dr = __fp_min;
00288               __fact = __a / (__cr * __cr + __ci * __ci);
00289               __cr = __br + __cr * __fact;
00290               __ci = __bi - __ci * __fact;
00291               if (std::abs(__cr) + std::abs(__ci) < __fp_min)
00292                 __cr = __fp_min;
00293               __den = __dr * __dr + __di * __di;
00294               __dr /= __den;
00295               __di /= -__den;
00296               __dlr = __cr * __dr - __ci * __di;
00297               __dli = __cr * __di + __ci * __dr;
00298               __temp = __p * __dlr - __q * __dli;
00299               __q = __p * __dli + __q * __dlr;
00300               __p = __temp;
00301               if (std::abs(__dlr - _Tp(1)) + std::abs(__dli) < __eps)
00302                 break;
00303           }
00304           if (__i > __max_iter)
00305             std::__throw_runtime_error(__N("Lentz's method failed "
00306                                            "in __bessel_jn."));
00307           const _Tp __gam = (__p - __f) / __q;
00308           __Jmu = std::sqrt(__w / ((__p - __f) * __gam + __q));
00309 #if _GLIBCXX_USE_C99_MATH_TR1
00310           __Jmu = std::tr1::copysign(__Jmu, __Jnul);
00311 #else
00312           if (__Jmu * __Jnul < _Tp(0))
00313             __Jmu = -__Jmu;
00314 #endif
00315           __Nmu = __gam * __Jmu;
00316           __Npmu = (__p + __q / __gam) * __Nmu;
00317           __Nnu1 = __mu * __xi * __Nmu - __Npmu;
00318       }
00319       __fact = __Jmu / __Jnul;
00320       __Jnu = __fact * __Jnul1;
00321       __Jpnu = __fact * __Jpnu1;
00322       for (__i = 1; __i <= __nl; ++__i)
00323         {
00324           const _Tp __Nnutemp = (__mu + __i) * __xi2 * __Nnu1 - __Nmu;
00325           __Nmu = __Nnu1;
00326           __Nnu1 = __Nnutemp;
00327         }
00328       __Nnu = __Nmu;
00329       __Npnu = __nu * __xi * __Nmu - __Nnu1;
00330 
00331       return;
00332     }
00333 
00334 
00335     /**
00336      *   @brief This routine computes the asymptotic cylindrical Bessel
00337      *          and Neumann functions of order nu: \f$ J_{\nu} \f$,
00338      *          \f$ N_{\nu} \f$.
00339      *
00340      *   References:
00341      *    (1) Handbook of Mathematical Functions,
00342      *        ed. Milton Abramowitz and Irene A. Stegun,
00343      *        Dover Publications,
00344      *        Section 9 p. 364, Equations 9.2.5-9.2.10
00345      *
00346      *   @param  __nu  The order of the Bessel functions.
00347      *   @param  __x   The argument of the Bessel functions.
00348      *   @param  __Jnu  The output Bessel function of the first kind.
00349      *   @param  __Nnu  The output Neumann function (Bessel function of the second kind).
00350      */
00351     template <typename _Tp>
00352     void
00353     __cyl_bessel_jn_asymp(const _Tp __nu, const _Tp __x,
00354                           _Tp & __Jnu, _Tp & __Nnu)
00355     {
00356       const _Tp __coef = std::sqrt(_Tp(2)
00357                              / (__numeric_constants<_Tp>::__pi() * __x));
00358       const _Tp __mu   = _Tp(4) * __nu * __nu;
00359       const _Tp __mum1 = __mu - _Tp(1);
00360       const _Tp __mum9 = __mu - _Tp(9);
00361       const _Tp __mum25 = __mu - _Tp(25);
00362       const _Tp __mum49 = __mu - _Tp(49);
00363       const _Tp __xx = _Tp(64) * __x * __x;
00364       const _Tp __P = _Tp(1) - __mum1 * __mum9 / (_Tp(2) * __xx)
00365                     * (_Tp(1) - __mum25 * __mum49 / (_Tp(12) * __xx));
00366       const _Tp __Q = __mum1 / (_Tp(8) * __x)
00367                     * (_Tp(1) - __mum9 * __mum25 / (_Tp(6) * __xx));
00368 
00369       const _Tp __chi = __x - (__nu + _Tp(0.5L))
00370                             * __numeric_constants<_Tp>::__pi_2();
00371       const _Tp __c = std::cos(__chi);
00372       const _Tp __s = std::sin(__chi);
00373 
00374       __Jnu = __coef * (__c * __P - __s * __Q);
00375       __Nnu = __coef * (__s * __P + __c * __Q);
00376 
00377       return;
00378     }
00379 
00380 
00381     /**
00382      *   @brief This routine returns the cylindrical Bessel functions
00383      *          of order \f$ \nu \f$: \f$ J_{\nu} \f$ or \f$ I_{\nu} \f$
00384      *          by series expansion.
00385      *
00386      *   The modified cylindrical Bessel function is:
00387      *   @f[
00388      *    Z_{\nu}(x) = \sum_{k=0}^{\infty}
00389      *              \frac{\sigma^k (x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
00390      *   @f]
00391      *   where \f$ \sigma = +1 \f$ or\f$  -1 \f$ for
00392      *   \f$ Z = I \f$ or \f$ J \f$ respectively.
00393      * 
00394      *   See Abramowitz & Stegun, 9.1.10
00395      *       Abramowitz & Stegun, 9.6.7
00396      *    (1) Handbook of Mathematical Functions,
00397      *        ed. Milton Abramowitz and Irene A. Stegun,
00398      *        Dover Publications,
00399      *        Equation 9.1.10 p. 360 and Equation 9.6.10 p. 375
00400      *
00401      *   @param  __nu  The order of the Bessel function.
00402      *   @param  __x   The argument of the Bessel function.
00403      *   @param  __sgn  The sign of the alternate terms
00404      *                  -1 for the Bessel function of the first kind.
00405      *                  +1 for the modified Bessel function of the first kind.
00406      *   @return  The output Bessel function.
00407      */
00408     template <typename _Tp>
00409     _Tp
00410     __cyl_bessel_ij_series(const _Tp __nu, const _Tp __x, const _Tp __sgn,
00411                            const unsigned int __max_iter)
00412     {
00413 
00414       const _Tp __x2 = __x / _Tp(2);
00415       _Tp __fact = __nu * std::log(__x2);
00416 #if _GLIBCXX_USE_C99_MATH_TR1
00417       __fact -= std::tr1::lgamma(__nu + _Tp(1));
00418 #else
00419       __fact -= __log_gamma(__nu + _Tp(1));
00420 #endif
00421       __fact = std::exp(__fact);
00422       const _Tp __xx4 = __sgn * __x2 * __x2;
00423       _Tp __Jn = _Tp(1);
00424       _Tp __term = _Tp(1);
00425 
00426       for (unsigned int __i = 1; __i < __max_iter; ++__i)
00427         {
00428           __term *= __xx4 / (_Tp(__i) * (__nu + _Tp(__i)));
00429           __Jn += __term;
00430           if (std::abs(__term / __Jn) < std::numeric_limits<_Tp>::epsilon())
00431             break;
00432         }
00433 
00434       return __fact * __Jn;
00435     }
00436 
00437 
00438     /**
00439      *   @brief  Return the Bessel function of order \f$ \nu \f$:
00440      *           \f$ J_{\nu}(x) \f$.
00441      *
00442      *   The cylindrical Bessel function is:
00443      *   @f[
00444      *    J_{\nu}(x) = \sum_{k=0}^{\infty}
00445      *              \frac{(-1)^k (x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
00446      *   @f]
00447      *
00448      *   @param  __nu  The order of the Bessel function.
00449      *   @param  __x   The argument of the Bessel function.
00450      *   @return  The output Bessel function.
00451      */
00452     template<typename _Tp>
00453     _Tp
00454     __cyl_bessel_j(const _Tp __nu, const _Tp __x)
00455     {
00456       if (__nu < _Tp(0) || __x < _Tp(0))
00457         std::__throw_domain_error(__N("Bad argument "
00458                                       "in __cyl_bessel_j."));
00459       else if (__isnan(__nu) || __isnan(__x))
00460         return std::numeric_limits<_Tp>::quiet_NaN();
00461       else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
00462         return __cyl_bessel_ij_series(__nu, __x, -_Tp(1), 200);
00463       else if (__x > _Tp(1000))
00464         {
00465           _Tp __J_nu, __N_nu;
00466           __cyl_bessel_jn_asymp(__nu, __x, __J_nu, __N_nu);
00467           return __J_nu;
00468         }
00469       else
00470         {
00471           _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
00472           __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
00473           return __J_nu;
00474         }
00475     }
00476 
00477 
00478     /**
00479      *   @brief  Return the Neumann function of order \f$ \nu \f$:
00480      *           \f$ N_{\nu}(x) \f$.
00481      *
00482      *   The Neumann function is defined by:
00483      *   @f[
00484      *      N_{\nu}(x) = \frac{J_{\nu}(x) \cos \nu\pi - J_{-\nu}(x)}
00485      *                        {\sin \nu\pi}
00486      *   @f]
00487      *   where for integral \f$ \nu = n \f$ a limit is taken:
00488      *   \f$ lim_{\nu \to n} \f$.
00489      *
00490      *   @param  __nu  The order of the Neumann function.
00491      *   @param  __x   The argument of the Neumann function.
00492      *   @return  The output Neumann function.
00493      */
00494     template<typename _Tp>
00495     _Tp
00496     __cyl_neumann_n(const _Tp __nu, const _Tp __x)
00497     {
00498       if (__nu < _Tp(0) || __x < _Tp(0))
00499         std::__throw_domain_error(__N("Bad argument "
00500                                       "in __cyl_neumann_n."));
00501       else if (__isnan(__nu) || __isnan(__x))
00502         return std::numeric_limits<_Tp>::quiet_NaN();
00503       else if (__x > _Tp(1000))
00504         {
00505           _Tp __J_nu, __N_nu;
00506           __cyl_bessel_jn_asymp(__nu, __x, __J_nu, __N_nu);
00507           return __N_nu;
00508         }
00509       else
00510         {
00511           _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
00512           __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
00513           return __N_nu;
00514         }
00515     }
00516 
00517 
00518     /**
00519      *   @brief  Compute the spherical Bessel @f$ j_n(x) @f$
00520      *           and Neumann @f$ n_n(x) @f$ functions and their first
00521      *           derivatives @f$ j'_n(x) @f$ and @f$ n'_n(x) @f$
00522      *           respectively.
00523      *
00524      *   @param  __n  The order of the spherical Bessel function.
00525      *   @param  __x  The argument of the spherical Bessel function.
00526      *   @param  __j_n  The output spherical Bessel function.
00527      *   @param  __n_n  The output spherical Neumann function.
00528      *   @param  __jp_n  The output derivative of the spherical Bessel function.
00529      *   @param  __np_n  The output derivative of the spherical Neumann function.
00530      */
00531     template <typename _Tp>
00532     void
00533     __sph_bessel_jn(const unsigned int __n, const _Tp __x,
00534                     _Tp & __j_n, _Tp & __n_n, _Tp & __jp_n, _Tp & __np_n)
00535     {
00536       const _Tp __nu = _Tp(__n) + _Tp(0.5L);
00537 
00538       _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
00539       __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
00540 
00541       const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
00542                          / std::sqrt(__x);
00543 
00544       __j_n = __factor * __J_nu;
00545       __n_n = __factor * __N_nu;
00546       __jp_n = __factor * __Jp_nu - __j_n / (_Tp(2) * __x);
00547       __np_n = __factor * __Np_nu - __n_n / (_Tp(2) * __x);
00548 
00549       return;
00550     }
00551 
00552 
00553     /**
00554      *   @brief  Return the spherical Bessel function
00555      *           @f$ j_n(x) @f$ of order n.
00556      *
00557      *   The spherical Bessel function is defined by:
00558      *   @f[
00559      *    j_n(x) = \left( \frac{\pi}{2x} \right) ^{1/2} J_{n+1/2}(x)
00560      *   @f]
00561      *
00562      *   @param  __n  The order of the spherical Bessel function.
00563      *   @param  __x  The argument of the spherical Bessel function.
00564      *   @return  The output spherical Bessel function.
00565      */
00566     template <typename _Tp>
00567     _Tp
00568     __sph_bessel(const unsigned int __n, const _Tp __x)
00569     {
00570       if (__x < _Tp(0))
00571         std::__throw_domain_error(__N("Bad argument "
00572                                       "in __sph_bessel."));
00573       else if (__isnan(__x))
00574         return std::numeric_limits<_Tp>::quiet_NaN();
00575       else if (__x == _Tp(0))
00576         {
00577           if (__n == 0)
00578             return _Tp(1);
00579           else
00580             return _Tp(0);
00581         }
00582       else
00583         {
00584           _Tp __j_n, __n_n, __jp_n, __np_n;
00585           __sph_bessel_jn(__n, __x, __j_n, __n_n, __jp_n, __np_n);
00586           return __j_n;
00587         }
00588     }
00589 
00590 
00591     /**
00592      *   @brief  Return the spherical Neumann function
00593      *           @f$ n_n(x) @f$.
00594      *
00595      *   The spherical Neumann function is defined by:
00596      *   @f[
00597      *    n_n(x) = \left( \frac{\pi}{2x} \right) ^{1/2} N_{n+1/2}(x)
00598      *   @f]
00599      *
00600      *   @param  __n  The order of the spherical Neumann function.
00601      *   @param  __x  The argument of the spherical Neumann function.
00602      *   @return  The output spherical Neumann function.
00603      */
00604     template <typename _Tp>
00605     _Tp
00606     __sph_neumann(const unsigned int __n, const _Tp __x)
00607     {
00608       if (__x < _Tp(0))
00609         std::__throw_domain_error(__N("Bad argument "
00610                                       "in __sph_neumann."));
00611       else if (__isnan(__x))
00612         return std::numeric_limits<_Tp>::quiet_NaN();
00613       else if (__x == _Tp(0))
00614         return -std::numeric_limits<_Tp>::infinity();
00615       else
00616         {
00617           _Tp __j_n, __n_n, __jp_n, __np_n;
00618           __sph_bessel_jn(__n, __x, __j_n, __n_n, __jp_n, __np_n);
00619           return __n_n;
00620         }
00621     }
00622 
00623   } // namespace std::tr1::__detail
00624 }
00625 }
00626 
00627 #endif // _GLIBCXX_TR1_BESSEL_FUNCTION_TCC

Generated on Thu Jul 23 21:16:00 2009 for libstdc++ by  doxygen 1.5.8