1 // Copyright (c) 2006 Xiaogang Zhang
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_BESSEL_KN_HPP
7 #define BOOST_MATH_BESSEL_KN_HPP
13 #include <boost/math/special_functions/detail/bessel_k0.hpp>
14 #include <boost/math/special_functions/detail/bessel_k1.hpp>
15 #include <boost/math/policies/error_handling.hpp>
17 // Modified Bessel function of the second kind of integer order
18 // K_n(z) is the dominant solution, forward recurrence always OK (though unstable)
20 namespace boost { namespace math { namespace detail{
22 template <typename T, typename Policy>
23 T bessel_kn(int n, T x, const Policy& pol)
25 T value, current, prev;
27 using namespace boost::math::tools;
29 static const char* function = "boost::math::bessel_kn<%1%>(%1%,%1%)";
33 return policies::raise_domain_error<T>(function,
34 "Got x = %1%, but argument x must be non-negative, complex number result not supported.", x, pol);
38 return policies::raise_overflow_error<T>(function, 0, pol);
43 n = -n; // K_{-n}(z) = K_n(z)
47 value = bessel_k0(x, pol);
51 value = bessel_k1(x, pol);
55 prev = bessel_k0(x, pol);
56 current = bessel_k1(x, pol);
63 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
69 value = fact * current + prev;
75 if(tools::max_value<T>() * scale < fabs(value))
76 return sign(scale) * sign(value) * policies::raise_overflow_error<T>(function, 0, pol);
84 #endif // BOOST_MATH_BESSEL_KN_HPP