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_errno.h>
24 #include <gsl/gsl_math.h>
25 #include <gsl/gsl_machine.h>
26 #include <gsl/gsl_histogram2d.h>
27 #include <gsl/gsl_test.h>
28 #include <gsl/gsl_ieee_utils.h>
41 { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
43 double yr[NR + 1] = { 90.0, 91.0, 92.0, 93.0, 94.0, 95.0 };
45 gsl_histogram2d *h, *h1, *g, *hr;
48 gsl_ieee_env_setup ();
50 h = gsl_histogram2d_calloc (M, N);
51 h1 = gsl_histogram2d_calloc (M, N);
52 g = gsl_histogram2d_calloc (M, N);
54 gsl_test (h->xrange == 0,
55 "gsl_histogram2d_calloc returns valid xrange pointer");
56 gsl_test (h->yrange == 0,
57 "gsl_histogram2d_calloc returns valid yrange pointer");
58 gsl_test (h->bin == 0, "gsl_histogram2d_calloc returns valid bin pointer");
60 gsl_test (h->nx != M, "gsl_histogram2d_calloc returns valid nx");
61 gsl_test (h->ny != N, "gsl_histogram2d_calloc returns valid ny");
63 hr = gsl_histogram2d_calloc_range (MR, NR, xr, yr);
65 gsl_test (hr->xrange == 0,
66 "gsl_histogram2d_calloc_range returns valid xrange pointer");
67 gsl_test (hr->yrange == 0,
68 "gsl_histogram2d_calloc_range returns valid yrange pointer");
69 gsl_test (hr->bin == 0,
70 "gsl_histogram2d_calloc_range returns valid bin pointer");
72 gsl_test (hr->nx != MR, "gsl_histogram2d_calloc_range returns valid nx");
73 gsl_test (hr->ny != NR, "gsl_histogram2d_calloc_range returns valid ny");
77 for (i = 0; i <= MR; i++)
79 if (hr->xrange[i] != xr[i])
86 "gsl_histogram2d_calloc_range creates xrange");
91 for (i = 0; i <= NR; i++)
93 if (hr->yrange[i] != yr[i])
100 "gsl_histogram2d_calloc_range creates yrange");
103 for (i = 0; i <= MR; i++)
108 for (i = 0; i <= NR; i++)
114 int status = gsl_histogram2d_set_ranges (hr, xr, MR + 1, yr, NR + 1);
116 for (i = 0; i <= MR; i++)
118 if (hr->xrange[i] != xr[i])
124 gsl_test (status, "gsl_histogram2d_set_ranges sets xrange");
129 for (i = 0; i <= NR; i++)
131 if (hr->yrange[i] != yr[i])
137 gsl_test (status, "gsl_histogram2d_set_ranges sets yrange");
142 for (i = 0; i < M; i++)
144 for (j = 0; j < N; j++)
147 gsl_histogram2d_accumulate (h, (double) i, (double) j, (double) k);
154 for (i = 0; i < M; i++)
156 for (j = 0; j < N; j++)
159 if (h->bin[i * N + j] != (double) k)
166 "gsl_histogram2d_accumulate writes into array");
172 for (i = 0; i < M; i++)
174 for (j = 0; j < N; j++)
177 if (gsl_histogram2d_get (h, i, j) != (double) k)
181 gsl_test (status, "gsl_histogram2d_get reads from array");
184 for (i = 0; i <= M; i++)
186 h1->xrange[i] = 100.0 + i;
189 for (i = 0; i <= N; i++)
191 h1->yrange[i] = 900.0 + i * i;
194 gsl_histogram2d_memcpy (h1, h);
198 for (i = 0; i <= M; i++)
200 if (h1->xrange[i] != h->xrange[i])
203 gsl_test (status, "gsl_histogram2d_memcpy copies bin xranges");
208 for (i = 0; i <= N; i++)
210 if (h1->yrange[i] != h->yrange[i])
213 gsl_test (status, "gsl_histogram2d_memcpy copies bin yranges");
218 for (i = 0; i < M; i++)
220 for (j = 0; j < N; j++)
222 if (gsl_histogram2d_get (h1, i, j) !=
223 gsl_histogram2d_get (h, i, j))
227 gsl_test (status, "gsl_histogram2d_memcpy copies bin values");
230 gsl_histogram2d_free (h1);
232 h1 = gsl_histogram2d_clone (h);
236 for (i = 0; i <= M; i++)
238 if (h1->xrange[i] != h->xrange[i])
241 gsl_test (status, "gsl_histogram2d_clone copies bin xranges");
246 for (i = 0; i <= N; i++)
248 if (h1->yrange[i] != h->yrange[i])
251 gsl_test (status, "gsl_histogram2d_clone copies bin yranges");
256 for (i = 0; i < M; i++)
258 for (j = 0; j < N; j++)
260 if (gsl_histogram2d_get (h1, i, j) !=
261 gsl_histogram2d_get (h, i, j))
265 gsl_test (status, "gsl_histogram2d_clone copies bin values");
269 gsl_histogram2d_reset (h);
274 for (i = 0; i < M * N; i++)
279 gsl_test (status, "gsl_histogram2d_reset zeros array");
282 gsl_histogram2d_free (h);
283 h = gsl_histogram2d_calloc (M1, N1);
289 for (i = 0; i < M1; i++)
291 for (j = 0; j < N1; j++)
293 gsl_histogram2d_increment (h, (double) i, (double) j);
295 for (k = 0; k <= i * N1 + j; k++)
303 for (k = i * N1 + j + 1; k < M1 * N1; k++)
312 gsl_test (status, "gsl_histogram2d_increment increases bin value");
315 gsl_histogram2d_free (h);
316 h = gsl_histogram2d_calloc (M, N);
320 for (i = 0; i < M; i++)
322 double x0 = 0, x1 = 0;
323 gsl_histogram2d_get_xrange (h, i, &x0, &x1);
325 if (x0 != i || x1 != i + 1)
331 "gsl_histogram2d_get_xlowerlimit and xupperlimit");
337 for (i = 0; i < N; i++)
339 double y0 = 0, y1 = 0;
340 gsl_histogram2d_get_yrange (h, i, &y0, &y1);
342 if (y0 != i || y1 != i + 1)
348 "gsl_histogram2d_get_ylowerlimit and yupperlimit");
354 if (gsl_histogram2d_xmax (h) != M)
356 gsl_test (status, "gsl_histogram2d_xmax");
361 if (gsl_histogram2d_xmin (h) != 0)
363 gsl_test (status, "gsl_histogram2d_xmin");
368 if (gsl_histogram2d_nx (h) != M)
370 gsl_test (status, "gsl_histogram2d_nx");
375 if (gsl_histogram2d_ymax (h) != N)
377 gsl_test (status, "gsl_histogram2d_ymax");
382 if (gsl_histogram2d_ymin (h) != 0)
384 gsl_test (status, "gsl_histogram2d_ymin");
389 if (gsl_histogram2d_ny (h) != N)
391 gsl_test (status, "gsl_histogram2d_ny");
394 h->bin[3 * N + 2] = 123456.0;
395 h->bin[4 * N + 3] = -654321;
398 double max = gsl_histogram2d_max_val (h);
399 gsl_test (max != 123456.0, "gsl_histogram2d_max_val finds maximum value");
403 double min = gsl_histogram2d_min_val (h);
404 gsl_test (min != -654321.0,
405 "gsl_histogram2d_min_val finds minimum value");
410 gsl_histogram2d_max_bin (h, &imax, &jmax);
413 "gsl_histogram2d_max_bin finds maximum value bin");
418 gsl_histogram2d_min_bin (h, &imin, &jmin);
420 || jmin != 3, "gsl_histogram2d_min_bin find minimum value bin");
423 for (i = 0; i < M * N; i++)
426 g->bin[i] = (i + 27) * (i + 1);
430 double sum = gsl_histogram2d_sum (h);
431 gsl_test (sum != N * M * 27 + ((N * M - 1) * N * M) / 2,
432 "gsl_histogram2d_sum sums all bin values");
437 const double xpos = 0.6;
438 const double ypos = 0.85;
443 gsl_histogram2d *h3 = gsl_histogram2d_alloc (M, N);
444 gsl_histogram2d_set_ranges_uniform (h3, 0, 1, 0, 1);
445 gsl_histogram2d_increment (h3, xpos, ypos);
446 gsl_histogram2d_find (h3, xpos, ypos, &xbin, &ybin);
447 xmean = gsl_histogram2d_xmean (h3);
448 ymean = gsl_histogram2d_ymean (h3);
451 double expected_xmean = (h3->xrange[xbin] + h3->xrange[xbin + 1]) / 2.0;
452 double expected_ymean = (h3->yrange[ybin] + h3->yrange[ybin + 1]) / 2.0;
453 gsl_test_abs (xmean, expected_xmean, 100.0 * GSL_DBL_EPSILON,
454 "gsl_histogram2d_xmean");
455 gsl_test_abs (ymean, expected_ymean, 100.0 * GSL_DBL_EPSILON,
456 "gsl_histogram2d_ymean");
458 gsl_histogram2d_free (h3);
462 /* test it with bivariate normal distribution */
463 const double xmean = 0.7;
464 const double ymean = 0.7;
465 const double xsigma = 0.1;
466 const double ysigma = 0.1;
467 const double correl = 0.5;
469 10.0 / M_PI / xsigma / ysigma / sqrt (1.0 - correl * correl);
472 gsl_histogram2d *h3 = gsl_histogram2d_alloc (M, N);
473 gsl_histogram2d_set_ranges_uniform (h3, 0, 1, 0, 1);
474 /* initialize with 2d gauss pdf in two directions */
475 for (xbin = 0; xbin < M; xbin++)
478 ((h3->xrange[xbin] + h3->xrange[xbin + 1]) / 2.0 - xmean) / xsigma;
479 for (ybin = 0; ybin < N; ybin++)
482 ((h3->yrange[ybin] + h3->yrange[ybin + 1]) / 2.0 -
485 norm * exp (-(xi * xi - 2.0 * correl * xi * yi + yi * yi) /
486 2.0 / (1 - correl * correl));
487 h3->bin[xbin * N + ybin] = prob;
491 double xs = gsl_histogram2d_xsigma (h3);
492 double ys = gsl_histogram2d_ysigma (h3);
493 /* evaluate results and compare with parameters */
495 gsl_test_abs (gsl_histogram2d_xmean (h3), xmean, 2.0/M,
496 "gsl_histogram2d_xmean histogram mean(x)");
497 gsl_test_abs (gsl_histogram2d_ymean (h3), ymean, 2.0/N,
498 "gsl_histogram2d_ymean histogram mean(y)");
499 gsl_test_abs (xs, xsigma, 2.0/M,
500 "gsl_histogram2d_xsigma histogram stdev(x)");
501 gsl_test_abs (ys, ysigma, 2.0/N,
502 "gsl_histogram2d_ysigma histogram stdev(y)");
503 gsl_test_abs (gsl_histogram2d_cov (h3) / xs / ys, correl,
504 2.0/((M < N) ? M : N),
505 "gsl_histogram2d_cov histogram covariance");
507 gsl_histogram2d_free (h3);
510 gsl_histogram2d_memcpy (h1, g);
511 gsl_histogram2d_add (h1, h);
515 for (i = 0; i < M * N; i++)
517 if (h1->bin[i] != g->bin[i] + h->bin[i])
520 gsl_test (status, "gsl_histogram2d_add histogram addition");
523 gsl_histogram2d_memcpy (h1, g);
524 gsl_histogram2d_sub (h1, h);
528 for (i = 0; i < M * N; i++)
530 if (h1->bin[i] != g->bin[i] - h->bin[i])
533 gsl_test (status, "gsl_histogram2d_sub histogram subtraction");
537 gsl_histogram2d_memcpy (h1, g);
538 gsl_histogram2d_mul (h1, h);
542 for (i = 0; i < M * N; i++)
544 if (h1->bin[i] != g->bin[i] * h->bin[i])
547 gsl_test (status, "gsl_histogram2d_mul histogram multiplication");
550 gsl_histogram2d_memcpy (h1, g);
551 gsl_histogram2d_div (h1, h);
555 for (i = 0; i < M * N; i++)
557 if (h1->bin[i] != g->bin[i] / h->bin[i])
560 gsl_test (status, "gsl_histogram2d_div histogram division");
563 gsl_histogram2d_memcpy (h1, g);
564 gsl_histogram2d_scale (h1, 0.5);
568 for (i = 0; i < M * N; i++)
570 if (h1->bin[i] != 0.5 * g->bin[i])
573 gsl_test (status, "gsl_histogram2d_scale histogram scaling");
576 gsl_histogram2d_memcpy (h1, g);
577 gsl_histogram2d_shift (h1, 0.25);
581 for (i = 0; i < M * N; i++)
583 if (h1->bin[i] != 0.25 + g->bin[i])
586 gsl_test (status, "gsl_histogram2d_shift histogram shift");
589 gsl_histogram2d_free (h); /* free whatever is in h */
591 h = gsl_histogram2d_calloc_uniform (M1, N1, 0.0, 5.0, 0.0, 5.0);
593 gsl_test (h->xrange == 0,
594 "gsl_histogram2d_calloc_uniform returns valid range pointer");
595 gsl_test (h->yrange == 0,
596 "gsl_histogram2d_calloc_uniform returns valid range pointer");
597 gsl_test (h->bin == 0,
598 "gsl_histogram2d_calloc_uniform returns valid bin pointer");
599 gsl_test (h->nx != M1, "gsl_histogram2d_calloc_uniform returns valid nx");
600 gsl_test (h->ny != N1, "gsl_histogram2d_calloc_uniform returns valid ny");
602 gsl_histogram2d_accumulate (h, 0.0, 3.01, 1.0);
603 gsl_histogram2d_accumulate (h, 0.1, 2.01, 2.0);
604 gsl_histogram2d_accumulate (h, 0.2, 1.01, 3.0);
605 gsl_histogram2d_accumulate (h, 0.3, 0.01, 4.0);
608 size_t i1, i2, i3, i4;
609 size_t j1, j2, j3, j4;
612 status = gsl_histogram2d_find (h, 0.0, 3.01, &i1, &j1);
613 status = gsl_histogram2d_find (h, 0.1, 2.01, &i2, &j2);
614 status = gsl_histogram2d_find (h, 0.2, 1.01, &i3, &j3);
615 status = gsl_histogram2d_find (h, 0.3, 0.01, &i4, &j4);
617 for (i = 0; i < M1; i++)
619 for (j = 0; j < N1; j++)
621 if (i == i1 && j == j1)
625 else if (i == i2 && j == j2)
629 else if (i == i3 && j == j3)
633 else if (i == i4 && j == j4)
642 if (h->bin[i * N1 + j] != expected)
648 gsl_test (status, "gsl_histogram2d_find returns index");
652 FILE *f = fopen ("test.txt", "w");
653 gsl_histogram2d_fprintf (f, h, "%.19e", "%.19e");
658 FILE *f = fopen ("test.txt", "r");
659 gsl_histogram2d *hh = gsl_histogram2d_calloc (M1, N1);
662 gsl_histogram2d_fscanf (f, hh);
664 for (i = 0; i <= M1; i++)
666 if (h->xrange[i] != hh->xrange[i])
668 printf ("xrange[%d] : %g orig vs %g\n",
669 (int) i, h->xrange[i], hh->xrange[i]);
674 for (j = 0; j <= N1; j++)
676 if (h->yrange[j] != hh->yrange[j])
678 printf ("yrange[%d] : %g orig vs %g\n",
679 (int) j, h->yrange[j], hh->yrange[j]);
684 for (i = 0; i < M1 * N1; i++)
686 if (h->bin[i] != hh->bin[i])
688 printf ("bin[%d] : %g orig vs %g\n",
689 (int) i, h->bin[i], hh->bin[i]);
694 gsl_test (status, "gsl_histogram2d_fprintf and fscanf");
696 gsl_histogram2d_free (hh);
701 FILE *f = fopen ("test.dat", "wb");
702 gsl_histogram2d_fwrite (f, h);
707 FILE *f = fopen ("test.dat", "rb");
708 gsl_histogram2d *hh = gsl_histogram2d_calloc (M1, N1);
711 gsl_histogram2d_fread (f, hh);
713 for (i = 0; i <= M1; i++)
715 if (h->xrange[i] != hh->xrange[i])
717 printf ("xrange[%d] : %g orig vs %g\n",
718 (int) i, h->xrange[i], hh->xrange[i]);
723 for (j = 0; j <= N1; j++)
725 if (h->yrange[j] != hh->yrange[j])
727 printf ("yrange[%d] : %g orig vs %g\n",
728 (int) j, h->yrange[j], hh->yrange[j]);
733 for (i = 0; i < M1 * N1; i++)
735 if (h->bin[i] != hh->bin[i])
737 printf ("bin[%d] : %g orig vs %g\n",
738 (int) i, h->bin[i], hh->bin[i]);
743 gsl_test (status, "gsl_histogram2d_fwrite and fread");
745 gsl_histogram2d_free (hh);
749 gsl_histogram2d_free (h);
750 gsl_histogram2d_free (h1);
751 gsl_histogram2d_free (g);
752 gsl_histogram2d_free (hr);