]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/tr1/bessel_function.tcc
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / tr1 / bessel_function.tcc
1 // Special functions -*- C++ -*-
2
3 // Copyright (C) 2006, 2007, 2008
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21 //
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /** @file tr1/bessel_function.tcc
32  *  This is an internal header file, included by other library headers.
33  *  You should not attempt to use it directly.
34  */
35
36 //
37 // ISO C++ 14882 TR1: 5.2  Special functions
38 //
39
40 // Written by Edward Smith-Rowland.
41 //
42 // References:
43 //   (1) Handbook of Mathematical Functions,
44 //       ed. Milton Abramowitz and Irene A. Stegun,
45 //       Dover Publications,
46 //       Section 9, pp. 355-434, Section 10 pp. 435-478
47 //   (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
48 //   (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
49 //       W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
50 //       2nd ed, pp. 240-245
51
52 #ifndef _GLIBCXX_TR1_BESSEL_FUNCTION_TCC
53 #define _GLIBCXX_TR1_BESSEL_FUNCTION_TCC 1
54
55 #include "special_function_util.h"
56
57 namespace std
58 {
59 namespace tr1
60 {
61
62   // [5.2] Special functions
63
64   // Implementation-space details.
65   namespace __detail
66   {
67
68     /**
69      *   @brief Compute the gamma functions required by the Temme series
70      *          expansions of @f$ N_\nu(x) @f$ and @f$ K_\nu(x) @f$.
71      *   @f[
72      *     \Gamma_1 = \frac{1}{2\mu}
73      *                [\frac{1}{\Gamma(1 - \mu)} - \frac{1}{\Gamma(1 + \mu)}]
74      *   @f]
75      *   and
76      *   @f[
77      *     \Gamma_2 = \frac{1}{2}
78      *                [\frac{1}{\Gamma(1 - \mu)} + \frac{1}{\Gamma(1 + \mu)}]
79      *   @f]
80      *   where @f$ -1/2 <= \mu <= 1/2 @f$ is @f$ \mu = \nu - N @f$ and @f$ N @f$.
81      *   is the nearest integer to @f$ \nu @f$.
82      *   The values of \f$ \Gamma(1 + \mu) \f$ and \f$ \Gamma(1 - \mu) \f$
83      *   are returned as well.
84      * 
85      *   The accuracy requirements on this are exquisite.
86      *
87      *   @param __mu     The input parameter of the gamma functions.
88      *   @param __gam1   The output function \f$ \Gamma_1(\mu) \f$
89      *   @param __gam2   The output function \f$ \Gamma_2(\mu) \f$
90      *   @param __gampl  The output function \f$ \Gamma(1 + \mu) \f$
91      *   @param __gammi  The output function \f$ \Gamma(1 - \mu) \f$
92      */
93     template <typename _Tp>
94     void
95     __gamma_temme(const _Tp __mu,
96                    _Tp & __gam1, _Tp & __gam2, _Tp & __gampl, _Tp & __gammi)
97     {
98 #if _GLIBCXX_USE_C99_MATH_TR1
99       __gampl = _Tp(1) / std::tr1::tgamma(_Tp(1) + __mu);
100       __gammi = _Tp(1) / std::tr1::tgamma(_Tp(1) - __mu);
101 #else
102       __gampl = _Tp(1) / __gamma(_Tp(1) + __mu);
103       __gammi = _Tp(1) / __gamma(_Tp(1) - __mu);
104 #endif
105
106       if (std::abs(__mu) < std::numeric_limits<_Tp>::epsilon())
107         __gam1 = -_Tp(__numeric_constants<_Tp>::__gamma_e());
108       else
109         __gam1 = (__gammi - __gampl) / (_Tp(2) * __mu);
110
111       __gam2 = (__gammi + __gampl) / (_Tp(2));
112
113       return;
114     }
115
116
117     /**
118      *   @brief  Compute the Bessel @f$ J_\nu(x) @f$ and Neumann
119      *           @f$ N_\nu(x) @f$ functions and their first derivatives
120      *           @f$ J'_\nu(x) @f$ and @f$ N'_\nu(x) @f$ respectively.
121      *           These four functions are computed together for numerical
122      *           stability.
123      *
124      *   @param  __nu  The order of the Bessel functions.
125      *   @param  __x   The argument of the Bessel functions.
126      *   @param  __Jnu  The output Bessel function of the first kind.
127      *   @param  __Nnu  The output Neumann function (Bessel function of the second kind).
128      *   @param  __Jpnu  The output derivative of the Bessel function of the first kind.
129      *   @param  __Npnu  The output derivative of the Neumann function.
130      */
131     template <typename _Tp>
132     void
133     __bessel_jn(const _Tp __nu, const _Tp __x,
134                 _Tp & __Jnu, _Tp & __Nnu, _Tp & __Jpnu, _Tp & __Npnu)
135     {
136       if (__x == _Tp(0))
137         {
138           if (__nu == _Tp(0))
139             {
140               __Jnu = _Tp(1);
141               __Jpnu = _Tp(0);
142             }
143           else if (__nu == _Tp(1))
144             {
145               __Jnu = _Tp(0);
146               __Jpnu = _Tp(0.5L);
147             }
148           else
149             {
150               __Jnu = _Tp(0);
151               __Jpnu = _Tp(0);
152             }
153           __Nnu = -std::numeric_limits<_Tp>::infinity();
154           __Npnu = std::numeric_limits<_Tp>::infinity();
155           return;
156         }
157
158       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
159       //  When the multiplier is N i.e.
160       //  fp_min = N * min()
161       //  Then J_0 and N_0 tank at x = 8 * N (J_0 = 0 and N_0 = nan)!
162       //const _Tp __fp_min = _Tp(20) * std::numeric_limits<_Tp>::min();
163       const _Tp __fp_min = std::sqrt(std::numeric_limits<_Tp>::min());
164       const int __max_iter = 15000;
165       const _Tp __x_min = _Tp(2);
166
167       const int __nl = (__x < __x_min
168                     ? static_cast<int>(__nu + _Tp(0.5L))
169                     : std::max(0, static_cast<int>(__nu - __x + _Tp(1.5L))));
170
171       const _Tp __mu = __nu - __nl;
172       const _Tp __mu2 = __mu * __mu;
173       const _Tp __xi = _Tp(1) / __x;
174       const _Tp __xi2 = _Tp(2) * __xi;
175       _Tp __w = __xi2 / __numeric_constants<_Tp>::__pi();
176       int __isign = 1;
177       _Tp __h = __nu * __xi;
178       if (__h < __fp_min)
179         __h = __fp_min;
180       _Tp __b = __xi2 * __nu;
181       _Tp __d = _Tp(0);
182       _Tp __c = __h;
183       int __i;
184       for (__i = 1; __i <= __max_iter; ++__i)
185         {
186           __b += __xi2;
187           __d = __b - __d;
188           if (std::abs(__d) < __fp_min)
189             __d = __fp_min;
190           __c = __b - _Tp(1) / __c;
191           if (std::abs(__c) < __fp_min)
192             __c = __fp_min;
193           __d = _Tp(1) / __d;
194           const _Tp __del = __c * __d;
195           __h *= __del;
196           if (__d < _Tp(0))
197             __isign = -__isign;
198           if (std::abs(__del - _Tp(1)) < __eps)
199             break;
200         }
201       if (__i > __max_iter)
202         std::__throw_runtime_error(__N("Argument x too large in __bessel_jn; "
203                                        "try asymptotic expansion."));
204       _Tp __Jnul = __isign * __fp_min;
205       _Tp __Jpnul = __h * __Jnul;
206       _Tp __Jnul1 = __Jnul;
207       _Tp __Jpnu1 = __Jpnul;
208       _Tp __fact = __nu * __xi;
209       for ( int __l = __nl; __l >= 1; --__l )
210         {
211           const _Tp __Jnutemp = __fact * __Jnul + __Jpnul;
212           __fact -= __xi;
213           __Jpnul = __fact * __Jnutemp - __Jnul;
214           __Jnul = __Jnutemp;
215         }
216       if (__Jnul == _Tp(0))
217         __Jnul = __eps;
218       _Tp __f= __Jpnul / __Jnul;
219       _Tp __Nmu, __Nnu1, __Npmu, __Jmu;
220       if (__x < __x_min)
221         {
222           const _Tp __x2 = __x / _Tp(2);
223           const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
224           _Tp __fact = (std::abs(__pimu) < __eps
225                       ? _Tp(1) : __pimu / std::sin(__pimu));
226           _Tp __d = -std::log(__x2);
227           _Tp __e = __mu * __d;
228           _Tp __fact2 = (std::abs(__e) < __eps
229                        ? _Tp(1) : std::sinh(__e) / __e);
230           _Tp __gam1, __gam2, __gampl, __gammi;
231           __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
232           _Tp __ff = (_Tp(2) / __numeric_constants<_Tp>::__pi())
233                    * __fact * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
234           __e = std::exp(__e);
235           _Tp __p = __e / (__numeric_constants<_Tp>::__pi() * __gampl);
236           _Tp __q = _Tp(1) / (__e * __numeric_constants<_Tp>::__pi() * __gammi);
237           const _Tp __pimu2 = __pimu / _Tp(2);
238           _Tp __fact3 = (std::abs(__pimu2) < __eps
239                        ? _Tp(1) : std::sin(__pimu2) / __pimu2 );
240           _Tp __r = __numeric_constants<_Tp>::__pi() * __pimu2 * __fact3 * __fact3;
241           _Tp __c = _Tp(1);
242           __d = -__x2 * __x2;
243           _Tp __sum = __ff + __r * __q;
244           _Tp __sum1 = __p;
245           for (__i = 1; __i <= __max_iter; ++__i)
246             {
247               __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
248               __c *= __d / _Tp(__i);
249               __p /= _Tp(__i) - __mu;
250               __q /= _Tp(__i) + __mu;
251               const _Tp __del = __c * (__ff + __r * __q);
252               __sum += __del; 
253               const _Tp __del1 = __c * __p - __i * __del;
254               __sum1 += __del1;
255               if ( std::abs(__del) < __eps * (_Tp(1) + std::abs(__sum)) )
256                 break;
257             }
258           if ( __i > __max_iter )
259             std::__throw_runtime_error(__N("Bessel y series failed to converge "
260                                            "in __bessel_jn."));
261           __Nmu = -__sum;
262           __Nnu1 = -__sum1 * __xi2;
263           __Npmu = __mu * __xi * __Nmu - __Nnu1;
264           __Jmu = __w / (__Npmu - __f * __Nmu);
265         }
266       else
267         {
268           _Tp __a = _Tp(0.25L) - __mu2;
269           _Tp __q = _Tp(1);
270           _Tp __p = -__xi / _Tp(2);
271           _Tp __br = _Tp(2) * __x;
272           _Tp __bi = _Tp(2);
273           _Tp __fact = __a * __xi / (__p * __p + __q * __q);
274           _Tp __cr = __br + __q * __fact;
275           _Tp __ci = __bi + __p * __fact;
276           _Tp __den = __br * __br + __bi * __bi;
277           _Tp __dr = __br / __den;
278           _Tp __di = -__bi / __den;
279           _Tp __dlr = __cr * __dr - __ci * __di;
280           _Tp __dli = __cr * __di + __ci * __dr;
281           _Tp __temp = __p * __dlr - __q * __dli;
282           __q = __p * __dli + __q * __dlr;
283           __p = __temp;
284           int __i;
285           for (__i = 2; __i <= __max_iter; ++__i)
286             {
287               __a += _Tp(2 * (__i - 1));
288               __bi += _Tp(2);
289               __dr = __a * __dr + __br;
290               __di = __a * __di + __bi;
291               if (std::abs(__dr) + std::abs(__di) < __fp_min)
292                 __dr = __fp_min;
293               __fact = __a / (__cr * __cr + __ci * __ci);
294               __cr = __br + __cr * __fact;
295               __ci = __bi - __ci * __fact;
296               if (std::abs(__cr) + std::abs(__ci) < __fp_min)
297                 __cr = __fp_min;
298               __den = __dr * __dr + __di * __di;
299               __dr /= __den;
300               __di /= -__den;
301               __dlr = __cr * __dr - __ci * __di;
302               __dli = __cr * __di + __ci * __dr;
303               __temp = __p * __dlr - __q * __dli;
304               __q = __p * __dli + __q * __dlr;
305               __p = __temp;
306               if (std::abs(__dlr - _Tp(1)) + std::abs(__dli) < __eps)
307                 break;
308           }
309           if (__i > __max_iter)
310             std::__throw_runtime_error(__N("Lentz's method failed "
311                                            "in __bessel_jn."));
312           const _Tp __gam = (__p - __f) / __q;
313           __Jmu = std::sqrt(__w / ((__p - __f) * __gam + __q));
314 #if _GLIBCXX_USE_C99_MATH_TR1
315           __Jmu = std::tr1::copysign(__Jmu, __Jnul);
316 #else
317           if (__Jmu * __Jnul < _Tp(0))
318             __Jmu = -__Jmu;
319 #endif
320           __Nmu = __gam * __Jmu;
321           __Npmu = (__p + __q / __gam) * __Nmu;
322           __Nnu1 = __mu * __xi * __Nmu - __Npmu;
323       }
324       __fact = __Jmu / __Jnul;
325       __Jnu = __fact * __Jnul1;
326       __Jpnu = __fact * __Jpnu1;
327       for (__i = 1; __i <= __nl; ++__i)
328         {
329           const _Tp __Nnutemp = (__mu + __i) * __xi2 * __Nnu1 - __Nmu;
330           __Nmu = __Nnu1;
331           __Nnu1 = __Nnutemp;
332         }
333       __Nnu = __Nmu;
334       __Npnu = __nu * __xi * __Nmu - __Nnu1;
335
336       return;
337     }
338
339
340     /**
341      *   @brief This routine computes the asymptotic cylindrical Bessel
342      *          and Neumann functions of order nu: \f$ J_{\nu} \f$,
343      *          \f$ N_{\nu} \f$.
344      *
345      *   References:
346      *    (1) Handbook of Mathematical Functions,
347      *        ed. Milton Abramowitz and Irene A. Stegun,
348      *        Dover Publications,
349      *        Section 9 p. 364, Equations 9.2.5-9.2.10
350      *
351      *   @param  __nu  The order of the Bessel functions.
352      *   @param  __x   The argument of the Bessel functions.
353      *   @param  __Jnu  The output Bessel function of the first kind.
354      *   @param  __Nnu  The output Neumann function (Bessel function of the second kind).
355      */
356     template <typename _Tp>
357     void
358     __cyl_bessel_jn_asymp(const _Tp __nu, const _Tp __x,
359                           _Tp & __Jnu, _Tp & __Nnu)
360     {
361       const _Tp __coef = std::sqrt(_Tp(2)
362                              / (__numeric_constants<_Tp>::__pi() * __x));
363       const _Tp __mu   = _Tp(4) * __nu * __nu;
364       const _Tp __mum1 = __mu - _Tp(1);
365       const _Tp __mum9 = __mu - _Tp(9);
366       const _Tp __mum25 = __mu - _Tp(25);
367       const _Tp __mum49 = __mu - _Tp(49);
368       const _Tp __xx = _Tp(64) * __x * __x;
369       const _Tp __P = _Tp(1) - __mum1 * __mum9 / (_Tp(2) * __xx)
370                     * (_Tp(1) - __mum25 * __mum49 / (_Tp(12) * __xx));
371       const _Tp __Q = __mum1 / (_Tp(8) * __x)
372                     * (_Tp(1) - __mum9 * __mum25 / (_Tp(6) * __xx));
373
374       const _Tp __chi = __x - (__nu + _Tp(0.5L))
375                             * __numeric_constants<_Tp>::__pi_2();
376       const _Tp __c = std::cos(__chi);
377       const _Tp __s = std::sin(__chi);
378
379       __Jnu = __coef * (__c * __P - __s * __Q);
380       __Nnu = __coef * (__s * __P + __c * __Q);
381
382       return;
383     }
384
385
386     /**
387      *   @brief This routine returns the cylindrical Bessel functions
388      *          of order \f$ \nu \f$: \f$ J_{\nu} \f$ or \f$ I_{\nu} \f$
389      *          by series expansion.
390      *
391      *   The modified cylindrical Bessel function is:
392      *   @f[
393      *    Z_{\nu}(x) = \sum_{k=0}^{\infty}
394      *              \frac{\sigma^k (x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
395      *   @f]
396      *   where \f$ \sigma = +1 \f$ or\f$  -1 \f$ for
397      *   \f$ Z = I \f$ or \f$ J \f$ respectively.
398      * 
399      *   See Abramowitz & Stegun, 9.1.10
400      *       Abramowitz & Stegun, 9.6.7
401      *    (1) Handbook of Mathematical Functions,
402      *        ed. Milton Abramowitz and Irene A. Stegun,
403      *        Dover Publications,
404      *        Equation 9.1.10 p. 360 and Equation 9.6.10 p. 375
405      *
406      *   @param  __nu  The order of the Bessel function.
407      *   @param  __x   The argument of the Bessel function.
408      *   @param  __sgn  The sign of the alternate terms
409      *                  -1 for the Bessel function of the first kind.
410      *                  +1 for the modified Bessel function of the first kind.
411      *   @return  The output Bessel function.
412      */
413     template <typename _Tp>
414     _Tp
415     __cyl_bessel_ij_series(const _Tp __nu, const _Tp __x, const _Tp __sgn,
416                            const unsigned int __max_iter)
417     {
418
419       const _Tp __x2 = __x / _Tp(2);
420       _Tp __fact = __nu * std::log(__x2);
421 #if _GLIBCXX_USE_C99_MATH_TR1
422       __fact -= std::tr1::lgamma(__nu + _Tp(1));
423 #else
424       __fact -= __log_gamma(__nu + _Tp(1));
425 #endif
426       __fact = std::exp(__fact);
427       const _Tp __xx4 = __sgn * __x2 * __x2;
428       _Tp __Jn = _Tp(1);
429       _Tp __term = _Tp(1);
430
431       for (unsigned int __i = 1; __i < __max_iter; ++__i)
432         {
433           __term *= __xx4 / (_Tp(__i) * (__nu + _Tp(__i)));
434           __Jn += __term;
435           if (std::abs(__term / __Jn) < std::numeric_limits<_Tp>::epsilon())
436             break;
437         }
438
439       return __fact * __Jn;
440     }
441
442
443     /**
444      *   @brief  Return the Bessel function of order \f$ \nu \f$:
445      *           \f$ J_{\nu}(x) \f$.
446      *
447      *   The cylindrical Bessel function is:
448      *   @f[
449      *    J_{\nu}(x) = \sum_{k=0}^{\infty}
450      *              \frac{(-1)^k (x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
451      *   @f]
452      *
453      *   @param  __nu  The order of the Bessel function.
454      *   @param  __x   The argument of the Bessel function.
455      *   @return  The output Bessel function.
456      */
457     template<typename _Tp>
458     _Tp
459     __cyl_bessel_j(const _Tp __nu, const _Tp __x)
460     {
461       if (__nu < _Tp(0) || __x < _Tp(0))
462         std::__throw_domain_error(__N("Bad argument "
463                                       "in __cyl_bessel_j."));
464       else if (__isnan(__nu) || __isnan(__x))
465         return std::numeric_limits<_Tp>::quiet_NaN();
466       else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
467         return __cyl_bessel_ij_series(__nu, __x, -_Tp(1), 200);
468       else if (__x > _Tp(1000))
469         {
470           _Tp __J_nu, __N_nu;
471           __cyl_bessel_jn_asymp(__nu, __x, __J_nu, __N_nu);
472           return __J_nu;
473         }
474       else
475         {
476           _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
477           __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
478           return __J_nu;
479         }
480     }
481
482
483     /**
484      *   @brief  Return the Neumann function of order \f$ \nu \f$:
485      *           \f$ N_{\nu}(x) \f$.
486      *
487      *   The Neumann function is defined by:
488      *   @f[
489      *      N_{\nu}(x) = \frac{J_{\nu}(x) \cos \nu\pi - J_{-\nu}(x)}
490      *                        {\sin \nu\pi}
491      *   @f]
492      *   where for integral \f$ \nu = n \f$ a limit is taken:
493      *   \f$ lim_{\nu \to n} \f$.
494      *
495      *   @param  __nu  The order of the Neumann function.
496      *   @param  __x   The argument of the Neumann function.
497      *   @return  The output Neumann function.
498      */
499     template<typename _Tp>
500     _Tp
501     __cyl_neumann_n(const _Tp __nu, const _Tp __x)
502     {
503       if (__nu < _Tp(0) || __x < _Tp(0))
504         std::__throw_domain_error(__N("Bad argument "
505                                       "in __cyl_neumann_n."));
506       else if (__isnan(__nu) || __isnan(__x))
507         return std::numeric_limits<_Tp>::quiet_NaN();
508       else if (__x > _Tp(1000))
509         {
510           _Tp __J_nu, __N_nu;
511           __cyl_bessel_jn_asymp(__nu, __x, __J_nu, __N_nu);
512           return __N_nu;
513         }
514       else
515         {
516           _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
517           __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
518           return __N_nu;
519         }
520     }
521
522
523     /**
524      *   @brief  Compute the spherical Bessel @f$ j_n(x) @f$
525      *           and Neumann @f$ n_n(x) @f$ functions and their first
526      *           derivatives @f$ j'_n(x) @f$ and @f$ n'_n(x) @f$
527      *           respectively.
528      *
529      *   @param  __n  The order of the spherical Bessel function.
530      *   @param  __x  The argument of the spherical Bessel function.
531      *   @param  __j_n  The output spherical Bessel function.
532      *   @param  __n_n  The output spherical Neumann function.
533      *   @param  __jp_n  The output derivative of the spherical Bessel function.
534      *   @param  __np_n  The output derivative of the spherical Neumann function.
535      */
536     template <typename _Tp>
537     void
538     __sph_bessel_jn(const unsigned int __n, const _Tp __x,
539                     _Tp & __j_n, _Tp & __n_n, _Tp & __jp_n, _Tp & __np_n)
540     {
541       const _Tp __nu = _Tp(__n) + _Tp(0.5L);
542
543       _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
544       __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
545
546       const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
547                          / std::sqrt(__x);
548
549       __j_n = __factor * __J_nu;
550       __n_n = __factor * __N_nu;
551       __jp_n = __factor * __Jp_nu - __j_n / (_Tp(2) * __x);
552       __np_n = __factor * __Np_nu - __n_n / (_Tp(2) * __x);
553
554       return;
555     }
556
557
558     /**
559      *   @brief  Return the spherical Bessel function
560      *           @f$ j_n(x) @f$ of order n.
561      *
562      *   The spherical Bessel function is defined by:
563      *   @f[
564      *    j_n(x) = \left( \frac{\pi}{2x} \right) ^{1/2} J_{n+1/2}(x)
565      *   @f]
566      *
567      *   @param  __n  The order of the spherical Bessel function.
568      *   @param  __x  The argument of the spherical Bessel function.
569      *   @return  The output spherical Bessel function.
570      */
571     template <typename _Tp>
572     _Tp
573     __sph_bessel(const unsigned int __n, const _Tp __x)
574     {
575       if (__x < _Tp(0))
576         std::__throw_domain_error(__N("Bad argument "
577                                       "in __sph_bessel."));
578       else if (__isnan(__x))
579         return std::numeric_limits<_Tp>::quiet_NaN();
580       else if (__x == _Tp(0))
581         {
582           if (__n == 0)
583             return _Tp(1);
584           else
585             return _Tp(0);
586         }
587       else
588         {
589           _Tp __j_n, __n_n, __jp_n, __np_n;
590           __sph_bessel_jn(__n, __x, __j_n, __n_n, __jp_n, __np_n);
591           return __j_n;
592         }
593     }
594
595
596     /**
597      *   @brief  Return the spherical Neumann function
598      *           @f$ n_n(x) @f$.
599      *
600      *   The spherical Neumann function is defined by:
601      *   @f[
602      *    n_n(x) = \left( \frac{\pi}{2x} \right) ^{1/2} N_{n+1/2}(x)
603      *   @f]
604      *
605      *   @param  __n  The order of the spherical Neumann function.
606      *   @param  __x  The argument of the spherical Neumann function.
607      *   @return  The output spherical Neumann function.
608      */
609     template <typename _Tp>
610     _Tp
611     __sph_neumann(const unsigned int __n, const _Tp __x)
612     {
613       if (__x < _Tp(0))
614         std::__throw_domain_error(__N("Bad argument "
615                                       "in __sph_neumann."));
616       else if (__isnan(__x))
617         return std::numeric_limits<_Tp>::quiet_NaN();
618       else if (__x == _Tp(0))
619         return -std::numeric_limits<_Tp>::infinity();
620       else
621         {
622           _Tp __j_n, __n_n, __jp_n, __np_n;
623           __sph_bessel_jn(__n, __x, __j_n, __n_n, __jp_n, __np_n);
624           return __n_n;
625         }
626     }
627
628   } // namespace std::tr1::__detail
629 }
630 }
631
632 #endif // _GLIBCXX_TR1_BESSEL_FUNCTION_TCC