]> git.donarmstrong.com Git - rsem.git/blob - boost/math/special_functions/detail/bessel_kn.hpp
Updated boost to v1.55.0
[rsem.git] / boost / math / special_functions / detail / bessel_kn.hpp
1 //  Copyright (c) 2006 Xiaogang Zhang
2 //  Use, modification and distribution are subject to the
3 //  Boost Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef BOOST_MATH_BESSEL_KN_HPP
7 #define BOOST_MATH_BESSEL_KN_HPP
8
9 #ifdef _MSC_VER
10 #pragma once
11 #endif
12
13 #include <boost/math/special_functions/detail/bessel_k0.hpp>
14 #include <boost/math/special_functions/detail/bessel_k1.hpp>
15 #include <boost/math/policies/error_handling.hpp>
16
17 // Modified Bessel function of the second kind of integer order
18 // K_n(z) is the dominant solution, forward recurrence always OK (though unstable)
19
20 namespace boost { namespace math { namespace detail{
21
22 template <typename T, typename Policy>
23 T bessel_kn(int n, T x, const Policy& pol)
24 {
25     T value, current, prev;
26
27     using namespace boost::math::tools;
28
29     static const char* function = "boost::math::bessel_kn<%1%>(%1%,%1%)";
30
31     if (x < 0)
32     {
33        return policies::raise_domain_error<T>(function,
34             "Got x = %1%, but argument x must be non-negative, complex number result not supported.", x, pol);
35     }
36     if (x == 0)
37     {
38        return policies::raise_overflow_error<T>(function, 0, pol);
39     }
40
41     if (n < 0)
42     {
43         n = -n;                             // K_{-n}(z) = K_n(z)
44     }
45     if (n == 0)
46     {
47         value = bessel_k0(x, pol);
48     }
49     else if (n == 1)
50     {
51         value = bessel_k1(x, pol);
52     }
53     else
54     {
55        prev = bessel_k0(x, pol);
56        current = bessel_k1(x, pol);
57        int k = 1;
58        BOOST_ASSERT(k < n);
59        T scale = 1;
60        do
61        {
62            T fact = 2 * k / x;
63            if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
64            {
65               scale /= current;
66               prev /= current;
67               current = 1;
68            }
69            value = fact * current + prev;
70            prev = current;
71            current = value;
72            ++k;
73        }
74        while(k < n);
75        if(tools::max_value<T>() * scale < fabs(value))
76           return sign(scale) * sign(value) * policies::raise_overflow_error<T>(function, 0, pol);
77        value /= scale;
78     }
79     return value;
80 }
81
82 }}} // namespaces
83
84 #endif // BOOST_MATH_BESSEL_KN_HPP
85