1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008
4 // Free Software Foundation, Inc.
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)
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.
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,
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.
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.
37 // ISO C++ 14882 TR1: 5.2 Special functions
40 // Written by Edward Smith-Rowland.
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.
52 #ifndef _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC
53 #define _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC 1
55 #include "special_function_util.h"
62 // [5.2] Special functions
64 // Implementation-space details.
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
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.
84 template <typename _Tp>
86 __bessel_ik(const _Tp __nu, const _Tp __x,
87 _Tp & __Inu, _Tp & __Knu, _Tp & __Ipnu, _Tp & __Kpnu)
96 else if (__nu == _Tp(1))
106 __Knu = std::numeric_limits<_Tp>::infinity();
107 __Kpnu = -std::numeric_limits<_Tp>::infinity();
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);
116 const int __nl = static_cast<int>(__nu + _Tp(0.5L));
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 )
125 _Tp __b = __xi2 * __nu;
129 for ( __i = 1; __i <= __max_iter; ++__i )
132 __d = _Tp(1) / (__b + __d);
133 __c = __b + _Tp(1) / __c;
134 const _Tp __del = __c * __d;
136 if (std::abs(__del - _Tp(1)) < __eps)
139 if (__i > __max_iter)
140 std::__throw_runtime_error(__N("Argument x too large "
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)
150 const _Tp __Inutemp = __fact * __Inul + __Ipnul;
152 __Ipnul = __fact * __Inutemp + __Inul;
155 _Tp __f = __Ipnul / __Inul;
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);
170 * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
173 _Tp __p = __e / (_Tp(2) * __gampl);
174 _Tp __q = _Tp(1) / (_Tp(2) * __e * __gammi);
179 for (__i = 1; __i <= __max_iter; ++__i)
181 __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
185 const _Tp __del = __c * __ff;
187 const _Tp __del1 = __c * (__p - __i * __ff);
189 if (std::abs(__del) < __eps * std::abs(__sum))
192 if (__i > __max_iter)
193 std::__throw_runtime_error(__N("Bessel k series failed to converge "
196 __Knu1 = __sum1 * __xi2;
200 _Tp __b = _Tp(2) * (_Tp(1) + __x);
201 _Tp __d = _Tp(1) / __b;
206 _Tp __a1 = _Tp(0.25L) - __mu2;
207 _Tp __q = __c = __a1;
209 _Tp __s = _Tp(1) + __q * __delh;
211 for (__i = 2; __i <= __max_iter; ++__i)
213 __a -= 2 * (__i - 1);
214 __c = -__a * __c / __i;
215 const _Tp __qnew = (__q1 - __b * __q2) / __a;
220 __d = _Tp(1) / (__b + __a * __d);
221 __delh = (__b * __d - _Tp(1)) * __delh;
223 const _Tp __dels = __q * __delh;
225 if ( std::abs(__dels / __s) < __eps )
228 if (__i > __max_iter)
229 std::__throw_runtime_error(__N("Steed's method failed "
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;
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 )
243 const _Tp __Knutemp = (__mu + __i) * __xi2 * __Knu1 + __Kmu;
248 __Kpnu = __nu * __xi * __Kmu - __Knu1;
255 * @brief Return the regular modified Bessel function of order
256 * \f$ \nu \f$: \f$ I_{\nu}(x) \f$.
258 * The regular modified cylindrical Bessel function is:
260 * I_{\nu}(x) = \sum_{k=0}^{\infty}
261 * \frac{(x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
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.
268 template<typename _Tp>
270 __cyl_bessel_i(const _Tp __nu, const _Tp __x)
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);
281 _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
282 __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
289 * @brief Return the irregular modified Bessel function
290 * \f$ K_{\nu}(x) \f$ of order \f$ \nu \f$.
292 * The irregular modified Bessel function is defined by:
294 * K_{\nu}(x) = \frac{\pi}{2}
295 * \frac{I_{-\nu}(x) - I_{\nu}(x)}{\sin \nu\pi}
297 * where for integral \f$ \nu = n \f$ a limit is taken:
298 * \f$ lim_{\nu \to n} \f$.
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.
304 template<typename _Tp>
306 __cyl_bessel_k(const _Tp __nu, const _Tp __x)
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();
315 _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
316 __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
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$
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
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.
338 template <typename _Tp>
340 __sph_bessel_ik(const unsigned int __n, const _Tp __x,
341 _Tp & __i_n, _Tp & __k_n, _Tp & __ip_n, _Tp & __kp_n)
343 const _Tp __nu = _Tp(__n) + _Tp(0.5L);
345 _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
346 __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
348 const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
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);
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$
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.
373 template <typename _Tp>
375 __airy(const _Tp __x,
376 _Tp & __Ai, _Tp & __Bi, _Tp & __Aip, _Tp & __Bip)
378 const _Tp __absx = std::abs(__x);
379 const _Tp __rootx = std::sqrt(__absx);
380 const _Tp __z = _Tp(2) * __absx * __rootx / _Tp(3);
383 return std::numeric_limits<_Tp>::quiet_NaN();
384 else if (__x > _Tp(0))
386 _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
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());
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()
401 / __numeric_constants<_Tp>::__sqrt3());
403 else if (__x < _Tp(0))
405 _Tp __J_nu, __Jp_nu, __N_nu, __Np_nu;
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);
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()
416 __Bip = __absx * (__J_nu / __numeric_constants<_Tp>::__sqrt3()
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();
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();
437 } // namespace std::tr1::__detail
441 #endif // _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC