3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 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.
23 #include <gsl/gsl_histogram.h>
24 #include <gsl/gsl_test.h>
25 #include <gsl/gsl_ieee_utils.h>
34 {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
36 gsl_histogram *h, *h1, *hr, *g;
39 gsl_ieee_env_setup ();
41 h = gsl_histogram_calloc (N);
42 h1 = gsl_histogram_calloc (N);
43 g = gsl_histogram_calloc (N);
45 gsl_test (h->range == 0, "gsl_histogram_alloc returns valid range pointer");
46 gsl_test (h->bin == 0, "gsl_histogram_alloc returns valid bin pointer");
47 gsl_test (h->n != N, "gsl_histogram_alloc returns valid size");
50 hr = gsl_histogram_calloc_range (NR, xr);
52 gsl_test (hr->range == 0, "gsl_histogram_calloc_range returns valid range pointer");
53 gsl_test (hr->bin == 0, "gsl_histogram_calloc_range returns valid bin pointer");
54 gsl_test (hr->n != NR, "gsl_histogram_calloc_range returns valid size");
58 for (i = 0; i <= NR; i++)
60 if (hr->range[i] != xr[i])
66 gsl_test (status, "gsl_histogram_calloc_range creates range");
69 for (i = 0; i <= NR; i++)
75 int status = gsl_histogram_set_ranges (hr, xr, NR+1);
77 for (i = 0; i <= NR; i++)
79 if (hr->range[i] != xr[i])
85 gsl_test (status, "gsl_histogram_set_range sets range");
89 for (i = 0; i < N; i++)
91 gsl_histogram_accumulate (h, (double) i, (double) i);
97 for (i = 0; i < N; i++)
99 if (h->bin[i] != (double) i)
105 gsl_test (status, "gsl_histogram_accumulate writes into array");
112 for (i = 0; i < N; i++)
114 if (gsl_histogram_get (h, i) != i)
117 gsl_test (status, "gsl_histogram_get reads from array");
120 for (i = 0; i <= N; i++)
122 h1->range[i] = 100.0 + i;
125 gsl_histogram_memcpy (h1, h);
129 for (i = 0; i <= N; i++)
131 if (h1->range[i] != h->range[i])
134 gsl_test (status, "gsl_histogram_memcpy copies bin ranges");
139 for (i = 0; i < N; i++)
141 if (gsl_histogram_get (h1, i) != gsl_histogram_get (h, i))
144 gsl_test (status, "gsl_histogram_memcpy copies bin values");
147 gsl_histogram_free (h1);
149 h1 = gsl_histogram_clone (h);
153 for (i = 0; i <= N; i++)
155 if (h1->range[i] != h->range[i])
158 gsl_test (status, "gsl_histogram_clone copies bin ranges");
163 for (i = 0; i < N; i++)
165 if (gsl_histogram_get (h1, i) != gsl_histogram_get (h, i))
168 gsl_test (status, "gsl_histogram_clone copies bin values");
171 gsl_histogram_reset (h);
176 for (i = 0; i < N; i++)
181 gsl_test (status, "gsl_histogram_reset zeros array");
188 for (i = 0; i < N; i++)
190 gsl_histogram_increment (h, (double) i);
192 for (j = 0; j <= i; j++)
200 for (j = i + 1; j < N; j++)
209 gsl_test (status, "gsl_histogram_increment increases bin value");
214 for (i = 0; i < N; i++)
216 double x0 = 0, x1 = 0;
218 gsl_histogram_get_range (h, i, &x0, &x1);
220 if (x0 != i || x1 != i + 1)
225 gsl_test (status, "gsl_histogram_getbinrange returns bin range");
230 if (gsl_histogram_max (h) != N)
232 gsl_test (status, "gsl_histogram_max returns maximum");
237 if (gsl_histogram_min (h) != 0)
239 gsl_test (status, "gsl_histogram_min returns minimum");
244 if (gsl_histogram_bins (h) != N)
246 gsl_test (status, "gsl_histogram_bins returns number of bins");
249 h->bin[2] = 123456.0;
253 double max = gsl_histogram_max_val (h);
254 gsl_test (max != 123456.0, "gsl_histogram_max_val finds maximum value");
258 double min = gsl_histogram_min_val (h);
259 gsl_test (min != -654321.0, "gsl_histogram_min_val finds minimum value");
263 size_t imax = gsl_histogram_max_bin (h);
264 gsl_test (imax != 2, "gsl_histogram_max_bin finds maximum value bin");
268 size_t imin = gsl_histogram_min_bin (h);
269 gsl_test (imin != 4, "gsl_histogram_min_bin find minimum value bin");
272 for (i = 0; i < N; i++)
275 g->bin[i] = (i + 27) * (i + 1);
279 double sum=gsl_histogram_sum (h);
280 gsl_test(sum != N*27+((N-1)*N)/2, "gsl_histogram_sum sums all bin values");
283 gsl_histogram_memcpy (h1, g);
284 gsl_histogram_add (h1, h);
288 for (i = 0; i < N; i++)
290 if (h1->bin[i] != g->bin[i] + h->bin[i])
293 gsl_test (status, "gsl_histogram_add histogram addition");
296 gsl_histogram_memcpy (h1, g);
297 gsl_histogram_sub (h1, h);
301 for (i = 0; i < N; i++)
303 if (h1->bin[i] != g->bin[i] - h->bin[i])
306 gsl_test (status, "gsl_histogram_sub histogram subtraction");
310 gsl_histogram_memcpy (h1, g);
311 gsl_histogram_mul (h1, h);
315 for (i = 0; i < N; i++)
317 if (h1->bin[i] != g->bin[i] * h->bin[i])
320 gsl_test (status, "gsl_histogram_mul histogram multiplication");
324 gsl_histogram_memcpy (h1, g);
325 gsl_histogram_div (h1, h);
329 for (i = 0; i < N; i++)
331 if (h1->bin[i] != g->bin[i] / h->bin[i])
334 gsl_test (status, "gsl_histogram_div histogram division");
337 gsl_histogram_memcpy (h1, g);
338 gsl_histogram_scale (h1, 0.5);
342 for (i = 0; i < N; i++)
344 if (h1->bin[i] != 0.5 * g->bin[i])
347 gsl_test (status, "gsl_histogram_scale histogram scaling");
350 gsl_histogram_memcpy (h1, g);
351 gsl_histogram_shift (h1, 0.25);
355 for (i = 0; i < N; i++)
357 if (h1->bin[i] != 0.25 + g->bin[i])
360 gsl_test (status, "gsl_histogram_shift histogram shift");
364 gsl_histogram_free (h); /* free whatever is in h */
366 h = gsl_histogram_calloc_uniform (N, 0.0, 1.0);
368 gsl_test (h->range == 0,
369 "gsl_histogram_calloc_uniform returns valid range pointer");
370 gsl_test (h->bin == 0,
371 "gsl_histogram_calloc_uniform returns valid bin pointer");
373 "gsl_histogram_calloc_uniform returns valid size");
375 gsl_histogram_accumulate (h, 0.0, 1.0);
376 gsl_histogram_accumulate (h, 0.1, 2.0);
377 gsl_histogram_accumulate (h, 0.2, 3.0);
378 gsl_histogram_accumulate (h, 0.3, 4.0);
381 size_t i1, i2, i3, i4;
383 int status = gsl_histogram_find (h, 0.0, &i1);
384 status = gsl_histogram_find (h, 0.1, &i2);
385 status = gsl_histogram_find (h, 0.2, &i3);
386 status = gsl_histogram_find (h, 0.3, &i4);
388 for (i = 0; i < N; i++)
411 if (h->bin[i] != expected)
417 gsl_test (status, "gsl_histogram_find returns index");
422 FILE *f = fopen ("test.txt", "w");
423 gsl_histogram_fprintf (f, h, "%.19e", "%.19e");
428 FILE *f = fopen ("test.txt", "r");
429 gsl_histogram *hh = gsl_histogram_calloc (N);
432 gsl_histogram_fscanf (f, hh);
434 for (i = 0; i < N; i++)
436 if (h->range[i] != hh->range[i])
438 if (h->bin[i] != hh->bin[i])
441 if (h->range[N] != hh->range[N])
444 gsl_test (status, "gsl_histogram_fprintf and fscanf");
446 gsl_histogram_free (hh);
451 FILE *f = fopen ("test.dat", "wb");
452 gsl_histogram_fwrite (f, h);
457 FILE *f = fopen ("test.dat", "rb");
458 gsl_histogram *hh = gsl_histogram_calloc (N);
461 gsl_histogram_fread (f, hh);
463 for (i = 0; i < N; i++)
465 if (h->range[i] != hh->range[i])
467 if (h->bin[i] != hh->bin[i])
470 if (h->range[N] != hh->range[N])
473 gsl_test (status, "gsl_histogram_fwrite and fread");
475 gsl_histogram_free (hh);
479 gsl_histogram_free (h);
480 gsl_histogram_free (g);
481 gsl_histogram_free (h1);
482 gsl_histogram_free (hr);