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_histogram2d.h>
27 gsl_histogram2d_alloc (const size_t nx, const size_t ny)
33 GSL_ERROR_VAL ("histogram2d length nx must be positive integer",
39 GSL_ERROR_VAL ("histogram2d length ny must be positive integer",
43 h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
47 GSL_ERROR_VAL ("failed to allocate space for histogram2d struct",
51 h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
55 free (h); /* exception in constructor, avoid memory leak */
57 GSL_ERROR_VAL ("failed to allocate space for histogram2d x ranges",
61 h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
66 free (h); /* exception in constructor, avoid memory leak */
68 GSL_ERROR_VAL ("failed to allocate space for histogram2d y ranges",
72 h->bin = (double *) malloc (nx * ny * sizeof (double));
78 free (h); /* exception in constructor, avoid memory leak */
80 GSL_ERROR_VAL ("failed to allocate space for histogram bins",
91 make_uniform (double range[], size_t n, double xmin, double xmax)
95 for (i = 0; i <= n; i++)
97 double f1 = ((double) (n-i) / (double) n);
98 double f2 = ((double) i / (double) n);
99 range[i] = f1 * xmin + f2 * xmax;
104 gsl_histogram2d_calloc_uniform (const size_t nx, const size_t ny,
105 const double xmin, const double xmax,
106 const double ymin, const double ymax)
112 GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
117 GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
120 h = gsl_histogram2d_calloc (nx, ny);
127 make_uniform (h->xrange, nx, xmin, xmax);
128 make_uniform (h->yrange, ny, ymin, ymax);
134 gsl_histogram2d_calloc (const size_t nx, const size_t ny)
140 GSL_ERROR_VAL ("histogram2d length nx must be positive integer",
146 GSL_ERROR_VAL ("histogram2d length ny must be positive integer",
150 h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
154 GSL_ERROR_VAL ("failed to allocate space for histogram2d struct",
158 h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
162 free (h); /* exception in constructor, avoid memory leak */
164 GSL_ERROR_VAL ("failed to allocate space for histogram2d x ranges",
168 h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
173 free (h); /* exception in constructor, avoid memory leak */
175 GSL_ERROR_VAL ("failed to allocate space for histogram2d y ranges",
179 h->bin = (double *) malloc (nx * ny * sizeof (double));
185 free (h); /* exception in constructor, avoid memory leak */
187 GSL_ERROR_VAL ("failed to allocate space for histogram bins",
194 for (i = 0; i < nx + 1; i++)
199 for (i = 0; i < ny + 1; i++)
204 for (i = 0; i < nx * ny; i++)
218 gsl_histogram2d_free (gsl_histogram2d * h)
228 gsl_histogram2d_set_ranges_uniform (gsl_histogram2d * h,
229 double xmin, double xmax,
230 double ymin, double ymax)
233 const size_t nx = h->nx, ny = h->ny;
237 GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
242 GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
245 /* initialize ranges */
247 make_uniform (h->xrange, nx, xmin, xmax);
248 make_uniform (h->yrange, ny, ymin, ymax);
252 for (i = 0; i < nx * ny; i++)
261 gsl_histogram2d_set_ranges (gsl_histogram2d * h,
262 const double xrange[], size_t xsize,
263 const double yrange[], size_t ysize)
266 const size_t nx = h->nx, ny = h->ny;
268 if (xsize != (nx + 1))
270 GSL_ERROR_VAL ("size of xrange must match size of histogram",
274 if (ysize != (ny + 1))
276 GSL_ERROR_VAL ("size of yrange must match size of histogram",
280 /* initialize ranges */
282 for (i = 0; i <= nx; i++)
284 h->xrange[i] = xrange[i];
287 for (i = 0; i <= ny; i++)
289 h->yrange[i] = yrange[i];
294 for (i = 0; i < nx * ny; i++)