File indexing completed on 2025-07-05 08:37:15
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MATH_BESSEL_JY_HPP
0007 #define BOOST_MATH_BESSEL_JY_HPP
0008
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #endif
0012
0013 #include <boost/math/tools/config.hpp>
0014 #include <boost/math/special_functions/gamma.hpp>
0015 #include <boost/math/special_functions/sign.hpp>
0016 #include <boost/math/special_functions/hypot.hpp>
0017 #include <boost/math/special_functions/sin_pi.hpp>
0018 #include <boost/math/special_functions/cos_pi.hpp>
0019 #include <boost/math/special_functions/detail/bessel_jy_asym.hpp>
0020 #include <boost/math/special_functions/detail/bessel_jy_series.hpp>
0021 #include <boost/math/constants/constants.hpp>
0022 #include <boost/math/policies/error_handling.hpp>
0023 #include <complex>
0024
0025
0026
0027 namespace boost { namespace math {
0028
0029 namespace detail {
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 template <class T, class Policy>
0041 bool hankel_PQ(T v, T x, T* p, T* q, const Policy& )
0042 {
0043 BOOST_MATH_STD_USING
0044 T tolerance = 2 * policies::get_epsilon<T, Policy>();
0045 *p = 1;
0046 *q = 0;
0047 T k = 1;
0048 T z8 = 8 * x;
0049 T sq = 1;
0050 T mu = 4 * v * v;
0051 T term = 1;
0052 bool ok = true;
0053 do
0054 {
0055 term *= (mu - sq * sq) / (k * z8);
0056 *q += term;
0057 k += 1;
0058 sq += 2;
0059 T mult = (sq * sq - mu) / (k * z8);
0060 ok = fabs(mult) < 0.5f;
0061 term *= mult;
0062 *p += term;
0063 k += 1;
0064 sq += 2;
0065 }
0066 while((fabs(term) > tolerance * *p) && ok);
0067 return ok;
0068 }
0069
0070
0071
0072 template <typename T, typename Policy>
0073 int temme_jy(T v, T x, T* Y, T* Y1, const Policy& pol)
0074 {
0075 T g, h, p, q, f, coef, sum, sum1, tolerance;
0076 T a, d, e, sigma;
0077 unsigned long k;
0078
0079 BOOST_MATH_STD_USING
0080 using namespace boost::math::tools;
0081 using namespace boost::math::constants;
0082
0083 BOOST_MATH_ASSERT(fabs(v) <= 0.5f);
0084
0085 T gp = boost::math::tgamma1pm1(v, pol);
0086 T gm = boost::math::tgamma1pm1(-v, pol);
0087 T spv = boost::math::sin_pi(v, pol);
0088 T spv2 = boost::math::sin_pi(v/2, pol);
0089 T xp = pow(x/2, v);
0090
0091 a = log(x / 2);
0092 sigma = -a * v;
0093 d = abs(sigma) < tools::epsilon<T>() ?
0094 T(1) : sinh(sigma) / sigma;
0095 e = abs(v) < tools::epsilon<T>() ? T(v*pi<T>()*pi<T>() / 2)
0096 : T(2 * spv2 * spv2 / v);
0097
0098 T g1 = (v == 0) ? T(-euler<T>()) : T((gp - gm) / ((1 + gp) * (1 + gm) * 2 * v));
0099 T g2 = (2 + gp + gm) / ((1 + gp) * (1 + gm) * 2);
0100 T vspv = (fabs(v) < tools::epsilon<T>()) ? T(1/constants::pi<T>()) : T(v / spv);
0101 f = (g1 * cosh(sigma) - g2 * a * d) * 2 * vspv;
0102
0103 p = vspv / (xp * (1 + gm));
0104 q = vspv * xp / (1 + gp);
0105
0106 g = f + e * q;
0107 h = p;
0108 coef = 1;
0109 sum = coef * g;
0110 sum1 = coef * h;
0111
0112 T v2 = v * v;
0113 T coef_mult = -x * x / 4;
0114
0115
0116 tolerance = policies::get_epsilon<T, Policy>();
0117 for (k = 1; k < policies::get_max_series_iterations<Policy>(); k++)
0118 {
0119 f = (k * f + p + q) / (k*k - v2);
0120 p /= k - v;
0121 q /= k + v;
0122 g = f + e * q;
0123 h = p - k * g;
0124 coef *= coef_mult / k;
0125 sum += coef * g;
0126 sum1 += coef * h;
0127 if (abs(coef * g) < abs(sum) * tolerance)
0128 {
0129 break;
0130 }
0131 }
0132 policies::check_series_iterations<T>("boost::math::bessel_jy<%1%>(%1%,%1%) in temme_jy", k, pol);
0133 *Y = -sum;
0134 *Y1 = -2 * sum1 / x;
0135
0136 return 0;
0137 }
0138
0139
0140
0141 template <typename T, typename Policy>
0142 int CF1_jy(T v, T x, T* fv, int* sign, const Policy& pol)
0143 {
0144 T C, D, f, a, b, delta, tiny, tolerance;
0145 unsigned long k;
0146 int s = 1;
0147
0148 BOOST_MATH_STD_USING
0149
0150
0151
0152
0153
0154
0155 tolerance = 2 * policies::get_epsilon<T, Policy>();
0156 tiny = sqrt(tools::min_value<T>());
0157 C = f = tiny;
0158 D = 0;
0159 for (k = 1; k < policies::get_max_series_iterations<Policy>() * 100; k++)
0160 {
0161 a = -1;
0162 b = 2 * (v + k) / x;
0163 C = b + a / C;
0164 D = b + a * D;
0165 if (C == 0) { C = tiny; }
0166 if (D == 0) { D = tiny; }
0167 D = 1 / D;
0168 delta = C * D;
0169 f *= delta;
0170 if (D < 0) { s = -s; }
0171 if (abs(delta - 1) < tolerance)
0172 { break; }
0173 }
0174 policies::check_series_iterations<T>("boost::math::bessel_jy<%1%>(%1%,%1%) in CF1_jy", k / 100, pol);
0175 *fv = -f;
0176 *sign = s;
0177
0178 return 0;
0179 }
0180
0181
0182
0183
0184
0185
0186
0187 template <typename T, typename Policy>
0188 int CF2_jy(T v, T x, T* p, T* q, const Policy& pol)
0189 {
0190 BOOST_MATH_STD_USING
0191
0192 T Cr, Ci, Dr, Di, fr, fi, a, br, bi, delta_r, delta_i, temp;
0193 T tiny;
0194 unsigned long k;
0195
0196
0197
0198 BOOST_MATH_ASSERT(fabs(x) > 1);
0199
0200
0201
0202 T tolerance = 2 * policies::get_epsilon<T, Policy>();
0203 tiny = sqrt(tools::min_value<T>());
0204 Cr = fr = -0.5f / x;
0205 Ci = fi = 1;
0206
0207 T v2 = v * v;
0208 a = (0.25f - v2) / x;
0209 br = 2 * x;
0210 bi = 2;
0211 temp = Cr * Cr + 1;
0212 Ci = bi + a * Cr / temp;
0213 Cr = br + a / temp;
0214 Dr = br;
0215 Di = bi;
0216 if (fabs(Cr) + fabs(Ci) < tiny) { Cr = tiny; }
0217 if (fabs(Dr) + fabs(Di) < tiny) { Dr = tiny; }
0218 temp = Dr * Dr + Di * Di;
0219 Dr = Dr / temp;
0220 Di = -Di / temp;
0221 delta_r = Cr * Dr - Ci * Di;
0222 delta_i = Ci * Dr + Cr * Di;
0223 temp = fr;
0224 fr = temp * delta_r - fi * delta_i;
0225 fi = temp * delta_i + fi * delta_r;
0226 for (k = 2; k < policies::get_max_series_iterations<Policy>(); k++)
0227 {
0228 a = k - 0.5f;
0229 a *= a;
0230 a -= v2;
0231 bi += 2;
0232 temp = Cr * Cr + Ci * Ci;
0233 Cr = br + a * Cr / temp;
0234 Ci = bi - a * Ci / temp;
0235 Dr = br + a * Dr;
0236 Di = bi + a * Di;
0237 if (fabs(Cr) + fabs(Ci) < tiny) { Cr = tiny; }
0238 if (fabs(Dr) + fabs(Di) < tiny) { Dr = tiny; }
0239 temp = Dr * Dr + Di * Di;
0240 Dr = Dr / temp;
0241 Di = -Di / temp;
0242 delta_r = Cr * Dr - Ci * Di;
0243 delta_i = Ci * Dr + Cr * Di;
0244 temp = fr;
0245 fr = temp * delta_r - fi * delta_i;
0246 fi = temp * delta_i + fi * delta_r;
0247 if (fabs(delta_r - 1) + fabs(delta_i) < tolerance)
0248 break;
0249 }
0250 policies::check_series_iterations<T>("boost::math::bessel_jy<%1%>(%1%,%1%) in CF2_jy", k, pol);
0251 *p = fr;
0252 *q = fi;
0253
0254 return 0;
0255 }
0256
0257 static const int need_j = 1;
0258 static const int need_y = 2;
0259
0260
0261
0262 template <typename T, typename Policy>
0263 int bessel_jy(T v, T x, T* J, T* Y, int kind, const Policy& pol)
0264 {
0265 BOOST_MATH_ASSERT(x >= 0);
0266
0267 T u, Jv, Ju, Yv, Yv1, Yu, Yu1(0), fv, fu;
0268 T W, p, q, gamma, current, prev, next;
0269 bool reflect = false;
0270 unsigned n, k;
0271 int s;
0272 int org_kind = kind;
0273 T cp = 0;
0274 T sp = 0;
0275
0276 static const char* function = "boost::math::bessel_jy<%1%>(%1%,%1%)";
0277
0278 BOOST_MATH_STD_USING
0279 using namespace boost::math::tools;
0280 using namespace boost::math::constants;
0281
0282 if (v < 0)
0283 {
0284 reflect = true;
0285 v = -v;
0286 }
0287 if (v > static_cast<T>((std::numeric_limits<int>::max)()))
0288 {
0289 *J = *Y = policies::raise_evaluation_error<T>(function, "Order of Bessel function is too large to evaluate: got %1%", v, pol);
0290 return 1;
0291 }
0292 n = iround(v, pol);
0293 u = v - n;
0294
0295 if(reflect)
0296 {
0297 T z = (u + n % 2);
0298 cp = boost::math::cos_pi(z, pol);
0299 sp = boost::math::sin_pi(z, pol);
0300 if(u != 0)
0301 kind = need_j|need_y;
0302 }
0303
0304 if(x == 0)
0305 {
0306 if (v == 0)
0307 *J = 1;
0308 else if ((u == 0) || !reflect)
0309 *J = 0;
0310 else if(kind & need_j)
0311 *J = policies::raise_domain_error<T>(function, "Value of Bessel J_v(x) is complex-infinity at %1%", x, pol);
0312 else
0313 *J = std::numeric_limits<T>::quiet_NaN();
0314
0315 if((kind & need_y) == 0)
0316 *Y = std::numeric_limits<T>::quiet_NaN();
0317 else
0318 {
0319
0320 BOOST_MATH_ASSERT(x != 0);
0321 }
0322 return 1;
0323 }
0324
0325
0326 W = T(2) / (x * pi<T>());
0327 T Yv_scale = 1;
0328 if(((kind & need_y) == 0) && ((x < 1) || (v > x * x / 4) || (x < 5)))
0329 {
0330
0331
0332
0333
0334
0335 Jv = bessel_j_small_z_series(v, x, pol);
0336 Yv = std::numeric_limits<T>::quiet_NaN();
0337 }
0338 else if((x < 1) && (u != 0) && (log(policies::get_epsilon<T, Policy>() / 2) > v * log((x/2) * (x/2) / v)))
0339 {
0340
0341
0342
0343
0344 if(kind&need_j)
0345 Jv = bessel_j_small_z_series(v, x, pol);
0346 else
0347 Jv = std::numeric_limits<T>::quiet_NaN();
0348 if((org_kind&need_y && (!reflect || (cp != 0)))
0349 || (org_kind & need_j && (reflect && (sp != 0))))
0350 {
0351
0352 Yv = bessel_y_small_z_series(v, x, &Yv_scale, pol);
0353 }
0354 else
0355 Yv = std::numeric_limits<T>::quiet_NaN();
0356 }
0357 else if((u == 0) && (x < policies::get_epsilon<T, Policy>()))
0358 {
0359
0360
0361
0362
0363 if(kind&need_j)
0364 Jv = bessel_j_small_z_series(v, x, pol);
0365 else
0366 Jv = std::numeric_limits<T>::quiet_NaN();
0367 if((org_kind&need_y && (!reflect || (cp != 0)))
0368 || (org_kind & need_j && (reflect && (sp != 0))))
0369 {
0370
0371 Yv = bessel_yn_small_z(n, x, &Yv_scale, pol);
0372 }
0373 else
0374 Yv = std::numeric_limits<T>::quiet_NaN();
0375
0376 }
0377 else if(asymptotic_bessel_large_x_limit(v, x))
0378 {
0379 if(kind&need_y)
0380 {
0381 Yv = asymptotic_bessel_y_large_x_2(v, x, pol);
0382 }
0383 else
0384 Yv = std::numeric_limits<T>::quiet_NaN();
0385 if(kind&need_j)
0386 {
0387 Jv = asymptotic_bessel_j_large_x_2(v, x, pol);
0388 }
0389 else
0390 Jv = std::numeric_limits<T>::quiet_NaN();
0391 }
0392 else if((x > 8) && hankel_PQ(v, x, &p, &q, pol))
0393 {
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407 T mod_v = fmod(T(v / 2 + 0.25f), T(2));
0408 T sx = sin(x);
0409 T cx = cos(x);
0410 T sv = boost::math::sin_pi(mod_v, pol);
0411 T cv = boost::math::cos_pi(mod_v, pol);
0412
0413 T sc = sx * cv - sv * cx;
0414 T cc = cx * cv + sx * sv;
0415 T chi = boost::math::constants::root_two<T>() / (boost::math::constants::root_pi<T>() * sqrt(x));
0416 Yv = chi * (p * sc + q * cc);
0417 Jv = chi * (p * cc - q * sc);
0418 }
0419 else if (x <= 2)
0420 {
0421 if(temme_jy(u, x, &Yu, &Yu1, pol))
0422 {
0423
0424 *J = *Y = Yu;
0425 return 1;
0426 }
0427 prev = Yu;
0428 current = Yu1;
0429 T scale = 1;
0430 policies::check_series_iterations<T>(function, n, pol);
0431 for (k = 1; k <= n; k++)
0432 {
0433 T fact = 2 * (u + k) / x;
0434 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
0435 {
0436 scale /= current;
0437 prev /= current;
0438 current = 1;
0439 }
0440 next = fact * current - prev;
0441 prev = current;
0442 current = next;
0443 }
0444 Yv = prev;
0445 Yv1 = current;
0446 if(kind&need_j)
0447 {
0448 CF1_jy(v, x, &fv, &s, pol);
0449 Jv = scale * W / (Yv * fv - Yv1);
0450 }
0451 else
0452 Jv = std::numeric_limits<T>::quiet_NaN();
0453 Yv_scale = scale;
0454 }
0455 else
0456 {
0457
0458
0459 T ratio;
0460 CF1_jy(v, x, &fv, &s, pol);
0461
0462 T init = sqrt(tools::min_value<T>());
0463 BOOST_MATH_INSTRUMENT_VARIABLE(init);
0464 prev = fv * s * init;
0465 current = s * init;
0466 if(v < max_factorial<T>::value)
0467 {
0468 policies::check_series_iterations<T>(function, n, pol);
0469 for (k = n; k > 0; k--)
0470 {
0471 next = 2 * (u + k) * current / x - prev;
0472
0473
0474
0475 if (next == 0)
0476 {
0477 next = prev * tools::epsilon<T>() / 2;
0478 }
0479 prev = current;
0480 current = next;
0481 }
0482 ratio = (s * init) / current;
0483
0484 fu = prev / current;
0485 }
0486 else
0487 {
0488
0489
0490
0491
0492 policies::check_series_iterations<T>(function, n, pol);
0493 bool over = false;
0494 for (k = n; k > 0; k--)
0495 {
0496 T t = 2 * (u + k) / x;
0497 if((t > 1) && (tools::max_value<T>() / t < current))
0498 {
0499 over = true;
0500 break;
0501 }
0502 next = t * current - prev;
0503 prev = current;
0504 current = next;
0505 }
0506 if(!over)
0507 {
0508 ratio = (s * init) / current;
0509
0510 fu = prev / current;
0511 }
0512 else
0513 {
0514 ratio = 0;
0515 fu = 1;
0516 }
0517 }
0518 CF2_jy(u, x, &p, &q, pol);
0519 T t = u / x - fu;
0520 gamma = (p - t) / q;
0521
0522
0523
0524
0525
0526
0527 if(gamma == 0)
0528 {
0529 gamma = u * tools::epsilon<T>() / x;
0530 }
0531 BOOST_MATH_INSTRUMENT_VARIABLE(current);
0532 BOOST_MATH_INSTRUMENT_VARIABLE(W);
0533 BOOST_MATH_INSTRUMENT_VARIABLE(q);
0534 BOOST_MATH_INSTRUMENT_VARIABLE(gamma);
0535 BOOST_MATH_INSTRUMENT_VARIABLE(p);
0536 BOOST_MATH_INSTRUMENT_VARIABLE(t);
0537 Ju = sign(current) * sqrt(W / (q + gamma * (p - t)));
0538 BOOST_MATH_INSTRUMENT_VARIABLE(Ju);
0539
0540 Jv = Ju * ratio;
0541
0542 Yu = gamma * Ju;
0543 Yu1 = Yu * (u/x - p - q/gamma);
0544
0545 if(kind&need_y)
0546 {
0547
0548 prev = Yu;
0549 current = Yu1;
0550 policies::check_series_iterations<T>(function, n, pol);
0551 for (k = 1; k <= n; k++)
0552 {
0553 T fact = 2 * (u + k) / x;
0554 if((tools::max_value<T>() - fabs(prev)) / fact < fabs(current))
0555 {
0556 prev /= current;
0557 Yv_scale /= current;
0558 current = 1;
0559 }
0560 next = fact * current - prev;
0561 prev = current;
0562 current = next;
0563 }
0564 Yv = prev;
0565 }
0566 else
0567 Yv = std::numeric_limits<T>::quiet_NaN();
0568 }
0569
0570 if (reflect)
0571 {
0572 if((sp != 0) && (tools::max_value<T>() * fabs(Yv_scale) < fabs(sp * Yv)))
0573 *J = org_kind & need_j ? T(-sign(sp) * sign(Yv) * (Yv_scale != 0 ? sign(Yv_scale) : 1) * policies::raise_overflow_error<T>(function, nullptr, pol)) : T(0);
0574 else
0575 *J = cp * Jv - (sp == 0 ? T(0) : T((sp * Yv) / Yv_scale));
0576 if((cp != 0) && (tools::max_value<T>() * fabs(Yv_scale) < fabs(cp * Yv)))
0577 *Y = org_kind & need_y ? T(-sign(cp) * sign(Yv) * (Yv_scale != 0 ? sign(Yv_scale) : 1) * policies::raise_overflow_error<T>(function, nullptr, pol)) : T(0);
0578 else
0579 *Y = (sp != 0 ? sp * Jv : T(0)) + (cp == 0 ? T(0) : T((cp * Yv) / Yv_scale));
0580 }
0581 else
0582 {
0583 *J = Jv;
0584 if(tools::max_value<T>() * fabs(Yv_scale) < fabs(Yv))
0585 *Y = org_kind & need_y ? T(sign(Yv) * sign(Yv_scale) * policies::raise_overflow_error<T>(function, nullptr, pol)) : T(0);
0586 else
0587 *Y = Yv / Yv_scale;
0588 }
0589
0590 return 0;
0591 }
0592
0593 }
0594
0595 }}
0596
0597 #endif