150276Speter// Special functions -*- C++ -*-
2262685Sdelphij
350276Speter// Copyright (C) 2006-2020 Free Software Foundation, Inc.
450276Speter//
550276Speter// This file is part of the GNU ISO C++ Library.  This library is free
650276Speter// software; you can redistribute it and/or modify it under the
750276Speter// terms of the GNU General Public License as published by the
850276Speter// Free Software Foundation; either version 3, or (at your option)
950276Speter// any later version.
1050276Speter//
1150276Speter// This library is distributed in the hope that it will be useful,
1250276Speter// but WITHOUT ANY WARRANTY; without even the implied warranty of
1350276Speter// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1450276Speter// GNU General Public License for more details.
1550276Speter//
1650276Speter// Under Section 7 of GPL version 3, you are granted additional
1750276Speter// permissions described in the GCC Runtime Library Exception, version
1850276Speter// 3.1, as published by the Free Software Foundation.
1950276Speter
2050276Speter// You should have received a copy of the GNU General Public License and
2150276Speter// a copy of the GCC Runtime Library Exception along with this program;
2250276Speter// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2350276Speter// <http://www.gnu.org/licenses/>.
2450276Speter
2550276Speter/** @file tr1/modified_bessel_func.tcc
2650276Speter *  This is an internal header file, included by other library headers.
2750276Speter *  Do not attempt to use it directly. @headername{tr1/cmath}
2850276Speter */
2950276Speter
30166124Srafan//
3150276Speter// ISO C++ 14882 TR1: 5.2  Special functions
3250276Speter//
33262685Sdelphij
3450276Speter// Written by Edward Smith-Rowland.
3550276Speter//
3650276Speter// References:
3750276Speter//   (1) Handbook of Mathematical Functions,
3850276Speter//       Ed. Milton Abramowitz and Irene A. Stegun,
3950276Speter//       Dover Publications,
4050276Speter//       Section 9, pp. 355-434, Section 10 pp. 435-478
4150276Speter//   (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
4266963Speter//   (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
4350276Speter//       W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
4450276Speter//       2nd ed, pp. 246-249.
45262685Sdelphij
4650276Speter#ifndef _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC
4750276Speter#define _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC 1
4850276Speter
4950276Speter#include <tr1/special_function_util.h>
5050276Speter
5150276Speternamespace std _GLIBCXX_VISIBILITY(default)
5250276Speter{
5350276Speter_GLIBCXX_BEGIN_NAMESPACE_VERSION
5450276Speter
5550276Speter#if _GLIBCXX_USE_STD_SPEC_FUNCS
5650276Speter#elif defined(_GLIBCXX_TR1_CMATH)
5750276Speternamespace tr1
5850276Speter{
5950276Speter#else
6050276Speter# error do not include this header directly, use <cmath> or <tr1/cmath>
6150276Speter#endif
6250276Speter  // [5.2] Special functions
6350276Speter
6450276Speter  // Implementation-space details.
6550276Speter  namespace __detail
6650276Speter  {
6750276Speter    /**
6850276Speter     *   @brief  Compute the modified Bessel functions @f$ I_\nu(x) @f$ and
6950276Speter     *           @f$ K_\nu(x) @f$ and their first derivatives
70166124Srafan     *           @f$ I'_\nu(x) @f$ and @f$ K'_\nu(x) @f$ respectively.
71166124Srafan     *           These four functions are computed together for numerical
72166124Srafan     *           stability.
73166124Srafan     *
74166124Srafan     *   @param  __nu  The order of the Bessel functions.
75166124Srafan     *   @param  __x   The argument of the Bessel functions.
76166124Srafan     *   @param  __Inu  The output regular modified Bessel function.
77166124Srafan     *   @param  __Knu  The output irregular modified Bessel function.
78166124Srafan     *   @param  __Ipnu  The output derivative of the regular
7950276Speter     *                   modified Bessel function.
80166124Srafan     *   @param  __Kpnu  The output derivative of the irregular
8150276Speter     *                   modified Bessel function.
8250276Speter     */
8350276Speter    template <typename _Tp>
8450276Speter    void
8550276Speter    __bessel_ik(_Tp __nu, _Tp __x,
8650276Speter                _Tp & __Inu, _Tp & __Knu, _Tp & __Ipnu, _Tp & __Kpnu)
8750276Speter    {
8850276Speter      if (__x == _Tp(0))
8950276Speter        {
9050276Speter          if (__nu == _Tp(0))
9150276Speter            {
9250276Speter              __Inu = _Tp(1);
93262685Sdelphij              __Ipnu = _Tp(0);
94262685Sdelphij            }
95262685Sdelphij          else if (__nu == _Tp(1))
96262685Sdelphij            {
97262685Sdelphij              __Inu = _Tp(0);
98262685Sdelphij              __Ipnu = _Tp(0.5L);
99262685Sdelphij            }
100262685Sdelphij          else
101184989Srafan            {
10250276Speter              __Inu = _Tp(0);
10350276Speter              __Ipnu = _Tp(0);
10450276Speter            }
10550276Speter          __Knu = std::numeric_limits<_Tp>::infinity();
10650276Speter          __Kpnu = -std::numeric_limits<_Tp>::infinity();
10750276Speter          return;
10850276Speter        }
10950276Speter
110262685Sdelphij      const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
11150276Speter      const _Tp __fp_min = _Tp(10) * std::numeric_limits<_Tp>::epsilon();
11250276Speter      const int __max_iter = 15000;
11350276Speter      const _Tp __x_min = _Tp(2);
11450276Speter
11550276Speter      const int __nl = static_cast<int>(__nu + _Tp(0.5L));
11650276Speter
11750276Speter      const _Tp __mu = __nu - __nl;
11850276Speter      const _Tp __mu2 = __mu * __mu;
11950276Speter      const _Tp __xi = _Tp(1) / __x;
120262685Sdelphij      const _Tp __xi2 = _Tp(2) * __xi;
12150276Speter      _Tp __h = __nu * __xi;
122166124Srafan      if ( __h < __fp_min )
123174993Srafan        __h = __fp_min;
124262685Sdelphij      _Tp __b = __xi2 * __nu;
12550276Speter      _Tp __d = _Tp(0);
126174993Srafan      _Tp __c = __h;
127174993Srafan      int __i;
128174993Srafan      for ( __i = 1; __i <= __max_iter; ++__i )
129174993Srafan        {
130174993Srafan          __b += __xi2;
131174993Srafan          __d = _Tp(1) / (__b + __d);
132174993Srafan          __c = __b + _Tp(1) / __c;
133174993Srafan          const _Tp __del = __c * __d;
13450276Speter          __h *= __del;
13550276Speter          if (std::abs(__del - _Tp(1)) < __eps)
13650276Speter            break;
13750276Speter        }
13850276Speter      if (__i > __max_iter)
13950276Speter        std::__throw_runtime_error(__N("Argument x too large "
14050276Speter                                       "in __bessel_ik; "
14150276Speter                                       "try asymptotic expansion."));
14250276Speter      _Tp __Inul = __fp_min;
14350276Speter      _Tp __Ipnul = __h * __Inul;
14450276Speter      _Tp __Inul1 = __Inul;
14550276Speter      _Tp __Ipnu1 = __Ipnul;
14650276Speter      _Tp __fact = __nu * __xi;
14750276Speter      for (int __l = __nl; __l >= 1; --__l)
14850276Speter        {
14950276Speter          const _Tp __Inutemp = __fact * __Inul + __Ipnul;
15050276Speter          __fact -= __xi;
15150276Speter          __Ipnul = __fact * __Inutemp + __Inul;
15262449Speter          __Inul = __Inutemp;
15362449Speter        }
15462449Speter      _Tp __f = __Ipnul / __Inul;
15562449Speter      _Tp __Kmu, __Knu1;
15662449Speter      if (__x < __x_min)
15762449Speter        {
15862449Speter          const _Tp __x2 = __x / _Tp(2);
15962449Speter          const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
16062449Speter          const _Tp __fact = (std::abs(__pimu) < __eps
16162449Speter                            ? _Tp(1) : __pimu / std::sin(__pimu));
16262449Speter          _Tp __d = -std::log(__x2);
16362449Speter          _Tp __e = __mu * __d;
16450276Speter          const _Tp __fact2 = (std::abs(__e) < __eps
16550276Speter                            ? _Tp(1) : std::sinh(__e) / __e);
16650276Speter          _Tp __gam1, __gam2, __gampl, __gammi;
16750276Speter          __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
16850276Speter          _Tp __ff = __fact
16950276Speter                   * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
17050276Speter          _Tp __sum = __ff;
17150276Speter          __e = std::exp(__e);
17250276Speter          _Tp __p = __e / (_Tp(2) * __gampl);
17350276Speter          _Tp __q = _Tp(1) / (_Tp(2) * __e * __gammi);
17450276Speter          _Tp __c = _Tp(1);
17550276Speter          __d = __x2 * __x2;
17650276Speter          _Tp __sum1 = __p;
17750276Speter          int __i;
17850276Speter          for (__i = 1; __i <= __max_iter; ++__i)
17950276Speter            {
18050276Speter              __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
18150276Speter              __c *= __d / __i;
18250276Speter              __p /= __i - __mu;
18350276Speter              __q /= __i + __mu;
184262685Sdelphij              const _Tp __del = __c * __ff;
18550276Speter              __sum += __del; 
18650276Speter              const _Tp __del1 = __c * (__p - __i * __ff);
18750276Speter              __sum1 += __del1;
18850276Speter              if (std::abs(__del) < __eps * std::abs(__sum))
18950276Speter                break;
19050276Speter            }
19166963Speter          if (__i > __max_iter)
19250276Speter            std::__throw_runtime_error(__N("Bessel k series failed to converge "
19350276Speter                                           "in __bessel_ik."));
19497049Speter          __Kmu = __sum;
19550276Speter          __Knu1 = __sum1 * __xi2;
196262685Sdelphij        }
19750276Speter      else
19850276Speter        {
19966963Speter          _Tp __b = _Tp(2) * (_Tp(1) + __x);
20097049Speter          _Tp __d = _Tp(1) / __b;
20176726Speter          _Tp __delh = __d;
20266963Speter          _Tp __h = __delh;
203          _Tp __q1 = _Tp(0);
204          _Tp __q2 = _Tp(1);
205          _Tp __a1 = _Tp(0.25L) - __mu2;
206          _Tp __q = __c = __a1;
207          _Tp __a = -__a1;
208          _Tp __s = _Tp(1) + __q * __delh;
209          int __i;
210          for (__i = 2; __i <= __max_iter; ++__i)
211            {
212              __a -= 2 * (__i - 1);
213              __c = -__a * __c / __i;
214              const _Tp __qnew = (__q1 - __b * __q2) / __a;
215              __q1 = __q2;
216              __q2 = __qnew;
217              __q += __c * __qnew;
218              __b += _Tp(2);
219              __d = _Tp(1) / (__b + __a * __d);
220              __delh = (__b * __d - _Tp(1)) * __delh;
221              __h += __delh;
222              const _Tp __dels = __q * __delh;
223              __s += __dels;
224              if ( std::abs(__dels / __s) < __eps )
225                break;
226            }
227          if (__i > __max_iter)
228            std::__throw_runtime_error(__N("Steed's method failed "
229                                           "in __bessel_ik."));
230          __h = __a1 * __h;
231          __Kmu = std::sqrt(__numeric_constants<_Tp>::__pi() / (_Tp(2) * __x))
232                * std::exp(-__x) / __s;
233          __Knu1 = __Kmu * (__mu + __x + _Tp(0.5L) - __h) * __xi;
234        }
235
236      _Tp __Kpmu = __mu * __xi * __Kmu - __Knu1;
237      _Tp __Inumu = __xi / (__f * __Kmu - __Kpmu);
238      __Inu = __Inumu * __Inul1 / __Inul;
239      __Ipnu = __Inumu * __Ipnu1 / __Inul;
240      for ( __i = 1; __i <= __nl; ++__i )
241        {
242          const _Tp __Knutemp = (__mu + __i) * __xi2 * __Knu1 + __Kmu;
243          __Kmu = __Knu1;
244          __Knu1 = __Knutemp;
245        }
246      __Knu = __Kmu;
247      __Kpnu = __nu * __xi * __Kmu - __Knu1;
248  
249      return;
250    }
251
252
253    /**
254     *   @brief  Return the regular modified Bessel function of order
255     *           \f$ \nu \f$: \f$ I_{\nu}(x) \f$.
256     *
257     *   The regular modified cylindrical Bessel function is:
258     *   @f[
259     *    I_{\nu}(x) = \sum_{k=0}^{\infty}
260     *              \frac{(x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
261     *   @f]
262     *
263     *   @param  __nu  The order of the regular modified Bessel function.
264     *   @param  __x   The argument of the regular modified Bessel function.
265     *   @return  The output regular modified Bessel function.
266     */
267    template<typename _Tp>
268    _Tp
269    __cyl_bessel_i(_Tp __nu, _Tp __x)
270    {
271      if (__nu < _Tp(0) || __x < _Tp(0))
272        std::__throw_domain_error(__N("Bad argument "
273                                      "in __cyl_bessel_i."));
274      else if (__isnan(__nu) || __isnan(__x))
275        return std::numeric_limits<_Tp>::quiet_NaN();
276      else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
277        return __cyl_bessel_ij_series(__nu, __x, +_Tp(1), 200);
278      else
279        {
280          _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
281          __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
282          return __I_nu;
283        }
284    }
285
286
287    /**
288     *   @brief  Return the irregular modified Bessel function
289     *           \f$ K_{\nu}(x) \f$ of order \f$ \nu \f$.
290     *
291     *   The irregular modified Bessel function is defined by:
292     *   @f[
293     *      K_{\nu}(x) = \frac{\pi}{2}
294     *                   \frac{I_{-\nu}(x) - I_{\nu}(x)}{\sin \nu\pi}
295     *   @f]
296     *   where for integral \f$ \nu = n \f$ a limit is taken:
297     *   \f$ lim_{\nu \to n} \f$.
298     *
299     *   @param  __nu  The order of the irregular modified Bessel function.
300     *   @param  __x   The argument of the irregular modified Bessel function.
301     *   @return  The output irregular modified Bessel function.
302     */
303    template<typename _Tp>
304    _Tp
305    __cyl_bessel_k(_Tp __nu, _Tp __x)
306    {
307      if (__nu < _Tp(0) || __x < _Tp(0))
308        std::__throw_domain_error(__N("Bad argument "
309                                      "in __cyl_bessel_k."));
310      else if (__isnan(__nu) || __isnan(__x))
311        return std::numeric_limits<_Tp>::quiet_NaN();
312      else
313        {
314          _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
315          __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
316          return __K_nu;
317        }
318    }
319
320
321    /**
322     *   @brief  Compute the spherical modified Bessel functions
323     *           @f$ i_n(x) @f$ and @f$ k_n(x) @f$ and their first
324     *           derivatives @f$ i'_n(x) @f$ and @f$ k'_n(x) @f$
325     *           respectively.
326     *
327     *   @param  __n  The order of the modified spherical Bessel function.
328     *   @param  __x  The argument of the modified spherical Bessel function.
329     *   @param  __i_n  The output regular modified spherical Bessel function.
330     *   @param  __k_n  The output irregular modified spherical
331     *                  Bessel function.
332     *   @param  __ip_n  The output derivative of the regular modified
333     *                   spherical Bessel function.
334     *   @param  __kp_n  The output derivative of the irregular modified
335     *                   spherical Bessel function.
336     */
337    template <typename _Tp>
338    void
339    __sph_bessel_ik(unsigned int __n, _Tp __x,
340                    _Tp & __i_n, _Tp & __k_n, _Tp & __ip_n, _Tp & __kp_n)
341    {
342      const _Tp __nu = _Tp(__n) + _Tp(0.5L);
343
344      _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
345      __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
346
347      const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
348                         / std::sqrt(__x);
349
350      __i_n = __factor * __I_nu;
351      __k_n = __factor * __K_nu;
352      __ip_n = __factor * __Ip_nu - __i_n / (_Tp(2) * __x);
353      __kp_n = __factor * __Kp_nu - __k_n / (_Tp(2) * __x);
354
355      return;
356    }
357
358
359    /**
360     *   @brief  Compute the Airy functions
361     *           @f$ Ai(x) @f$ and @f$ Bi(x) @f$ and their first
362     *           derivatives @f$ Ai'(x) @f$ and @f$ Bi(x) @f$
363     *           respectively.
364     *
365     *   @param  __x  The argument of the Airy functions.
366     *   @param  __Ai  The output Airy function of the first kind.
367     *   @param  __Bi  The output Airy function of the second kind.
368     *   @param  __Aip  The output derivative of the Airy function
369     *                  of the first kind.
370     *   @param  __Bip  The output derivative of the Airy function
371     *                  of the second kind.
372     */
373    template <typename _Tp>
374    void
375    __airy(_Tp __x, _Tp & __Ai, _Tp & __Bi, _Tp & __Aip, _Tp & __Bip)
376    {
377      const _Tp __absx = std::abs(__x);
378      const _Tp __rootx = std::sqrt(__absx);
379      const _Tp __z = _Tp(2) * __absx * __rootx / _Tp(3);
380      const _Tp _S_NaN = std::numeric_limits<_Tp>::quiet_NaN();
381      const _Tp _S_inf = std::numeric_limits<_Tp>::infinity();
382
383      if (__isnan(__x))
384        __Bip = __Aip = __Bi = __Ai = std::numeric_limits<_Tp>::quiet_NaN();
385      else if (__z == _S_inf)
386        {
387	  __Aip = __Ai = _Tp(0);
388	  __Bip = __Bi = _S_inf;
389	}
390      else if (__z == -_S_inf)
391	__Bip = __Aip = __Bi = __Ai = _Tp(0);
392      else if (__x > _Tp(0))
393        {
394          _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
395
396          __bessel_ik(_Tp(1) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
397          __Ai = __rootx * __K_nu
398               / (__numeric_constants<_Tp>::__sqrt3()
399                * __numeric_constants<_Tp>::__pi());
400          __Bi = __rootx * (__K_nu / __numeric_constants<_Tp>::__pi()
401                 + _Tp(2) * __I_nu / __numeric_constants<_Tp>::__sqrt3());
402
403          __bessel_ik(_Tp(2) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
404          __Aip = -__x * __K_nu
405                / (__numeric_constants<_Tp>::__sqrt3()
406                 * __numeric_constants<_Tp>::__pi());
407          __Bip = __x * (__K_nu / __numeric_constants<_Tp>::__pi()
408                      + _Tp(2) * __I_nu
409                      / __numeric_constants<_Tp>::__sqrt3());
410        }
411      else if (__x < _Tp(0))
412        {
413          _Tp __J_nu, __Jp_nu, __N_nu, __Np_nu;
414
415          __bessel_jn(_Tp(1) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
416          __Ai = __rootx * (__J_nu
417                    - __N_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
418          __Bi = -__rootx * (__N_nu
419                    + __J_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
420
421          __bessel_jn(_Tp(2) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
422          __Aip = __absx * (__N_nu / __numeric_constants<_Tp>::__sqrt3()
423                          + __J_nu) / _Tp(2);
424          __Bip = __absx * (__J_nu / __numeric_constants<_Tp>::__sqrt3()
425                          - __N_nu) / _Tp(2);
426        }
427      else
428        {
429          //  Reference:
430          //    Abramowitz & Stegun, page 446 section 10.4.4 on Airy functions.
431          //  The number is Ai(0) = 3^{-2/3}/\Gamma(2/3).
432          __Ai = _Tp(0.35502805388781723926L);
433          __Bi = __Ai * __numeric_constants<_Tp>::__sqrt3();
434
435          //  Reference:
436          //    Abramowitz & Stegun, page 446 section 10.4.5 on Airy functions.
437          //  The number is Ai'(0) = -3^{-1/3}/\Gamma(1/3).
438          __Aip = -_Tp(0.25881940379280679840L);
439          __Bip = -__Aip * __numeric_constants<_Tp>::__sqrt3();
440        }
441
442      return;
443    }
444  } // namespace __detail
445#if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
446} // namespace tr1
447#endif
448
449_GLIBCXX_END_NAMESPACE_VERSION
450}
451
452#endif // _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC
453