Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:37:16

0001 //  Copyright (c) 2006 Xiaogang Zhang
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
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 // Modified Bessel function of the second kind of integer order
0018 // K_n(z) is the dominant solution, forward recurrence always OK (though unstable)
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;                             // K_{-n}(z) = K_n(z)
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 }}} // namespaces
0085 
0086 #endif // BOOST_MATH_BESSEL_KN_HPP
0087