...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#include <boost/math/special_functions/bessel.hpp>
template <class T1, class T2> calculatedresulttype cyl_bessel_i(T1 v, T2 x); template <class T1, class T2, class Policy> calculatedresulttype cyl_bessel_i(T1 v, T2 x, const Policy&); template <class T1, class T2> calculatedresulttype cyl_bessel_k(T1 v, T2 x); template <class T1, class T2, class Policy> calculatedresulttype cyl_bessel_k(T1 v, T2 x, const Policy&);
The functions cyl_bessel_i and cyl_bessel_k return the result of the modified Bessel functions of the first and second kind respectively:
cyl_bessel_i(v, x) = I_{v}(x)
cyl_bessel_k(v, x) = K_{v}(x)
where:
The return type of these functions is computed using the result type calculation rules when T1 and T2 are different types. The functions are also optimised for the relatively common case that T1 is an integer.
The final Policy argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the policy documentation for more details.
The functions return the result of domain_error
whenever the result is undefined or complex. For cyl_bessel_j
this occurs when x <
0
and v is not an integer, or when
x ==
0
and v
!= 0
.
For cyl_neumann this
occurs when x <=
0
.
The following graph illustrates the exponential behaviour of I_{v}.
The following graph illustrates the exponential decay of K_{v}.
There are two sets of test values: spot values calculated using functions.wolfram.com, and a much larger set of tests computed using a simplified version of this implementation (with all the special case handling removed).
The following tables show how the accuracy of these functions varies on various platforms, along with comparison to other libraries. Note that only results for the widest floatingpoint type on the system are given, as narrower types have effectively zero error. All values are relative errors in units of epsilon. Note that our test suite includes some fairly extreme inputs which results in most of the worst problem cases in other libraries:
Table 6.44. Error rates for cyl_bessel_i (integer orders)
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


Bessel I0: Mathworld Data (Integer Version) 
Max = 0.877ε (Mean = 0.549ε) 
Max = 4.57ε (Mean = 2.1ε) 
Max = 0ε (Mean = 0ε) 
Max = 4.54ε (Mean = 2.11ε) 
Bessel I1: Mathworld Data (Integer Version) 
Max = 0.885ε (Mean = 0.55ε) 
Max = 7.83ε (Mean = 2.79ε) 
Max = 0ε (Mean = 0ε) 
Max = 6.52ε (Mean = 2.25ε) 
Bessel In: Mathworld Data (Integer Version) 
Max = 3.46ε (Mean = 1.32ε) 
Max = 1.8ε (Mean = 1.33ε) 
Max = 0ε (Mean = 0ε) 
Max = 463ε (Mean = 140ε) 
Table 6.45. Error rates for cyl_bessel_i
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


Bessel I0: Mathworld Data 
Max = 0.877ε (Mean = 0.549ε) 
Max = 4.57ε (Mean = 2.1ε) 
Max = 0ε (Mean = 0ε) 
Max = 4.54ε (Mean = 2.11ε) 
Bessel I1: Mathworld Data 
Max = 0.885ε (Mean = 0.55ε) 
Max = 7.83ε (Mean = 2.79ε) 
Max = 0ε (Mean = 0ε) 
Max = 6.52ε (Mean = 2.25ε) 
Bessel In: Mathworld Data 
Max = 3.46ε (Mean = 1.32ε) 
Max = 1.8ε (Mean = 1.33ε) 
Max = 0ε (Mean = 0ε) 
Max = 463ε (Mean = 140ε) 
Bessel Iv: Mathworld Data 
Max = 2.97ε (Mean = 1.33ε) 
Max = 4.12ε (Mean = 1.85ε) 
Max = 0ε (Mean = 0ε) 
Max = 4.12ε (Mean = 1.95ε) 
Bessel In: Random Data 
Max = 9.67ε (Mean = 1.89ε) 
Max = 6.79ε (Mean = 1.15ε) 
Max = 0ε (Mean = 0ε) 
Max = 176ε (Mean = 39.2ε) 
Bessel Iv: Random Data 
Max = 7.46ε (Mean = 1.54ε) 
Max = 8.35ε (Mean = 1.49ε) 
Max = 0.661ε (Mean = 0.0441ε) 
Max = 283ε (Mean = 88.4ε) 
Bessel Iv: Mathworld Data (large values) 
Max = 3.67ε (Mean = 1.64ε) 
Max = 14.7ε (Mean = 6.57ε) 
Max = 0ε (Mean = 0ε) 
Max = 14.7ε (Mean = 6.59ε) 
Table 6.46. Error rates for cyl_bessel_k (integer orders)
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


Bessel K0: Mathworld Data (Integer Version) 
Max = 1.55ε (Mean = 0.837ε) 
Max = 4.16ε (Mean = 1.46ε) 
Max = 0ε (Mean = 0ε) 
Max = 4.16ε (Mean = 1.49ε) 
Bessel K1: Mathworld Data (Integer Version) 
Max = 1ε (Mean = 0.573ε) 
Max = 1.8ε (Mean = 1.02ε) 
Max = 0ε (Mean = 0ε) 
Max = 1.8ε (Mean = 1.02ε) 
Bessel Kn: Mathworld Data (Integer Version) 
Max = 3.63ε (Mean = 1.46ε) 
Max = 4.48ε (Mean = 2.14ε) 
Max = 0ε (Mean = 0ε) 
Max = 4.48ε (Mean = 1.98ε) 
Table 6.47. Error rates for cyl_bessel_k
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


Bessel K0: Mathworld Data 
Max = 1.55ε (Mean = 0.837ε) 
Max = 4.16ε (Mean = 1.46ε) 
Max = 0ε (Mean = 0ε) 
Max = 4.16ε (Mean = 1.49ε) 
Bessel K1: Mathworld Data 
Max = 1ε (Mean = 0.573ε) 
Max = 1.8ε (Mean = 1.02ε) 
Max = 0ε (Mean = 0ε) 
Max = 1.8ε (Mean = 1.02ε) 
Bessel Kn: Mathworld Data 
Max = 3.63ε (Mean = 1.46ε) 
Max = 4.48ε (Mean = 2.14ε) 
Max = 0ε (Mean = 0ε) 
Max = 4.48ε (Mean = 1.98ε) 
Bessel Kv: Mathworld Data 
Max = 4.78ε (Mean = 2.2ε) 
Max = 3.58ε (Mean = 2.44ε) 
Max = 0ε (Mean = 0ε) 
Max = 3.58ε (Mean = 2.29ε) 
Bessel Kv: Mathworld Data (large values) 
Max = 59.8ε (Mean = 26.9ε) 
Max = 42.3ε (Mean = 21ε) 
Max = 0ε (Mean = 0ε) 
Max = 43.1ε (Mean = 21.3ε) 
Bessel Kn: Random Data 
Max = 7.47ε (Mean = 1.4ε) 
Max = 4.55ε (Mean = 1.09ε) 
Max = 0.764ε (Mean = 0.0348ε) 
Max = 4.55ε (Mean = 1.21ε) 
Bessel Kv: Random Data 
Max = 8.33ε (Mean = 1.62ε) 
Max = 7.88ε (Mean = 1.48ε) 
Max = 0.507ε (Mean = 0.0313ε) 
Max = 7.88ε (Mean = 1.49ε) 
The following are handled as special cases first:
When computing I_{v} for x < 0, then ν must be an integer or a domain error occurs. If ν is an integer, then the function is odd if ν is odd and even if ν is even, and we can reflect to x > 0.
For I_{v} with v equal to 0, 1 or 0.5 are handled as special cases.
The 0 and 1 cases use polynomial approximations on finite and infinite intervals. The approximating forms are based on "Rational Approximations for the Modified Bessel Function of the First Kind  I_{0}(x) for Computations with Double Precision" by Pavel Holoborodko, extended by us to deal with up to 128bit precision (with different approximations for each target precision).
Similarly we have:
The 0.5 case is a simple trigonometric function:
I_{0.5}(x) = sqrt(2 / πx) * sinh(x)
For K_{v} with v an integer, the result is calculated using the recurrence relation:
starting from K_{0} and K_{1} which are calculated using rational the approximations above. These rational approximations are accurate to around 19 digits, and are therefore only used when T has no more than 64 binary digits of precision.
When x is small compared to v, I_{v}x is best computed directly from the series:
In the general case, we first normalize ν to [0, [inf]
)
with the help of the reflection formulae:
Let μ = ν  floor(ν + 1/2), then μ is the fractional part of ν such that μ <= 1/2 (we need this for convergence later). The idea is to calculate K_{μ}(x) and K_{μ+1}(x), and use them to obtain I_{ν}(x) and K_{ν}(x).
The algorithm is proposed by Temme in N.M. Temme, On the numerical evaluation of the modified bessel function of the third kind, Journal of Computational Physics, vol 19, 324 (1975), which needs two continued fractions as well as the Wronskian:
The continued fractions are computed using the modified Lentz's method (W.J. Lentz, Generating Bessel functions in Mie scattering calculations using continued fractions, Applied Optics, vol 15, 668 (1976)). Their convergence rates depend on x, therefore we need different strategies for large x and small x.
x > v, CF1 needs O(x) iterations to converge, CF2 converges rapidly.
x <= v, CF1 converges rapidly, CF2 fails to converge
when x >
0.
When x is large (x > 2), both continued fractions converge (CF1 may be slow for really large x). K_{μ} and K_{μ+1} can be calculated by
where
S is also a series that is summed along with CF2, see I.J. Thompson and A.R. Barnett, Modified Bessel functions I_v and K_v of real order and complex argument to selected accuracy, Computer Physics Communications, vol 47, 245 (1987).
When x is small (x <= 2), CF2 convergence may fail (but CF1 works very well). The solution here is Temme's series:
where
f_{k} and h_{k} are also computed by recursions (involving gamma functions), but the formulas are a little complicated, readers are referred to N.M. Temme, On the numerical evaluation of the modified Bessel function of the third kind, Journal of Computational Physics, vol 19, 324 (1975). Note: Temme's series converge only for μ <= 1/2.
K_{ν}(x) is then calculated from the forward recurrence, as is K_{ν+1}(x). With these two values and f_{ν}, the Wronskian yields I_{ν}(x) directly.