1 /* histogram/test2d_resample.c
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_histogram2d.h>
24 #include <gsl/gsl_test.h>
25 #include <gsl/gsl_ieee_utils.h>
30 test2d_resample (void)
39 gsl_ieee_env_setup ();
41 h = gsl_histogram2d_calloc_uniform (10, 10, 0.0, 1.0, 0.0, 1.0);
43 for (i = 0; i < 10; i++)
45 for (j = 0; j < 10; j++)
47 double w = 10.0 * i + j;
49 gsl_histogram2d_accumulate (h, 0.1 * i, 0.1 * i, w);
54 gsl_histogram2d_pdf *p = gsl_histogram2d_pdf_alloc (10,10);
56 gsl_histogram2d *hh = gsl_histogram2d_calloc_uniform (20, 20,
60 gsl_histogram2d_pdf_init (p, h);
62 for (i = 0; i < N; i++)
67 status = gsl_histogram2d_pdf_sample (p, u, v, &x, &y);
68 status = gsl_histogram2d_increment (hh, x, y);
72 for (i = 0; i < 20; i++)
74 for (j = 0; j < 20; j++)
76 double z = 4 * total * gsl_histogram2d_get (hh, i, j) / (double) N;
79 double x, xmax, y, ymax;
81 gsl_histogram2d_get_xrange (hh, i, &x, &xmax);
82 gsl_histogram2d_get_yrange (hh, j, &y, &ymax);
84 gsl_histogram2d_find (h, x, y, &k1, &k2);
85 ya = gsl_histogram2d_get (h, k1, k2);
92 printf ("(%d,%d): %g vs %g\n", (int)i, (int)j, z, ya);
97 double err = 1 / sqrt (gsl_histogram2d_get (hh, i, j));
98 double sigma = fabs ((z - ya) / (ya * err));
102 printf ("%g vs %g err=%g sigma=%g\n", z, ya, err, sigma);
108 gsl_histogram2d_pdf_free (p) ;
109 gsl_histogram2d_free (hh) ;
111 gsl_test (status, "gsl_histogram2d_pdf_sample within statistical errors");
114 gsl_histogram2d_free (h) ;