3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
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.
21 #include <gsl/gsl_math.h>
22 #include <gsl/gsl_test.h>
23 #include <gsl/gsl_errno.h>
24 #include <gsl/gsl_sum.h>
27 gsl_sum_levin_u_accel (const double *array, const size_t array_size,
28 gsl_sum_levin_u_workspace * w,
29 double *sum_accel, double *abserr)
31 return gsl_sum_levin_u_minmax (array, array_size,
32 0, array_size - 1, w, sum_accel, abserr);
36 gsl_sum_levin_u_minmax (const double *array, const size_t array_size,
37 const size_t min_terms, const size_t max_terms,
38 gsl_sum_levin_u_workspace * w,
39 double *sum_accel, double *abserr)
49 else if (array_size == 1)
51 *sum_accel = array[0];
53 w->sum_plain = array[0];
59 const double SMALL = 0.01;
60 const size_t nmax = GSL_MAX (max_terms, array_size) - 1;
61 double noise_n = 0.0, noise_nm1 = 0.0;
62 double trunc_n = 0.0, trunc_nm1 = 0.0;
63 double actual_trunc_n = 0.0, actual_trunc_nm1 = 0.0;
64 double result_n = 0.0, result_nm1 = 0.0;
71 double least_trunc = GSL_DBL_MAX;
72 double least_trunc_noise = GSL_DBL_MAX;
73 double least_trunc_result;
75 /* Calculate specified minimum number of terms. No convergence
76 tests are made, and no truncation information is stored. */
78 for (n = 0; n < min_terms; n++)
80 const double t = array[n];
81 result_nm1 = result_n;
82 gsl_sum_levin_u_step (t, n, nmax, w, &result_n);
85 least_trunc_result = result_n;
88 for (i = 0; i < n; i++)
90 double dn = w->dsum[i] * GSL_MACH_EPS * array[i];
93 noise_n = sqrt (variance);
95 /* Calculate up to maximum number of terms. Check truncation
98 for (; n <= nmax; n++)
100 const double t = array[n];
102 result_nm1 = result_n;
103 gsl_sum_levin_u_step (t, n, nmax, w, &result_n);
105 /* Compute the truncation error directly */
107 actual_trunc_nm1 = actual_trunc_n;
108 actual_trunc_n = fabs (result_n - result_nm1);
110 /* Average results to make a more reliable estimate of the
111 real truncation error */
114 trunc_n = 0.5 * (actual_trunc_n + actual_trunc_nm1);
119 for (i = 0; i <= n; i++)
121 double dn = w->dsum[i] * GSL_MACH_EPS * array[i];
125 noise_n = sqrt (variance);
127 /* Determine if we are in the convergence region. */
129 better = (trunc_n < trunc_nm1 || trunc_n < SMALL * fabs (result_n));
130 converging = converging || (better && before);
135 if (trunc_n < least_trunc)
137 /* Found a low truncation point in the convergence
140 least_trunc_result = result_n;
141 least_trunc = trunc_n;
142 least_trunc_noise = noise_n;
145 if (noise_n > trunc_n / 3.0)
148 if (trunc_n < 10.0 * GSL_MACH_EPS * fabs (result_n))
156 /* Stopped in the convergence region. Return result and
159 *sum_accel = least_trunc_result;
160 *abserr = GSL_MAX_DBL (least_trunc, least_trunc_noise);
166 /* Never reached the convergence region. Use the last
167 calculated values. */
169 *sum_accel = result_n;
170 *abserr = GSL_MAX_DBL (trunc_n, noise_n);
179 gsl_sum_levin_u_step (const double term, const size_t n, const size_t nmax,
180 gsl_sum_levin_u_workspace * w, double *sum_accel)
183 #define I(i,j) ((i)*(nmax+1) + (j))
190 w->q_den[0] = 1.0 / term;
193 w->dq_den[I (0, 0)] = -1.0 / (term * term);
194 w->dq_num[I (0, 0)] = 0.0;
204 double ratio = (double) n / (n + 1.0);
208 w->sum_plain += term;
210 w->q_den[n] = 1.0 / (term * (n + 1.0) * (n + 1.0));
211 w->q_num[n] = w->sum_plain * w->q_den[n];
213 for (i = 0; i < n; i++)
215 w->dq_den[I (i, n)] = 0;
216 w->dq_num[I (i, n)] = w->q_den[n];
219 w->dq_den[I (n, n)] = -w->q_den[n] / term;
220 w->dq_num[I (n, n)] =
221 w->q_den[n] + w->sum_plain * (w->dq_den[I (n, n)]);
223 for (j = n - 1; j >= 0; j--)
225 double c = factor * (j + 1) / (n + 1);
227 w->q_den[j] = w->q_den[j + 1] - c * w->q_den[j];
228 w->q_num[j] = w->q_num[j + 1] - c * w->q_num[j];
230 for (i = 0; i < n; i++)
232 w->dq_den[I (i, j)] =
233 w->dq_den[I (i, j + 1)] - c * w->dq_den[I (i, j)];
234 w->dq_num[I (i, j)] =
235 w->dq_num[I (i, j + 1)] - c * w->dq_num[I (i, j)];
238 w->dq_den[I (n, j)] = w->dq_den[I (n, j + 1)];
239 w->dq_num[I (n, j)] = w->dq_num[I (n, j + 1)];
242 result = w->q_num[0] / w->q_den[0];
246 for (i = 0; i <= n; i++)
249 (w->dq_num[I (i, 0)] -
250 result * w->dq_den[I (i, 0)]) / w->q_den[0];