]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/tr1/modified_bessel_func.tcc
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / tr1 / modified_bessel_func.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/modified_bessel_func.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. 246-249.
51
52 #ifndef _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC
53 #define _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_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 modified Bessel functions @f$ I_\nu(x) @f$ and
70      *           @f$ K_\nu(x) @f$ and their first derivatives
71      *           @f$ I'_\nu(x) @f$ and @f$ K'_\nu(x) @f$ respectively.
72      *           These four functions are computed together for numerical
73      *           stability.
74      *
75      *   @param  __nu  The order of the Bessel functions.
76      *   @param  __x   The argument of the Bessel functions.
77      *   @param  __Inu  The output regular modified Bessel function.
78      *   @param  __Knu  The output irregular modified Bessel function.
79      *   @param  __Ipnu  The output derivative of the regular
80      *                   modified Bessel function.
81      *   @param  __Kpnu  The output derivative of the irregular
82      *                   modified Bessel function.
83      */
84     template <typename _Tp>
85     void
86     __bessel_ik(const _Tp __nu, const _Tp __x,
87                 _Tp & __Inu, _Tp & __Knu, _Tp & __Ipnu, _Tp & __Kpnu)
88     {
89       if (__x == _Tp(0))
90         {
91           if (__nu == _Tp(0))
92             {
93               __Inu = _Tp(1);
94               __Ipnu = _Tp(0);
95             }
96           else if (__nu == _Tp(1))
97             {
98               __Inu = _Tp(0);
99               __Ipnu = _Tp(0.5L);
100             }
101           else
102             {
103               __Inu = _Tp(0);
104               __Ipnu = _Tp(0);
105             }
106           __Knu = std::numeric_limits<_Tp>::infinity();
107           __Kpnu = -std::numeric_limits<_Tp>::infinity();
108           return;
109         }
110
111       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
112       const _Tp __fp_min = _Tp(10) * std::numeric_limits<_Tp>::epsilon();
113       const int __max_iter = 15000;
114       const _Tp __x_min = _Tp(2);
115
116       const int __nl = static_cast<int>(__nu + _Tp(0.5L));
117
118       const _Tp __mu = __nu - __nl;
119       const _Tp __mu2 = __mu * __mu;
120       const _Tp __xi = _Tp(1) / __x;
121       const _Tp __xi2 = _Tp(2) * __xi;
122       _Tp __h = __nu * __xi;
123       if ( __h < __fp_min )
124         __h = __fp_min;
125       _Tp __b = __xi2 * __nu;
126       _Tp __d = _Tp(0);
127       _Tp __c = __h;
128       int __i;
129       for ( __i = 1; __i <= __max_iter; ++__i )
130         {
131           __b += __xi2;
132           __d = _Tp(1) / (__b + __d);
133           __c = __b + _Tp(1) / __c;
134           const _Tp __del = __c * __d;
135           __h *= __del;
136           if (std::abs(__del - _Tp(1)) < __eps)
137             break;
138         }
139       if (__i > __max_iter)
140         std::__throw_runtime_error(__N("Argument x too large "
141                                        "in __bessel_jn; "
142                                        "try asymptotic expansion."));
143       _Tp __Inul = __fp_min;
144       _Tp __Ipnul = __h * __Inul;
145       _Tp __Inul1 = __Inul;
146       _Tp __Ipnu1 = __Ipnul;
147       _Tp __fact = __nu * __xi;
148       for (int __l = __nl; __l >= 1; --__l)
149         {
150           const _Tp __Inutemp = __fact * __Inul + __Ipnul;
151           __fact -= __xi;
152           __Ipnul = __fact * __Inutemp + __Inul;
153           __Inul = __Inutemp;
154         }
155       _Tp __f = __Ipnul / __Inul;
156       _Tp __Kmu, __Knu1;
157       if (__x < __x_min)
158         {
159           const _Tp __x2 = __x / _Tp(2);
160           const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
161           const _Tp __fact = (std::abs(__pimu) < __eps
162                             ? _Tp(1) : __pimu / std::sin(__pimu));
163           _Tp __d = -std::log(__x2);
164           _Tp __e = __mu * __d;
165           const _Tp __fact2 = (std::abs(__e) < __eps
166                             ? _Tp(1) : std::sinh(__e) / __e);
167           _Tp __gam1, __gam2, __gampl, __gammi;
168           __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
169           _Tp __ff = __fact
170                    * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
171           _Tp __sum = __ff;
172           __e = std::exp(__e);
173           _Tp __p = __e / (_Tp(2) * __gampl);
174           _Tp __q = _Tp(1) / (_Tp(2) * __e * __gammi);
175           _Tp __c = _Tp(1);
176           __d = __x2 * __x2;
177           _Tp __sum1 = __p;
178           int __i;
179           for (__i = 1; __i <= __max_iter; ++__i)
180             {
181               __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
182               __c *= __d / __i;
183               __p /= __i - __mu;
184               __q /= __i + __mu;
185               const _Tp __del = __c * __ff;
186               __sum += __del; 
187               const _Tp __del1 = __c * (__p - __i * __ff);
188               __sum1 += __del1;
189               if (std::abs(__del) < __eps * std::abs(__sum))
190                 break;
191             }
192           if (__i > __max_iter)
193             std::__throw_runtime_error(__N("Bessel k series failed to converge "
194                                            "in __bessel_jn."));
195           __Kmu = __sum;
196           __Knu1 = __sum1 * __xi2;
197         }
198       else
199         {
200           _Tp __b = _Tp(2) * (_Tp(1) + __x);
201           _Tp __d = _Tp(1) / __b;
202           _Tp __delh = __d;
203           _Tp __h = __delh;
204           _Tp __q1 = _Tp(0);
205           _Tp __q2 = _Tp(1);
206           _Tp __a1 = _Tp(0.25L) - __mu2;
207           _Tp __q = __c = __a1;
208           _Tp __a = -__a1;
209           _Tp __s = _Tp(1) + __q * __delh;
210           int __i;
211           for (__i = 2; __i <= __max_iter; ++__i)
212             {
213               __a -= 2 * (__i - 1);
214               __c = -__a * __c / __i;
215               const _Tp __qnew = (__q1 - __b * __q2) / __a;
216               __q1 = __q2;
217               __q2 = __qnew;
218               __q += __c * __qnew;
219               __b += _Tp(2);
220               __d = _Tp(1) / (__b + __a * __d);
221               __delh = (__b * __d - _Tp(1)) * __delh;
222               __h += __delh;
223               const _Tp __dels = __q * __delh;
224               __s += __dels;
225               if ( std::abs(__dels / __s) < __eps )
226                 break;
227             }
228           if (__i > __max_iter)
229             std::__throw_runtime_error(__N("Steed's method failed "
230                                            "in __bessel_jn."));
231           __h = __a1 * __h;
232           __Kmu = std::sqrt(__numeric_constants<_Tp>::__pi() / (_Tp(2) * __x))
233                 * std::exp(-__x) / __s;
234           __Knu1 = __Kmu * (__mu + __x + _Tp(0.5L) - __h) * __xi;
235         }
236
237       _Tp __Kpmu = __mu * __xi * __Kmu - __Knu1;
238       _Tp __Inumu = __xi / (__f * __Kmu - __Kpmu);
239       __Inu = __Inumu * __Inul1 / __Inul;
240       __Ipnu = __Inumu * __Ipnu1 / __Inul;
241       for ( __i = 1; __i <= __nl; ++__i )
242         {
243           const _Tp __Knutemp = (__mu + __i) * __xi2 * __Knu1 + __Kmu;
244           __Kmu = __Knu1;
245           __Knu1 = __Knutemp;
246         }
247       __Knu = __Kmu;
248       __Kpnu = __nu * __xi * __Kmu - __Knu1;
249   
250       return;
251     }
252
253
254     /**
255      *   @brief  Return the regular modified Bessel function of order
256      *           \f$ \nu \f$: \f$ I_{\nu}(x) \f$.
257      *
258      *   The regular modified cylindrical Bessel function is:
259      *   @f[
260      *    I_{\nu}(x) = \sum_{k=0}^{\infty}
261      *              \frac{(x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
262      *   @f]
263      *
264      *   @param  __nu  The order of the regular modified Bessel function.
265      *   @param  __x   The argument of the regular modified Bessel function.
266      *   @return  The output regular modified Bessel function.
267      */
268     template<typename _Tp>
269     _Tp
270     __cyl_bessel_i(const _Tp __nu, const _Tp __x)
271     {
272       if (__nu < _Tp(0) || __x < _Tp(0))
273         std::__throw_domain_error(__N("Bad argument "
274                                       "in __cyl_bessel_i."));
275       else if (__isnan(__nu) || __isnan(__x))
276         return std::numeric_limits<_Tp>::quiet_NaN();
277       else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
278         return __cyl_bessel_ij_series(__nu, __x, +_Tp(1), 200);
279       else
280         {
281           _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
282           __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
283           return __I_nu;
284         }
285     }
286
287
288     /**
289      *   @brief  Return the irregular modified Bessel function
290      *           \f$ K_{\nu}(x) \f$ of order \f$ \nu \f$.
291      *
292      *   The irregular modified Bessel function is defined by:
293      *   @f[
294      *      K_{\nu}(x) = \frac{\pi}{2}
295      *                   \frac{I_{-\nu}(x) - I_{\nu}(x)}{\sin \nu\pi}
296      *   @f]
297      *   where for integral \f$ \nu = n \f$ a limit is taken:
298      *   \f$ lim_{\nu \to n} \f$.
299      *
300      *   @param  __nu  The order of the irregular modified Bessel function.
301      *   @param  __x   The argument of the irregular modified Bessel function.
302      *   @return  The output irregular modified Bessel function.
303      */
304     template<typename _Tp>
305     _Tp
306     __cyl_bessel_k(const _Tp __nu, const _Tp __x)
307     {
308       if (__nu < _Tp(0) || __x < _Tp(0))
309         std::__throw_domain_error(__N("Bad argument "
310                                       "in __cyl_bessel_k."));
311       else if (__isnan(__nu) || __isnan(__x))
312         return std::numeric_limits<_Tp>::quiet_NaN();
313       else
314         {
315           _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
316           __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
317           return __K_nu;
318         }
319     }
320
321
322     /**
323      *   @brief  Compute the spherical modified Bessel functions
324      *           @f$ i_n(x) @f$ and @f$ k_n(x) @f$ and their first
325      *           derivatives @f$ i'_n(x) @f$ and @f$ k'_n(x) @f$
326      *           respectively.
327      *
328      *   @param  __n  The order of the modified spherical Bessel function.
329      *   @param  __x  The argument of the modified spherical Bessel function.
330      *   @param  __i_n  The output regular modified spherical Bessel function.
331      *   @param  __k_n  The output irregular modified spherical
332      *                  Bessel function.
333      *   @param  __ip_n  The output derivative of the regular modified
334      *                   spherical Bessel function.
335      *   @param  __kp_n  The output derivative of the irregular modified
336      *                   spherical Bessel function.
337      */
338     template <typename _Tp>
339     void
340     __sph_bessel_ik(const unsigned int __n, const _Tp __x,
341                     _Tp & __i_n, _Tp & __k_n, _Tp & __ip_n, _Tp & __kp_n)
342     {
343       const _Tp __nu = _Tp(__n) + _Tp(0.5L);
344
345       _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
346       __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
347
348       const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
349                          / std::sqrt(__x);
350
351       __i_n = __factor * __I_nu;
352       __k_n = __factor * __K_nu;
353       __ip_n = __factor * __Ip_nu - __i_n / (_Tp(2) * __x);
354       __kp_n = __factor * __Kp_nu - __k_n / (_Tp(2) * __x);
355
356       return;
357     }
358
359
360     /**
361      *   @brief  Compute the Airy functions
362      *           @f$ Ai(x) @f$ and @f$ Bi(x) @f$ and their first
363      *           derivatives @f$ Ai'(x) @f$ and @f$ Bi(x) @f$
364      *           respectively.
365      *
366      *   @param  __n  The order of the Airy functions.
367      *   @param  __x  The argument of the Airy functions.
368      *   @param  __i_n  The output Airy function.
369      *   @param  __k_n  The output Airy function.
370      *   @param  __ip_n  The output derivative of the Airy function.
371      *   @param  __kp_n  The output derivative of the Airy function.
372      */
373     template <typename _Tp>
374     void
375     __airy(const _Tp __x,
376            _Tp & __Ai, _Tp & __Bi, _Tp & __Aip, _Tp & __Bip)
377     {
378       const _Tp __absx = std::abs(__x);
379       const _Tp __rootx = std::sqrt(__absx);
380       const _Tp __z = _Tp(2) * __absx * __rootx / _Tp(3);
381
382       if (__isnan(__x))
383         return std::numeric_limits<_Tp>::quiet_NaN();
384       else if (__x > _Tp(0))
385         {
386           _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
387
388           __bessel_ik(_Tp(1) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
389           __Ai = __rootx * __K_nu
390                / (__numeric_constants<_Tp>::__sqrt3()
391                 * __numeric_constants<_Tp>::__pi());
392           __Bi = __rootx * (__K_nu / __numeric_constants<_Tp>::__pi()
393                  + _Tp(2) * __I_nu / __numeric_constants<_Tp>::__sqrt3());
394
395           __bessel_ik(_Tp(2) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
396           __Aip = -__x * __K_nu
397                 / (__numeric_constants<_Tp>::__sqrt3()
398                  * __numeric_constants<_Tp>::__pi());
399           __Bip = __x * (__K_nu / __numeric_constants<_Tp>::__pi()
400                       + _Tp(2) * __I_nu
401                       / __numeric_constants<_Tp>::__sqrt3());
402         }
403       else if (__x < _Tp(0))
404         {
405           _Tp __J_nu, __Jp_nu, __N_nu, __Np_nu;
406
407           __bessel_jn(_Tp(1) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
408           __Ai = __rootx * (__J_nu
409                     - __N_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
410           __Bi = -__rootx * (__N_nu
411                     + __J_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
412
413           __bessel_jn(_Tp(2) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
414           __Aip = __absx * (__N_nu / __numeric_constants<_Tp>::__sqrt3()
415                           + __J_nu) / _Tp(2);
416           __Bip = __absx * (__J_nu / __numeric_constants<_Tp>::__sqrt3()
417                           - __N_nu) / _Tp(2);
418         }
419       else
420         {
421           //  Reference:
422           //    Abramowitz & Stegun, page 446 section 10.4.4 on Airy functions.
423           //  The number is Ai(0) = 3^{-2/3}/\Gamma(2/3).
424           __Ai = _Tp(0.35502805388781723926L);
425           __Bi = __Ai * __numeric_constants<_Tp>::__sqrt3();
426
427           //  Reference:
428           //    Abramowitz & Stegun, page 446 section 10.4.5 on Airy functions.
429           //  The number is Ai'(0) = -3^{-1/3}/\Gamma(1/3).
430           __Aip = -_Tp(0.25881940379280679840L);
431           __Bip = -__Aip * __numeric_constants<_Tp>::__sqrt3();
432         }
433
434       return;
435     }
436
437   } // namespace std::tr1::__detail
438 }
439 }
440
441 #endif // _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC