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_JY_HPP
|
Chris@16
|
7 #define BOOST_MATH_BESSEL_JY_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/tools/config.hpp>
|
Chris@16
|
14 #include <boost/math/special_functions/gamma.hpp>
|
Chris@16
|
15 #include <boost/math/special_functions/sign.hpp>
|
Chris@16
|
16 #include <boost/math/special_functions/hypot.hpp>
|
Chris@16
|
17 #include <boost/math/special_functions/sin_pi.hpp>
|
Chris@16
|
18 #include <boost/math/special_functions/cos_pi.hpp>
|
Chris@16
|
19 #include <boost/math/special_functions/detail/bessel_jy_asym.hpp>
|
Chris@16
|
20 #include <boost/math/special_functions/detail/bessel_jy_series.hpp>
|
Chris@16
|
21 #include <boost/math/constants/constants.hpp>
|
Chris@16
|
22 #include <boost/math/policies/error_handling.hpp>
|
Chris@16
|
23 #include <boost/mpl/if.hpp>
|
Chris@16
|
24 #include <boost/type_traits/is_floating_point.hpp>
|
Chris@16
|
25 #include <complex>
|
Chris@16
|
26
|
Chris@16
|
27 // Bessel functions of the first and second kind of fractional order
|
Chris@16
|
28
|
Chris@16
|
29 namespace boost { namespace math {
|
Chris@16
|
30
|
Chris@16
|
31 namespace detail {
|
Chris@16
|
32
|
Chris@16
|
33 //
|
Chris@16
|
34 // Simultaneous calculation of A&S 9.2.9 and 9.2.10
|
Chris@16
|
35 // for use in A&S 9.2.5 and 9.2.6.
|
Chris@16
|
36 // This series is quick to evaluate, but divergent unless
|
Chris@16
|
37 // x is very large, in fact it's pretty hard to figure out
|
Chris@16
|
38 // with any degree of precision when this series actually
|
Chris@16
|
39 // *will* converge!! Consequently, we may just have to
|
Chris@16
|
40 // try it and see...
|
Chris@16
|
41 //
|
Chris@16
|
42 template <class T, class Policy>
|
Chris@16
|
43 bool hankel_PQ(T v, T x, T* p, T* q, const Policy& )
|
Chris@16
|
44 {
|
Chris@16
|
45 BOOST_MATH_STD_USING
|
Chris@16
|
46 T tolerance = 2 * policies::get_epsilon<T, Policy>();
|
Chris@16
|
47 *p = 1;
|
Chris@16
|
48 *q = 0;
|
Chris@16
|
49 T k = 1;
|
Chris@16
|
50 T z8 = 8 * x;
|
Chris@16
|
51 T sq = 1;
|
Chris@16
|
52 T mu = 4 * v * v;
|
Chris@16
|
53 T term = 1;
|
Chris@16
|
54 bool ok = true;
|
Chris@16
|
55 do
|
Chris@16
|
56 {
|
Chris@16
|
57 term *= (mu - sq * sq) / (k * z8);
|
Chris@16
|
58 *q += term;
|
Chris@16
|
59 k += 1;
|
Chris@16
|
60 sq += 2;
|
Chris@16
|
61 T mult = (sq * sq - mu) / (k * z8);
|
Chris@16
|
62 ok = fabs(mult) < 0.5f;
|
Chris@16
|
63 term *= mult;
|
Chris@16
|
64 *p += term;
|
Chris@16
|
65 k += 1;
|
Chris@16
|
66 sq += 2;
|
Chris@16
|
67 }
|
Chris@16
|
68 while((fabs(term) > tolerance * *p) && ok);
|
Chris@16
|
69 return ok;
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 // Calculate Y(v, x) and Y(v+1, x) by Temme's method, see
|
Chris@16
|
73 // Temme, Journal of Computational Physics, vol 21, 343 (1976)
|
Chris@16
|
74 template <typename T, typename Policy>
|
Chris@16
|
75 int temme_jy(T v, T x, T* Y, T* Y1, const Policy& pol)
|
Chris@16
|
76 {
|
Chris@16
|
77 T g, h, p, q, f, coef, sum, sum1, tolerance;
|
Chris@16
|
78 T a, d, e, sigma;
|
Chris@16
|
79 unsigned long k;
|
Chris@16
|
80
|
Chris@16
|
81 BOOST_MATH_STD_USING
|
Chris@16
|
82 using namespace boost::math::tools;
|
Chris@16
|
83 using namespace boost::math::constants;
|
Chris@16
|
84
|
Chris@16
|
85 BOOST_ASSERT(fabs(v) <= 0.5f); // precondition for using this routine
|
Chris@16
|
86
|
Chris@16
|
87 T gp = boost::math::tgamma1pm1(v, pol);
|
Chris@16
|
88 T gm = boost::math::tgamma1pm1(-v, pol);
|
Chris@16
|
89 T spv = boost::math::sin_pi(v, pol);
|
Chris@16
|
90 T spv2 = boost::math::sin_pi(v/2, pol);
|
Chris@16
|
91 T xp = pow(x/2, v);
|
Chris@16
|
92
|
Chris@16
|
93 a = log(x / 2);
|
Chris@16
|
94 sigma = -a * v;
|
Chris@16
|
95 d = abs(sigma) < tools::epsilon<T>() ?
|
Chris@16
|
96 T(1) : sinh(sigma) / sigma;
|
Chris@16
|
97 e = abs(v) < tools::epsilon<T>() ? T(v*pi<T>()*pi<T>() / 2)
|
Chris@16
|
98 : T(2 * spv2 * spv2 / v);
|
Chris@16
|
99
|
Chris@16
|
100 T g1 = (v == 0) ? T(-euler<T>()) : T((gp - gm) / ((1 + gp) * (1 + gm) * 2 * v));
|
Chris@16
|
101 T g2 = (2 + gp + gm) / ((1 + gp) * (1 + gm) * 2);
|
Chris@16
|
102 T vspv = (fabs(v) < tools::epsilon<T>()) ? T(1/constants::pi<T>()) : T(v / spv);
|
Chris@16
|
103 f = (g1 * cosh(sigma) - g2 * a * d) * 2 * vspv;
|
Chris@16
|
104
|
Chris@16
|
105 p = vspv / (xp * (1 + gm));
|
Chris@16
|
106 q = vspv * xp / (1 + gp);
|
Chris@16
|
107
|
Chris@16
|
108 g = f + e * q;
|
Chris@16
|
109 h = p;
|
Chris@16
|
110 coef = 1;
|
Chris@16
|
111 sum = coef * g;
|
Chris@16
|
112 sum1 = coef * h;
|
Chris@16
|
113
|
Chris@16
|
114 T v2 = v * v;
|
Chris@16
|
115 T coef_mult = -x * x / 4;
|
Chris@16
|
116
|
Chris@16
|
117 // series summation
|
Chris@16
|
118 tolerance = policies::get_epsilon<T, Policy>();
|
Chris@16
|
119 for (k = 1; k < policies::get_max_series_iterations<Policy>(); k++)
|
Chris@16
|
120 {
|
Chris@16
|
121 f = (k * f + p + q) / (k*k - v2);
|
Chris@16
|
122 p /= k - v;
|
Chris@16
|
123 q /= k + v;
|
Chris@16
|
124 g = f + e * q;
|
Chris@16
|
125 h = p - k * g;
|
Chris@16
|
126 coef *= coef_mult / k;
|
Chris@16
|
127 sum += coef * g;
|
Chris@16
|
128 sum1 += coef * h;
|
Chris@16
|
129 if (abs(coef * g) < abs(sum) * tolerance)
|
Chris@16
|
130 {
|
Chris@16
|
131 break;
|
Chris@16
|
132 }
|
Chris@16
|
133 }
|
Chris@16
|
134 policies::check_series_iterations<T>("boost::math::bessel_jy<%1%>(%1%,%1%) in temme_jy", k, pol);
|
Chris@16
|
135 *Y = -sum;
|
Chris@16
|
136 *Y1 = -2 * sum1 / x;
|
Chris@16
|
137
|
Chris@16
|
138 return 0;
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 // Evaluate continued fraction fv = J_(v+1) / J_v, see
|
Chris@16
|
142 // Abramowitz and Stegun, Handbook of Mathematical Functions, 1972, 9.1.73
|
Chris@16
|
143 template <typename T, typename Policy>
|
Chris@16
|
144 int CF1_jy(T v, T x, T* fv, int* sign, const Policy& pol)
|
Chris@16
|
145 {
|
Chris@16
|
146 T C, D, f, a, b, delta, tiny, tolerance;
|
Chris@16
|
147 unsigned long k;
|
Chris@16
|
148 int s = 1;
|
Chris@16
|
149
|
Chris@16
|
150 BOOST_MATH_STD_USING
|
Chris@16
|
151
|
Chris@16
|
152 // |x| <= |v|, CF1_jy converges rapidly
|
Chris@16
|
153 // |x| > |v|, CF1_jy needs O(|x|) iterations to converge
|
Chris@16
|
154
|
Chris@16
|
155 // modified Lentz's method, see
|
Chris@16
|
156 // Lentz, Applied Optics, vol 15, 668 (1976)
|
Chris@16
|
157 tolerance = 2 * policies::get_epsilon<T, Policy>();;
|
Chris@16
|
158 tiny = sqrt(tools::min_value<T>());
|
Chris@16
|
159 C = f = tiny; // b0 = 0, replace with tiny
|
Chris@16
|
160 D = 0;
|
Chris@16
|
161 for (k = 1; k < policies::get_max_series_iterations<Policy>() * 100; k++)
|
Chris@16
|
162 {
|
Chris@16
|
163 a = -1;
|
Chris@16
|
164 b = 2 * (v + k) / x;
|
Chris@16
|
165 C = b + a / C;
|
Chris@16
|
166 D = b + a * D;
|
Chris@16
|
167 if (C == 0) { C = tiny; }
|
Chris@16
|
168 if (D == 0) { D = tiny; }
|
Chris@16
|
169 D = 1 / D;
|
Chris@16
|
170 delta = C * D;
|
Chris@16
|
171 f *= delta;
|
Chris@16
|
172 if (D < 0) { s = -s; }
|
Chris@16
|
173 if (abs(delta - 1) < tolerance)
|
Chris@16
|
174 { break; }
|
Chris@16
|
175 }
|
Chris@16
|
176 policies::check_series_iterations<T>("boost::math::bessel_jy<%1%>(%1%,%1%) in CF1_jy", k / 100, pol);
|
Chris@16
|
177 *fv = -f;
|
Chris@16
|
178 *sign = s; // sign of denominator
|
Chris@16
|
179
|
Chris@16
|
180 return 0;
|
Chris@16
|
181 }
|
Chris@16
|
182 //
|
Chris@16
|
183 // This algorithm was originally written by Xiaogang Zhang
|
Chris@16
|
184 // using std::complex to perform the complex arithmetic.
|
Chris@16
|
185 // However, that turns out to 10x or more slower than using
|
Chris@16
|
186 // all real-valued arithmetic, so it's been rewritten using
|
Chris@16
|
187 // real values only.
|
Chris@16
|
188 //
|
Chris@16
|
189 template <typename T, typename Policy>
|
Chris@16
|
190 int CF2_jy(T v, T x, T* p, T* q, const Policy& pol)
|
Chris@16
|
191 {
|
Chris@16
|
192 BOOST_MATH_STD_USING
|
Chris@16
|
193
|
Chris@16
|
194 T Cr, Ci, Dr, Di, fr, fi, a, br, bi, delta_r, delta_i, temp;
|
Chris@16
|
195 T tiny;
|
Chris@16
|
196 unsigned long k;
|
Chris@16
|
197
|
Chris@16
|
198 // |x| >= |v|, CF2_jy converges rapidly
|
Chris@16
|
199 // |x| -> 0, CF2_jy fails to converge
|
Chris@16
|
200 BOOST_ASSERT(fabs(x) > 1);
|
Chris@16
|
201
|
Chris@16
|
202 // modified Lentz's method, complex numbers involved, see
|
Chris@16
|
203 // Lentz, Applied Optics, vol 15, 668 (1976)
|
Chris@16
|
204 T tolerance = 2 * policies::get_epsilon<T, Policy>();
|
Chris@16
|
205 tiny = sqrt(tools::min_value<T>());
|
Chris@16
|
206 Cr = fr = -0.5f / x;
|
Chris@16
|
207 Ci = fi = 1;
|
Chris@16
|
208 //Dr = Di = 0;
|
Chris@16
|
209 T v2 = v * v;
|
Chris@16
|
210 a = (0.25f - v2) / x; // Note complex this one time only!
|
Chris@16
|
211 br = 2 * x;
|
Chris@16
|
212 bi = 2;
|
Chris@16
|
213 temp = Cr * Cr + 1;
|
Chris@16
|
214 Ci = bi + a * Cr / temp;
|
Chris@16
|
215 Cr = br + a / temp;
|
Chris@16
|
216 Dr = br;
|
Chris@16
|
217 Di = bi;
|
Chris@16
|
218 if (fabs(Cr) + fabs(Ci) < tiny) { Cr = tiny; }
|
Chris@16
|
219 if (fabs(Dr) + fabs(Di) < tiny) { Dr = tiny; }
|
Chris@16
|
220 temp = Dr * Dr + Di * Di;
|
Chris@16
|
221 Dr = Dr / temp;
|
Chris@16
|
222 Di = -Di / temp;
|
Chris@16
|
223 delta_r = Cr * Dr - Ci * Di;
|
Chris@16
|
224 delta_i = Ci * Dr + Cr * Di;
|
Chris@16
|
225 temp = fr;
|
Chris@16
|
226 fr = temp * delta_r - fi * delta_i;
|
Chris@16
|
227 fi = temp * delta_i + fi * delta_r;
|
Chris@16
|
228 for (k = 2; k < policies::get_max_series_iterations<Policy>(); k++)
|
Chris@16
|
229 {
|
Chris@16
|
230 a = k - 0.5f;
|
Chris@16
|
231 a *= a;
|
Chris@16
|
232 a -= v2;
|
Chris@16
|
233 bi += 2;
|
Chris@16
|
234 temp = Cr * Cr + Ci * Ci;
|
Chris@16
|
235 Cr = br + a * Cr / temp;
|
Chris@16
|
236 Ci = bi - a * Ci / temp;
|
Chris@16
|
237 Dr = br + a * Dr;
|
Chris@16
|
238 Di = bi + a * Di;
|
Chris@16
|
239 if (fabs(Cr) + fabs(Ci) < tiny) { Cr = tiny; }
|
Chris@16
|
240 if (fabs(Dr) + fabs(Di) < tiny) { Dr = tiny; }
|
Chris@16
|
241 temp = Dr * Dr + Di * Di;
|
Chris@16
|
242 Dr = Dr / temp;
|
Chris@16
|
243 Di = -Di / temp;
|
Chris@16
|
244 delta_r = Cr * Dr - Ci * Di;
|
Chris@16
|
245 delta_i = Ci * Dr + Cr * Di;
|
Chris@16
|
246 temp = fr;
|
Chris@16
|
247 fr = temp * delta_r - fi * delta_i;
|
Chris@16
|
248 fi = temp * delta_i + fi * delta_r;
|
Chris@16
|
249 if (fabs(delta_r - 1) + fabs(delta_i) < tolerance)
|
Chris@16
|
250 break;
|
Chris@16
|
251 }
|
Chris@16
|
252 policies::check_series_iterations<T>("boost::math::bessel_jy<%1%>(%1%,%1%) in CF2_jy", k, pol);
|
Chris@16
|
253 *p = fr;
|
Chris@16
|
254 *q = fi;
|
Chris@16
|
255
|
Chris@16
|
256 return 0;
|
Chris@16
|
257 }
|
Chris@16
|
258
|
Chris@16
|
259 static const int need_j = 1;
|
Chris@16
|
260 static const int need_y = 2;
|
Chris@16
|
261
|
Chris@16
|
262 // Compute J(v, x) and Y(v, x) simultaneously by Steed's method, see
|
Chris@16
|
263 // Barnett et al, Computer Physics Communications, vol 8, 377 (1974)
|
Chris@16
|
264 template <typename T, typename Policy>
|
Chris@16
|
265 int bessel_jy(T v, T x, T* J, T* Y, int kind, const Policy& pol)
|
Chris@16
|
266 {
|
Chris@16
|
267 BOOST_ASSERT(x >= 0);
|
Chris@16
|
268
|
Chris@16
|
269 T u, Jv, Ju, Yv, Yv1, Yu, Yu1(0), fv, fu;
|
Chris@16
|
270 T W, p, q, gamma, current, prev, next;
|
Chris@16
|
271 bool reflect = false;
|
Chris@16
|
272 unsigned n, k;
|
Chris@16
|
273 int s;
|
Chris@16
|
274 int org_kind = kind;
|
Chris@16
|
275 T cp = 0;
|
Chris@16
|
276 T sp = 0;
|
Chris@16
|
277
|
Chris@16
|
278 static const char* function = "boost::math::bessel_jy<%1%>(%1%,%1%)";
|
Chris@16
|
279
|
Chris@16
|
280 BOOST_MATH_STD_USING
|
Chris@16
|
281 using namespace boost::math::tools;
|
Chris@16
|
282 using namespace boost::math::constants;
|
Chris@16
|
283
|
Chris@16
|
284 if (v < 0)
|
Chris@16
|
285 {
|
Chris@16
|
286 reflect = true;
|
Chris@16
|
287 v = -v; // v is non-negative from here
|
Chris@16
|
288 }
|
Chris@101
|
289 if (v > static_cast<T>((std::numeric_limits<int>::max)()))
|
Chris@101
|
290 {
|
Chris@101
|
291 *J = *Y = policies::raise_evaluation_error<T>(function, "Order of Bessel function is too large to evaluate: got %1%", v, pol);
|
Chris@101
|
292 return 1;
|
Chris@101
|
293 }
|
Chris@16
|
294 n = iround(v, pol);
|
Chris@16
|
295 u = v - n; // -1/2 <= u < 1/2
|
Chris@16
|
296
|
Chris@16
|
297 if(reflect)
|
Chris@16
|
298 {
|
Chris@16
|
299 T z = (u + n % 2);
|
Chris@16
|
300 cp = boost::math::cos_pi(z, pol);
|
Chris@16
|
301 sp = boost::math::sin_pi(z, pol);
|
Chris@16
|
302 if(u != 0)
|
Chris@16
|
303 kind = need_j|need_y; // need both for reflection formula
|
Chris@16
|
304 }
|
Chris@16
|
305
|
Chris@16
|
306 if(x == 0)
|
Chris@16
|
307 {
|
Chris@16
|
308 if(v == 0)
|
Chris@16
|
309 *J = 1;
|
Chris@16
|
310 else if((u == 0) || !reflect)
|
Chris@16
|
311 *J = 0;
|
Chris@16
|
312 else if(kind & need_j)
|
Chris@16
|
313 *J = policies::raise_domain_error<T>(function, "Value of Bessel J_v(x) is complex-infinity at %1%", x, pol); // complex infinity
|
Chris@16
|
314 else
|
Chris@16
|
315 *J = std::numeric_limits<T>::quiet_NaN(); // any value will do, not using J.
|
Chris@16
|
316
|
Chris@16
|
317 if((kind & need_y) == 0)
|
Chris@16
|
318 *Y = std::numeric_limits<T>::quiet_NaN(); // any value will do, not using Y.
|
Chris@16
|
319 else if(v == 0)
|
Chris@16
|
320 *Y = -policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@16
|
321 else
|
Chris@16
|
322 *Y = policies::raise_domain_error<T>(function, "Value of Bessel Y_v(x) is complex-infinity at %1%", x, pol); // complex infinity
|
Chris@16
|
323 return 1;
|
Chris@16
|
324 }
|
Chris@16
|
325
|
Chris@16
|
326 // x is positive until reflection
|
Chris@16
|
327 W = T(2) / (x * pi<T>()); // Wronskian
|
Chris@16
|
328 T Yv_scale = 1;
|
Chris@16
|
329 if(((kind & need_y) == 0) && ((x < 1) || (v > x * x / 4) || (x < 5)))
|
Chris@16
|
330 {
|
Chris@16
|
331 //
|
Chris@16
|
332 // This series will actually converge rapidly for all small
|
Chris@16
|
333 // x - say up to x < 20 - but the first few terms are large
|
Chris@16
|
334 // and divergent which leads to large errors :-(
|
Chris@16
|
335 //
|
Chris@16
|
336 Jv = bessel_j_small_z_series(v, x, pol);
|
Chris@16
|
337 Yv = std::numeric_limits<T>::quiet_NaN();
|
Chris@16
|
338 }
|
Chris@16
|
339 else if((x < 1) && (u != 0) && (log(policies::get_epsilon<T, Policy>() / 2) > v * log((x/2) * (x/2) / v)))
|
Chris@16
|
340 {
|
Chris@16
|
341 // Evaluate using series representations.
|
Chris@16
|
342 // This is particularly important for x << v as in this
|
Chris@16
|
343 // area temme_jy may be slow to converge, if it converges at all.
|
Chris@16
|
344 // Requires x is not an integer.
|
Chris@16
|
345 if(kind&need_j)
|
Chris@16
|
346 Jv = bessel_j_small_z_series(v, x, pol);
|
Chris@16
|
347 else
|
Chris@16
|
348 Jv = std::numeric_limits<T>::quiet_NaN();
|
Chris@16
|
349 if((org_kind&need_y && (!reflect || (cp != 0)))
|
Chris@16
|
350 || (org_kind & need_j && (reflect && (sp != 0))))
|
Chris@16
|
351 {
|
Chris@16
|
352 // Only calculate if we need it, and if the reflection formula will actually use it:
|
Chris@16
|
353 Yv = bessel_y_small_z_series(v, x, &Yv_scale, pol);
|
Chris@16
|
354 }
|
Chris@16
|
355 else
|
Chris@16
|
356 Yv = std::numeric_limits<T>::quiet_NaN();
|
Chris@16
|
357 }
|
Chris@16
|
358 else if((u == 0) && (x < policies::get_epsilon<T, Policy>()))
|
Chris@16
|
359 {
|
Chris@16
|
360 // Truncated series evaluation for small x and v an integer,
|
Chris@16
|
361 // much quicker in this area than temme_jy below.
|
Chris@16
|
362 if(kind&need_j)
|
Chris@16
|
363 Jv = bessel_j_small_z_series(v, x, pol);
|
Chris@16
|
364 else
|
Chris@16
|
365 Jv = std::numeric_limits<T>::quiet_NaN();
|
Chris@16
|
366 if((org_kind&need_y && (!reflect || (cp != 0)))
|
Chris@16
|
367 || (org_kind & need_j && (reflect && (sp != 0))))
|
Chris@16
|
368 {
|
Chris@16
|
369 // Only calculate if we need it, and if the reflection formula will actually use it:
|
Chris@16
|
370 Yv = bessel_yn_small_z(n, x, &Yv_scale, pol);
|
Chris@16
|
371 }
|
Chris@16
|
372 else
|
Chris@16
|
373 Yv = std::numeric_limits<T>::quiet_NaN();
|
Chris@16
|
374 }
|
Chris@16
|
375 else if(asymptotic_bessel_large_x_limit(v, x))
|
Chris@16
|
376 {
|
Chris@16
|
377 if(kind&need_y)
|
Chris@16
|
378 {
|
Chris@16
|
379 Yv = asymptotic_bessel_y_large_x_2(v, x);
|
Chris@16
|
380 }
|
Chris@16
|
381 else
|
Chris@16
|
382 Yv = std::numeric_limits<T>::quiet_NaN(); // any value will do, we're not using it.
|
Chris@16
|
383 if(kind&need_j)
|
Chris@16
|
384 {
|
Chris@16
|
385 Jv = asymptotic_bessel_j_large_x_2(v, x);
|
Chris@16
|
386 }
|
Chris@16
|
387 else
|
Chris@16
|
388 Jv = std::numeric_limits<T>::quiet_NaN(); // any value will do, we're not using it.
|
Chris@16
|
389 }
|
Chris@16
|
390 else if((x > 8) && hankel_PQ(v, x, &p, &q, pol))
|
Chris@16
|
391 {
|
Chris@16
|
392 //
|
Chris@16
|
393 // Hankel approximation: note that this method works best when x
|
Chris@16
|
394 // is large, but in that case we end up calculating sines and cosines
|
Chris@16
|
395 // of large values, with horrendous resulting accuracy. It is fast though
|
Chris@16
|
396 // when it works....
|
Chris@16
|
397 //
|
Chris@16
|
398 // Normally we calculate sin/cos(chi) where:
|
Chris@16
|
399 //
|
Chris@16
|
400 // chi = x - fmod(T(v / 2 + 0.25f), T(2)) * boost::math::constants::pi<T>();
|
Chris@16
|
401 //
|
Chris@16
|
402 // But this introduces large errors, so use sin/cos addition formulae to
|
Chris@16
|
403 // improve accuracy:
|
Chris@16
|
404 //
|
Chris@16
|
405 T mod_v = fmod(T(v / 2 + 0.25f), T(2));
|
Chris@16
|
406 T sx = sin(x);
|
Chris@16
|
407 T cx = cos(x);
|
Chris@16
|
408 T sv = sin_pi(mod_v);
|
Chris@16
|
409 T cv = cos_pi(mod_v);
|
Chris@16
|
410
|
Chris@16
|
411 T sc = sx * cv - sv * cx; // == sin(chi);
|
Chris@16
|
412 T cc = cx * cv + sx * sv; // == cos(chi);
|
Chris@16
|
413 T chi = boost::math::constants::root_two<T>() / (boost::math::constants::root_pi<T>() * sqrt(x)); //sqrt(2 / (boost::math::constants::pi<T>() * x));
|
Chris@16
|
414 Yv = chi * (p * sc + q * cc);
|
Chris@16
|
415 Jv = chi * (p * cc - q * sc);
|
Chris@16
|
416 }
|
Chris@16
|
417 else if (x <= 2) // x in (0, 2]
|
Chris@16
|
418 {
|
Chris@16
|
419 if(temme_jy(u, x, &Yu, &Yu1, pol)) // Temme series
|
Chris@16
|
420 {
|
Chris@16
|
421 // domain error:
|
Chris@16
|
422 *J = *Y = Yu;
|
Chris@16
|
423 return 1;
|
Chris@16
|
424 }
|
Chris@16
|
425 prev = Yu;
|
Chris@16
|
426 current = Yu1;
|
Chris@16
|
427 T scale = 1;
|
Chris@16
|
428 policies::check_series_iterations<T>(function, n, pol);
|
Chris@16
|
429 for (k = 1; k <= n; k++) // forward recurrence for Y
|
Chris@16
|
430 {
|
Chris@16
|
431 T fact = 2 * (u + k) / x;
|
Chris@16
|
432 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
|
Chris@16
|
433 {
|
Chris@16
|
434 scale /= current;
|
Chris@16
|
435 prev /= current;
|
Chris@16
|
436 current = 1;
|
Chris@16
|
437 }
|
Chris@16
|
438 next = fact * current - prev;
|
Chris@16
|
439 prev = current;
|
Chris@16
|
440 current = next;
|
Chris@16
|
441 }
|
Chris@16
|
442 Yv = prev;
|
Chris@16
|
443 Yv1 = current;
|
Chris@16
|
444 if(kind&need_j)
|
Chris@16
|
445 {
|
Chris@16
|
446 CF1_jy(v, x, &fv, &s, pol); // continued fraction CF1_jy
|
Chris@16
|
447 Jv = scale * W / (Yv * fv - Yv1); // Wronskian relation
|
Chris@16
|
448 }
|
Chris@16
|
449 else
|
Chris@16
|
450 Jv = std::numeric_limits<T>::quiet_NaN(); // any value will do, we're not using it.
|
Chris@16
|
451 Yv_scale = scale;
|
Chris@16
|
452 }
|
Chris@16
|
453 else // x in (2, \infty)
|
Chris@16
|
454 {
|
Chris@16
|
455 // Get Y(u, x):
|
Chris@16
|
456
|
Chris@16
|
457 T ratio;
|
Chris@16
|
458 CF1_jy(v, x, &fv, &s, pol);
|
Chris@16
|
459 // tiny initial value to prevent overflow
|
Chris@16
|
460 T init = sqrt(tools::min_value<T>());
|
Chris@101
|
461 BOOST_MATH_INSTRUMENT_VARIABLE(init);
|
Chris@16
|
462 prev = fv * s * init;
|
Chris@16
|
463 current = s * init;
|
Chris@16
|
464 if(v < max_factorial<T>::value)
|
Chris@16
|
465 {
|
Chris@16
|
466 policies::check_series_iterations<T>(function, n, pol);
|
Chris@16
|
467 for (k = n; k > 0; k--) // backward recurrence for J
|
Chris@16
|
468 {
|
Chris@16
|
469 next = 2 * (u + k) * current / x - prev;
|
Chris@16
|
470 prev = current;
|
Chris@16
|
471 current = next;
|
Chris@16
|
472 }
|
Chris@16
|
473 ratio = (s * init) / current; // scaling ratio
|
Chris@16
|
474 // can also call CF1_jy() to get fu, not much difference in precision
|
Chris@16
|
475 fu = prev / current;
|
Chris@16
|
476 }
|
Chris@16
|
477 else
|
Chris@16
|
478 {
|
Chris@16
|
479 //
|
Chris@16
|
480 // When v is large we may get overflow in this calculation
|
Chris@16
|
481 // leading to NaN's and other nasty surprises:
|
Chris@16
|
482 //
|
Chris@16
|
483 policies::check_series_iterations<T>(function, n, pol);
|
Chris@16
|
484 bool over = false;
|
Chris@16
|
485 for (k = n; k > 0; k--) // backward recurrence for J
|
Chris@16
|
486 {
|
Chris@16
|
487 T t = 2 * (u + k) / x;
|
Chris@16
|
488 if((t > 1) && (tools::max_value<T>() / t < current))
|
Chris@16
|
489 {
|
Chris@16
|
490 over = true;
|
Chris@16
|
491 break;
|
Chris@16
|
492 }
|
Chris@16
|
493 next = t * current - prev;
|
Chris@16
|
494 prev = current;
|
Chris@16
|
495 current = next;
|
Chris@16
|
496 }
|
Chris@16
|
497 if(!over)
|
Chris@16
|
498 {
|
Chris@16
|
499 ratio = (s * init) / current; // scaling ratio
|
Chris@16
|
500 // can also call CF1_jy() to get fu, not much difference in precision
|
Chris@16
|
501 fu = prev / current;
|
Chris@16
|
502 }
|
Chris@16
|
503 else
|
Chris@16
|
504 {
|
Chris@16
|
505 ratio = 0;
|
Chris@16
|
506 fu = 1;
|
Chris@16
|
507 }
|
Chris@16
|
508 }
|
Chris@16
|
509 CF2_jy(u, x, &p, &q, pol); // continued fraction CF2_jy
|
Chris@16
|
510 T t = u / x - fu; // t = J'/J
|
Chris@16
|
511 gamma = (p - t) / q;
|
Chris@16
|
512 //
|
Chris@16
|
513 // We can't allow gamma to cancel out to zero competely as it messes up
|
Chris@16
|
514 // the subsequent logic. So pretend that one bit didn't cancel out
|
Chris@16
|
515 // and set to a suitably small value. The only test case we've been able to
|
Chris@16
|
516 // find for this, is when v = 8.5 and x = 4*PI.
|
Chris@16
|
517 //
|
Chris@16
|
518 if(gamma == 0)
|
Chris@16
|
519 {
|
Chris@16
|
520 gamma = u * tools::epsilon<T>() / x;
|
Chris@16
|
521 }
|
Chris@101
|
522 BOOST_MATH_INSTRUMENT_VARIABLE(current);
|
Chris@101
|
523 BOOST_MATH_INSTRUMENT_VARIABLE(W);
|
Chris@101
|
524 BOOST_MATH_INSTRUMENT_VARIABLE(q);
|
Chris@101
|
525 BOOST_MATH_INSTRUMENT_VARIABLE(gamma);
|
Chris@101
|
526 BOOST_MATH_INSTRUMENT_VARIABLE(p);
|
Chris@101
|
527 BOOST_MATH_INSTRUMENT_VARIABLE(t);
|
Chris@16
|
528 Ju = sign(current) * sqrt(W / (q + gamma * (p - t)));
|
Chris@101
|
529 BOOST_MATH_INSTRUMENT_VARIABLE(Ju);
|
Chris@16
|
530
|
Chris@16
|
531 Jv = Ju * ratio; // normalization
|
Chris@16
|
532
|
Chris@16
|
533 Yu = gamma * Ju;
|
Chris@16
|
534 Yu1 = Yu * (u/x - p - q/gamma);
|
Chris@16
|
535
|
Chris@16
|
536 if(kind&need_y)
|
Chris@16
|
537 {
|
Chris@16
|
538 // compute Y:
|
Chris@16
|
539 prev = Yu;
|
Chris@16
|
540 current = Yu1;
|
Chris@16
|
541 policies::check_series_iterations<T>(function, n, pol);
|
Chris@16
|
542 for (k = 1; k <= n; k++) // forward recurrence for Y
|
Chris@16
|
543 {
|
Chris@16
|
544 T fact = 2 * (u + k) / x;
|
Chris@16
|
545 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
|
Chris@16
|
546 {
|
Chris@16
|
547 prev /= current;
|
Chris@16
|
548 Yv_scale /= current;
|
Chris@16
|
549 current = 1;
|
Chris@16
|
550 }
|
Chris@16
|
551 next = fact * current - prev;
|
Chris@16
|
552 prev = current;
|
Chris@16
|
553 current = next;
|
Chris@16
|
554 }
|
Chris@16
|
555 Yv = prev;
|
Chris@16
|
556 }
|
Chris@16
|
557 else
|
Chris@16
|
558 Yv = std::numeric_limits<T>::quiet_NaN(); // any value will do, we're not using it.
|
Chris@16
|
559 }
|
Chris@16
|
560
|
Chris@16
|
561 if (reflect)
|
Chris@16
|
562 {
|
Chris@16
|
563 if((sp != 0) && (tools::max_value<T>() * fabs(Yv_scale) < fabs(sp * Yv)))
|
Chris@16
|
564 *J = org_kind & need_j ? T(-sign(sp) * sign(Yv) * sign(Yv_scale) * policies::raise_overflow_error<T>(function, 0, pol)) : T(0);
|
Chris@16
|
565 else
|
Chris@16
|
566 *J = cp * Jv - (sp == 0 ? T(0) : T((sp * Yv) / Yv_scale)); // reflection formula
|
Chris@16
|
567 if((cp != 0) && (tools::max_value<T>() * fabs(Yv_scale) < fabs(cp * Yv)))
|
Chris@16
|
568 *Y = org_kind & need_y ? T(-sign(cp) * sign(Yv) * sign(Yv_scale) * policies::raise_overflow_error<T>(function, 0, pol)) : T(0);
|
Chris@16
|
569 else
|
Chris@16
|
570 *Y = (sp != 0 ? sp * Jv : T(0)) + (cp == 0 ? T(0) : T((cp * Yv) / Yv_scale));
|
Chris@16
|
571 }
|
Chris@16
|
572 else
|
Chris@16
|
573 {
|
Chris@16
|
574 *J = Jv;
|
Chris@16
|
575 if(tools::max_value<T>() * fabs(Yv_scale) < fabs(Yv))
|
Chris@16
|
576 *Y = org_kind & need_y ? T(sign(Yv) * sign(Yv_scale) * policies::raise_overflow_error<T>(function, 0, pol)) : T(0);
|
Chris@16
|
577 else
|
Chris@16
|
578 *Y = Yv / Yv_scale;
|
Chris@16
|
579 }
|
Chris@16
|
580
|
Chris@16
|
581 return 0;
|
Chris@16
|
582 }
|
Chris@16
|
583
|
Chris@16
|
584 } // namespace detail
|
Chris@16
|
585
|
Chris@16
|
586 }} // namespaces
|
Chris@16
|
587
|
Chris@16
|
588 #endif // BOOST_MATH_BESSEL_JY_HPP
|
Chris@16
|
589
|