1 /* specfunc/bessel_Jn.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
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.
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.
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.
20 /* Author: G. Jungman */
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_sf_pow_int.h>
27 #include "bessel_amp_phase.h"
28 #include "bessel_olver.h"
29 #include <gsl/gsl_sf_bessel.h>
33 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
36 int gsl_sf_bessel_Jn_e(int n, double x, gsl_sf_result * result)
41 /* reduce to case n >= 0 */
43 if(GSL_IS_ODD(n)) sign = -sign;
47 /* reduce to case x >= 0. */
49 if(GSL_IS_ODD(n)) sign = -sign;
52 /* CHECK_POINTER(result) */
56 int stat_J0 = gsl_sf_bessel_J0_e(x, &b0);
57 result->val = sign * b0.val;
63 int stat_J1 = gsl_sf_bessel_J1_e(x, &b1);
64 result->val = sign * b1.val;
74 else if(x*x < 10.0*(n+1.0)*GSL_ROOT5_DBL_EPSILON) {
76 int status = gsl_sf_bessel_IJ_taylor_e((double)n, x, -1, 50, GSL_DBL_EPSILON, &b);
77 result->val = sign * b.val;
79 result->err += GSL_DBL_EPSILON * fabs(result->val);
82 else if(GSL_ROOT4_DBL_EPSILON * x > (n*n+1.0)) {
83 int status = gsl_sf_bessel_Jnu_asympx_e((double)n, x, result);
88 int status = gsl_sf_bessel_Jnu_asymp_Olver_e((double)n, x, result);
94 /* We need this to avoid feeding large x to CF1; note that
95 * due to the above check, we know that n <= 50.
97 int status = gsl_sf_bessel_Jnu_asympx_e((double)n, x, result);
107 int stat_CF1 = gsl_sf_bessel_J_CF1((double)n, x, &ratio, &sgn);
109 /* backward recurrence */
110 double Jkp1 = GSL_SQRT_DBL_MIN * ratio;
111 double Jk = GSL_SQRT_DBL_MIN;
116 Jkm1 = 2.0*k/x * Jk - Jkp1;
121 if(fabs(Jkp1) > fabs(Jk)) {
123 stat_b = gsl_sf_bessel_J1_e(x, &b1);
124 ans = b1.val/Jkp1 * GSL_SQRT_DBL_MIN;
125 err = b1.err/Jkp1 * GSL_SQRT_DBL_MIN;
129 stat_b = gsl_sf_bessel_J0_e(x, &b0);
130 ans = b0.val/Jk * GSL_SQRT_DBL_MIN;
131 err = b0.err/Jk * GSL_SQRT_DBL_MIN;
134 result->val = sign * ans;
135 result->err = fabs(err);
136 return GSL_ERROR_SELECT_2(stat_CF1, stat_b);
143 gsl_sf_bessel_Jn_array(int nmin, int nmax, double x, double * result_array)
145 /* CHECK_POINTER(result_array) */
147 if(nmin < 0 || nmax < nmin) {
149 for(n=nmax; n>=nmin; n--) {
150 result_array[n-nmin] = 0.0;
152 GSL_ERROR ("domain error", GSL_EDOM);
156 for(n=nmax; n>=nmin; n--) {
157 result_array[n-nmin] = 0.0;
159 if(nmin == 0) result_array[0] = 1.0;
163 gsl_sf_result r_Jnp1;
165 int stat_np1 = gsl_sf_bessel_Jn_e(nmax+1, x, &r_Jnp1);
166 int stat_n = gsl_sf_bessel_Jn_e(nmax, x, &r_Jn);
167 int stat = GSL_ERROR_SELECT_2(stat_np1, stat_n);
169 double Jnp1 = r_Jnp1.val;
170 double Jn = r_Jn.val;
174 if(stat == GSL_SUCCESS) {
175 for(n=nmax; n>=nmin; n--) {
176 result_array[n-nmin] = Jn;
177 Jnm1 = -Jnp1 + 2.0*n/x * Jn;
183 for(n=nmax; n>=nmin; n--) {
184 result_array[n-nmin] = 0.0;
192 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
196 double gsl_sf_bessel_Jn(const int n, const double x)
198 EVAL_RESULT(gsl_sf_bessel_Jn_e(n, x, &result));