30 #ifndef MATHLIB_STANDALONE 31 #include <R_ext/Memory.h> 34 #define min0(x, y) (((x) <= (y)) ? (x) : (y)) 37 static void J_bessel(Float *x, Float *alpha,
int *nb,
38 Float *b,
int *ncalc);
42 Float bessel_j(Float x, Float alpha)
46 #ifndef MATHLIB_STANDALONE 52 if (ISNAN(x) || ISNAN(alpha))
return x + alpha;
55 ML_ERROR(ME_RANGE,
"bessel_j");
62 return(((alpha - na == 0.5) ? 0 : bessel_j<Float>(x, -alpha) * cospi(alpha)) +
63 ((alpha == na ) ? 0 : bessel_y<Float>(x, -alpha) * sinpi(alpha)));
65 else if (alpha > 1e7) {
66 MATHLIB_WARNING(_(
"besselJ(x, nu): nu=%g too large for bessel_j() algorithm"),
70 nb = 1 + (int)trunc(na);
71 alpha -= (double)(nb-1);
72 #ifdef MATHLIB_STANDALONE 73 bj = (Float *) calloc(nb,
sizeof(Float));
74 if (!bj) MATHLIB_ERROR(
"%s", _(
"bessel_j allocation error"));
77 bj = (Float *) R_alloc((
size_t) nb,
sizeof(Float));
79 J_bessel(&x, &alpha, &nb, bj, &ncalc);
82 MATHLIB_WARNING4(_(
"bessel_j(%g): ncalc (=%d) != nb (=%d); alpha=%g. Arg. out of range?\n"),
85 MATHLIB_WARNING2(_(
"bessel_j(%g,nu=%g): precision lost in result\n"),
86 x, alpha+(
double)nb-1);
89 #ifdef MATHLIB_STANDALONE 100 Float bessel_j_ex(Float x, Float alpha, Float *bj)
107 if (ISNAN(x) || ISNAN(alpha))
return x + alpha;
110 ML_ERROR(ME_RANGE,
"bessel_j");
117 return(((alpha - na == 0.5) ? 0 : bessel_j_ex(x, -alpha, bj) * cospi(alpha)) +
118 ((alpha == na ) ? 0 : bessel_y_ex(x, -alpha, bj) * sinpi(alpha)));
120 else if (alpha > 1e7) {
121 MATHLIB_WARNING(_(
"besselJ(x, nu): nu=%g too large for bessel_j() algorithm"),
125 nb = 1 + (int)trunc(na);
126 alpha -= (double)(nb-1);
127 J_bessel(&x, &alpha, &nb, bj, &ncalc);
130 MATHLIB_WARNING4(_(
"bessel_j(%g): ncalc (=%d) != nb (=%d); alpha=%g. Arg. out of range?\n"),
131 x, ncalc, nb, alpha);
133 MATHLIB_WARNING2(_(
"bessel_j(%g,nu=%g): precision lost in result\n"),
134 x, alpha+(
double)nb-1);
140 template<
class Float>
141 static void J_bessel(Float *x, Float *alpha,
int *nb,
142 Float *b,
int *ncalc)
223 const static double pi2 = .636619772367581343075535;
224 const static double twopi1 = 6.28125;
225 const static double twopi2 = .001935307179586476925286767;
230 const static double fact[25] = { 1.,1.,2.,6.,24.,120.,720.,5040.,40320.,
231 362880.,3628800.,39916800.,479001600.,6227020800.,87178291200.,
232 1.307674368e12,2.0922789888e13,3.55687428096e14,6.402373705728e15,
233 1.21645100408832e17,2.43290200817664e18,5.109094217170944e19,
234 1.12400072777760768e21,2.585201673888497664e22,
235 6.2044840173323943936e23 };
238 int nend, intx, nbmx, i, j, k, l, m, n, nstart;
240 Float nu, twonu, capp, capq, pold, vcos, test, vsin;
241 Float p, s, t, z, alpem, halfx, aa, bb, cc, psave, plast;
242 Float tover, t1, alp2em, em, en, xc, xk, xm, psavel, gnu, xin,
sum;
254 if (*nb > 0 && *x >= 0. && 0. <= nu && nu < 1.) {
257 if(*x > xlrg_BESS_IJ) {
258 ML_ERROR(ME_RANGE,
"J_bessel");
261 for(i=1; i <= *nb; i++)
265 intx = (int) trunc(*x);
267 for (i = 1; i <= *nb; ++i)
277 if (*x < rtnsig_BESS) {
283 halfx = (*x > enmten_BESS) ? .5 * *x : 0.;
284 aa = (nu != 0.) ? pow(halfx, nu) / (nu * Rf_gamma_cody(nu)) : 1.;
285 bb = (*x + 1. > 1.)? -halfx * halfx : 0.;
286 b[1] = aa + aa * bb / alpem;
287 if (*x != 0. && b[1] == 0.)
292 for (n = 2; n <= *nb; ++n)
300 tover = (enmten_BESS + enmten_BESS) / *x;
302 tover = enmten_BESS / bb;
304 for (n = 2; n <= *nb; ++n) {
308 if (aa <= tover * alpem)
311 b[n] = aa + aa * bb / alpem;
312 if (b[n] == 0. && *ncalc > n)
317 }
else if (*x > 25. && *nb <= intx + 1) {
322 xin = 1 / (64 * *x * *x);
323 if (*x >= 130.) m = 4;
324 else if (*x >= 35.) m = 8;
326 xm = 4. * (double) m;
330 t = trunc(*x / (twopi1 + twopi2) + .5);
331 z = (*x - t * twopi1) - t * twopi2 - (nu + .5) / pi2;
335 for (i = 1; i <= 2; ++i) {
336 s = (xm - 1. - gnu) * (xm - 1. + gnu) * xin * .5;
337 t = (gnu - (xm - 3.)) * (gnu + (xm - 3.));
338 t1= (gnu - (xm + 1.)) * (gnu + (xm + 1.));
340 capp = s * t / fact[k];
341 capq = s * t1/ fact[k + 1];
343 for (; k >= 4; k -= 2) {
345 s = (xk - 1. - gnu) * (xk - 1. + gnu);
347 t = (gnu - (xk - 3.)) * (gnu + (xk - 3.));
348 capp = (capp + 1. / fact[k - 2]) * s * t * xin;
349 capq = (capq + 1. / fact[k - 1]) * s * t1 * xin;
353 capq = (capq + 1.) * (gnu * gnu - 1.) * (.125 / *x);
354 b[i] = xc * (capp * vcos - capq * vsin);
358 t = vsin; vsin = -vcos; vcos = t;
365 for (gnu = twonu + 2., j = 3; j <= *nb; j++, gnu += 2.)
366 b[j] = gnu * b[j - 1] / *x - b[j - 2];
376 en = (double)(n + n) + twonu;
382 test = ensig_BESS + ensig_BESS;
387 tover = enten_BESS / ensig_BESS;
390 en = (double) (nstart + nstart) - 2. + twonu;
391 for (k = nstart; k <= nend; ++k) {
396 p = en * plast / *x - pold;
413 p = en * plast / *x - pold;
421 test = pold * plast * (.5 - .5 / (bb * bb));
427 for (l = nstart; l <= nend; ++l) {
430 psave = en * psavel / *x - pold;
431 if (psave * psavel > test) {
441 en = (double) (n + n) + twonu;
445 test = fmax2(test, sqrt(plast * ensig_BESS) * sqrt(p + p));
454 p = en * plast / *x - pold;
467 m = (n << 1) - (m << 2);
472 alpem = em - 1. + nu;
473 alp2em = em + em + nu;
474 sum = aa * alpem * alp2em / em;
482 for (l = 1; l <= nend; ++l) {
487 aa = en * bb / *x - cc;
491 alp2em = em + em + nu;
495 alpem = em - 1. + nu;
498 sum = (sum + aa * alp2em) * alpem / em;
511 sum += b[1] * alp2em;
519 b[n] = en * aa / *x - bb;
526 alp2em = em + em + nu;
527 alpem = em - 1. + nu;
530 sum = (sum + b[n] * alp2em) * alpem / em;
540 for (n = n-1; n >= 2; n--) {
542 b[n] = en * b[n + 1] / *x - b[n + 2];
546 alp2em = em + em + nu;
547 alpem = em - 1. + nu;
550 sum = (sum + b[n] * alp2em) * alpem / em;
556 b[1] = 2. * (nu + 1.) * b[2] / *x - b[3];
560 alp2em = em + em + nu;
563 sum += b[1] * alp2em;
571 sum *= (Rf_gamma_cody(nu) * pow(.5* *x, -nu));
576 for (n = 1; n <= *nb; ++n) {
588 *ncalc = min0(*nb,0) - 1;
Type sum(Vector< Type > x)