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>
26 #include <gsl/gsl_sf_gamma.h>
27 #include <gsl/gsl_sf_bessel.h>
34 /*-*-*-*-*-*-*-*-*-*-*-* Private Section *-*-*-*-*-*-*-*-*-*-*-*/
36 /* [Abramowitz+Stegun, 10.2.4 + 10.2.6]
37 * with lmax=15, precision ~ 15D for x < 3
41 static int bessel_kl_scaled_small_x(int l, const double x, gsl_sf_result * result)
43 gsl_sf_result num_fact;
44 double den = gsl_sf_pow_int(x, l+1);
45 int stat_df = gsl_sf_doublefact_e((unsigned int) (2*l-1), &num_fact);
47 if(stat_df != GSL_SUCCESS || den == 0.0) {
48 OVERFLOW_ERROR(result);
52 gsl_sf_result ipos_term;
54 double sgn = (GSL_IS_ODD(l) ? -1.0 : 1.0);
64 for(i=1; i<lmax; i++) {
65 t_coeff /= i*(2*(i-l) - 1);
67 delta = t_power*t_coeff;
69 if(fabs(delta/sum) < GSL_DBL_EPSILON) break;
72 stat_il = gsl_sf_bessel_il_scaled_e(l, x, &ipos_term);
73 ineg_term = sgn * num_fact.val/den * sum;
74 result->val = -sgn * 0.5*M_PI * (ex*ipos_term.val - ineg_term);
76 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
82 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
84 int gsl_sf_bessel_k0_scaled_e(const double x, gsl_sf_result * result)
86 /* CHECK_POINTER(result) */
92 result->val = M_PI/(2.0*x);
93 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
94 CHECK_UNDERFLOW(result);
100 int gsl_sf_bessel_k1_scaled_e(const double x, gsl_sf_result * result)
102 /* CHECK_POINTER(result) */
105 DOMAIN_ERROR(result);
107 else if(x < (M_SQRTPI+1.0)/(M_SQRT2*GSL_SQRT_DBL_MAX)) {
108 OVERFLOW_ERROR(result);
111 result->val = M_PI/(2.0*x) * (1.0 + 1.0/x);
112 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
113 CHECK_UNDERFLOW(result);
119 int gsl_sf_bessel_k2_scaled_e(const double x, gsl_sf_result * result)
121 /* CHECK_POINTER(result) */
124 DOMAIN_ERROR(result);
126 else if(x < 2.0/GSL_ROOT3_DBL_MAX) {
127 OVERFLOW_ERROR(result);
130 result->val = M_PI/(2.0*x) * (1.0 + 3.0/x * (1.0 + 1.0/x));
131 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
132 CHECK_UNDERFLOW(result);
138 int gsl_sf_bessel_kl_scaled_e(int l, const double x, gsl_sf_result * result)
140 if(l < 0 || x <= 0.0) {
141 DOMAIN_ERROR(result);
144 return gsl_sf_bessel_k0_scaled_e(x, result);
147 return gsl_sf_bessel_k1_scaled_e(x, result);
150 return gsl_sf_bessel_k2_scaled_e(x, result);
153 return bessel_kl_scaled_small_x(l, x, result);
155 else if(GSL_ROOT3_DBL_EPSILON * x > (l*l + l + 1)) {
156 int status = gsl_sf_bessel_Knu_scaled_asympx_e(l + 0.5, x, result);
157 double pre = sqrt((0.5*M_PI)/x);
162 else if(GSL_MIN(0.29/(l*l+1.0), 0.5/(l*l+1.0+x*x)) < GSL_ROOT3_DBL_EPSILON) {
163 int status = gsl_sf_bessel_Knu_scaled_asymp_unif_e(l + 0.5, x, result);
164 double pre = sqrt((0.5*M_PI)/x);
173 int stat_1 = gsl_sf_bessel_k1_scaled_e(x, &r_bk);
174 int stat_0 = gsl_sf_bessel_k0_scaled_e(x, &r_bkm);
176 double bk = r_bk.val;
177 double bkm = r_bkm.val;
180 bkp = (2*j+1)/x*bk + bkm;
185 result->err = fabs(bk) * (fabs(r_bk.err/r_bk.val) + fabs(r_bkm.err/r_bkm.val));
186 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
188 return GSL_ERROR_SELECT_2(stat_1, stat_0);
193 gsl_sf_bessel_kl_scaled_array(const int lmax, const double x, double * result_array)
195 if(lmax < 0 || x <= 0.0) {
196 GSL_ERROR("domain error", GSL_EDOM);
197 } else if (lmax == 0) {
198 gsl_sf_result result;
199 int stat = gsl_sf_bessel_k0_scaled_e(x, &result);
200 result_array[0] = result.val;
204 double kellp1, kell, kellm1;
205 gsl_sf_result r_kell;
206 gsl_sf_result r_kellm1;
207 gsl_sf_bessel_k1_scaled_e(x, &r_kell);
208 gsl_sf_bessel_k0_scaled_e(x, &r_kellm1);
210 kellm1 = r_kellm1.val;
211 result_array[0] = kellm1;
212 result_array[1] = kell;
213 for(ell = 1; ell < lmax; ell++) {
214 kellp1 = (2*ell+1)/x * kell + kellm1;
215 result_array[ell+1] = kellp1;
224 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
228 double gsl_sf_bessel_k0_scaled(const double x)
230 EVAL_RESULT(gsl_sf_bessel_k0_scaled_e(x, &result));
233 double gsl_sf_bessel_k1_scaled(const double x)
235 EVAL_RESULT(gsl_sf_bessel_k1_scaled_e(x, &result));
238 double gsl_sf_bessel_k2_scaled(const double x)
240 EVAL_RESULT(gsl_sf_bessel_k2_scaled_e(x, &result));
243 double gsl_sf_bessel_kl_scaled(const int l, const double x)
245 EVAL_RESULT(gsl_sf_bessel_kl_scaled_e(l, x, &result));