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_Y0_HPP |
7 | #define BOOST_MATH_BESSEL_Y0_HPP |
8 | |
9 | #ifdef _MSC_VER |
10 | #pragma once |
11 | #pragma warning(push) |
12 | #pragma warning(disable:4702) // Unreachable code (release mode only warning) |
13 | #endif |
14 | |
15 | #include <boost/math/special_functions/detail/bessel_j0.hpp> |
16 | #include <boost/math/constants/constants.hpp> |
17 | #include <boost/math/tools/rational.hpp> |
18 | #include <boost/math/tools/big_constant.hpp> |
19 | #include <boost/math/policies/error_handling.hpp> |
20 | #include <boost/assert.hpp> |
21 | |
22 | #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128) |
23 | // |
24 | // This is the only way we can avoid |
25 | // warning: non-standard suffix on floating constant [-Wpedantic] |
26 | // when building with -Wall -pedantic. Neither __extension__ |
27 | // nor #pragma diagnostic ignored work :( |
28 | // |
29 | #pragma GCC system_header |
30 | #endif |
31 | |
32 | // Bessel function of the second kind of order zero |
33 | // x <= 8, minimax rational approximations on root-bracketing intervals |
34 | // x > 8, Hankel asymptotic expansion in Hart, Computer Approximations, 1968 |
35 | |
36 | namespace boost { namespace math { namespace detail{ |
37 | |
38 | template <typename T, typename Policy> |
39 | T bessel_y0(T x, const Policy&); |
40 | |
41 | template <class T, class Policy> |
42 | struct bessel_y0_initializer |
43 | { |
44 | struct init |
45 | { |
46 | init() |
47 | { |
48 | do_init(); |
49 | } |
50 | static void do_init() |
51 | { |
52 | bessel_y0(T(1), Policy()); |
53 | } |
54 | void force_instantiate()const{} |
55 | }; |
56 | static const init initializer; |
57 | static void force_instantiate() |
58 | { |
59 | initializer.force_instantiate(); |
60 | } |
61 | }; |
62 | |
63 | template <class T, class Policy> |
64 | const typename bessel_y0_initializer<T, Policy>::init bessel_y0_initializer<T, Policy>::initializer; |
65 | |
66 | template <typename T, typename Policy> |
67 | T bessel_y0(T x, const Policy& pol) |
68 | { |
69 | bessel_y0_initializer<T, Policy>::force_instantiate(); |
70 | |
71 | static const T P1[] = { |
72 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0723538782003176831e+11)), |
73 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.3716255451260504098e+09)), |
74 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.0422274357376619816e+08)), |
75 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.1287548474401797963e+06)), |
76 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0102532948020907590e+04)), |
77 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.8402381979244993524e+01)), |
78 | }; |
79 | static const T Q1[] = { |
80 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.8873865738997033405e+11)), |
81 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.1617187777290363573e+09)), |
82 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.5662956624278251596e+07)), |
83 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.3889393209447253406e+05)), |
84 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.6475986689240190091e+02)), |
85 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)), |
86 | }; |
87 | static const T P2[] = { |
88 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2213976967566192242e+13)), |
89 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -5.5107435206722644429e+11)), |
90 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.3600098638603061642e+10)), |
91 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -6.9590439394619619534e+08)), |
92 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.6905288611678631510e+06)), |
93 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.4566865832663635920e+04)), |
94 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.7427031242901594547e+01)), |
95 | }; |
96 | static const T Q2[] = { |
97 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.3386146580707264428e+14)), |
98 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.4266824419412347550e+12)), |
99 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4015103849971240096e+10)), |
100 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.3960202770986831075e+08)), |
101 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.0669982352539552018e+05)), |
102 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.3030857612070288823e+02)), |
103 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)), |
104 | }; |
105 | static const T P3[] = { |
106 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.0728726905150210443e+15)), |
107 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.7016641869173237784e+14)), |
108 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2829912364088687306e+11)), |
109 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.9363051266772083678e+11)), |
110 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1958827170518100757e+09)), |
111 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.0085539923498211426e+07)), |
112 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1363534169313901632e+04)), |
113 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.7439661319197499338e+01)), |
114 | }; |
115 | static const T Q3[] = { |
116 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4563724628846457519e+17)), |
117 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.9272425569640309819e+15)), |
118 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2598377924042897629e+13)), |
119 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.6926121104209825246e+10)), |
120 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.4727219475672302327e+08)), |
121 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.3924739209768057030e+05)), |
122 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.7903362168128450017e+02)), |
123 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)), |
124 | }; |
125 | static const T PC[] = { |
126 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2779090197304684302e+04)), |
127 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.1345386639580765797e+04)), |
128 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1170523380864944322e+04)), |
129 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4806486443249270347e+03)), |
130 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.5376201909008354296e+02)), |
131 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.8961548424210455236e-01)), |
132 | }; |
133 | static const T QC[] = { |
134 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2779090197304684318e+04)), |
135 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.1370412495510416640e+04)), |
136 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1215350561880115730e+04)), |
137 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.5028735138235608207e+03)), |
138 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.5711159858080893649e+02)), |
139 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)), |
140 | }; |
141 | static const T PS[] = { |
142 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.9226600200800094098e+01)), |
143 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.8591953644342993800e+02)), |
144 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.1183429920482737611e+02)), |
145 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2300261666214198472e+01)), |
146 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2441026745835638459e+00)), |
147 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.8033303048680751817e-03)), |
148 | }; |
149 | static const T QS[] = { |
150 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.7105024128512061905e+03)), |
151 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.1951131543434613647e+04)), |
152 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.2642780169211018836e+03)), |
153 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.4887231232283756582e+03)), |
154 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 9.0593769594993125859e+01)), |
155 | static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)), |
156 | }; |
157 | static const T x1 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.9357696627916752158e-01)), |
158 | x2 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.9576784193148578684e+00)), |
159 | x3 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.0860510603017726976e+00)), |
160 | x11 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.280e+02)), |
161 | x12 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.9519662791675215849e-03)), |
162 | x21 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0130e+03)), |
163 | x22 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.4716931485786837568e-04)), |
164 | x31 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.8140e+03)), |
165 | x32 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.1356030177269762362e-04)) |
166 | ; |
167 | T value, factor, r, rc, rs; |
168 | |
169 | BOOST_MATH_STD_USING |
170 | using namespace boost::math::tools; |
171 | using namespace boost::math::constants; |
172 | |
173 | static const char* function = "boost::math::bessel_y0<%1%>(%1%,%1%)" ; |
174 | |
175 | if (x < 0) |
176 | { |
177 | return policies::raise_domain_error<T>(function, |
178 | "Got x = %1% but x must be non-negative, complex result not supported." , x, pol); |
179 | } |
180 | if (x == 0) |
181 | { |
182 | return -policies::raise_overflow_error<T>(function, 0, pol); |
183 | } |
184 | if (x <= 3) // x in (0, 3] |
185 | { |
186 | T y = x * x; |
187 | T z = 2 * log(x/x1) * bessel_j0(x) / pi<T>(); |
188 | r = evaluate_rational(P1, Q1, y); |
189 | factor = (x + x1) * ((x - x11/256) - x12); |
190 | value = z + factor * r; |
191 | } |
192 | else if (x <= 5.5f) // x in (3, 5.5] |
193 | { |
194 | T y = x * x; |
195 | T z = 2 * log(x/x2) * bessel_j0(x) / pi<T>(); |
196 | r = evaluate_rational(P2, Q2, y); |
197 | factor = (x + x2) * ((x - x21/256) - x22); |
198 | value = z + factor * r; |
199 | } |
200 | else if (x <= 8) // x in (5.5, 8] |
201 | { |
202 | T y = x * x; |
203 | T z = 2 * log(x/x3) * bessel_j0(x) / pi<T>(); |
204 | r = evaluate_rational(P3, Q3, y); |
205 | factor = (x + x3) * ((x - x31/256) - x32); |
206 | value = z + factor * r; |
207 | } |
208 | else // x in (8, \infty) |
209 | { |
210 | T y = 8 / x; |
211 | T y2 = y * y; |
212 | rc = evaluate_rational(PC, QC, y2); |
213 | rs = evaluate_rational(PS, QS, y2); |
214 | factor = constants::one_div_root_pi<T>() / sqrt(x); |
215 | // |
216 | // The following code is really just: |
217 | // |
218 | // T z = x - 0.25f * pi<T>(); |
219 | // value = factor * (rc * sin(z) + y * rs * cos(z)); |
220 | // |
221 | // But using the sin/cos addition formulae and constant values for |
222 | // sin/cos of PI/4 which then cancel part of the "factor" term as they're all |
223 | // 1 / sqrt(2): |
224 | // |
225 | T sx = sin(x); |
226 | T cx = cos(x); |
227 | value = factor * (rc * (sx - cx) + y * rs * (cx + sx)); |
228 | } |
229 | |
230 | return value; |
231 | } |
232 | |
233 | }}} // namespaces |
234 | |
235 | #ifdef _MSC_VER |
236 | #pragma warning(pop) |
237 | #endif |
238 | |
239 | #endif // BOOST_MATH_BESSEL_Y0_HPP |
240 | |
241 | |