-
Notifications
You must be signed in to change notification settings - Fork 213
/
bessel_j.c
401 lines (360 loc) · 10.9 KB
/
bessel_j.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
/* specfunc/bessel_j.c
*
* Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003 Gerard Jungman
*
* 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 3 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Author: G. Jungman */
#include <config.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_sf_pow_int.h>
#include <gsl/gsl_sf_trig.h>
#include <gsl/gsl_sf_bessel.h>
#include "error.h"
#include "bessel.h"
#include "bessel_olver.h"
/*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
int gsl_sf_bessel_j0_e(const double x, gsl_sf_result * result)
{
double ax = fabs(x);
/* CHECK_POINTER(result) */
if(ax < 0.5) {
const double y = x*x;
const double c1 = -1.0/6.0;
const double c2 = 1.0/120.0;
const double c3 = -1.0/5040.0;
const double c4 = 1.0/362880.0;
const double c5 = -1.0/39916800.0;
const double c6 = 1.0/6227020800.0;
result->val = 1.0 y*(c1 y*(c2 y*(c3 y*(c4 y*(c5 y*c6)))));
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
return GSL_SUCCESS;
}
else {
result->val = sin(x) / x;
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
return GSL_SUCCESS;
}
}
int gsl_sf_bessel_j1_e(const double x, gsl_sf_result * result)
{
double ax = fabs(x);
/* CHECK_POINTER(result) */
if(x == 0.0) {
result->val = 0.0;
result->err = 0.0;
return GSL_SUCCESS;
}
else if(ax < 3.1*GSL_DBL_MIN) {
UNDERFLOW_ERROR(result);
}
else if(ax < 0.25) {
const double y = x*x;
const double c1 = -1.0/10.0;
const double c2 = 1.0/280.0;
const double c3 = -1.0/15120.0;
const double c4 = 1.0/1330560.0;
const double c5 = -1.0/172972800.0;
const double sum = 1.0 y*(c1 y*(c2 y*(c3 y*(c4 y*c5))));
result->val = x/3.0 * sum;
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
return GSL_SUCCESS;
}
else {
const double cos_x = cos(x);
const double sin_x = sin(x);
result->val = (sin_x/x - cos_x)/x;
result->err = 2.0 * GSL_DBL_EPSILON * (fabs(sin_x/(x*x)) fabs(cos_x/x));
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
return GSL_SUCCESS;
}
}
int gsl_sf_bessel_j2_e(const double x, gsl_sf_result * result)
{
double ax = fabs(x);
/* CHECK_POINTER(result) */
if(x == 0.0) {
result->val = 0.0;
result->err = 0.0;
return GSL_SUCCESS;
}
else if(ax < 4.0*GSL_SQRT_DBL_MIN) {
UNDERFLOW_ERROR(result);
}
else if(ax < 1.3) {
const double y = x*x;
const double c1 = -1.0/14.0;
const double c2 = 1.0/504.0;
const double c3 = -1.0/33264.0;
const double c4 = 1.0/3486356.0;
const double c5 = -1.0/518918400;
const double c6 = 1.0/105859353600.0;
const double c7 = -1.0/28158588057600.0;
const double c8 = 1.0/9461285587353600.0;
const double c9 = -1.0/3916972233164390400.0;
const double sum = 1.0 y*(c1 y*(c2 y*(c3 y*(c4 y*(c5 y*(c6 y*(c7 y*(c8 y*c9))))))));
result->val = y/15.0 * sum;
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
return GSL_SUCCESS;
}
else {
/* bug #45730: switch from gsl_sf_{cos,sin} to cos/sin to fix large inputs */
#if 0
gsl_sf_result cos_result;
gsl_sf_result sin_result;
const int stat_cos = gsl_sf_cos_e(x, &cos_result);
const int stat_sin = gsl_sf_sin_e(x, &sin_result);
const double cos_x = cos_result.val;
const double sin_x = sin_result.val;
const double f = (3.0/(x*x) - 1.0);
result->val = (f * sin_x - 3.0*cos_x/x)/x;
result->err = fabs(f * sin_result.err/x) fabs((3.0*cos_result.err/x)/x);
result->err = 2.0 * GSL_DBL_EPSILON * (fabs(f*sin_x/x) 3.0*fabs(cos_x/(x*x)));
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
return GSL_ERROR_SELECT_2(stat_cos, stat_sin);
#else
const double cos_x = cos(x);
const double sin_x = sin(x);
const double f = (3.0/(x*x) - 1.0);
result->val = (f * sin_x - 3.0*cos_x/x)/x;
result->err = 2.0 * GSL_DBL_EPSILON * (fabs(f*sin_x/x) 3.0*fabs(cos_x/(x*x)));
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
/*return GSL_ERROR_SELECT_2(stat_cos, stat_sin);*/
return GSL_SUCCESS;
#endif
}
}
int
gsl_sf_bessel_jl_e(const int l, const double x, gsl_sf_result * result)
{
if(l < 0 || x < 0.0) {
DOMAIN_ERROR(result);
}
else if(x == 0.0) {
result->val = ( l > 0 ? 0.0 : 1.0 );
result->err = 0.0;
return GSL_SUCCESS;
}
else if(l == 0) {
return gsl_sf_bessel_j0_e(x, result);
}
else if(l == 1) {
return gsl_sf_bessel_j1_e(x, result);
}
else if(l == 2) {
return gsl_sf_bessel_j2_e(x, result);
}
else if(x*x < 10.0*(l 0.5)/M_E) {
gsl_sf_result b;
int status = gsl_sf_bessel_IJ_taylor_e(l 0.5, x, -1, 50, GSL_DBL_EPSILON, &b);
double pre = sqrt((0.5*M_PI)/x);
result->val = pre * b.val;
result->err = pre * b.err;
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
return status;
}
else if(GSL_ROOT4_DBL_EPSILON * x > (l*l l 1.0)) {
gsl_sf_result b;
int status = gsl_sf_bessel_Jnu_asympx_e(l 0.5, x, &b);
double pre = sqrt((0.5*M_PI)/x);
result->val = pre * b.val;
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) pre * b.err;
return status;
}
else if(l > 1.0/GSL_ROOT6_DBL_EPSILON) {
gsl_sf_result b;
int status = gsl_sf_bessel_Jnu_asymp_Olver_e(l 0.5, x, &b);
double pre = sqrt((0.5*M_PI)/x);
result->val = pre * b.val;
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) pre * b.err;
return status;
}
else if(x > 1000.0 && x > l*l)
{
/* We need this path to avoid feeding large x to CF1 below; */
gsl_sf_result b;
int status = gsl_sf_bessel_Jnu_asympx_e(l 0.5, x, &b);
double pre = sqrt((0.5*M_PI)/x);
result->val = pre * b.val;
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) pre * b.err;
return status;
}
else {
double sgn;
double ratio;
/* The CF1 call will hit 10000 iterations for x > 10000 l */
int stat_CF1 = gsl_sf_bessel_J_CF1(l 0.5, x, &ratio, &sgn);
const double BESSEL_J_SMALL = GSL_DBL_MIN / GSL_DBL_EPSILON;
double jellp1 = BESSEL_J_SMALL * ratio;
double jell = BESSEL_J_SMALL;
double jellm1;
int ell;
for(ell = l; ell > 0; ell--) {
jellm1 = -jellp1 (2*ell 1)/x * jell;
jellp1 = jell;
jell = jellm1;
}
if(fabs(jell) > fabs(jellp1)) {
gsl_sf_result j0_result;
int stat_j0 = gsl_sf_bessel_j0_e(x, &j0_result);
double pre = BESSEL_J_SMALL / jell;
result->val = j0_result.val * pre;
result->err = j0_result.err * fabs(pre);
result->err = 4.0 * GSL_DBL_EPSILON * (0.5*l 1.0) * fabs(result->val);
return GSL_ERROR_SELECT_2(stat_j0, stat_CF1);
}
else {
gsl_sf_result j1_result;
int stat_j1 = gsl_sf_bessel_j1_e(x, &j1_result);
double pre = BESSEL_J_SMALL / jellp1;
result->val = j1_result.val * pre;
result->err = j1_result.err * fabs(pre);
result->err = 4.0 * GSL_DBL_EPSILON * (0.5*l 1.0) * fabs(result->val);
return GSL_ERROR_SELECT_2(stat_j1, stat_CF1);
}
}
}
int
gsl_sf_bessel_jl_array(const int lmax, const double x, double * result_array)
{
/* CHECK_POINTER(result_array) */
if(lmax < 0 || x < 0.0) {
int j;
for(j=0; j<=lmax; j ) result_array[j] = 0.0;
GSL_ERROR ("error", GSL_EDOM);
}
else if(x == 0.0) {
int j;
for(j=1; j<=lmax; j ) result_array[j] = 0.0;
result_array[0] = 1.0;
return GSL_SUCCESS;
}
else {
gsl_sf_result r_jellp1;
gsl_sf_result r_jell;
int stat_0 = gsl_sf_bessel_jl_e(lmax 1, x, &r_jellp1);
int stat_1 = gsl_sf_bessel_jl_e(lmax, x, &r_jell);
double jellp1 = r_jellp1.val;
double jell = r_jell.val;
double jellm1;
int ell;
result_array[lmax] = jell;
for(ell = lmax; ell >= 1; ell--) {
jellm1 = -jellp1 (2*ell 1)/x * jell;
jellp1 = jell;
jell = jellm1;
result_array[ell-1] = jellm1;
}
return GSL_ERROR_SELECT_2(stat_0, stat_1);
}
}
int gsl_sf_bessel_jl_steed_array(const int lmax, const double x, double * jl_x)
{
/* CHECK_POINTER(jl_x) */
if(lmax < 0 || x < 0.0) {
int j;
for(j=0; j<=lmax; j ) jl_x[j] = 0.0;
GSL_ERROR ("error", GSL_EDOM);
}
else if(x == 0.0) {
int j;
for(j=1; j<=lmax; j ) jl_x[j] = 0.0;
jl_x[0] = 1.0;
return GSL_SUCCESS;
}
else if(x < 2.0*GSL_ROOT4_DBL_EPSILON) {
/* first two terms of Taylor series */
double inv_fact = 1.0; /* 1/(1 3 5 ... (2l 1)) */
double x_l = 1.0; /* x^l */
int l;
for(l=0; l<=lmax; l ) {
jl_x[l] = x_l * inv_fact;
jl_x[l] *= 1.0 - 0.5*x*x/(2.0*l 3.0);
inv_fact /= 2.0*l 3.0;
x_l *= x;
}
return GSL_SUCCESS;
}
else {
/* Steed/Barnett algorithm [Comp. Phys. Comm. 21, 297 (1981)] */
double x_inv = 1.0/x;
double W = 2.0*x_inv;
double F = 1.0;
double FP = (lmax 1.0) * x_inv;
double B = 2.0*FP x_inv;
double end = B 20000.0*W;
double D = 1.0/B;
double del = -D;
FP = del;
/* continued fraction */
do {
B = W;
D = 1.0/(B-D);
del *= (B*D - 1.);
FP = del;
if(D < 0.0) F = -F;
if(B > end) {
GSL_ERROR ("error", GSL_EMAXITER);
}
}
while(fabs(del) >= fabs(FP) * GSL_DBL_EPSILON);
FP *= F;
if(lmax > 0) {
/* downward recursion */
double XP2 = FP;
double PL = lmax * x_inv;
int L = lmax;
int LP;
jl_x[lmax] = F;
for(LP = 1; LP<=lmax; LP ) {
jl_x[L-1] = PL * jl_x[L] XP2;
FP = PL*jl_x[L-1] - jl_x[L];
XP2 = FP;
PL -= x_inv;
--L;
}
F = jl_x[0];
}
/* normalization */
W = x_inv / hypot(FP, F);
jl_x[0] = W*F;
if(lmax > 0) {
int L;
for(L=1; L<=lmax; L ) {
jl_x[L] *= W;
}
}
return GSL_SUCCESS;
}
}
/*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
#include "eval.h"
double gsl_sf_bessel_j0(const double x)
{
EVAL_RESULT(gsl_sf_bessel_j0_e(x, &result));
}
double gsl_sf_bessel_j1(const double x)
{
EVAL_RESULT(gsl_sf_bessel_j1_e(x, &result));
}
double gsl_sf_bessel_j2(const double x)
{
EVAL_RESULT(gsl_sf_bessel_j2_e(x, &result));
}
double gsl_sf_bessel_jl(const int l, const double x)
{
EVAL_RESULT(gsl_sf_bessel_jl_e(l, x, &result));
}