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.
20 /* Author: G. Jungman */
25 #include <gsl/gsl_math.h>
26 #include <gsl/gsl_test.h>
27 #include <gsl/gsl_sum.h>
29 #include <gsl/gsl_ieee_utils.h>
33 void check_trunc (double * t, double expected, const char * desc);
34 void check_full (double * t, double expected, const char * desc);
39 gsl_ieee_env_setup ();
45 const double zeta_2 = M_PI * M_PI / 6.0;
47 /* terms for zeta(2) */
49 for (n = 0; n < N; n++)
52 t[n] = 1.0 / (np1 * np1);
55 check_trunc (t, zeta_2, "zeta(2)");
56 check_full (t, zeta_2, "zeta(2)");
64 /* terms for exp(10.0) */
69 for (n = 1; n < N; n++)
71 t[n] = t[n - 1] * (x / n);
74 check_trunc (t, y, "exp(10)");
75 check_full (t, y, "exp(10)");
83 /* terms for exp(-10.0) */
88 for (n = 1; n < N; n++)
90 t[n] = t[n - 1] * (x / n);
93 check_trunc (t, y, "exp(-10)");
94 check_full (t, y, "exp(-10)");
102 /* terms for -log(1-x) */
106 for (n = 1; n < N; n++)
108 t[n] = t[n - 1] * (x * n) / (n + 1.0);
111 check_trunc (t, y, "-log(1/2)");
112 check_full (t, y, "-log(1/2)");
120 /* terms for -log(1-x) */
124 for (n = 1; n < N; n++)
126 t[n] = t[n - 1] * (x * n) / (n + 1.0);
129 check_trunc (t, y, "-log(2)");
130 check_full (t, y, "-log(2)");
137 double result = 0.192594048773;
139 /* terms for an alternating asymptotic series */
141 t[0] = 3.0 / (M_PI * M_PI);
143 for (n = 1; n < N; n++)
145 t[n] = -t[n - 1] * (4.0 * (n + 1.0) - 1.0) / (M_PI * M_PI);
148 check_trunc (t, result, "asymptotic series");
149 check_full (t, result, "asymptotic series");
156 /* Euler's gamma from GNU Calc (precision = 32) */
158 double result = 0.5772156649015328606065120900824;
160 /* terms for Euler's gamma */
164 for (n = 1; n < N; n++)
166 t[n] = 1/(n+1.0) + log(n/(n+1.0));
169 check_trunc (t, result, "Euler's constant");
170 check_full (t, result, "Euler's constant");
177 /* eta(1/2) = sum_{k=1}^{\infty} (-1)^(k+1) / sqrt(k)
179 From Levin, Intern. J. Computer Math. B3:371--388, 1973.
181 I=(1-sqrt(2))zeta(1/2)
182 =(2/sqrt(pi))*integ(1/(exp(x^2)+1),x,0,inf) */
184 double result = 0.6048986434216305; /* approx */
186 /* terms for eta(1/2) */
188 for (n = 0; n < N; n++)
190 t[n] = (n%2 ? -1 : 1) * 1.0 /sqrt(n + 1.0);
193 check_trunc (t, result, "eta(1/2)");
194 check_full (t, result, "eta(1/2)");
197 exit (gsl_test_summary ());
201 check_trunc (double * t, double expected, const char * desc)
203 double sum_accel, prec;
205 gsl_sum_levin_utrunc_workspace * w = gsl_sum_levin_utrunc_alloc (N);
207 gsl_sum_levin_utrunc_accel (t, N, w, &sum_accel, &prec);
208 gsl_test_rel (sum_accel, expected, 1e-8, "trunc result, %s", desc);
210 /* No need to check precision for truncated result since this is not
211 a meaningful number */
213 gsl_sum_levin_utrunc_free (w);
217 check_full (double * t, double expected, const char * desc)
219 double sum_accel, err_est, sd_actual, sd_est;
221 gsl_sum_levin_u_workspace * w = gsl_sum_levin_u_alloc (N);
223 gsl_sum_levin_u_accel (t, N, w, &sum_accel, &err_est);
224 gsl_test_rel (sum_accel, expected, 1e-8, "full result, %s", desc);
226 sd_est = -log10 (err_est/fabs(sum_accel));
227 sd_actual = -log10 (DBL_EPSILON + fabs ((sum_accel - expected)/expected));
229 /* Allow one digit of slop */
231 gsl_test (sd_est > sd_actual + 1.0, "full significant digits, %s (%g vs %g)", desc, sd_est, sd_actual);
233 gsl_sum_levin_u_free (w);