Chris@16
|
1 // Copyright (c) 2006 Xiaogang Zhang
|
Chris@16
|
2 // Use, modification and distribution are subject to the
|
Chris@16
|
3 // Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_MATH_BESSEL_YN_HPP
|
Chris@16
|
7 #define BOOST_MATH_BESSEL_YN_HPP
|
Chris@16
|
8
|
Chris@16
|
9 #ifdef _MSC_VER
|
Chris@16
|
10 #pragma once
|
Chris@16
|
11 #endif
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/math/special_functions/detail/bessel_y0.hpp>
|
Chris@16
|
14 #include <boost/math/special_functions/detail/bessel_y1.hpp>
|
Chris@16
|
15 #include <boost/math/special_functions/detail/bessel_jy_series.hpp>
|
Chris@16
|
16 #include <boost/math/policies/error_handling.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 // Bessel function of the second kind of integer order
|
Chris@16
|
19 // Y_n(z) is the dominant solution, forward recurrence always OK (though unstable)
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost { namespace math { namespace detail{
|
Chris@16
|
22
|
Chris@16
|
23 template <typename T, typename Policy>
|
Chris@16
|
24 T bessel_yn(int n, T x, const Policy& pol)
|
Chris@16
|
25 {
|
Chris@16
|
26 BOOST_MATH_STD_USING
|
Chris@16
|
27 T value, factor, current, prev;
|
Chris@16
|
28
|
Chris@16
|
29 using namespace boost::math::tools;
|
Chris@16
|
30
|
Chris@16
|
31 static const char* function = "boost::math::bessel_yn<%1%>(%1%,%1%)";
|
Chris@16
|
32
|
Chris@16
|
33 if ((x == 0) && (n == 0))
|
Chris@16
|
34 {
|
Chris@16
|
35 return -policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@16
|
36 }
|
Chris@16
|
37 if (x <= 0)
|
Chris@16
|
38 {
|
Chris@16
|
39 return policies::raise_domain_error<T>(function,
|
Chris@16
|
40 "Got x = %1%, but x must be > 0, complex result not supported.", x, pol);
|
Chris@16
|
41 }
|
Chris@16
|
42
|
Chris@16
|
43 //
|
Chris@16
|
44 // Reflection comes first:
|
Chris@16
|
45 //
|
Chris@16
|
46 if (n < 0)
|
Chris@16
|
47 {
|
Chris@16
|
48 factor = (n & 0x1) ? -1 : 1; // Y_{-n}(z) = (-1)^n Y_n(z)
|
Chris@16
|
49 n = -n;
|
Chris@16
|
50 }
|
Chris@16
|
51 else
|
Chris@16
|
52 {
|
Chris@16
|
53 factor = 1;
|
Chris@16
|
54 }
|
Chris@16
|
55
|
Chris@16
|
56 if(x < policies::get_epsilon<T, Policy>())
|
Chris@16
|
57 {
|
Chris@16
|
58 T scale = 1;
|
Chris@16
|
59 value = bessel_yn_small_z(n, x, &scale, pol);
|
Chris@16
|
60 if(tools::max_value<T>() * fabs(scale) < fabs(value))
|
Chris@16
|
61 return boost::math::sign(scale) * boost::math::sign(value) * policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@16
|
62 value /= scale;
|
Chris@16
|
63 }
|
Chris@16
|
64 else if (n == 0)
|
Chris@16
|
65 {
|
Chris@16
|
66 value = bessel_y0(x, pol);
|
Chris@16
|
67 }
|
Chris@16
|
68 else if (n == 1)
|
Chris@16
|
69 {
|
Chris@16
|
70 value = factor * bessel_y1(x, pol);
|
Chris@16
|
71 }
|
Chris@16
|
72 else
|
Chris@16
|
73 {
|
Chris@16
|
74 prev = bessel_y0(x, pol);
|
Chris@16
|
75 current = bessel_y1(x, pol);
|
Chris@16
|
76 int k = 1;
|
Chris@16
|
77 BOOST_ASSERT(k < n);
|
Chris@16
|
78 policies::check_series_iterations<T>("boost::math::bessel_y_n<%1%>(%1%,%1%)", n, pol);
|
Chris@16
|
79 do
|
Chris@16
|
80 {
|
Chris@16
|
81 T fact = 2 * k / x;
|
Chris@16
|
82 if((fact > 1) && ((tools::max_value<T>() - fabs(prev)) / fact < fabs(current)))
|
Chris@16
|
83 {
|
Chris@16
|
84 prev /= current;
|
Chris@16
|
85 factor /= current;
|
Chris@16
|
86 current = 1;
|
Chris@16
|
87 }
|
Chris@16
|
88 value = fact * current - prev;
|
Chris@16
|
89 prev = current;
|
Chris@16
|
90 current = value;
|
Chris@16
|
91 ++k;
|
Chris@16
|
92 }
|
Chris@16
|
93 while(k < n);
|
Chris@16
|
94 if(fabs(tools::max_value<T>() * factor) < fabs(value))
|
Chris@16
|
95 return sign(value) * sign(value) * policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@16
|
96 value /= factor;
|
Chris@16
|
97 }
|
Chris@16
|
98 return value;
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 }}} // namespaces
|
Chris@16
|
102
|
Chris@16
|
103 #endif // BOOST_MATH_BESSEL_YN_HPP
|
Chris@16
|
104
|