File indexing completed on 2025-07-05 08:37:16
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MATH_BESSEL_KN_HPP
0007 #define BOOST_MATH_BESSEL_KN_HPP
0008
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #endif
0012
0013 #include <boost/math/special_functions/detail/bessel_k0.hpp>
0014 #include <boost/math/special_functions/detail/bessel_k1.hpp>
0015 #include <boost/math/policies/error_handling.hpp>
0016
0017
0018
0019
0020 namespace boost { namespace math { namespace detail{
0021
0022 template <typename T, typename Policy>
0023 T bessel_kn(int n, T x, const Policy& pol)
0024 {
0025 BOOST_MATH_STD_USING
0026 T value, current, prev;
0027
0028 using namespace boost::math::tools;
0029
0030 static const char* function = "boost::math::bessel_kn<%1%>(%1%,%1%)";
0031
0032 if (x < 0)
0033 {
0034 return policies::raise_domain_error<T>(function, "Got x = %1%, but argument x must be non-negative, complex number result not supported.", x, pol);
0035 }
0036 if (x == 0)
0037 {
0038 return (n == 0) ?
0039 policies::raise_overflow_error<T>(function, nullptr, pol)
0040 : policies::raise_domain_error<T>(function, "Got x = %1%, but argument x must be positive, complex number result not supported.", x, pol);
0041 }
0042
0043 if (n < 0)
0044 {
0045 n = -n;
0046 }
0047 if (n == 0)
0048 {
0049 value = bessel_k0(x);
0050 }
0051 else if (n == 1)
0052 {
0053 value = bessel_k1(x);
0054 }
0055 else
0056 {
0057 prev = bessel_k0(x);
0058 current = bessel_k1(x);
0059 int k = 1;
0060 BOOST_MATH_ASSERT(k < n);
0061 T scale = 1;
0062 do
0063 {
0064 T fact = 2 * k / x;
0065 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
0066 {
0067 scale /= current;
0068 prev /= current;
0069 current = 1;
0070 }
0071 value = fact * current + prev;
0072 prev = current;
0073 current = value;
0074 ++k;
0075 }
0076 while(k < n);
0077 if (tools::max_value<T>() * scale < fabs(value))
0078 return ((boost::math::signbit)(scale) ? -1 : 1) * sign(value) * policies::raise_overflow_error<T>(function, nullptr, pol);
0079 value /= scale;
0080 }
0081 return value;
0082 }
0083
0084 }}}
0085
0086 #endif
0087