// Copyright (c) 2006 Xiaogang Zhang // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_MATH_BESSEL_KN_HPP #define BOOST_MATH_BESSEL_KN_HPP #include #include #include // Modified Bessel function of the second kind of integer order // K_n(z) is the dominant solution, forward recurrence always OK (though unstable) namespace boost { namespace math { namespace detail{ template T bessel_kn(int n, T x, const Policy& pol) { T value, current, prev; using namespace boost::math::tools; static const char* function = "boost::math::bessel_kn<%1%>(%1%,%1%)"; if (x < 0) { return policies::raise_domain_error(function, "Got x = %1%, but argument x must be non-negative, complex number result not supported.", x, pol); } if (x == 0) { return policies::raise_overflow_error(function, 0, pol); } if (n < 0) { n = -n; // K_{-n}(z) = K_n(z) } if (n == 0) { value = bessel_k0(x, pol); } else if (n == 1) { value = bessel_k1(x, pol); } else { prev = bessel_k0(x, pol); current = bessel_k1(x, pol); int k = 1; BOOST_ASSERT(k < n); do { value = 2 * k * current / x + prev; prev = current; current = value; ++k; } while(k < n); } return value; } }}} // namespaces #endif // BOOST_MATH_BESSEL_KN_HPP