bessel_ik.qbk 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. [section:mbessel Modified Bessel Functions of the First and Second Kinds]
  2. [h4 Synopsis]
  3. `#include <boost/math/special_functions/bessel.hpp>`
  4. template <class T1, class T2>
  5. ``__sf_result`` cyl_bessel_i(T1 v, T2 x);
  6. template <class T1, class T2, class ``__Policy``>
  7. ``__sf_result`` cyl_bessel_i(T1 v, T2 x, const ``__Policy``&);
  8. template <class T1, class T2>
  9. ``__sf_result`` cyl_bessel_k(T1 v, T2 x);
  10. template <class T1, class T2, class ``__Policy``>
  11. ``__sf_result`` cyl_bessel_k(T1 v, T2 x, const ``__Policy``&);
  12. [h4 Description]
  13. The functions __cyl_bessel_i and __cyl_bessel_k return the result of the
  14. modified Bessel functions of the first and second kind respectively:
  15. [:cyl_bessel_i(v, x) = I[sub v](x)]
  16. [:cyl_bessel_k(v, x) = K[sub v](x)]
  17. where:
  18. [equation mbessel2]
  19. [equation mbessel3]
  20. The return type of these functions is computed using the __arg_promotion_rules
  21. when T1 and T2 are different types. The functions are also optimised for the
  22. relatively common case that T1 is an integer.
  23. [optional_policy]
  24. The functions return the result of __domain_error whenever the result is
  25. undefined or complex. For __cyl_bessel_j this occurs when `x < 0` and v is not
  26. an integer, or when `x == 0` and `v != 0`. For __cyl_neumann this occurs
  27. when `x <= 0`.
  28. The following graph illustrates the exponential behaviour of I[sub v].
  29. [graph cyl_bessel_i]
  30. The following graph illustrates the exponential decay of K[sub v].
  31. [graph cyl_bessel_k]
  32. [h4 Testing]
  33. There are two sets of test values: spot values calculated using
  34. [@http://functions.wolfram.com functions.wolfram.com],
  35. and a much larger set of tests computed using
  36. a simplified version of this implementation
  37. (with all the special case handling removed).
  38. [h4 Accuracy]
  39. The following tables show how the accuracy of these functions
  40. varies on various platforms, along with comparison to other libraries.
  41. Note that only results for the widest floating-point type on the
  42. system are given, as narrower types have __zero_error. All values
  43. are relative errors in units of epsilon. Note that our test suite
  44. includes some fairly extreme inputs which results in most of the worst
  45. problem cases in other libraries:
  46. [table_cyl_bessel_i_integer_orders_]
  47. [table_cyl_bessel_i]
  48. [table_cyl_bessel_k_integer_orders_]
  49. [table_cyl_bessel_k]
  50. The following error plot are based on an exhaustive search of the functions domain for I0, I1, K0, and K1,
  51. MSVC-15.5 at `double` precision, and GCC-7.1/Ubuntu for `long double` and `__float128`.
  52. [graph i0__double]
  53. [graph i0__80_bit_long_double]
  54. [graph i0____float128]
  55. [graph i1__double]
  56. [graph i1__80_bit_long_double]
  57. [graph i1____float128]
  58. [graph k0__double]
  59. [graph k0__80_bit_long_double]
  60. [graph k0____float128]
  61. [graph k1__double]
  62. [graph k1__80_bit_long_double]
  63. [graph k1____float128]
  64. [h4 Implementation]
  65. The following are handled as special cases first:
  66. When computing I[sub v] for ['x < 0], then [nu] must be an integer
  67. or a domain error occurs. If [nu] is an integer, then the function is
  68. odd if [nu] is odd and even if [nu] is even, and we can reflect to
  69. ['x > 0].
  70. For I[sub v] with v equal to 0, 1 or 0.5 are handled as special cases.
  71. The 0 and 1 cases use polynomial approximations on
  72. finite and infinite intervals. The approximating forms
  73. are based on
  74. [@http://www.advanpix.com/2015/11/11/rational-approximations-for-the-modified-bessel-function-of-the-first-kind-i0-computations-double-precision/
  75. "Rational Approximations for the Modified Bessel Function of the First Kind - I[sub 0](x) for Computations with Double Precision"]
  76. by Pavel Holoborodko, extended by us to deal with up to 128-bit precision (with different approximations for each target precision).
  77. [equation bessel21]
  78. [equation bessel20]
  79. [equation bessel17]
  80. [equation bessel18]
  81. Similarly we have:
  82. [equation bessel22]
  83. [equation bessel23]
  84. [equation bessel24]
  85. [equation bessel25]
  86. The 0.5 case is a simple trigonometric function:
  87. [:I[sub 0.5](x) = sqrt(2 / [pi]x) * sinh(x)]
  88. For K[sub v] with /v/ an integer, the result is calculated using the recurrence relation:
  89. [equation mbessel5]
  90. starting from K[sub 0] and K[sub 1] which are calculated
  91. using rational the approximations above. These rational approximations are
  92. accurate to around 19 digits, and are therefore only used when T has
  93. no more than 64 binary digits of precision.
  94. When /x/ is small compared to /v/, I[sub v]x is best computed directly from the series:
  95. [equation mbessel17]
  96. In the general case, we first normalize [nu] to \[[^0, [inf]])
  97. with the help of the reflection formulae:
  98. [equation mbessel9]
  99. [equation mbessel10]
  100. Let [mu] = [nu] - floor([nu] + 1/2), then [mu] is the fractional part of
  101. [nu] such that |[mu]| <= 1/2 (we need this for convergence later). The idea is to
  102. calculate K[sub [mu]](x) and K[sub [mu]+1](x), and use them to obtain
  103. I[sub [nu]](x) and K[sub [nu]](x).
  104. The algorithm is proposed by Temme in
  105. [:N.M. Temme, ['On the numerical evaluation of the modified bessel function
  106. of the third kind], Journal of Computational Physics, vol 19, 324 (1975),]
  107. which needs two continued fractions as well as the Wronskian:
  108. [equation mbessel11]
  109. [equation mbessel12]
  110. [equation mbessel8]
  111. The continued fractions are computed using the modified Lentz's method
  112. [:(W.J. Lentz, ['Generating Bessel functions in Mie scattering calculations
  113. using continued fractions], Applied Optics, vol 15, 668 (1976)).]
  114. Their convergence rates depend on ['x], therefore we need
  115. different strategies for large ['x] and small ['x].
  116. ['x > v], CF1 needs O(['x]) iterations to converge, CF2 converges rapidly.
  117. ['x <= v], CF1 converges rapidly, CF2 fails to converge when ['x] [^->] 0.
  118. When ['x] is large (['x] > 2), both continued fractions converge (CF1
  119. may be slow for really large ['x]). K[sub [mu]] and K[sub [mu]+1]
  120. can be calculated by
  121. [equation mbessel13]
  122. where
  123. [equation mbessel14]
  124. ['S] is also a series that is summed along with CF2, see
  125. [:I.J. Thompson and A.R. Barnett, ['Modified Bessel functions I_v and K_v
  126. of real order and complex argument to selected accuracy], Computer Physics
  127. Communications, vol 47, 245 (1987).]
  128. When ['x] is small (['x] <= 2), CF2 convergence may fail (but CF1
  129. works very well). The solution here is Temme's series:
  130. [equation mbessel15]
  131. where
  132. [equation mbessel16]
  133. f[sub k] and h[sub k]
  134. are also computed by recursions (involving gamma functions), but the
  135. formulas are a little complicated, readers are referred to
  136. [:N.M. Temme, ['On the numerical evaluation of the modified Bessel function
  137. of the third kind], Journal of Computational Physics, vol 19, 324 (1975).]
  138. Note: Temme's series converge only for |[mu]| <= 1/2.
  139. K[sub [nu]](x) is then calculated from the forward
  140. recurrence, as is K[sub [nu]+1](x). With these two values and
  141. f[sub [nu]], the Wronskian yields I[sub [nu]](x) directly.
  142. [endsect] [/section:mbessel Modified Bessel Functions of the First and Second Kinds]
  143. [/
  144. Copyright 2006 John Maddock, Paul A. Bristow and Xiaogang Zhang.
  145. Distributed under the Boost Software License, Version 1.0.
  146. (See accompanying file LICENSE_1_0.txt or copy at
  147. http://www.boost.org/LICENSE_1_0.txt).
  148. ]