/src/r-source/src/nmath/bessel_k.c
Line | Count | Source |
1 | | /* |
2 | | * Mathlib : A C Library of Special Functions |
3 | | * Copyright (C) 1998-2026 Ross Ihaka and the R Core team. |
4 | | * Copyright (C) 2002-2026 The R Foundation |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, a copy is available at |
18 | | * https://www.R-project.org/Licenses/ |
19 | | */ |
20 | | |
21 | | /* DESCRIPTION --> see below */ |
22 | | |
23 | | |
24 | | /* From http://www.netlib.org/specfun/rkbesl Fortran translated by f2c,... |
25 | | * ------------------------------=#---- Martin Maechler, ETH Zurich |
26 | | */ |
27 | | #include "nmath.h" |
28 | | #include "bessel.h" |
29 | | |
30 | | // set in Rmath.h: #define M_bessel_ik_max_alpha 1e9 -- new (2026, R 4.7.0) |
31 | | static const double max_alpha_k = M_bessel_ik_max_alpha; |
32 | | |
33 | | #ifndef MATHLIB_STANDALONE |
34 | | #include <R_ext/Memory.h> |
35 | | #endif |
36 | | |
37 | 0 | #define min0(x, y) (((x) <= (y)) ? (x) : (y)) |
38 | 0 | #define max0(x, y) (((x) <= (y)) ? (y) : (x)) |
39 | | |
40 | | static void K_bessel(double *x, double *alpha, int *nb, |
41 | | int *ize, double *bk, int *ncalc); |
42 | | |
43 | | // in API, Rmath.h -- but *not* called from R ---> bessel_k_ex() below |
44 | | double bessel_k(double x, double alpha, double expo) |
45 | 0 | { |
46 | 0 | double *bk; |
47 | 0 | #ifndef MATHLIB_STANDALONE |
48 | 0 | const void *vmax; |
49 | 0 | #endif |
50 | |
|
51 | 0 | #ifdef IEEE_754 |
52 | | /* NaNs propagated correctly */ |
53 | 0 | if (ISNAN(x) || ISNAN(alpha)) return x + alpha; |
54 | 0 | #endif |
55 | 0 | if (x < 0) { |
56 | 0 | ML_WARNING(ME_RANGE, "bessel_k"); |
57 | 0 | return ML_NAN; |
58 | 0 | } |
59 | 0 | if(alpha < 0) |
60 | 0 | alpha = -alpha; |
61 | 0 | if (alpha > max_alpha_k) { // NB: same bound in math_3B() [ ../main/arithmetic.c ] |
62 | 0 | MATHLIB_WARNING2(_("besselK(x, nu): |nu|=%g > max_alpha_k (= %g): too large for bessel_k() algorithm"), |
63 | 0 | alpha, max_alpha_k); |
64 | | // FIXME? asymptotic formula (w/ potential accuracy loss) would be better than NaN |
65 | 0 | return ML_NAN; |
66 | 0 | } |
67 | 0 | int ncalc, ize = (int)expo, nb = 1+ (int)floor(alpha);/* nb-1 <= |alpha| < nb */ |
68 | 0 | alpha -= (double)(nb-1); |
69 | | #ifdef MATHLIB_STANDALONE |
70 | | bk = (double *) calloc(nb, sizeof(double)); |
71 | | if (!bk) MATHLIB_ERROR("%s", _("bessel_k allocation error")); |
72 | | #else |
73 | 0 | vmax = vmaxget(); |
74 | 0 | bk = (double *) R_alloc((size_t) nb, sizeof(double)); |
75 | 0 | #endif |
76 | 0 | K_bessel(&x, &alpha, &nb, &ize, bk, &ncalc); |
77 | 0 | if(ncalc != nb) {/* error input */ |
78 | 0 | if(ncalc < 0) |
79 | 0 | MATHLIB_WARNING4(_("bessel_k(%g): ncalc (=%d) != nb (=%d); alpha=%g. Arg. out of range?\n"), |
80 | 0 | x, ncalc, nb, alpha); |
81 | 0 | else |
82 | 0 | MATHLIB_WARNING2(_("bessel_k(%g,nu=%g): precision lost in result\n"), |
83 | 0 | x, alpha+(double)nb-1); |
84 | 0 | } |
85 | 0 | x = bk[nb-1]; |
86 | | #ifdef MATHLIB_STANDALONE |
87 | | free(bk); |
88 | | #else |
89 | 0 | vmaxset(vmax); |
90 | 0 | #endif |
91 | 0 | return x; |
92 | 0 | } |
93 | | |
94 | | /* modified version of bessel_k() accepting a work array instead of allocating one. |
95 | | called from R via Math3B() in ../main/arithmetic.c */ |
96 | | double bessel_k_ex(double x, double alpha, double expo, double *bk) |
97 | 0 | { |
98 | 0 | #ifdef IEEE_754 |
99 | | /* NaNs propagated correctly */ |
100 | 0 | if (ISNAN(x) || ISNAN(alpha)) return x + alpha; |
101 | 0 | #endif |
102 | 0 | if (x < 0) { |
103 | 0 | ML_WARNING(ME_RANGE, "bessel_k"); |
104 | 0 | return ML_NAN; |
105 | 0 | } |
106 | 0 | if(alpha < 0) |
107 | 0 | alpha = -alpha; |
108 | 0 | if (alpha > max_alpha_k) { // NB: same bound in math_3B() [ ../main/arithmetic.c ] |
109 | 0 | MATHLIB_WARNING2(_("besselK(x, nu): |nu|=%g > max_alpha_k (= %g): too large for bessel_k() algorithm"), |
110 | 0 | alpha, max_alpha_k); |
111 | | // FIXME? asymptotic formula (w/ potential accuracy loss) would be better than NaN |
112 | 0 | return ML_NAN; |
113 | 0 | } |
114 | 0 | int ncalc, ize = (int)expo, nb = 1+ (int)floor(alpha);/* nb-1 <= |alpha| < nb */ |
115 | 0 | alpha -= (double)(nb-1); |
116 | 0 | K_bessel(&x, &alpha, &nb, &ize, bk, &ncalc); |
117 | 0 | if(ncalc != nb) {/* error input */ |
118 | 0 | if(ncalc < 0) |
119 | 0 | MATHLIB_WARNING4(_("bessel_k(%g): ncalc (=%d) != nb (=%d); alpha=%g. Arg. out of range?\n"), |
120 | 0 | x, ncalc, nb, alpha); |
121 | 0 | else |
122 | 0 | MATHLIB_WARNING2(_("bessel_k(%g,nu=%g): precision lost in result\n"), |
123 | 0 | x, alpha+(double)nb-1); |
124 | 0 | } |
125 | 0 | x = bk[nb-1]; |
126 | 0 | return x; |
127 | 0 | } |
128 | | |
129 | | static void K_bessel(double *x, double *alpha, int *nb, |
130 | | int *ize, double *bk, int *ncalc) |
131 | 0 | { |
132 | | /*------------------------------------------------------------------- |
133 | | |
134 | | This routine calculates modified Bessel functions |
135 | | of the third kind, K_(N+ALPHA) (X), for non-negative |
136 | | argument X, and non-negative order N+ALPHA, with or without |
137 | | exponential scaling. |
138 | | |
139 | | Explanation of variables in the calling sequence |
140 | | |
141 | | X - Non-negative argument for which |
142 | | K's or exponentially scaled K's (K*EXP(X)) |
143 | | are to be calculated. If K's are to be calculated, |
144 | | X must not be greater than XMAX_BESS_K. |
145 | | ALPHA - Fractional part of order for which |
146 | | K's or exponentially scaled K's (K*EXP(X)) are |
147 | | to be calculated. 0 <= ALPHA < 1.0. |
148 | | NB - Number of functions to be calculated, NB > 0. |
149 | | The first function calculated is of order ALPHA, and the |
150 | | last is of order (NB - 1 + ALPHA). |
151 | | IZE - Type. IZE = 1 if unscaled K's are to be calculated, |
152 | | = 2 if exponentially scaled K's are to be calculated. |
153 | | BK - Output vector of length NB. If the |
154 | | routine terminates normally (NCALC=NB), the vector BK |
155 | | contains the functions K(ALPHA,X), ... , K(NB-1+ALPHA,X), |
156 | | or the corresponding exponentially scaled functions. |
157 | | If (0 < NCALC < NB), BK(I) contains correct function |
158 | | values for I <= NCALC, and contains the ratios |
159 | | K(ALPHA+I-1,X)/K(ALPHA+I-2,X) for the rest of the array. |
160 | | NCALC - Output variable indicating possible errors. |
161 | | Before using the vector BK, the user should check that |
162 | | NCALC=NB, i.e., all orders have been calculated to |
163 | | the desired accuracy. See error returns below. |
164 | | |
165 | | |
166 | | ******************************************************************* |
167 | | |
168 | | Error returns |
169 | | |
170 | | In case of an error, NCALC != NB, and not all K's are |
171 | | calculated to the desired accuracy. |
172 | | |
173 | | NCALC < -1: An argument is out of range. For example, |
174 | | NB <= 0, IZE is not 1 or 2, or IZE=1 and ABS(X) >= XMAX_BESS_K. |
175 | | In this case, the B-vector is not calculated, |
176 | | and NCALC is set to MIN0(NB,0)-2 so that NCALC != NB. |
177 | | NCALC = -1: Either K(ALPHA,X) >= XINF or |
178 | | K(ALPHA+NB-1,X)/K(ALPHA+NB-2,X) >= XINF. In this case, |
179 | | the B-vector is not calculated. Note that again |
180 | | NCALC != NB. |
181 | | |
182 | | 0 < NCALC < NB: Not all requested function values could |
183 | | be calculated accurately. BK(I) contains correct function |
184 | | values for I <= NCALC, and contains the ratios |
185 | | K(ALPHA+I-1,X)/K(ALPHA+I-2,X) for the rest of the array. |
186 | | |
187 | | |
188 | | Intrinsic functions required are: |
189 | | |
190 | | ABS, AINT, EXP, INT, LOG, MAX, MIN, SINH, SQRT |
191 | | |
192 | | |
193 | | Acknowledgement |
194 | | |
195 | | This program is based on a program written by J. B. Campbell |
196 | | (2) that computes values of the Bessel functions K of float |
197 | | argument and float order. Modifications include the addition |
198 | | of non-scaled functions, parameterization of machine |
199 | | dependencies, and the use of more accurate approximations |
200 | | for SINH and SIN. |
201 | | |
202 | | References: "On Temme's Algorithm for the Modified Bessel |
203 | | Functions of the Third Kind," Campbell, J. B., |
204 | | TOMS 6(4), Dec. 1980, pp. 581-586. |
205 | | |
206 | | "A FORTRAN IV Subroutine for the Modified Bessel |
207 | | Functions of the Third Kind of Real Order and Real |
208 | | Argument," Campbell, J. B., Report NRC/ERB-925, |
209 | | National Research Council, Canada. |
210 | | |
211 | | Latest modification: May 30, 1989 |
212 | | |
213 | | Modified by: W. J. Cody and L. Stoltz |
214 | | Applied Mathematics Division |
215 | | Argonne National Laboratory |
216 | | Argonne, IL 60439 |
217 | | |
218 | | ------------------------------------------------------------------- |
219 | | */ |
220 | | /*--------------------------------------------------------------------- |
221 | | * Mathematical constants |
222 | | * A = LOG(2) - Euler's constant |
223 | | * D = SQRT(2/PI) |
224 | | ---------------------------------------------------------------------*/ |
225 | 0 | const static double a = .11593151565841244881; |
226 | | |
227 | | /*--------------------------------------------------------------------- |
228 | | P, Q - Approximation for LOG(GAMMA(1+ALPHA))/ALPHA + Euler's constant |
229 | | Coefficients converted from hex to decimal and modified |
230 | | by W. J. Cody, 2/26/82 */ |
231 | 0 | const static double p[8] = { .805629875690432845,20.4045500205365151, |
232 | 0 | 157.705605106676174,536.671116469207504,900.382759291288778, |
233 | 0 | 730.923886650660393,229.299301509425145,.822467033424113231 }; |
234 | 0 | const static double q[7] = { 29.4601986247850434,277.577868510221208, |
235 | 0 | 1206.70325591027438,2762.91444159791519,3443.74050506564618, |
236 | 0 | 2210.63190113378647,572.267338359892221 }; |
237 | | /* R, S - Approximation for (1-ALPHA*PI/SIN(ALPHA*PI))/(2.D0*ALPHA) */ |
238 | 0 | const static double r[5] = { -.48672575865218401848,13.079485869097804016, |
239 | 0 | -101.96490580880537526,347.65409106507813131, |
240 | 0 | 3.495898124521934782e-4 }; |
241 | 0 | const static double s[4] = { -25.579105509976461286,212.57260432226544008, |
242 | 0 | -610.69018684944109624,422.69668805777760407 }; |
243 | | /* T - Approximation for SINH(Y)/Y */ |
244 | 0 | const static double t[6] = { 1.6125990452916363814e-10, |
245 | 0 | 2.5051878502858255354e-8,2.7557319615147964774e-6, |
246 | 0 | 1.9841269840928373686e-4,.0083333333333334751799, |
247 | 0 | .16666666666666666446 }; |
248 | | /*---------------------------------------------------------------------*/ |
249 | 0 | const static double estm[6] = { 52.0583,5.7607,2.7782,14.4303,185.3004, 9.3715 }; |
250 | 0 | const static double estf[7] = { 41.8341,7.1075,6.4306,42.511,1.35633,84.5096,20.}; |
251 | | |
252 | | /* Local variables */ |
253 | 0 | int iend, i, j, k, m, ii, mplus1; |
254 | 0 | double x2by4, twox, c, blpha, ratio, wminf; |
255 | 0 | double d1, d2, d3, f0, f1, f2, p0, q0, t1, t2, twonu; |
256 | 0 | double dm, ex, bk1, bk2, nu; |
257 | |
|
258 | 0 | ii = 0; /* -Wall */ |
259 | |
|
260 | 0 | ex = *x; |
261 | 0 | nu = *alpha; |
262 | 0 | *ncalc = min0(*nb,0) - 2; |
263 | 0 | if (*nb > 0 && (0. <= nu && nu < 1.) && (1 <= *ize && *ize <= 2)) { |
264 | 0 | if(ex <= 0 || (*ize == 1 && ex > xmax_BESS_K)) { |
265 | 0 | if(ex <= 0) { |
266 | 0 | if(ex < 0) ML_WARNING(ME_RANGE, "K_bessel"); |
267 | 0 | for(i=0; i < *nb; i++) |
268 | 0 | bk[i] = ML_POSINF; |
269 | 0 | } else /* would only have underflow */ |
270 | 0 | for(i=0; i < *nb; i++) |
271 | 0 | bk[i] = 0.; |
272 | 0 | *ncalc = *nb; |
273 | 0 | return; |
274 | 0 | } |
275 | 0 | k = 0; |
276 | 0 | if (nu < sqxmin_BESS_K) { |
277 | 0 | nu = 0.; |
278 | 0 | } else if (nu > .5) { |
279 | 0 | k = 1; |
280 | 0 | nu -= 1.; |
281 | 0 | } |
282 | 0 | twonu = nu + nu; |
283 | 0 | iend = *nb + k - 1; |
284 | 0 | c = nu * nu; |
285 | 0 | d3 = -c; |
286 | 0 | if (ex <= 1.) { |
287 | | /* ------------------------------------------------------------ |
288 | | Calculation of P0 = GAMMA(1+ALPHA) * (2/X)**ALPHA |
289 | | Q0 = GAMMA(1-ALPHA) * (X/2)**ALPHA |
290 | | ------------------------------------------------------------ */ |
291 | 0 | d1 = 0.; d2 = p[0]; |
292 | 0 | t1 = 1.; t2 = q[0]; |
293 | 0 | for (i = 2; i <= 7; i += 2) { |
294 | 0 | d1 = c * d1 + p[i - 1]; |
295 | 0 | d2 = c * d2 + p[i]; |
296 | 0 | t1 = c * t1 + q[i - 1]; |
297 | 0 | t2 = c * t2 + q[i]; |
298 | 0 | } |
299 | 0 | d1 = nu * d1; |
300 | 0 | t1 = nu * t1; |
301 | 0 | f1 = log(ex); |
302 | 0 | f0 = a + nu * (p[7] - nu * (d1 + d2) / (t1 + t2)) - f1; |
303 | 0 | q0 = exp(-nu * (a - nu * (p[7] + nu * (d1-d2) / (t1-t2)) - f1)); |
304 | 0 | f1 = nu * f0; |
305 | 0 | p0 = exp(f1); |
306 | | /* ----------------------------------------------------------- |
307 | | Calculation of F0 = |
308 | | ----------------------------------------------------------- */ |
309 | 0 | d1 = r[4]; |
310 | 0 | t1 = 1.; |
311 | 0 | for (i = 0; i < 4; ++i) { |
312 | 0 | d1 = c * d1 + r[i]; |
313 | 0 | t1 = c * t1 + s[i]; |
314 | 0 | } |
315 | | /* d2 := sinh(f1)/ nu = sinh(f1)/(f1/f0) |
316 | | * = f0 * sinh(f1)/f1 */ |
317 | 0 | if (fabs(f1) <= .5) { |
318 | 0 | f1 *= f1; |
319 | 0 | d2 = 0.; |
320 | 0 | for (i = 0; i < 6; ++i) { |
321 | 0 | d2 = f1 * d2 + t[i]; |
322 | 0 | } |
323 | 0 | d2 = f0 + f0 * f1 * d2; |
324 | 0 | } else { |
325 | 0 | d2 = sinh(f1) / nu; |
326 | 0 | } |
327 | 0 | f0 = d2 - nu * d1 / (t1 * p0); |
328 | 0 | if (ex <= 1e-10) { |
329 | | /* --------------------------------------------------------- |
330 | | X <= 1.0E-10 |
331 | | Calculation of K(ALPHA,X) and X*K(ALPHA+1,X)/K(ALPHA,X) |
332 | | --------------------------------------------------------- */ |
333 | 0 | bk[0] = f0 + ex * f0; |
334 | 0 | if (*ize == 1) { |
335 | 0 | bk[0] -= ex * bk[0]; |
336 | 0 | } |
337 | 0 | ratio = p0 / f0; |
338 | 0 | c = ex * DBL_MAX; |
339 | 0 | if (k != 0) { |
340 | | /* --------------------------------------------------- |
341 | | Calculation of K(ALPHA,X) |
342 | | and X*K(ALPHA+1,X)/K(ALPHA,X), ALPHA >= 1/2 |
343 | | --------------------------------------------------- */ |
344 | 0 | *ncalc = -1; |
345 | 0 | if (bk[0] >= c / ratio) { |
346 | 0 | return; |
347 | 0 | } |
348 | 0 | bk[0] = ratio * bk[0] / ex; |
349 | 0 | twonu += 2.; |
350 | 0 | ratio = twonu; |
351 | 0 | } |
352 | 0 | *ncalc = 1; |
353 | 0 | if (*nb == 1) |
354 | 0 | return; |
355 | | |
356 | | /* ----------------------------------------------------- |
357 | | Calculate K(ALPHA+L,X)/K(ALPHA+L-1,X), |
358 | | L = 1, 2, ... , NB-1 |
359 | | ----------------------------------------------------- */ |
360 | 0 | *ncalc = -1; |
361 | 0 | for (i = 1; i < *nb; ++i) { |
362 | 0 | if (ratio >= c) |
363 | 0 | return; |
364 | | |
365 | 0 | bk[i] = ratio / ex; |
366 | 0 | twonu += 2.; |
367 | 0 | ratio = twonu; |
368 | 0 | } |
369 | 0 | *ncalc = 1; |
370 | 0 | goto L420; |
371 | 0 | } else { |
372 | | /* ------------------------------------------------------ |
373 | | 10^-10 < X <= 1.0 |
374 | | ------------------------------------------------------ */ |
375 | 0 | c = 1.; |
376 | 0 | x2by4 = ex * ex / 4.; |
377 | 0 | p0 = .5 * p0; |
378 | 0 | q0 = .5 * q0; |
379 | 0 | d1 = -1.; |
380 | 0 | d2 = 0.; |
381 | 0 | bk1 = 0.; |
382 | 0 | bk2 = 0.; |
383 | 0 | f1 = f0; |
384 | 0 | f2 = p0; |
385 | 0 | do { |
386 | 0 | d1 += 2.; |
387 | 0 | d2 += 1.; |
388 | 0 | d3 = d1 + d3; |
389 | 0 | c = x2by4 * c / d2; |
390 | 0 | f0 = (d2 * f0 + p0 + q0) / d3; |
391 | 0 | p0 /= d2 - nu; |
392 | 0 | q0 /= d2 + nu; |
393 | 0 | t1 = c * f0; |
394 | 0 | t2 = c * (p0 - d2 * f0); |
395 | 0 | bk1 += t1; |
396 | 0 | bk2 += t2; |
397 | 0 | } while (fabs(t1 / (f1 + bk1)) > DBL_EPSILON || |
398 | 0 | fabs(t2 / (f2 + bk2)) > DBL_EPSILON); |
399 | 0 | bk1 = f1 + bk1; |
400 | 0 | bk2 = 2. * (f2 + bk2) / ex; |
401 | 0 | if (*ize == 2) { |
402 | 0 | d1 = exp(ex); |
403 | 0 | bk1 *= d1; |
404 | 0 | bk2 *= d1; |
405 | 0 | } |
406 | 0 | wminf = estf[0] * ex + estf[1]; |
407 | 0 | } |
408 | 0 | } else if (DBL_EPSILON * ex > 1.) { |
409 | | /* ------------------------------------------------- |
410 | | X > 1./EPS |
411 | | ------------------------------------------------- */ |
412 | 0 | *ncalc = *nb; |
413 | 0 | bk1 = 1. / (M_SQRT_2dPI * sqrt(ex)); |
414 | 0 | for (i = 0; i < *nb; ++i) |
415 | 0 | bk[i] = bk1; |
416 | 0 | return; |
417 | |
|
418 | 0 | } else { |
419 | | /* ------------------------------------------------------- |
420 | | X > 1.0 |
421 | | ------------------------------------------------------- */ |
422 | 0 | twox = ex + ex; |
423 | 0 | blpha = 0.; |
424 | 0 | ratio = 0.; |
425 | 0 | if (ex <= 4.) { |
426 | | /* ---------------------------------------------------------- |
427 | | Calculation of K(ALPHA+1,X)/K(ALPHA,X), 1.0 <= X <= 4.0 |
428 | | ----------------------------------------------------------*/ |
429 | 0 | d2 = trunc(estm[0] / ex + estm[1]); |
430 | 0 | m = (int) d2; |
431 | 0 | d1 = d2 + d2; |
432 | 0 | d2 -= .5; |
433 | 0 | d2 *= d2; |
434 | 0 | for (i = 2; i <= m; ++i) { |
435 | 0 | d1 -= 2.; |
436 | 0 | d2 -= d1; |
437 | 0 | ratio = (d3 + d2) / (twox + d1 - ratio); |
438 | 0 | } |
439 | | /* ----------------------------------------------------------- |
440 | | Calculation of I(|ALPHA|,X) and I(|ALPHA|+1,X) by backward |
441 | | recurrence and K(ALPHA,X) from the wronskian |
442 | | -----------------------------------------------------------*/ |
443 | 0 | d2 = trunc(estm[2] * ex + estm[3]); |
444 | 0 | m = (int) d2; |
445 | 0 | c = fabs(nu); |
446 | 0 | d3 = c + c; |
447 | 0 | d1 = d3 - 1.; |
448 | 0 | f1 = DBL_MIN; |
449 | 0 | f0 = (2. * (c + d2) / ex + .5 * ex / (c + d2 + 1.)) * DBL_MIN; |
450 | 0 | for (i = 3; i <= m; ++i) { |
451 | 0 | d2 -= 1.; |
452 | 0 | f2 = (d3 + d2 + d2) * f0; |
453 | 0 | blpha = (1. + d1 / d2) * (f2 + blpha); |
454 | 0 | f2 = f2 / ex + f1; |
455 | 0 | f1 = f0; |
456 | 0 | f0 = f2; |
457 | 0 | } |
458 | 0 | f1 = (d3 + 2.) * f0 / ex + f1; |
459 | 0 | d1 = 0.; |
460 | 0 | t1 = 1.; |
461 | 0 | for (i = 1; i <= 7; ++i) { |
462 | 0 | d1 = c * d1 + p[i - 1]; |
463 | 0 | t1 = c * t1 + q[i - 1]; |
464 | 0 | } |
465 | 0 | p0 = exp(c * (a + c * (p[7] - c * d1 / t1) - log(ex))) / ex; |
466 | 0 | f2 = (c + .5 - ratio) * f1 / ex; |
467 | 0 | bk1 = p0 + (d3 * f0 - f2 + f0 + blpha) / (f2 + f1 + f0) * p0; |
468 | 0 | if (*ize == 1) { |
469 | 0 | bk1 *= exp(-ex); |
470 | 0 | } |
471 | 0 | wminf = estf[2] * ex + estf[3]; |
472 | 0 | } else { |
473 | | /* --------------------------------------------------------- |
474 | | Calculation of K(ALPHA,X) and K(ALPHA+1,X)/K(ALPHA,X), by |
475 | | backward recurrence, for X > 4.0 |
476 | | ----------------------------------------------------------*/ |
477 | 0 | dm = trunc(estm[4] / ex + estm[5]); |
478 | 0 | m = (int) dm; |
479 | 0 | d2 = dm - .5; |
480 | 0 | d2 *= d2; |
481 | 0 | d1 = dm + dm; |
482 | 0 | for (i = 2; i <= m; ++i) { |
483 | 0 | dm -= 1.; |
484 | 0 | d1 -= 2.; |
485 | 0 | d2 -= d1; |
486 | 0 | ratio = (d3 + d2) / (twox + d1 - ratio); |
487 | 0 | blpha = (ratio + ratio * blpha) / dm; |
488 | 0 | } |
489 | 0 | bk1 = 1. / ((M_SQRT_2dPI + M_SQRT_2dPI * blpha) * sqrt(ex)); |
490 | 0 | if (*ize == 1) |
491 | 0 | bk1 *= exp(-ex); |
492 | 0 | wminf = estf[4] * (ex - fabs(ex - estf[6])) + estf[5]; |
493 | 0 | } |
494 | | /* --------------------------------------------------------- |
495 | | Calculation of K(ALPHA+1,X) |
496 | | from K(ALPHA,X) and K(ALPHA+1,X)/K(ALPHA,X) |
497 | | --------------------------------------------------------- */ |
498 | 0 | bk2 = bk1 + bk1 * (nu + .5 - ratio) / ex; |
499 | 0 | } |
500 | | /*-------------------------------------------------------------------- |
501 | | Calculation of 'NCALC', K(ALPHA+I,X), I = 0, 1, ... , NCALC-1, |
502 | | & K(ALPHA+I,X)/K(ALPHA+I-1,X), I = NCALC, NCALC+1, ... , NB-1 |
503 | | -------------------------------------------------------------------*/ |
504 | 0 | *ncalc = *nb; |
505 | 0 | bk[0] = bk1; |
506 | 0 | if (iend == 0) |
507 | 0 | return; |
508 | | |
509 | 0 | j = 1 - k; |
510 | 0 | if (j >= 0) |
511 | 0 | bk[j] = bk2; |
512 | |
|
513 | 0 | if (iend == 1) |
514 | 0 | return; |
515 | | |
516 | 0 | m = min0((int) (wminf - nu),iend); |
517 | 0 | for (i = 2; i <= m; ++i) { |
518 | 0 | t1 = bk1; |
519 | 0 | bk1 = bk2; |
520 | 0 | twonu += 2.; |
521 | 0 | if (ex < 1.) { |
522 | 0 | if (bk1 >= DBL_MAX / twonu * ex) |
523 | 0 | break; |
524 | 0 | } else { |
525 | 0 | if (bk1 / ex >= DBL_MAX / twonu) |
526 | 0 | break; |
527 | 0 | } |
528 | 0 | bk2 = twonu / ex * bk1 + t1; |
529 | 0 | ii = i; |
530 | 0 | ++j; |
531 | 0 | if (j >= 0) { |
532 | 0 | bk[j] = bk2; |
533 | 0 | } |
534 | 0 | } |
535 | |
|
536 | 0 | m = ii; |
537 | 0 | if (m == iend) { |
538 | 0 | return; |
539 | 0 | } |
540 | 0 | ratio = bk2 / bk1; |
541 | 0 | mplus1 = m + 1; |
542 | 0 | *ncalc = -1; |
543 | 0 | for (i = mplus1; i <= iend; ++i) { |
544 | 0 | twonu += 2.; |
545 | 0 | ratio = twonu / ex + 1./ratio; |
546 | 0 | ++j; |
547 | 0 | if (j >= 1) { |
548 | 0 | bk[j] = ratio; |
549 | 0 | } else { |
550 | 0 | if (bk2 >= DBL_MAX / ratio) |
551 | 0 | return; |
552 | | |
553 | 0 | bk2 *= ratio; |
554 | 0 | } |
555 | 0 | } |
556 | 0 | *ncalc = max0(1, mplus1 - k); |
557 | 0 | if (*ncalc == 1) |
558 | 0 | bk[0] = bk2; |
559 | 0 | if (*nb == 1) |
560 | 0 | return; |
561 | | |
562 | 0 | L420: |
563 | 0 | for (i = *ncalc; i < *nb; ++i) { /* i == *ncalc */ |
564 | | #ifndef IEEE_754 |
565 | | if (bk[i-1] >= DBL_MAX / bk[i]) |
566 | | return; |
567 | | #endif |
568 | 0 | bk[i] *= bk[i-1]; |
569 | 0 | (*ncalc)++; |
570 | 0 | } |
571 | 0 | } |
572 | 0 | } |