Chris@16
|
1 // Copyright (c) 2006 Xiaogang Zhang
|
Chris@16
|
2 // Use, modification and distribution are subject to the
|
Chris@16
|
3 // Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_MATH_BESSEL_KN_HPP
|
Chris@16
|
7 #define BOOST_MATH_BESSEL_KN_HPP
|
Chris@16
|
8
|
Chris@16
|
9 #ifdef _MSC_VER
|
Chris@16
|
10 #pragma once
|
Chris@16
|
11 #endif
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/math/special_functions/detail/bessel_k0.hpp>
|
Chris@16
|
14 #include <boost/math/special_functions/detail/bessel_k1.hpp>
|
Chris@16
|
15 #include <boost/math/policies/error_handling.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 // Modified Bessel function of the second kind of integer order
|
Chris@16
|
18 // K_n(z) is the dominant solution, forward recurrence always OK (though unstable)
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost { namespace math { namespace detail{
|
Chris@16
|
21
|
Chris@16
|
22 template <typename T, typename Policy>
|
Chris@16
|
23 T bessel_kn(int n, T x, const Policy& pol)
|
Chris@16
|
24 {
|
Chris@101
|
25 BOOST_MATH_STD_USING
|
Chris@16
|
26 T value, current, prev;
|
Chris@16
|
27
|
Chris@16
|
28 using namespace boost::math::tools;
|
Chris@16
|
29
|
Chris@16
|
30 static const char* function = "boost::math::bessel_kn<%1%>(%1%,%1%)";
|
Chris@16
|
31
|
Chris@16
|
32 if (x < 0)
|
Chris@16
|
33 {
|
Chris@16
|
34 return policies::raise_domain_error<T>(function,
|
Chris@16
|
35 "Got x = %1%, but argument x must be non-negative, complex number result not supported.", x, pol);
|
Chris@16
|
36 }
|
Chris@16
|
37 if (x == 0)
|
Chris@16
|
38 {
|
Chris@16
|
39 return policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@16
|
40 }
|
Chris@16
|
41
|
Chris@16
|
42 if (n < 0)
|
Chris@16
|
43 {
|
Chris@16
|
44 n = -n; // K_{-n}(z) = K_n(z)
|
Chris@16
|
45 }
|
Chris@16
|
46 if (n == 0)
|
Chris@16
|
47 {
|
Chris@16
|
48 value = bessel_k0(x, pol);
|
Chris@16
|
49 }
|
Chris@16
|
50 else if (n == 1)
|
Chris@16
|
51 {
|
Chris@16
|
52 value = bessel_k1(x, pol);
|
Chris@16
|
53 }
|
Chris@16
|
54 else
|
Chris@16
|
55 {
|
Chris@16
|
56 prev = bessel_k0(x, pol);
|
Chris@16
|
57 current = bessel_k1(x, pol);
|
Chris@16
|
58 int k = 1;
|
Chris@16
|
59 BOOST_ASSERT(k < n);
|
Chris@16
|
60 T scale = 1;
|
Chris@16
|
61 do
|
Chris@16
|
62 {
|
Chris@16
|
63 T fact = 2 * k / x;
|
Chris@16
|
64 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
|
Chris@16
|
65 {
|
Chris@16
|
66 scale /= current;
|
Chris@16
|
67 prev /= current;
|
Chris@16
|
68 current = 1;
|
Chris@16
|
69 }
|
Chris@16
|
70 value = fact * current + prev;
|
Chris@16
|
71 prev = current;
|
Chris@16
|
72 current = value;
|
Chris@16
|
73 ++k;
|
Chris@16
|
74 }
|
Chris@16
|
75 while(k < n);
|
Chris@16
|
76 if(tools::max_value<T>() * scale < fabs(value))
|
Chris@16
|
77 return sign(scale) * sign(value) * policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@16
|
78 value /= scale;
|
Chris@16
|
79 }
|
Chris@16
|
80 return value;
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 }}} // namespaces
|
Chris@16
|
84
|
Chris@16
|
85 #endif // BOOST_MATH_BESSEL_KN_HPP
|
Chris@16
|
86
|