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