comparison DEPENDENCIES/generic/include/boost/math/special_functions/detail/bessel_kn.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
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