1 /* gsl_histogram2d_calloc_range.c
2 * Copyright (C) 2000 Simone Piccardi
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 /***************************************************************
21 * File gsl_histogram2d_calloc_range.c:
22 * Routine to create a variable binning 2D histogram providing
23 * the input range vectors. Need GSL library and header.
24 * Do range check and allocate the histogram data.
29 ***************************************************************/
32 #include <gsl/gsl_errno.h>
33 #include <gsl/gsl_histogram2d.h>
35 * Routine that create a 2D histogram using the given
36 * values for X and Y ranges
39 gsl_histogram2d_calloc_range (size_t nx, size_t ny,
50 GSL_ERROR_VAL ("histogram length nx must be positive integer",
56 GSL_ERROR_VAL ("histogram length ny must be positive integer",
62 for (i = 0; i < nx; i++)
64 if (xrange[i] >= xrange[i + 1])
66 GSL_ERROR_VAL ("histogram xrange not in increasing order",
71 for (j = 0; j < ny; j++)
73 if (yrange[j] >= yrange[j + 1])
75 GSL_ERROR_VAL ("histogram yrange not in increasing order"
80 /* Allocate histogram */
82 h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
86 GSL_ERROR_VAL ("failed to allocate space for histogram struct",
90 h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
94 /* exception in constructor, avoid memory leak */
97 GSL_ERROR_VAL ("failed to allocate space for histogram xrange",
101 h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
105 /* exception in constructor, avoid memory leak */
108 GSL_ERROR_VAL ("failed to allocate space for histogram yrange",
112 h->bin = (double *) malloc (nx * ny * sizeof (double));
116 /* exception in constructor, avoid memory leak */
121 GSL_ERROR_VAL ("failed to allocate space for histogram bins",
129 for (i = 0; i <= nx; i++)
131 h->xrange[i] = xrange[i];
134 for (j = 0; j <= ny; j++)
136 h->yrange[j] = yrange[j];
141 for (i = 0; i < nx; i++)
143 for (j = 0; j < ny; j++)
145 h->bin[i * ny + j] = 0;