1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/* sum/test.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Author: G. Jungman */
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_test.h>
#include <gsl/gsl_sum.h>
#include <gsl/gsl_ieee_utils.h>
#define N 50
void check_trunc (double * t, double expected, const char * desc);
void check_full (double * t, double expected, const char * desc);
int
main (void)
{
gsl_ieee_env_setup ();
{
double t[N];
int n;
const double zeta_2 = M_PI * M_PI / 6.0;
/* terms for zeta(2) */
for (n = 0; n < N; n++)
{
double np1 = n + 1.0;
t[n] = 1.0 / (np1 * np1);
}
check_trunc (t, zeta_2, "zeta(2)");
check_full (t, zeta_2, "zeta(2)");
}
{
double t[N];
double x, y;
int n;
/* terms for exp(10.0) */
x = 10.0;
y = exp(x);
t[0] = 1.0;
for (n = 1; n < N; n++)
{
t[n] = t[n - 1] * (x / n);
}
check_trunc (t, y, "exp(10)");
check_full (t, y, "exp(10)");
}
{
double t[N];
double x, y;
int n;
/* terms for exp(-10.0) */
x = -10.0;
y = exp(x);
t[0] = 1.0;
for (n = 1; n < N; n++)
{
t[n] = t[n - 1] * (x / n);
}
check_trunc (t, y, "exp(-10)");
check_full (t, y, "exp(-10)");
}
{
double t[N];
double x, y;
int n;
/* terms for -log(1-x) */
x = 0.5;
y = -log(1-x);
t[0] = x;
for (n = 1; n < N; n++)
{
t[n] = t[n - 1] * (x * n) / (n + 1.0);
}
check_trunc (t, y, "-log(1/2)");
check_full (t, y, "-log(1/2)");
}
{
double t[N];
double x, y;
int n;
/* terms for -log(1-x) */
x = -1.0;
y = -log(1-x);
t[0] = x;
for (n = 1; n < N; n++)
{
t[n] = t[n - 1] * (x * n) / (n + 1.0);
}
check_trunc (t, y, "-log(2)");
check_full (t, y, "-log(2)");
}
{
double t[N];
int n;
double result = 0.192594048773;
/* terms for an alternating asymptotic series */
t[0] = 3.0 / (M_PI * M_PI);
for (n = 1; n < N; n++)
{
t[n] = -t[n - 1] * (4.0 * (n + 1.0) - 1.0) / (M_PI * M_PI);
}
check_trunc (t, result, "asymptotic series");
check_full (t, result, "asymptotic series");
}
{
double t[N];
int n;
/* Euler's gamma from GNU Calc (precision = 32) */
double result = 0.5772156649015328606065120900824;
/* terms for Euler's gamma */
t[0] = 1.0;
for (n = 1; n < N; n++)
{
t[n] = 1/(n+1.0) + log(n/(n+1.0));
}
check_trunc (t, result, "Euler's constant");
check_full (t, result, "Euler's constant");
}
{
double t[N];
int n;
/* eta(1/2) = sum_{k=1}^{\infty} (-1)^(k+1) / sqrt(k)
From Levin, Intern. J. Computer Math. B3:371--388, 1973.
I=(1-sqrt(2))zeta(1/2)
=(2/sqrt(pi))*integ(1/(exp(x^2)+1),x,0,inf) */
double result = 0.6048986434216305; /* approx */
/* terms for eta(1/2) */
for (n = 0; n < N; n++)
{
t[n] = (n%2 ? -1 : 1) * 1.0 /sqrt(n + 1.0);
}
check_trunc (t, result, "eta(1/2)");
check_full (t, result, "eta(1/2)");
}
{
double t[N];
int n;
double result = 1.23;
for (n = 0; n < N; n++)
{
t[n] = (n == 0) ? 1.23 : 0.0;
}
check_trunc (t, result, "1.23 + 0 + 0 + 0...");
check_full (t, result, "1.23 + 0 + 0 + 0...");
}
exit (gsl_test_summary ());
}
void
check_trunc (double * t, double expected, const char * desc)
{
double sum_accel, prec;
gsl_sum_levin_utrunc_workspace * w = gsl_sum_levin_utrunc_alloc (N);
gsl_sum_levin_utrunc_accel (t, N, w, &sum_accel, &prec);
gsl_test_rel (sum_accel, expected, 1e-8, "trunc result, %s", desc);
/* No need to check precision for truncated result since this is not
a meaningful number */
gsl_sum_levin_utrunc_free (w);
}
void
check_full (double * t, double expected, const char * desc)
{
double sum_accel, err_est, sd_actual, sd_est;
gsl_sum_levin_u_workspace * w = gsl_sum_levin_u_alloc (N);
gsl_sum_levin_u_accel (t, N, w, &sum_accel, &err_est);
gsl_test_rel (sum_accel, expected, 1e-8, "full result, %s", desc);
sd_est = -log10 (err_est/fabs(sum_accel) + GSL_DBL_EPSILON);
sd_actual = -log10 (DBL_EPSILON + fabs ((sum_accel - expected)/expected));
/* Allow one digit of slop */
gsl_test (sd_est > sd_actual + 1.0, "full significant digits, %s (%g vs %g)", desc, sd_est, sd_actual);
gsl_sum_levin_u_free (w);
}
|