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