STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Macros | Functions
modified_bessel_func.tcc File Reference
#include "special_function_util.h"

Macros

#define _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC   1
 

Functions

namespace std _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This is an internal header file, included by other library headers. Do not attempt to use it directly. {tr1/cmath}

Macro Definition Documentation

#define _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC   1

Function Documentation

namespace std _GLIBCXX_VISIBILITY ( default  )

Compute the modified Bessel functions $ I_\nu(x) $ and $ K_\nu(x) $ and their first derivatives $ I'_\nu(x) $ and $ K'_\nu(x) $ respectively. These four functions are computed together for numerical stability.

Parameters
__nuThe order of the Bessel functions.
__xThe argument of the Bessel functions.
__InuThe output regular modified Bessel function.
__KnuThe output irregular modified Bessel function.
__IpnuThe output derivative of the regular modified Bessel function.
__KpnuThe output derivative of the irregular modified Bessel function.

Return the regular modified Bessel function of order $ \nu $: $ I_{\nu}(x) $.

The regular modified cylindrical Bessel function is:

\[ I_{\nu}(x) = \sum_{k=0}^{\infty} \frac{(x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)} \]

Parameters
__nuThe order of the regular modified Bessel function.
__xThe argument of the regular modified Bessel function.
Returns
The output regular modified Bessel function.

Return the irregular modified Bessel function $ K_{\nu}(x) $ of order $ \nu $.

The irregular modified Bessel function is defined by:

\[ K_{\nu}(x) = \frac{\pi}{2} \frac{I_{-\nu}(x) - I_{\nu}(x)}{\sin \nu\pi} \]

where for integral $ \nu = n $ a limit is taken: $ lim_{\nu \to n} $.

Parameters
__nuThe order of the irregular modified Bessel function.
__xThe argument of the irregular modified Bessel function.
Returns
The output irregular modified Bessel function.

Compute the spherical modified Bessel functions $ i_n(x) $ and $ k_n(x) $ and their first derivatives $ i'_n(x) $ and $ k'_n(x) $ respectively.

Parameters
__nThe order of the modified spherical Bessel function.
__xThe argument of the modified spherical Bessel function.
__i_nThe output regular modified spherical Bessel function.
__k_nThe output irregular modified spherical Bessel function.
__ip_nThe output derivative of the regular modified spherical Bessel function.
__kp_nThe output derivative of the irregular modified spherical Bessel function.

Compute the Airy functions $ Ai(x) $ and $ Bi(x) $ and their first derivatives $ Ai'(x) $ and $ Bi(x) $ respectively.

Parameters
__nThe order of the Airy functions.
__xThe argument of the Airy functions.
__i_nThe output Airy function.
__k_nThe output Airy function.
__ip_nThe output derivative of the Airy function.
__kp_nThe output derivative of the Airy function.
52 {
53 namespace tr1
54 {
55  // [5.2] Special functions
56 
57  // Implementation-space details.
58  namespace __detail
59  {
60  _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 
78  template <typename _Tp>
79  void
80  __bessel_ik(_Tp __nu, _Tp __x,
81  _Tp & __Inu, _Tp & __Knu, _Tp & __Ipnu, _Tp & __Kpnu)
82  {
83  if (__x == _Tp(0))
84  {
85  if (__nu == _Tp(0))
86  {
87  __Inu = _Tp(1);
88  __Ipnu = _Tp(0);
89  }
90  else if (__nu == _Tp(1))
91  {
92  __Inu = _Tp(0);
93  __Ipnu = _Tp(0.5L);
94  }
95  else
96  {
97  __Inu = _Tp(0);
98  __Ipnu = _Tp(0);
99  }
100  __Knu = std::numeric_limits<_Tp>::infinity();
101  __Kpnu = -std::numeric_limits<_Tp>::infinity();
102  return;
103  }
104 
105  const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
106  const _Tp __fp_min = _Tp(10) * std::numeric_limits<_Tp>::epsilon();
107  const int __max_iter = 15000;
108  const _Tp __x_min = _Tp(2);
109 
110  const int __nl = static_cast<int>(__nu + _Tp(0.5L));
111 
112  const _Tp __mu = __nu - __nl;
113  const _Tp __mu2 = __mu * __mu;
114  const _Tp __xi = _Tp(1) / __x;
115  const _Tp __xi2 = _Tp(2) * __xi;
116  _Tp __h = __nu * __xi;
117  if ( __h < __fp_min )
118  __h = __fp_min;
119  _Tp __b = __xi2 * __nu;
120  _Tp __d = _Tp(0);
121  _Tp __c = __h;
122  int __i;
123  for ( __i = 1; __i <= __max_iter; ++__i )
124  {
125  __b += __xi2;
126  __d = _Tp(1) / (__b + __d);
127  __c = __b + _Tp(1) / __c;
128  const _Tp __del = __c * __d;
129  __h *= __del;
130  if (std::abs(__del - _Tp(1)) < __eps)
131  break;
132  }
133  if (__i > __max_iter)
134  std::__throw_runtime_error(__N("Argument x too large "
135  "in __bessel_ik; "
136  "try asymptotic expansion."));
137  _Tp __Inul = __fp_min;
138  _Tp __Ipnul = __h * __Inul;
139  _Tp __Inul1 = __Inul;
140  _Tp __Ipnu1 = __Ipnul;
141  _Tp __fact = __nu * __xi;
142  for (int __l = __nl; __l >= 1; --__l)
143  {
144  const _Tp __Inutemp = __fact * __Inul + __Ipnul;
145  __fact -= __xi;
146  __Ipnul = __fact * __Inutemp + __Inul;
147  __Inul = __Inutemp;
148  }
149  _Tp __f = __Ipnul / __Inul;
150  _Tp __Kmu, __Knu1;
151  if (__x < __x_min)
152  {
153  const _Tp __x2 = __x / _Tp(2);
154  const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
155  const _Tp __fact = (std::abs(__pimu) < __eps
156  ? _Tp(1) : __pimu / std::sin(__pimu));
157  _Tp __d = -std::log(__x2);
158  _Tp __e = __mu * __d;
159  const _Tp __fact2 = (std::abs(__e) < __eps
160  ? _Tp(1) : std::sinh(__e) / __e);
161  _Tp __gam1, __gam2, __gampl, __gammi;
162  __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
163  _Tp __ff = __fact
164  * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
165  _Tp __sum = __ff;
166  __e = std::exp(__e);
167  _Tp __p = __e / (_Tp(2) * __gampl);
168  _Tp __q = _Tp(1) / (_Tp(2) * __e * __gammi);
169  _Tp __c = _Tp(1);
170  __d = __x2 * __x2;
171  _Tp __sum1 = __p;
172  int __i;
173  for (__i = 1; __i <= __max_iter; ++__i)
174  {
175  __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
176  __c *= __d / __i;
177  __p /= __i - __mu;
178  __q /= __i + __mu;
179  const _Tp __del = __c * __ff;
180  __sum += __del;
181  const _Tp __del1 = __c * (__p - __i * __ff);
182  __sum1 += __del1;
183  if (std::abs(__del) < __eps * std::abs(__sum))
184  break;
185  }
186  if (__i > __max_iter)
187  std::__throw_runtime_error(__N("Bessel k series failed to converge "
188  "in __bessel_ik."));
189  __Kmu = __sum;
190  __Knu1 = __sum1 * __xi2;
191  }
192  else
193  {
194  _Tp __b = _Tp(2) * (_Tp(1) + __x);
195  _Tp __d = _Tp(1) / __b;
196  _Tp __delh = __d;
197  _Tp __h = __delh;
198  _Tp __q1 = _Tp(0);
199  _Tp __q2 = _Tp(1);
200  _Tp __a1 = _Tp(0.25L) - __mu2;
201  _Tp __q = __c = __a1;
202  _Tp __a = -__a1;
203  _Tp __s = _Tp(1) + __q * __delh;
204  int __i;
205  for (__i = 2; __i <= __max_iter; ++__i)
206  {
207  __a -= 2 * (__i - 1);
208  __c = -__a * __c / __i;
209  const _Tp __qnew = (__q1 - __b * __q2) / __a;
210  __q1 = __q2;
211  __q2 = __qnew;
212  __q += __c * __qnew;
213  __b += _Tp(2);
214  __d = _Tp(1) / (__b + __a * __d);
215  __delh = (__b * __d - _Tp(1)) * __delh;
216  __h += __delh;
217  const _Tp __dels = __q * __delh;
218  __s += __dels;
219  if ( std::abs(__dels / __s) < __eps )
220  break;
221  }
222  if (__i > __max_iter)
223  std::__throw_runtime_error(__N("Steed's method failed "
224  "in __bessel_ik."));
225  __h = __a1 * __h;
226  __Kmu = std::sqrt(__numeric_constants<_Tp>::__pi() / (_Tp(2) * __x))
227  * std::exp(-__x) / __s;
228  __Knu1 = __Kmu * (__mu + __x + _Tp(0.5L) - __h) * __xi;
229  }
230 
231  _Tp __Kpmu = __mu * __xi * __Kmu - __Knu1;
232  _Tp __Inumu = __xi / (__f * __Kmu - __Kpmu);
233  __Inu = __Inumu * __Inul1 / __Inul;
234  __Ipnu = __Inumu * __Ipnu1 / __Inul;
235  for ( __i = 1; __i <= __nl; ++__i )
236  {
237  const _Tp __Knutemp = (__mu + __i) * __xi2 * __Knu1 + __Kmu;
238  __Kmu = __Knu1;
239  __Knu1 = __Knutemp;
240  }
241  __Knu = __Kmu;
242  __Kpnu = __nu * __xi * __Kmu - __Knu1;
243 
244  return;
245  }
246 
247 
262  template<typename _Tp>
263  _Tp
264  __cyl_bessel_i(_Tp __nu, _Tp __x)
265  {
266  if (__nu < _Tp(0) || __x < _Tp(0))
267  std::__throw_domain_error(__N("Bad argument "
268  "in __cyl_bessel_i."));
269  else if (__isnan(__nu) || __isnan(__x))
270  return std::numeric_limits<_Tp>::quiet_NaN();
271  else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
272  return __cyl_bessel_ij_series(__nu, __x, +_Tp(1), 200);
273  else
274  {
275  _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
276  __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
277  return __I_nu;
278  }
279  }
280 
281 
298  template<typename _Tp>
299  _Tp
300  __cyl_bessel_k(_Tp __nu, _Tp __x)
301  {
302  if (__nu < _Tp(0) || __x < _Tp(0))
303  std::__throw_domain_error(__N("Bad argument "
304  "in __cyl_bessel_k."));
305  else if (__isnan(__nu) || __isnan(__x))
306  return std::numeric_limits<_Tp>::quiet_NaN();
307  else
308  {
309  _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
310  __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
311  return __K_nu;
312  }
313  }
314 
315 
332  template <typename _Tp>
333  void
334  __sph_bessel_ik(unsigned int __n, _Tp __x,
335  _Tp & __i_n, _Tp & __k_n, _Tp & __ip_n, _Tp & __kp_n)
336  {
337  const _Tp __nu = _Tp(__n) + _Tp(0.5L);
338 
339  _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
340  __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
341 
342  const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
343  / std::sqrt(__x);
344 
345  __i_n = __factor * __I_nu;
346  __k_n = __factor * __K_nu;
347  __ip_n = __factor * __Ip_nu - __i_n / (_Tp(2) * __x);
348  __kp_n = __factor * __Kp_nu - __k_n / (_Tp(2) * __x);
349 
350  return;
351  }
352 
353 
367  template <typename _Tp>
368  void
369  __airy(_Tp __x, _Tp & __Ai, _Tp & __Bi, _Tp & __Aip, _Tp & __Bip)
370  {
371  const _Tp __absx = std::abs(__x);
372  const _Tp __rootx = std::sqrt(__absx);
373  const _Tp __z = _Tp(2) * __absx * __rootx / _Tp(3);
374 
375  if (__isnan(__x))
376  return std::numeric_limits<_Tp>::quiet_NaN();
377  else if (__x > _Tp(0))
378  {
379  _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
380 
381  __bessel_ik(_Tp(1) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
382  __Ai = __rootx * __K_nu
383  / (__numeric_constants<_Tp>::__sqrt3()
384  * __numeric_constants<_Tp>::__pi());
385  __Bi = __rootx * (__K_nu / __numeric_constants<_Tp>::__pi()
386  + _Tp(2) * __I_nu / __numeric_constants<_Tp>::__sqrt3());
387 
388  __bessel_ik(_Tp(2) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
389  __Aip = -__x * __K_nu
390  / (__numeric_constants<_Tp>::__sqrt3()
391  * __numeric_constants<_Tp>::__pi());
392  __Bip = __x * (__K_nu / __numeric_constants<_Tp>::__pi()
393  + _Tp(2) * __I_nu
394  / __numeric_constants<_Tp>::__sqrt3());
395  }
396  else if (__x < _Tp(0))
397  {
398  _Tp __J_nu, __Jp_nu, __N_nu, __Np_nu;
399 
400  __bessel_jn(_Tp(1) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
401  __Ai = __rootx * (__J_nu
402  - __N_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
403  __Bi = -__rootx * (__N_nu
404  + __J_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
405 
406  __bessel_jn(_Tp(2) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
407  __Aip = __absx * (__N_nu / __numeric_constants<_Tp>::__sqrt3()
408  + __J_nu) / _Tp(2);
409  __Bip = __absx * (__J_nu / __numeric_constants<_Tp>::__sqrt3()
410  - __N_nu) / _Tp(2);
411  }
412  else
413  {
414  // Reference:
415  // Abramowitz & Stegun, page 446 section 10.4.4 on Airy functions.
416  // The number is Ai(0) = 3^{-2/3}/\Gamma(2/3).
417  __Ai = _Tp(0.35502805388781723926L);
418  __Bi = __Ai * __numeric_constants<_Tp>::__sqrt3();
419 
420  // Reference:
421  // Abramowitz & Stegun, page 446 section 10.4.5 on Airy functions.
422  // The number is Ai'(0) = -3^{-1/3}/\Gamma(1/3).
423  __Aip = -_Tp(0.25881940379280679840L);
424  __Bip = -__Aip * __numeric_constants<_Tp>::__sqrt3();
425  }
426 
427  return;
428  }
429 
430  _GLIBCXX_END_NAMESPACE_VERSION
431  } // namespace std::tr1::__detail
432 }
433 }