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.
22 #include <gsl/gsl_errno.h>
23 #include <gsl/gsl_histogram.h>
24 #include <gsl/gsl_histogram2d.h>
29 gsl_histogram2d_pdf_sample (const gsl_histogram2d_pdf * p,
36 /* Wrap the exclusive top of the bin down to the inclusive bottom of
37 the bin. Since this is a single point it should not affect the
49 status = find (p->nx * p->ny, p->sum, r1, &k);
53 GSL_ERROR ("cannot find r1 in cumulative pdf", GSL_EDOM);
58 size_t j = k - (i * p->ny);
59 double delta = (r1 - p->sum[k]) / (p->sum[k + 1] - p->sum[k]);
60 *x = p->xrange[i] + delta * (p->xrange[i + 1] - p->xrange[i]);
61 *y = p->yrange[j] + r2 * (p->yrange[j + 1] - p->yrange[j]);
67 gsl_histogram2d_pdf_alloc (const size_t nx, const size_t ny)
69 const size_t n = nx * ny;
71 gsl_histogram2d_pdf *p;
75 GSL_ERROR_VAL ("histogram2d pdf length n must be positive integer",
79 p = (gsl_histogram2d_pdf *) malloc (sizeof (gsl_histogram2d_pdf));
83 GSL_ERROR_VAL ("failed to allocate space for histogram2d pdf struct",
87 p->xrange = (double *) malloc ((nx + 1) * sizeof (double));
91 free (p); /* exception in constructor, avoid memory leak */
93 GSL_ERROR_VAL ("failed to allocate space for histogram2d pdf xranges",
97 p->yrange = (double *) malloc ((ny + 1) * sizeof (double));
102 free (p); /* exception in constructor, avoid memory leak */
104 GSL_ERROR_VAL ("failed to allocate space for histogram2d pdf yranges",
108 p->sum = (double *) malloc ((n + 1) * sizeof (double));
114 free (p); /* exception in constructor, avoid memory leak */
116 GSL_ERROR_VAL ("failed to allocate space for histogram2d pdf sums",
127 gsl_histogram2d_pdf_init (gsl_histogram2d_pdf * p, const gsl_histogram2d * h)
130 const size_t nx = p->nx;
131 const size_t ny = p->ny;
132 const size_t n = nx * ny;
134 if (nx != h->nx || ny != h->ny)
136 GSL_ERROR ("histogram2d size must match pdf size", GSL_EDOM);
139 for (i = 0; i < n; i++)
143 GSL_ERROR ("histogram bins must be non-negative to compute"
144 "a probability distribution", GSL_EDOM);
148 for (i = 0; i < nx + 1; i++)
150 p->xrange[i] = h->xrange[i];
153 for (i = 0; i < ny + 1; i++)
155 p->yrange[i] = h->yrange[i];
159 double mean = 0, sum = 0;
161 for (i = 0; i < n; i++)
163 mean += (h->bin[i] - mean) / ((double) (i + 1));
168 for (i = 0; i < n; i++)
170 sum += (h->bin[i] / mean) / n;
180 gsl_histogram2d_pdf_free (gsl_histogram2d_pdf * p)