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

Macros

#define _GLIBCXX_TR1_BESSEL_FUNCTION_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_BESSEL_FUNCTION_TCC   1

Function Documentation

namespace std _GLIBCXX_VISIBILITY ( default  )

Compute the gamma functions required by the Temme series expansions of $ N_\nu(x) $ and $ K_\nu(x) $.

\[ \Gamma_1 = \frac{1}{2\mu} [\frac{1}{\Gamma(1 - \mu)} - \frac{1}{\Gamma(1 + \mu)}] \]

and

\[ \Gamma_2 = \frac{1}{2} [\frac{1}{\Gamma(1 - \mu)} + \frac{1}{\Gamma(1 + \mu)}] \]

where $ -1/2 <= \mu <= 1/2 $ is $ \mu = \nu - N $ and $ N $. is the nearest integer to $ \nu $. The values of $ \Gamma(1 + \mu) $ and $ \Gamma(1 - \mu) $ are returned as well.

The accuracy requirements on this are exquisite.

Parameters
__muThe input parameter of the gamma functions.
__gam1The output function $ \Gamma_1(\mu) $
__gam2The output function $ \Gamma_2(\mu) $
__gamplThe output function $ \Gamma(1 + \mu) $
__gammiThe output function $ \Gamma(1 - \mu) $

Compute the Bessel $ J_\nu(x) $ and Neumann $ N_\nu(x) $ functions and their first derivatives $ J'_\nu(x) $ and $ N'_\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.
__JnuThe output Bessel function of the first kind.
__NnuThe output Neumann function (Bessel function of the second kind).
__JpnuThe output derivative of the Bessel function of the first kind.
__NpnuThe output derivative of the Neumann function.

This routine computes the asymptotic cylindrical Bessel and Neumann functions of order nu: $ J_{\nu} $, $ N_{\nu} $.

References: (1) Handbook of Mathematical Functions, ed. Milton Abramowitz and Irene A. Stegun, Dover Publications, Section 9 p. 364, Equations 9.2.5-9.2.10

Parameters
__nuThe order of the Bessel functions.
__xThe argument of the Bessel functions.
__JnuThe output Bessel function of the first kind.
__NnuThe output Neumann function (Bessel function of the second kind).

This routine returns the cylindrical Bessel functions of order $ \nu $: $ J_{\nu} $ or $ I_{\nu} $ by series expansion.

The modified cylindrical Bessel function is:

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

where $ \sigma = +1 $ or $ -1 $ for $ Z = I $ or $ J $ respectively.

See Abramowitz & Stegun, 9.1.10 Abramowitz & Stegun, 9.6.7 (1) Handbook of Mathematical Functions, ed. Milton Abramowitz and Irene A. Stegun, Dover Publications, Equation 9.1.10 p. 360 and Equation 9.6.10 p. 375

Parameters
__nuThe order of the Bessel function.
__xThe argument of the Bessel function.
__sgnThe sign of the alternate terms -1 for the Bessel function of the first kind. +1 for the modified Bessel function of the first kind.
Returns
The output Bessel function.

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

The cylindrical Bessel function is:

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

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

Return the Neumann function of order $ \nu $: $ N_{\nu}(x) $.

The Neumann function is defined by:

\[ N_{\nu}(x) = \frac{J_{\nu}(x) \cos \nu\pi - J_{-\nu}(x)} {\sin \nu\pi} \]

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

Parameters
__nuThe order of the Neumann function.
__xThe argument of the Neumann function.
Returns
The output Neumann function.

Compute the spherical Bessel $ j_n(x) $ and Neumann $ n_n(x) $ functions and their first derivatives $ j'_n(x) $ and $ n'_n(x) $ respectively.

Parameters
__nThe order of the spherical Bessel function.
__xThe argument of the spherical Bessel function.
__j_nThe output spherical Bessel function.
__n_nThe output spherical Neumann function.
__jp_nThe output derivative of the spherical Bessel function.
__np_nThe output derivative of the spherical Neumann function.

Return the spherical Bessel function $ j_n(x) $ of order n.

The spherical Bessel function is defined by:

\[ j_n(x) = \left( \frac{\pi}{2x} \right) ^{1/2} J_{n+1/2}(x) \]

Parameters
__nThe order of the spherical Bessel function.
__xThe argument of the spherical Bessel function.
Returns
The output spherical Bessel function.

Return the spherical Neumann function $ n_n(x) $.

The spherical Neumann function is defined by:

\[ n_n(x) = \left( \frac{\pi}{2x} \right) ^{1/2} N_{n+1/2}(x) \]

Parameters
__nThe order of the spherical Neumann function.
__xThe argument of the spherical Neumann function.
Returns
The output spherical Neumann 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 
87  template <typename _Tp>
88  void
89  __gamma_temme(_Tp __mu,
90  _Tp & __gam1, _Tp & __gam2, _Tp & __gampl, _Tp & __gammi)
91  {
92 #if _GLIBCXX_USE_C99_MATH_TR1
93  __gampl = _Tp(1) / std::tr1::tgamma(_Tp(1) + __mu);
94  __gammi = _Tp(1) / std::tr1::tgamma(_Tp(1) - __mu);
95 #else
96  __gampl = _Tp(1) / __gamma(_Tp(1) + __mu);
97  __gammi = _Tp(1) / __gamma(_Tp(1) - __mu);
98 #endif
99 
100  if (std::abs(__mu) < std::numeric_limits<_Tp>::epsilon())
101  __gam1 = -_Tp(__numeric_constants<_Tp>::__gamma_e());
102  else
103  __gam1 = (__gammi - __gampl) / (_Tp(2) * __mu);
104 
105  __gam2 = (__gammi + __gampl) / (_Tp(2));
106 
107  return;
108  }
109 
110 
125  template <typename _Tp>
126  void
127  __bessel_jn(_Tp __nu, _Tp __x,
128  _Tp & __Jnu, _Tp & __Nnu, _Tp & __Jpnu, _Tp & __Npnu)
129  {
130  if (__x == _Tp(0))
131  {
132  if (__nu == _Tp(0))
133  {
134  __Jnu = _Tp(1);
135  __Jpnu = _Tp(0);
136  }
137  else if (__nu == _Tp(1))
138  {
139  __Jnu = _Tp(0);
140  __Jpnu = _Tp(0.5L);
141  }
142  else
143  {
144  __Jnu = _Tp(0);
145  __Jpnu = _Tp(0);
146  }
147  __Nnu = -std::numeric_limits<_Tp>::infinity();
148  __Npnu = std::numeric_limits<_Tp>::infinity();
149  return;
150  }
151 
152  const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
153  // When the multiplier is N i.e.
154  // fp_min = N * min()
155  // Then J_0 and N_0 tank at x = 8 * N (J_0 = 0 and N_0 = nan)!
156  //const _Tp __fp_min = _Tp(20) * std::numeric_limits<_Tp>::min();
157  const _Tp __fp_min = std::sqrt(std::numeric_limits<_Tp>::min());
158  const int __max_iter = 15000;
159  const _Tp __x_min = _Tp(2);
160 
161  const int __nl = (__x < __x_min
162  ? static_cast<int>(__nu + _Tp(0.5L))
163  : std::max(0, static_cast<int>(__nu - __x + _Tp(1.5L))));
164 
165  const _Tp __mu = __nu - __nl;
166  const _Tp __mu2 = __mu * __mu;
167  const _Tp __xi = _Tp(1) / __x;
168  const _Tp __xi2 = _Tp(2) * __xi;
169  _Tp __w = __xi2 / __numeric_constants<_Tp>::__pi();
170  int __isign = 1;
171  _Tp __h = __nu * __xi;
172  if (__h < __fp_min)
173  __h = __fp_min;
174  _Tp __b = __xi2 * __nu;
175  _Tp __d = _Tp(0);
176  _Tp __c = __h;
177  int __i;
178  for (__i = 1; __i <= __max_iter; ++__i)
179  {
180  __b += __xi2;
181  __d = __b - __d;
182  if (std::abs(__d) < __fp_min)
183  __d = __fp_min;
184  __c = __b - _Tp(1) / __c;
185  if (std::abs(__c) < __fp_min)
186  __c = __fp_min;
187  __d = _Tp(1) / __d;
188  const _Tp __del = __c * __d;
189  __h *= __del;
190  if (__d < _Tp(0))
191  __isign = -__isign;
192  if (std::abs(__del - _Tp(1)) < __eps)
193  break;
194  }
195  if (__i > __max_iter)
196  std::__throw_runtime_error(__N("Argument x too large in __bessel_jn; "
197  "try asymptotic expansion."));
198  _Tp __Jnul = __isign * __fp_min;
199  _Tp __Jpnul = __h * __Jnul;
200  _Tp __Jnul1 = __Jnul;
201  _Tp __Jpnu1 = __Jpnul;
202  _Tp __fact = __nu * __xi;
203  for ( int __l = __nl; __l >= 1; --__l )
204  {
205  const _Tp __Jnutemp = __fact * __Jnul + __Jpnul;
206  __fact -= __xi;
207  __Jpnul = __fact * __Jnutemp - __Jnul;
208  __Jnul = __Jnutemp;
209  }
210  if (__Jnul == _Tp(0))
211  __Jnul = __eps;
212  _Tp __f= __Jpnul / __Jnul;
213  _Tp __Nmu, __Nnu1, __Npmu, __Jmu;
214  if (__x < __x_min)
215  {
216  const _Tp __x2 = __x / _Tp(2);
217  const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
218  _Tp __fact = (std::abs(__pimu) < __eps
219  ? _Tp(1) : __pimu / std::sin(__pimu));
220  _Tp __d = -std::log(__x2);
221  _Tp __e = __mu * __d;
222  _Tp __fact2 = (std::abs(__e) < __eps
223  ? _Tp(1) : std::sinh(__e) / __e);
224  _Tp __gam1, __gam2, __gampl, __gammi;
225  __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
226  _Tp __ff = (_Tp(2) / __numeric_constants<_Tp>::__pi())
227  * __fact * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
228  __e = std::exp(__e);
229  _Tp __p = __e / (__numeric_constants<_Tp>::__pi() * __gampl);
230  _Tp __q = _Tp(1) / (__e * __numeric_constants<_Tp>::__pi() * __gammi);
231  const _Tp __pimu2 = __pimu / _Tp(2);
232  _Tp __fact3 = (std::abs(__pimu2) < __eps
233  ? _Tp(1) : std::sin(__pimu2) / __pimu2 );
234  _Tp __r = __numeric_constants<_Tp>::__pi() * __pimu2 * __fact3 * __fact3;
235  _Tp __c = _Tp(1);
236  __d = -__x2 * __x2;
237  _Tp __sum = __ff + __r * __q;
238  _Tp __sum1 = __p;
239  for (__i = 1; __i <= __max_iter; ++__i)
240  {
241  __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
242  __c *= __d / _Tp(__i);
243  __p /= _Tp(__i) - __mu;
244  __q /= _Tp(__i) + __mu;
245  const _Tp __del = __c * (__ff + __r * __q);
246  __sum += __del;
247  const _Tp __del1 = __c * __p - __i * __del;
248  __sum1 += __del1;
249  if ( std::abs(__del) < __eps * (_Tp(1) + std::abs(__sum)) )
250  break;
251  }
252  if ( __i > __max_iter )
253  std::__throw_runtime_error(__N("Bessel y series failed to converge "
254  "in __bessel_jn."));
255  __Nmu = -__sum;
256  __Nnu1 = -__sum1 * __xi2;
257  __Npmu = __mu * __xi * __Nmu - __Nnu1;
258  __Jmu = __w / (__Npmu - __f * __Nmu);
259  }
260  else
261  {
262  _Tp __a = _Tp(0.25L) - __mu2;
263  _Tp __q = _Tp(1);
264  _Tp __p = -__xi / _Tp(2);
265  _Tp __br = _Tp(2) * __x;
266  _Tp __bi = _Tp(2);
267  _Tp __fact = __a * __xi / (__p * __p + __q * __q);
268  _Tp __cr = __br + __q * __fact;
269  _Tp __ci = __bi + __p * __fact;
270  _Tp __den = __br * __br + __bi * __bi;
271  _Tp __dr = __br / __den;
272  _Tp __di = -__bi / __den;
273  _Tp __dlr = __cr * __dr - __ci * __di;
274  _Tp __dli = __cr * __di + __ci * __dr;
275  _Tp __temp = __p * __dlr - __q * __dli;
276  __q = __p * __dli + __q * __dlr;
277  __p = __temp;
278  int __i;
279  for (__i = 2; __i <= __max_iter; ++__i)
280  {
281  __a += _Tp(2 * (__i - 1));
282  __bi += _Tp(2);
283  __dr = __a * __dr + __br;
284  __di = __a * __di + __bi;
285  if (std::abs(__dr) + std::abs(__di) < __fp_min)
286  __dr = __fp_min;
287  __fact = __a / (__cr * __cr + __ci * __ci);
288  __cr = __br + __cr * __fact;
289  __ci = __bi - __ci * __fact;
290  if (std::abs(__cr) + std::abs(__ci) < __fp_min)
291  __cr = __fp_min;
292  __den = __dr * __dr + __di * __di;
293  __dr /= __den;
294  __di /= -__den;
295  __dlr = __cr * __dr - __ci * __di;
296  __dli = __cr * __di + __ci * __dr;
297  __temp = __p * __dlr - __q * __dli;
298  __q = __p * __dli + __q * __dlr;
299  __p = __temp;
300  if (std::abs(__dlr - _Tp(1)) + std::abs(__dli) < __eps)
301  break;
302  }
303  if (__i > __max_iter)
304  std::__throw_runtime_error(__N("Lentz's method failed "
305  "in __bessel_jn."));
306  const _Tp __gam = (__p - __f) / __q;
307  __Jmu = std::sqrt(__w / ((__p - __f) * __gam + __q));
308 #if _GLIBCXX_USE_C99_MATH_TR1
309  __Jmu = std::tr1::copysign(__Jmu, __Jnul);
310 #else
311  if (__Jmu * __Jnul < _Tp(0))
312  __Jmu = -__Jmu;
313 #endif
314  __Nmu = __gam * __Jmu;
315  __Npmu = (__p + __q / __gam) * __Nmu;
316  __Nnu1 = __mu * __xi * __Nmu - __Npmu;
317  }
318  __fact = __Jmu / __Jnul;
319  __Jnu = __fact * __Jnul1;
320  __Jpnu = __fact * __Jpnu1;
321  for (__i = 1; __i <= __nl; ++__i)
322  {
323  const _Tp __Nnutemp = (__mu + __i) * __xi2 * __Nnu1 - __Nmu;
324  __Nmu = __Nnu1;
325  __Nnu1 = __Nnutemp;
326  }
327  __Nnu = __Nmu;
328  __Npnu = __nu * __xi * __Nmu - __Nnu1;
329 
330  return;
331  }
332 
333 
350  template <typename _Tp>
351  void
352  __cyl_bessel_jn_asymp(_Tp __nu, _Tp __x, _Tp & __Jnu, _Tp & __Nnu)
353  {
354  const _Tp __mu = _Tp(4) * __nu * __nu;
355  const _Tp __mum1 = __mu - _Tp(1);
356  const _Tp __mum9 = __mu - _Tp(9);
357  const _Tp __mum25 = __mu - _Tp(25);
358  const _Tp __mum49 = __mu - _Tp(49);
359  const _Tp __xx = _Tp(64) * __x * __x;
360  const _Tp __P = _Tp(1) - __mum1 * __mum9 / (_Tp(2) * __xx)
361  * (_Tp(1) - __mum25 * __mum49 / (_Tp(12) * __xx));
362  const _Tp __Q = __mum1 / (_Tp(8) * __x)
363  * (_Tp(1) - __mum9 * __mum25 / (_Tp(6) * __xx));
364 
365  const _Tp __chi = __x - (__nu + _Tp(0.5L))
366  * __numeric_constants<_Tp>::__pi_2();
367  const _Tp __c = std::cos(__chi);
368  const _Tp __s = std::sin(__chi);
369 
370  const _Tp __coef = std::sqrt(_Tp(2)
371  / (__numeric_constants<_Tp>::__pi() * __x));
372  __Jnu = __coef * (__c * __P - __s * __Q);
373  __Nnu = __coef * (__s * __P + __c * __Q);
374 
375  return;
376  }
377 
378 
406  template <typename _Tp>
407  _Tp
408  __cyl_bessel_ij_series(_Tp __nu, _Tp __x, _Tp __sgn,
409  unsigned int __max_iter)
410  {
411  if (__x == _Tp(0))
412  return __nu == _Tp(0) ? _Tp(1) : _Tp(0);
413 
414  const _Tp __x2 = __x / _Tp(2);
415  _Tp __fact = __nu * std::log(__x2);
416 #if _GLIBCXX_USE_C99_MATH_TR1
417  __fact -= std::tr1::lgamma(__nu + _Tp(1));
418 #else
419  __fact -= __log_gamma(__nu + _Tp(1));
420 #endif
421  __fact = std::exp(__fact);
422  const _Tp __xx4 = __sgn * __x2 * __x2;
423  _Tp __Jn = _Tp(1);
424  _Tp __term = _Tp(1);
425 
426  for (unsigned int __i = 1; __i < __max_iter; ++__i)
427  {
428  __term *= __xx4 / (_Tp(__i) * (__nu + _Tp(__i)));
429  __Jn += __term;
430  if (std::abs(__term / __Jn) < std::numeric_limits<_Tp>::epsilon())
431  break;
432  }
433 
434  return __fact * __Jn;
435  }
436 
437 
452  template<typename _Tp>
453  _Tp
454  __cyl_bessel_j(_Tp __nu, _Tp __x)
455  {
456  if (__nu < _Tp(0) || __x < _Tp(0))
457  std::__throw_domain_error(__N("Bad argument "
458  "in __cyl_bessel_j."));
459  else if (__isnan(__nu) || __isnan(__x))
460  return std::numeric_limits<_Tp>::quiet_NaN();
461  else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
462  return __cyl_bessel_ij_series(__nu, __x, -_Tp(1), 200);
463  else if (__x > _Tp(1000))
464  {
465  _Tp __J_nu, __N_nu;
466  __cyl_bessel_jn_asymp(__nu, __x, __J_nu, __N_nu);
467  return __J_nu;
468  }
469  else
470  {
471  _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
472  __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
473  return __J_nu;
474  }
475  }
476 
477 
494  template<typename _Tp>
495  _Tp
496  __cyl_neumann_n(_Tp __nu, _Tp __x)
497  {
498  if (__nu < _Tp(0) || __x < _Tp(0))
499  std::__throw_domain_error(__N("Bad argument "
500  "in __cyl_neumann_n."));
501  else if (__isnan(__nu) || __isnan(__x))
502  return std::numeric_limits<_Tp>::quiet_NaN();
503  else if (__x > _Tp(1000))
504  {
505  _Tp __J_nu, __N_nu;
506  __cyl_bessel_jn_asymp(__nu, __x, __J_nu, __N_nu);
507  return __N_nu;
508  }
509  else
510  {
511  _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
512  __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
513  return __N_nu;
514  }
515  }
516 
517 
531  template <typename _Tp>
532  void
533  __sph_bessel_jn(unsigned int __n, _Tp __x,
534  _Tp & __j_n, _Tp & __n_n, _Tp & __jp_n, _Tp & __np_n)
535  {
536  const _Tp __nu = _Tp(__n) + _Tp(0.5L);
537 
538  _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu;
539  __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu);
540 
541  const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
542  / std::sqrt(__x);
543 
544  __j_n = __factor * __J_nu;
545  __n_n = __factor * __N_nu;
546  __jp_n = __factor * __Jp_nu - __j_n / (_Tp(2) * __x);
547  __np_n = __factor * __Np_nu - __n_n / (_Tp(2) * __x);
548 
549  return;
550  }
551 
552 
566  template <typename _Tp>
567  _Tp
568  __sph_bessel(unsigned int __n, _Tp __x)
569  {
570  if (__x < _Tp(0))
571  std::__throw_domain_error(__N("Bad argument "
572  "in __sph_bessel."));
573  else if (__isnan(__x))
574  return std::numeric_limits<_Tp>::quiet_NaN();
575  else if (__x == _Tp(0))
576  {
577  if (__n == 0)
578  return _Tp(1);
579  else
580  return _Tp(0);
581  }
582  else
583  {
584  _Tp __j_n, __n_n, __jp_n, __np_n;
585  __sph_bessel_jn(__n, __x, __j_n, __n_n, __jp_n, __np_n);
586  return __j_n;
587  }
588  }
589 
590 
604  template <typename _Tp>
605  _Tp
606  __sph_neumann(unsigned int __n, _Tp __x)
607  {
608  if (__x < _Tp(0))
609  std::__throw_domain_error(__N("Bad argument "
610  "in __sph_neumann."));
611  else if (__isnan(__x))
612  return std::numeric_limits<_Tp>::quiet_NaN();
613  else if (__x == _Tp(0))
614  return -std::numeric_limits<_Tp>::infinity();
615  else
616  {
617  _Tp __j_n, __n_n, __jp_n, __np_n;
618  __sph_bessel_jn(__n, __x, __j_n, __n_n, __jp_n, __np_n);
619  return __n_n;
620  }
621  }
622 
623  _GLIBCXX_END_NAMESPACE_VERSION
624  } // namespace std::tr1::__detail
625 }
626 }
__inline unsigned char unsigned int unsigned int unsigned int * __P
Definition: adxintrin.h:35
const _Tp & min(const _Tp &__a, const _Tp &__b)
Equivalent to std::min.
Definition: base.h:144
const _Tp & max(const _Tp &__a, const _Tp &__b)
Equivalent to std::max.
Definition: base.h:150