29 #ifndef MATHLIB_STANDALONE 30 #include <R_ext/Memory.h> 33 #define min0(x, y) (((x) <= (y)) ? (x) : (y)) 36 static void Y_bessel(Float *x, Float *alpha,
int *nb,
37 Float *by,
int *ncalc);
41 Float bessel_y(Float x, Float alpha)
45 #ifndef MATHLIB_STANDALONE 51 if (ISNAN(x) || ISNAN(alpha))
return x + alpha;
54 ML_ERROR(ME_RANGE,
"bessel_y");
61 return(((alpha - na == 0.5) ? 0 : bessel_y<Float>(x, -alpha) * cospi(alpha)) -
62 ((alpha == na ) ? 0 : bessel_j<Float>(x, -alpha) * sinpi(alpha)));
64 else if (alpha > 1e7) {
65 MATHLIB_WARNING(_(
"besselY(x, nu): nu=%g too large for bessel_y() algorithm"),
69 nb = 1+ (int)trunc(na);
70 alpha -= (double)(nb-1);
71 #ifdef MATHLIB_STANDALONE 72 by = (Float *) calloc(nb,
sizeof(Float));
73 if (!by) MATHLIB_ERROR(
"%s", _(
"bessel_y allocation error"));
76 by = (Float *) R_alloc((
size_t) nb,
sizeof(Float));
78 Y_bessel(&x, &alpha, &nb, by, &ncalc);
81 #ifdef MATHLIB_STANDALONE 89 MATHLIB_WARNING4(_(
"bessel_y(%g): ncalc (=%d) != nb (=%d); alpha=%g. Arg. out of range?\n"),
92 MATHLIB_WARNING2(_(
"bessel_y(%g,nu=%g): precision lost in result\n"),
93 x, alpha+(
double)nb-1);
96 #ifdef MATHLIB_STANDALONE 106 template<
class Float>
107 Float bessel_y_ex(Float x, Float alpha, Float *by)
114 if (ISNAN(x) || ISNAN(alpha))
return x + alpha;
117 ML_ERROR(ME_RANGE,
"bessel_y");
124 return(((alpha - na == 0.5) ? 0 : bessel_y_ex(x, -alpha, by) * cospi(alpha)) -
125 ((alpha == na ) ? 0 : bessel_j_ex(x, -alpha, by) * sinpi(alpha)));
127 else if (alpha > 1e7) {
128 MATHLIB_WARNING(_(
"besselY(x, nu): nu=%g too large for bessel_y() algorithm"),
132 nb = 1+ (int)trunc(na);
133 alpha -= (double)(nb-1);
134 Y_bessel(&x, &alpha, &nb, by, &ncalc);
139 MATHLIB_WARNING4(_(
"bessel_y(%g): ncalc (=%d) != nb (=%d); alpha=%g. Arg. out of range?\n"),
140 x, ncalc, nb, alpha);
142 MATHLIB_WARNING2(_(
"bessel_y(%g,nu=%g): precision lost in result\n"),
143 x, alpha+(
double)nb-1);
149 template<
class Float>
150 static void Y_bessel(Float *x, Float *alpha,
int *nb,
151 Float *by,
int *ncalc)
238 const static double fivpi = 15.707963267948966192;
239 const static double pim5 = .70796326794896619231;
245 const static double ch[21] = { -6.7735241822398840964e-24,
246 -6.1455180116049879894e-23,2.9017595056104745456e-21,
247 1.3639417919073099464e-19,2.3826220476859635824e-18,
248 -9.0642907957550702534e-18,-1.4943667065169001769e-15,
249 -3.3919078305362211264e-14,-1.7023776642512729175e-13,
250 9.1609750938768647911e-12,2.4230957900482704055e-10,
251 1.7451364971382984243e-9,-3.3126119768180852711e-8,
252 -8.6592079961391259661e-7,-4.9717367041957398581e-6,
253 7.6309597585908126618e-5,.0012719271366545622927,
254 .0017063050710955562222,-.07685284084478667369,
255 -.28387654227602353814,.92187029365045265648 };
260 Float alfa, div, ddiv, even, gamma, term, cosmu, sinmu,
261 b, c, d, e, f, g, h, p, q, r, s, d1, d2, q0, pa,pa1, qa,qa1,
262 en, en1, nu, ex, ya,ya1, twobyx, den, odd, aye, dmu, x2, xna;
268 if (*nb > 0 && 0. <= nu && nu < 1.) {
269 if(ex < DBL_MIN || ex > xlrg_BESS_Y) {
274 if(ex > xlrg_BESS_Y) by[0]= 0.;
275 else if(ex < DBL_MIN) by[0]=ML_NEGINF;
276 for(i=0; i < *nb; i++)
280 xna = trunc(nu + .5);
281 na = (int) trunc(xna);
300 if (fabs(nu) < M_eps_sinc)
303 c = M_1_PI / ( 1. - (M_PI * M_PI / 6.) * nu * nu );
314 for (i = 1; i <= 9; ++i) {
315 s = s * x2 / en / (en - 1.) + 1.;
319 s = (e - 1. / e) * .5 / f;
328 for (i = 3; i <= 19; i += 2) {
329 even = -(aye + aye + even);
330 aye = -even * x2 - aye + ch[i - 1];
331 odd = -(alfa + alfa + odd);
332 alfa = -odd * x2 - alfa + ch[i];
334 even = (even * .5 + aye) * x2 - aye + ch[20];
335 odd = (odd + alfa) * 2.;
336 gamma = odd * nu + even;
340 e = (e + 1. / e) * .5;
341 f = 2. * c * (odd * e + even * s * d);
346 if (fabs(c) < M_eps_sinc)
351 r = M_PI * c * r * r;
359 while (fabs(g / (1. + fabs(ya))) +
360 fabs(h / (1. + fabs(ya1))) > DBL_EPSILON) {
361 f = (f * en + p + q) / (en * en - e);
373 }
else if (ex < thresh_BESS_Y) {
377 c = (.5 - nu) * (.5 + nu);
383 e = ex * M_1_PI * ( fabs(cospi(nu)) + tiny ) / (1. + tiny) / DBL_EPSILON;
390 while (r * en * en < e) {
392 d = (en - 1. + c / en) / s;
393 p = (en + en - p * d) / en1;
394 q = (-b + q * d) / en1;
406 r = en1 * (2. - p) - 2.;
408 d = (en - 1. + c / en) / (r * r + s * s);
423 pa1 = (pa * q - qa * d) / ex;
424 qa1 = (qa * q + pa * d) / ex;
425 b = ex - M_PI_2 * (nu + .5);
428 d = M_SQRT_2dPI / sqrt(ex);
429 ya = d * (pa * s + qa * c);
430 ya1 = d * (qa1 * s - pa1 * c);
436 d1 = trunc(ex / fivpi);
438 dmu = ex - 15. * d1 - d1 * pim5 - (*alpha + .5) * M_PI_2;
439 if (i - (i / 2 << 1) == 0) {
449 for (k = 1; k <= 2; ++k) {
453 d1 = (2. * dmu - 1.) * (2. * dmu + 1.);
460 for (i = 2; i <= 20; ++i) {
464 term = -term * d1 / div;
471 if (fabs(term) <= DBL_EPSILON) {
478 ya = M_SQRT_2dPI * (p * cosmu - q * sinmu) / den;
480 ya1 = M_SQRT_2dPI * (p * cosmu - q * sinmu) / den;
485 h = 2. * (nu + 1.) / ex;
487 if (fabs(ya1) > DBL_MAX / h) {
508 for (i = 2; i < *nb; ++i) {
510 if (fabs(by[i - 1]) * twobyx >= DBL_MAX / aye)
513 if (fabs(by[i - 1]) >= DBL_MAX / aye / twobyx)
516 by[i] = twobyx * aye * by[i - 1] - by[i - 2];
523 for (i = *ncalc; i < *nb; ++i)
528 *ncalc = min0(*nb,0) - 1;