-
Notifications
You must be signed in to change notification settings - Fork 1
/
bessel_y_standalone.c
496 lines (449 loc) · 14.3 KB
/
bessel_y_standalone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* Mathlib : A C Library of Special Functions
* Copyright (C) 1998-2015 Ross Ihaka and the R Core team.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
*/
/* DESCRIPTION --> see below */
/* From http://www.netlib.org/specfun/rybesl Fortran translated by f2c,...
* ------------------------------=#---- Martin Maechler, ETH Zurich
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#define MATHLIB_STANDALONE
#define IEEE_754
#define min0(x, y) (((x) <= (y)) ? (x) : (y))
#define ME_RANGE "range error"
#define ML_WARNING(x, y) fprintf(stderr, "%s %s\n", x, y)
#define MATHLIB_ERROR(x, y) fprintf(stderr, x, y);
#define MATHLIB_WARNING4(a, b, c, d, e) fprintf(stderr, a, b, c, d, e)
#define MATHLIB_WARNING2(a, b, c) fprintf(stderr, a, b, c)
#define MATHLIB_WARNING(a, b) fprintf(stderr, a, b)
#define thresh_BESS_Y 16.
#define M_eps_sinc 2.149e-8
#define M_SQRT_2dPI 0.797884560802865355879892119869 // sqrt(2/pi)
#define xlrg_BESS_Y 1e8
#define ME_RANGE_NAN 0x7FF8000000000001
double cospi(double x);
double sinpi(double x);
double bessel_j(double x, double alpha);
static void Y_bessel(double *x, double *alpha, int *nb,
double *by, int *ncalc);
// unused now from R
double bessel_y(double x, double alpha)
{
int nb, ncalc;
double na, *by;
#ifndef MATHLIB_STANDALONE
const void *vmax;
#endif
#ifdef IEEE_754
/* NaNs propagated correctly */
if (isnan(x) || isnan(alpha)) return x + alpha;
#endif
if (x < 0) {
ML_WARNING(ME_RANGE, "bessel_y");
return NAN; //ME_RANGE_NAN;
}
na = floor(alpha);
if (alpha < 0) {
/* Using Abramowitz & Stegun 9.1.2
* this may not be quite optimal (CPU and accuracy wise) */
return(((alpha - na == 0.5) ? 0 : bessel_y(x, -alpha) * cospi(alpha)) -
((alpha == na ) ? 0 : bessel_j(x, -alpha) * sinpi(alpha)));
}
else if (alpha > 1e7) {
MATHLIB_WARNING("besselY(x, nu): nu=%g too large for bessel_y() algorithm",
alpha);
return NAN; //ME_RANGE_NAN;
}
nb = 1+ (int)na;/* nb-1 <= alpha < nb */
alpha -= (double)(nb-1);
#ifdef MATHLIB_STANDALONE
by = (double *) calloc(nb, sizeof(double));
if (!by) MATHLIB_ERROR("%s", "bessel_y allocation error");
#else
vmax = vmaxget();
by = (double *) R_alloc((size_t) nb, sizeof(double));
#endif
Y_bessel(&x, &alpha, &nb, by, &ncalc);
if(ncalc != nb) {/* error input */
if(ncalc == -1) {
#ifdef MATHLIB_STANDALONE
free(by);
#else
vmaxset(vmax);
#endif
return INFINITY;
}
else if(ncalc < -1)
MATHLIB_WARNING4("bessel_y(%g): ncalc (=%d) != nb (=%d); alpha=%g. Arg. out of range?\n",
x, ncalc, nb, alpha);
else /* ncalc >= 0 */
MATHLIB_WARNING2("bessel_y(%g,nu=%g): precision lost in result\n" ,
x, alpha+(double)nb-1);
}
x = by[nb-1];
#ifdef MATHLIB_STANDALONE
free(by);
#else
vmaxset(vmax);
#endif
return x;
}
static void Y_bessel(double *x, double *alpha, int *nb,
double *by, int *ncalc)
{
/* ----------------------------------------------------------------------
This routine calculates Bessel functions Y_(N+ALPHA) (X)
v for non-negative argument X, and non-negative order N+ALPHA.
Explanation of variables in the calling sequence
X - Non-negative argument for which
Y's are to be calculated.
ALPHA - Fractional part of order for which
Y's are to be calculated. 0 <= ALPHA < 1.0.
NB - Number of functions to be calculated, NB > 0.
The first function calculated is of order ALPHA, and the
last is of order (NB - 1 + ALPHA).
BY - Output vector of length NB. If the
routine terminates normally (NCALC=NB), the vector BY
contains the functions Y(ALPHA,X), ... , Y(NB-1+ALPHA,X),
If (0 < NCALC < NB), BY(I) contains correct function
values for I <= NCALC, and contains the ratios
Y(ALPHA+I-1,X)/Y(ALPHA+I-2,X) for the rest of the array.
NCALC - Output variable indicating possible errors.
Before using the vector BY, the user should check that
NCALC=NB, i.e., all orders have been calculated to
the desired accuracy. See error returns below.
*******************************************************************
Error returns
In case of an error, NCALC != NB, and not all Y's are
calculated to the desired accuracy.
NCALC < -1: An argument is out of range. For example,
NB <= 0, IZE is not 1 or 2, or IZE=1 and ABS(X) >=
XMAX. In this case, BY[0] = 0.0, the remainder of the
BY-vector is not calculated, and NCALC is set to
MIN0(NB,0)-2 so that NCALC != NB.
NCALC = -1: Y(ALPHA,X) >= XINF. The requested function
values are set to 0.0.
1 < NCALC < NB: Not all requested function values could
be calculated accurately. BY(I) contains correct function
values for I <= NCALC, and and the remaining NB-NCALC
array elements contain 0.0.
Intrinsic functions required are:
DBLE, EXP, INT, MAX, MIN, REAL, SQRT
Acknowledgement
This program draws heavily on Temme's Algol program for Y(a,x)
and Y(a+1,x) and on Campbell's programs for Y_nu(x). Temme's
scheme is used for x < THRESH, and Campbell's scheme is used
in the asymptotic region. Segments of code from both sources
have been translated into Fortran 77, merged, and heavily modified.
Modifications include parameterization of machine dependencies,
use of a new approximation for ln(gamma(x)), and built-in
protection against over/underflow.
References: "Bessel functions J_nu(x) and Y_nu(x) of float
order and float argument," Campbell, J. B.,
Comp. Phy. Comm. 18, 1979, pp. 133-142.
"On the numerical evaluation of the ordinary
Bessel function of the second kind," Temme,
N. M., J. Comput. Phys. 21, 1976, pp. 343-350.
Latest modification: March 19, 1990
Modified by: W. J. Cody
Applied Mathematics Division
Argonne National Laboratory
Argonne, IL 60439
----------------------------------------------------------------------*/
/* ----------------------------------------------------------------------
Mathematical constants
FIVPI = 5*PI
PIM5 = 5*PI - 15
----------------------------------------------------------------------*/
const static double fivpi = 15.707963267948966192;
const static double pim5 = .70796326794896619231;
/*----------------------------------------------------------------------
Coefficients for Chebyshev polynomial expansion of
1/gamma(1-x), abs(x) <= .5
----------------------------------------------------------------------*/
const static double ch[21] = { -6.7735241822398840964e-24,
-6.1455180116049879894e-23,2.9017595056104745456e-21,
1.3639417919073099464e-19,2.3826220476859635824e-18,
-9.0642907957550702534e-18,-1.4943667065169001769e-15,
-3.3919078305362211264e-14,-1.7023776642512729175e-13,
9.1609750938768647911e-12,2.4230957900482704055e-10,
1.7451364971382984243e-9,-3.3126119768180852711e-8,
-8.6592079961391259661e-7,-4.9717367041957398581e-6,
7.6309597585908126618e-5,.0012719271366545622927,
.0017063050710955562222,-.07685284084478667369,
-.28387654227602353814,.92187029365045265648 };
/* Local variables */
int i, k, na;
double alfa, div, ddiv, even, gamma, term, cosmu, sinmu,
b, c, d, e, f, g, h, p, q, r, s, d1, d2, q0, pa,pa1, qa,qa1,
en, en1, nu, ex, ya,ya1, twobyx, den, odd, aye, dmu, x2, xna;
en1 = ya = ya1 = 0; /* -Wall */
ex = *x;
nu = *alpha;
if (*nb > 0 && 0. <= nu && nu < 1.) {
if(ex < DBL_MIN || ex > xlrg_BESS_Y) {
/* Warning is not really appropriate, give
* proper limit:
* ML_WARNING(ME_RANGE, "Y_bessel"); */
*ncalc = *nb;
if(ex > xlrg_BESS_Y) by[0]= 0.; /*was ML_POSINF */
else if(ex < DBL_MIN) by[0]= - INFINITY;
for(i=0; i < *nb; i++)
by[i] = by[0];
return;
}
xna = trunc(nu + .5);
na = (int) xna;
if (na == 1) {/* <==> .5 <= *alpha < 1 <==> -5. <= nu < 0 */
nu -= xna;
}
if (nu == -.5) {
p = M_SQRT_2dPI / sqrt(ex);
ya = p * sin(ex);
ya1 = -p * cos(ex);
} else if (ex < 3.) {
/* -------------------------------------------------------------
Use Temme's scheme for small X
------------------------------------------------------------- */
b = ex * .5;
d = -log(b);
f = nu * d;
e = pow(b, -nu);
if (fabs(nu) < M_eps_sinc)
c = M_1_PI;
else
c = nu / sinpi(nu);
/* ------------------------------------------------------------
Computation of sinh(f)/f
------------------------------------------------------------ */
if (fabs(f) < 1.) {
x2 = f * f;
en = 19.;
s = 1.;
for (i = 1; i <= 9; ++i) {
s = s * x2 / en / (en - 1.) + 1.;
en -= 2.;
}
} else {
s = (e - 1. / e) * .5 / f;
}
/* --------------------------------------------------------
Computation of 1/gamma(1-a) using Chebyshev polynomials */
x2 = nu * nu * 8.;
aye = ch[0];
even = 0.;
alfa = ch[1];
odd = 0.;
for (i = 3; i <= 19; i += 2) {
even = -(aye + aye + even);
aye = -even * x2 - aye + ch[i - 1];
odd = -(alfa + alfa + odd);
alfa = -odd * x2 - alfa + ch[i];
}
even = (even * .5 + aye) * x2 - aye + ch[20];
odd = (odd + alfa) * 2.;
gamma = odd * nu + even;
/* End of computation of 1/gamma(1-a)
----------------------------------------------------------- */
g = e * gamma;
e = (e + 1. / e) * .5;
f = 2. * c * (odd * e + even * s * d);
e = nu * nu;
p = g * c;
q = M_1_PI / g;
c = nu * M_PI_2;
if (fabs(c) < M_eps_sinc)
r = 1.;
else
r = sinpi(nu/2) / c;
r = M_PI * c * r * r;
c = 1.;
d = -b * b;
h = 0.;
ya = f + r * q;
ya1 = p;
en = 1.;
while (fabs(g / (1. + fabs(ya))) +
fabs(h / (1. + fabs(ya1))) > DBL_EPSILON) {
f = (f * en + p + q) / (en * en - e);
c *= (d / en);
p /= en - nu;
q /= en + nu;
g = c * (f + r * q);
h = c * p - en * g;
ya += g;
ya1+= h;
en += 1.;
}
ya = -ya;
ya1 = -ya1 / b;
} else if (ex < thresh_BESS_Y) {
/* --------------------------------------------------------------
Use Temme's scheme for moderate X : 3 <= x < 16
-------------------------------------------------------------- */
c = (.5 - nu) * (.5 + nu);
b = ex + ex;
e = ex * M_1_PI * cospi(nu) / DBL_EPSILON;
e *= e;
p = 1.;
q = -ex;
r = 1. + ex * ex;
s = r;
en = 2.;
while (r * en * en < e) {
en1 = en + 1.;
d = (en - 1. + c / en) / s;
p = (en + en - p * d) / en1;
q = (-b + q * d) / en1;
s = p * p + q * q;
r *= s;
en = en1;
}
f = p / s;
p = f;
g = -q / s;
q = g;
L220:
en -= 1.;
if (en > 0.)
{
r = en1 * (2. - p) - 2.;
s = b + en1 * q;
d = (en - 1. + c / en) / (r * r + s * s);
p = d * r;
q = d * s;
e = f + 1.;
f = p * e - g * q;
g = q * e + p * g;
en1 = en;
goto L220;
}
f = 1. + f;
d = f * f + g * g;
pa = f / d;
qa = -g / d;
d = nu + .5 - p;
q += ex;
pa1 = (pa * q - qa * d) / ex;
qa1 = (qa * q + pa * d) / ex;
b = ex - M_PI_2 * (nu + .5);
c = cos(b);
s = sin(b);
d = M_SQRT_2dPI / sqrt(ex);
ya = d * (pa * s + qa * c);
ya1 = d * (qa1 * s - pa1 * c);
} else { /* x > thresh_BESS_Y */
/* ----------------------------------------------------------
Use Campbell's asymptotic scheme.
---------------------------------------------------------- */
na = 0;
d1 = trunc(ex / fivpi);
i = (int) d1;
dmu = ex - 15. * d1 - d1 * pim5 - (*alpha + .5) * M_PI_2;
if (i - (i / 2 << 1) == 0) {
cosmu = cos(dmu);
sinmu = sin(dmu);
} else {
cosmu = -cos(dmu);
sinmu = -sin(dmu);
}
ddiv = 8. * ex;
dmu = *alpha;
den = sqrt(ex);
for (k = 1; k <= 2; ++k) {
p = cosmu;
cosmu = sinmu;
sinmu = -p;
d1 = (2. * dmu - 1.) * (2. * dmu + 1.);
d2 = 0.;
div = ddiv;
p = 0.;
q = 0.;
q0 = d1 / div;
term = q0;
for (i = 2; i <= 20; ++i) {
d2 += 8.;
d1 -= d2;
div += ddiv;
term = -term * d1 / div;
p += term;
d2 += 8.;
d1 -= d2;
div += ddiv;
term *= (d1 / div);
q += term;
if (fabs(term) <= DBL_EPSILON) {
break;
}
}
p += 1.;
q += q0;
if (k == 1)
ya = M_SQRT_2dPI * (p * cosmu - q * sinmu) / den;
else
ya1 = M_SQRT_2dPI * (p * cosmu - q * sinmu) / den;
dmu += 1.;
}
}
if (na == 1) {
h = 2. * (nu + 1.) / ex;
if (h > 1.) {
if (fabs(ya1) > DBL_MAX / h) {
h = 0.;
ya = 0.;
}
}
h = h * ya1 - ya;
ya = ya1;
ya1 = h;
}
/* ---------------------------------------------------------------
Now have first one or two Y's
--------------------------------------------------------------- */
by[0] = ya;
*ncalc = 1;
if(*nb > 1) {
by[1] = ya1;
if (ya1 != 0.) {
aye = 1. + *alpha;
twobyx = 2. / ex;
*ncalc = 2;
for (i = 2; i < *nb; ++i) {
if (twobyx < 1.) {
if (fabs(by[i - 1]) * twobyx >= DBL_MAX / aye)
goto L450;
} else {
if (fabs(by[i - 1]) >= DBL_MAX / aye / twobyx)
goto L450;
}
by[i] = twobyx * aye * by[i - 1] - by[i - 2];
aye += 1.;
++(*ncalc);
}
}
}
L450:
for (i = *ncalc; i < *nb; ++i)
by[i] = - INFINITY;/* was 0 */
} else {
by[0] = 0.;
*ncalc = min0(*nb,0) - 1;
}
}