File indexing completed on 2025-01-18 09:40:00
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,
0035 "Got x = %1%, but argument x must be non-negative, complex number result not supported.", x, pol);
0036 }
0037 if (x == 0)
0038 {
0039 return policies::raise_overflow_error<T>(function, nullptr, pol);
0040 }
0041
0042 if (n < 0)
0043 {
0044 n = -n;
0045 }
0046 if (n == 0)
0047 {
0048 value = bessel_k0(x);
0049 }
0050 else if (n == 1)
0051 {
0052 value = bessel_k1(x);
0053 }
0054 else
0055 {
0056 prev = bessel_k0(x);
0057 current = bessel_k1(x);
0058 int k = 1;
0059 BOOST_MATH_ASSERT(k < n);
0060 T scale = 1;
0061 do
0062 {
0063 T fact = 2 * k / x;
0064 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
0065 {
0066 scale /= current;
0067 prev /= current;
0068 current = 1;
0069 }
0070 value = fact * current + prev;
0071 prev = current;
0072 current = value;
0073 ++k;
0074 }
0075 while(k < n);
0076 if(tools::max_value<T>() * scale < fabs(value))
0077 return sign(scale) * sign(value) * policies::raise_overflow_error<T>(function, nullptr, pol);
0078 value /= scale;
0079 }
0080 return value;
0081 }
0082
0083 }}}
0084
0085 #endif
0086