Added MACS source
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / specfunc / bessel_j.c
1 /* specfunc/bessel_j.c
2  * 
3  * Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003 Gerard Jungman
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /* Author:  G. Jungman */
21
22 #include <config.h>
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_sf_pow_int.h>
26 #include <gsl/gsl_sf_trig.h>
27 #include <gsl/gsl_sf_bessel.h>
28
29 #include "error.h"
30
31 #include "bessel.h"
32 #include "bessel_olver.h"
33
34 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
35
36 int gsl_sf_bessel_j0_e(const double x, gsl_sf_result * result)
37 {
38   double ax = fabs(x);
39
40   /* CHECK_POINTER(result) */
41
42   if(ax < 0.5) {
43     const double y = x*x;
44     const double c1 = -1.0/6.0;
45     const double c2 =  1.0/120.0;
46     const double c3 = -1.0/5040.0;
47     const double c4 =  1.0/362880.0;
48     const double c5 = -1.0/39916800.0;
49     const double c6 =  1.0/6227020800.0;
50     result->val = 1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*(c5 + y*c6)))));
51     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
52     return GSL_SUCCESS;
53   }
54   else {
55     gsl_sf_result sin_result;
56     const int stat = gsl_sf_sin_e(x, &sin_result);
57     result->val  = sin_result.val/x;
58     result->err  = fabs(sin_result.err/x);
59     result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
60     return stat;
61   }
62 }
63
64
65 int gsl_sf_bessel_j1_e(const double x, gsl_sf_result * result)
66 {
67   double ax = fabs(x);
68
69   /* CHECK_POINTER(result) */
70
71   if(x == 0.0) {
72     result->val = 0.0;
73     result->err = 0.0;
74     return GSL_SUCCESS;
75   }
76   else if(ax < 3.1*GSL_DBL_MIN) {
77     UNDERFLOW_ERROR(result);
78   }
79   else if(ax < 0.25) {
80     const double y = x*x;
81     const double c1 = -1.0/10.0;
82     const double c2 =  1.0/280.0;
83     const double c3 = -1.0/15120.0;
84     const double c4 =  1.0/1330560.0;
85     const double c5 = -1.0/172972800.0;
86     const double sum = 1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*c5))));
87     result->val = x/3.0 * sum;
88     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
89     return GSL_SUCCESS;
90   }
91   else {
92     gsl_sf_result cos_result;
93     gsl_sf_result sin_result;
94     const int stat_cos = gsl_sf_cos_e(x, &cos_result);
95     const int stat_sin = gsl_sf_sin_e(x, &sin_result);
96     const double cos_x = cos_result.val;
97     const double sin_x = sin_result.val;
98     result->val  = (sin_x/x - cos_x)/x;
99     result->err  = (fabs(sin_result.err/x) + fabs(cos_result.err))/fabs(x);
100     result->err += 2.0 * GSL_DBL_EPSILON * (fabs(sin_x/(x*x)) + fabs(cos_x/x));
101     result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
102     return GSL_ERROR_SELECT_2(stat_cos, stat_sin);
103   }
104 }
105
106
107 int gsl_sf_bessel_j2_e(const double x, gsl_sf_result * result)
108 {
109   double ax = fabs(x);
110
111   /* CHECK_POINTER(result) */
112   
113   if(x == 0.0) {
114     result->val = 0.0;
115     result->err = 0.0;
116     return GSL_SUCCESS;
117   }
118   else if(ax < 4.0*GSL_SQRT_DBL_MIN) {
119     UNDERFLOW_ERROR(result);
120   }
121   else if(ax < 1.3) {
122     const double y  = x*x;
123     const double c1 = -1.0/14.0;
124     const double c2 =  1.0/504.0;
125     const double c3 = -1.0/33264.0;
126     const double c4 =  1.0/3459456.0;
127     const double c5 = -1.0/518918400;
128     const double c6 =  1.0/105859353600.0;
129     const double c7 = -1.0/28158588057600.0;
130     const double c8 =  1.0/9461285587353600.0;
131     const double c9 = -1.0/3916972233164390400.0;
132     const double sum = 1.0+y*(c1+y*(c2+y*(c3+y*(c4+y*(c5+y*(c6+y*(c7+y*(c8+y*c9))))))));
133     result->val = y/15.0 * sum;
134     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
135     return GSL_SUCCESS;
136   }
137   else {
138     gsl_sf_result cos_result;
139     gsl_sf_result sin_result;
140     const int stat_cos = gsl_sf_cos_e(x, &cos_result);
141     const int stat_sin = gsl_sf_sin_e(x, &sin_result);
142     const double cos_x = cos_result.val;
143     const double sin_x = sin_result.val;
144     const double f = (3.0/(x*x) - 1.0);
145     result->val  = (f * sin_x - 3.0*cos_x/x)/x;
146     result->err  = fabs(f * sin_result.err/x) + fabs((3.0*cos_result.err/x)/x);
147     result->err += 2.0 * GSL_DBL_EPSILON * (fabs(f*sin_x/x) + 3.0*fabs(cos_x/(x*x)));
148     result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
149     return GSL_ERROR_SELECT_2(stat_cos, stat_sin);
150   }
151 }
152
153
154 int
155 gsl_sf_bessel_jl_e(const int l, const double x, gsl_sf_result * result)
156 {
157   if(l < 0 || x < 0.0) {
158     DOMAIN_ERROR(result);
159   }
160   else if(x == 0.0) {
161     result->val = ( l > 0 ? 0.0 : 1.0 );
162     result->err = 0.0;
163     return GSL_SUCCESS;
164   }
165   else if(l == 0) {
166     return gsl_sf_bessel_j0_e(x, result);
167   }
168   else if(l == 1) {
169     return gsl_sf_bessel_j1_e(x, result);
170   }
171   else if(l == 2) {
172     return gsl_sf_bessel_j2_e(x, result);
173   }
174   else if(x*x < 10.0*(l+0.5)/M_E) {
175     gsl_sf_result b;
176     int status = gsl_sf_bessel_IJ_taylor_e(l+0.5, x, -1, 50, GSL_DBL_EPSILON, &b);
177     double pre   = sqrt((0.5*M_PI)/x);
178     result->val  = pre * b.val;
179     result->err  = pre * b.err;
180     result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
181     return status;
182   }
183   else if(GSL_ROOT4_DBL_EPSILON * x > (l*l + l + 1.0)) {
184     gsl_sf_result b;
185     int status = gsl_sf_bessel_Jnu_asympx_e(l + 0.5, x, &b);
186     double pre = sqrt((0.5*M_PI)/x);
187     result->val = pre * b.val;
188     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err;
189     return status;
190   }
191   else if(l > 1.0/GSL_ROOT6_DBL_EPSILON) {
192     gsl_sf_result b;
193     int status = gsl_sf_bessel_Jnu_asymp_Olver_e(l + 0.5, x, &b);
194     double pre = sqrt((0.5*M_PI)/x);
195     result->val = pre * b.val;
196     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err;
197     return status;
198   }
199   else if(x > 1000.0 && x > l*l)
200   {
201     /* We need this path to avoid feeding large x to CF1 below; */
202     gsl_sf_result b;
203     int status = gsl_sf_bessel_Jnu_asympx_e(l + 0.5, x, &b);
204     double pre = sqrt((0.5*M_PI)/x);
205     result->val = pre * b.val;
206     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err;
207     return status;  
208   }
209   else {
210     double sgn;
211     double ratio;
212     /* The CF1 call will hit 10000 iterations for x > 10000 + l */
213     int stat_CF1 = gsl_sf_bessel_J_CF1(l+0.5, x, &ratio, &sgn);
214     double jellp1 = GSL_SQRT_DBL_EPSILON * ratio;
215     double jell   = GSL_SQRT_DBL_EPSILON;
216     double jellm1;
217     int ell;
218     for(ell = l; ell > 0; ell--) {
219       jellm1 = -jellp1 + (2*ell + 1)/x * jell;
220       jellp1 = jell;
221       jell   = jellm1;
222     }
223
224     if(fabs(jell) > fabs(jellp1)) {
225       gsl_sf_result j0_result;
226       int stat_j0  = gsl_sf_bessel_j0_e(x, &j0_result);
227       double pre   = GSL_SQRT_DBL_EPSILON / jell;
228       result->val  = j0_result.val * pre;
229       result->err  = j0_result.err * fabs(pre);
230       result->err += 4.0 * GSL_DBL_EPSILON * (0.5*l + 1.0) * fabs(result->val);
231       return GSL_ERROR_SELECT_2(stat_j0, stat_CF1);
232     }
233     else {
234       gsl_sf_result j1_result;
235       int stat_j1  = gsl_sf_bessel_j1_e(x, &j1_result);
236       double pre   = GSL_SQRT_DBL_EPSILON / jellp1;
237       result->val  = j1_result.val * pre;
238       result->err  = j1_result.err * fabs(pre);
239       result->err += 4.0 * GSL_DBL_EPSILON * (0.5*l + 1.0) * fabs(result->val);
240       return GSL_ERROR_SELECT_2(stat_j1, stat_CF1);
241     }
242   }
243 }
244
245
246 int
247 gsl_sf_bessel_jl_array(const int lmax, const double x, double * result_array)
248 {
249   /* CHECK_POINTER(result_array) */
250
251   if(lmax < 0 || x < 0.0) {
252     int j;
253     for(j=0; j<=lmax; j++) result_array[j] = 0.0;
254     GSL_ERROR ("error", GSL_EDOM);
255   }
256   else if(x == 0.0) {
257     int j;
258     for(j=1; j<=lmax; j++) result_array[j] = 0.0;
259     result_array[0] = 1.0;
260     return GSL_SUCCESS;
261   }
262   else {
263     gsl_sf_result r_jellp1;
264     gsl_sf_result r_jell;
265     int stat_0 = gsl_sf_bessel_jl_e(lmax+1, x, &r_jellp1);
266     int stat_1 = gsl_sf_bessel_jl_e(lmax,   x, &r_jell);
267     double jellp1 = r_jellp1.val;
268     double jell   = r_jell.val;
269     double jellm1;
270     int ell;
271
272     result_array[lmax] = jell;
273     for(ell = lmax; ell >= 1; ell--) {
274       jellm1 = -jellp1 + (2*ell + 1)/x * jell;
275       jellp1 = jell;
276       jell   = jellm1;
277       result_array[ell-1] = jellm1;
278     }
279
280     return GSL_ERROR_SELECT_2(stat_0, stat_1);
281   }
282 }
283
284
285 int gsl_sf_bessel_jl_steed_array(const int lmax, const double x, double * jl_x)
286 {
287   /* CHECK_POINTER(jl_x) */
288
289   if(lmax < 0 || x < 0.0) {
290     int j;
291     for(j=0; j<=lmax; j++) jl_x[j] = 0.0;
292     GSL_ERROR ("error", GSL_EDOM);
293   }
294   else if(x == 0.0) {
295     int j;
296     for(j=1; j<=lmax; j++) jl_x[j] = 0.0;
297     jl_x[0] = 1.0;
298     return GSL_SUCCESS;
299   }
300   else if(x < 2.0*GSL_ROOT4_DBL_EPSILON) {
301     /* first two terms of Taylor series */
302     double inv_fact = 1.0;  /* 1/(1 3 5 ... (2l+1)) */
303     double x_l      = 1.0;  /* x^l */
304     int l;
305     for(l=0; l<=lmax; l++) {
306       jl_x[l]  = x_l * inv_fact;
307       jl_x[l] *= 1.0 - 0.5*x*x/(2.0*l+3.0);
308       inv_fact /= 2.0*l+3.0;
309       x_l      *= x;
310     }
311     return GSL_SUCCESS;
312   }
313   else {
314     /* Steed/Barnett algorithm [Comp. Phys. Comm. 21, 297 (1981)] */
315     double x_inv = 1.0/x;
316     double W = 2.0*x_inv;
317     double F = 1.0;
318     double FP = (lmax+1.0) * x_inv;
319     double B = 2.0*FP + x_inv;
320     double end = B + 20000.0*W;
321     double D = 1.0/B;
322     double del = -D;
323     
324     FP += del;
325     
326     /* continued fraction */
327     do {
328       B += W;
329       D = 1.0/(B-D);
330       del *= (B*D - 1.);
331       FP += del;
332       if(D < 0.0) F = -F;
333       if(B > end) {
334         GSL_ERROR ("error", GSL_EMAXITER);
335       }
336     }
337     while(fabs(del) >= fabs(FP) * GSL_DBL_EPSILON);
338     
339     FP *= F;
340     
341     if(lmax > 0) {
342       /* downward recursion */
343       double XP2 = FP;
344       double PL = lmax * x_inv;
345       int L  = lmax;
346       int LP;
347       jl_x[lmax] = F;
348       for(LP = 1; LP<=lmax; LP++) {
349         jl_x[L-1] = PL * jl_x[L] + XP2;
350         FP = PL*jl_x[L-1] - jl_x[L];
351         XP2 = FP;
352         PL -= x_inv;
353         --L;
354       }
355       F = jl_x[0];
356     }
357     
358     /* normalization */
359     W = x_inv / hypot(FP, F);
360     jl_x[0] = W*F;
361     if(lmax > 0) {
362       int L;
363       for(L=1; L<=lmax; L++) {
364         jl_x[L] *= W;
365       }
366     }
367
368     return GSL_SUCCESS;
369   }
370 }
371
372
373 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
374
375 #include "eval.h"
376
377 double gsl_sf_bessel_j0(const double x)
378 {
379   EVAL_RESULT(gsl_sf_bessel_j0_e(x, &result));
380 }
381
382 double gsl_sf_bessel_j1(const double x)
383 {
384   EVAL_RESULT(gsl_sf_bessel_j1_e(x, &result));
385 }
386
387 double gsl_sf_bessel_j2(const double x)
388 {
389   EVAL_RESULT(gsl_sf_bessel_j2_e(x, &result));
390 }
391
392 double gsl_sf_bessel_jl(const int l, const double x)
393 {
394   EVAL_RESULT(gsl_sf_bessel_jl_e(l, x, &result));
395 }
396