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@16
|
25 T value, current, prev;
|
Chris@16
|
26
|
Chris@16
|
27 using namespace boost::math::tools;
|
Chris@16
|
28
|
Chris@16
|
29 static const char* function = "boost::math::bessel_kn<%1%>(%1%,%1%)";
|
Chris@16
|
30
|
Chris@16
|
31 if (x < 0)
|
Chris@16
|
32 {
|
Chris@16
|
33 return policies::raise_domain_error<T>(function,
|
Chris@16
|
34 "Got x = %1%, but argument x must be non-negative, complex number result not supported.", x, pol);
|
Chris@16
|
35 }
|
Chris@16
|
36 if (x == 0)
|
Chris@16
|
37 {
|
Chris@16
|
38 return policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@16
|
39 }
|
Chris@16
|
40
|
Chris@16
|
41 if (n < 0)
|
Chris@16
|
42 {
|
Chris@16
|
43 n = -n; // K_{-n}(z) = K_n(z)
|
Chris@16
|
44 }
|
Chris@16
|
45 if (n == 0)
|
Chris@16
|
46 {
|
Chris@16
|
47 value = bessel_k0(x, pol);
|
Chris@16
|
48 }
|
Chris@16
|
49 else if (n == 1)
|
Chris@16
|
50 {
|
Chris@16
|
51 value = bessel_k1(x, pol);
|
Chris@16
|
52 }
|
Chris@16
|
53 else
|
Chris@16
|
54 {
|
Chris@16
|
55 prev = bessel_k0(x, pol);
|
Chris@16
|
56 current = bessel_k1(x, pol);
|
Chris@16
|
57 int k = 1;
|
Chris@16
|
58 BOOST_ASSERT(k < n);
|
Chris@16
|
59 T scale = 1;
|
Chris@16
|
60 do
|
Chris@16
|
61 {
|
Chris@16
|
62 T fact = 2 * k / x;
|
Chris@16
|
63 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
|
Chris@16
|
64 {
|
Chris@16
|
65 scale /= current;
|
Chris@16
|
66 prev /= current;
|
Chris@16
|
67 current = 1;
|
Chris@16
|
68 }
|
Chris@16
|
69 value = fact * current + prev;
|
Chris@16
|
70 prev = current;
|
Chris@16
|
71 current = value;
|
Chris@16
|
72 ++k;
|
Chris@16
|
73 }
|
Chris@16
|
74 while(k < n);
|
Chris@16
|
75 if(tools::max_value<T>() * scale < fabs(value))
|
Chris@16
|
76 return sign(scale) * sign(value) * policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@16
|
77 value /= scale;
|
Chris@16
|
78 }
|
Chris@16
|
79 return value;
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 }}} // namespaces
|
Chris@16
|
83
|
Chris@16
|
84 #endif // BOOST_MATH_BESSEL_KN_HPP
|
Chris@16
|
85
|