1 /* histogram/init2d.c
2  *
3  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
4  *
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.
9  *
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.
14  *
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.
18  */
19 
20 #include "gsl__config.h"
21 #include <stdlib.h>
22 #include <math.h>
23 #include "gsl_errno.h"
24 #include "gsl_histogram2d.h"
25 
26 gsl_histogram2d *
gsl_histogram2d_alloc(const size_t nx,const size_t ny)27 gsl_histogram2d_alloc (const size_t nx, const size_t ny)
28 {
29   gsl_histogram2d *h;
30 
31   if (nx == 0)
32     {
33       GSL_ERROR_VAL ("histogram2d length nx must be positive integer",
34                         GSL_EDOM, 0);
35     }
36 
37   if (ny == 0)
38     {
39       GSL_ERROR_VAL ("histogram2d length ny must be positive integer",
40                         GSL_EDOM, 0);
41     }
42 
43   h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
44 
45   if (h == 0)
46     {
47       GSL_ERROR_VAL ("failed to allocate space for histogram2d struct",
48                         GSL_ENOMEM, 0);
49     }
50 
51   h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
52 
53   if (h->xrange == 0)
54     {
55       free (h);         /* exception in constructor, avoid memory leak */
56 
57       GSL_ERROR_VAL ("failed to allocate space for histogram2d x ranges",
58                         GSL_ENOMEM, 0);
59     }
60 
61   h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
62 
63   if (h->yrange == 0)
64     {
65       free (h->xrange);
66       free (h);         /* exception in constructor, avoid memory leak */
67 
68       GSL_ERROR_VAL ("failed to allocate space for histogram2d y ranges",
69                         GSL_ENOMEM, 0);
70     }
71 
72   h->bin = (double *) malloc (nx * ny * sizeof (double));
73 
74   if (h->bin == 0)
75     {
76       free (h->xrange);
77       free (h->yrange);
78       free (h);         /* exception in constructor, avoid memory leak */
79 
80       GSL_ERROR_VAL ("failed to allocate space for histogram bins",
81                         GSL_ENOMEM, 0);
82     }
83 
84   h->nx = nx;
85   h->ny = ny;
86 
87   return h;
88 }
89 
90 static void
make_uniform(double range[],size_t n,double xmin,double xmax)91 make_uniform (double range[], size_t n, double xmin, double xmax)
92 {
93   size_t i;
94 
95   for (i = 0; i <= n; i++)
96     {
97       double f1 = ((double) (n-i) / (double) n);
98       double f2 = ((double) i / (double) n);
99       range[i] = f1 * xmin +  f2 * xmax;
100     }
101 }
102 
103 gsl_histogram2d *
gsl_histogram2d_calloc_uniform(const size_t nx,const size_t ny,const double xmin,const double xmax,const double ymin,const double ymax)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)
107 {
108   gsl_histogram2d *h;
109 
110   if (xmin >= xmax)
111     {
112       GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
113     }
114 
115   if (ymin >= ymax)
116     {
117       GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
118     }
119 
120   h = gsl_histogram2d_calloc (nx, ny);
121 
122   if (h == 0)
123     {
124       return h;
125     }
126 
127   make_uniform (h->xrange, nx, xmin, xmax);
128   make_uniform (h->yrange, ny, ymin, ymax);
129 
130   return h;
131 }
132 
133 gsl_histogram2d *
gsl_histogram2d_calloc(const size_t nx,const size_t ny)134 gsl_histogram2d_calloc (const size_t nx, const size_t ny)
135 {
136   gsl_histogram2d *h;
137 
138   if (nx == 0)
139     {
140       GSL_ERROR_VAL ("histogram2d length nx must be positive integer",
141                         GSL_EDOM, 0);
142     }
143 
144   if (ny == 0)
145     {
146       GSL_ERROR_VAL ("histogram2d length ny must be positive integer",
147                         GSL_EDOM, 0);
148     }
149 
150   h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
151 
152   if (h == 0)
153     {
154       GSL_ERROR_VAL ("failed to allocate space for histogram2d struct",
155                         GSL_ENOMEM, 0);
156     }
157 
158   h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
159 
160   if (h->xrange == 0)
161     {
162       free (h);         /* exception in constructor, avoid memory leak */
163 
164       GSL_ERROR_VAL ("failed to allocate space for histogram2d x ranges",
165                         GSL_ENOMEM, 0);
166     }
167 
168   h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
169 
170   if (h->yrange == 0)
171     {
172       free (h->xrange);
173       free (h);         /* exception in constructor, avoid memory leak */
174 
175       GSL_ERROR_VAL ("failed to allocate space for histogram2d y ranges",
176                         GSL_ENOMEM, 0);
177     }
178 
179   h->bin = (double *) malloc (nx * ny * sizeof (double));
180 
181   if (h->bin == 0)
182     {
183       free (h->xrange);
184       free (h->yrange);
185       free (h);         /* exception in constructor, avoid memory leak */
186 
187       GSL_ERROR_VAL ("failed to allocate space for histogram bins",
188                         GSL_ENOMEM, 0);
189     }
190 
191   {
192     size_t i;
193 
194     for (i = 0; i < nx + 1; i++)
195       {
196         h->xrange[i] = i;
197       }
198 
199     for (i = 0; i < ny + 1; i++)
200       {
201         h->yrange[i] = i;
202       }
203 
204     for (i = 0; i < nx * ny; i++)
205       {
206         h->bin[i] = 0;
207       }
208   }
209 
210   h->nx = nx;
211   h->ny = ny;
212 
213   return h;
214 }
215 
216 
217 void
gsl_histogram2d_free(gsl_histogram2d * h)218 gsl_histogram2d_free (gsl_histogram2d * h)
219 {
220   free (h->xrange);
221   free (h->yrange);
222   free (h->bin);
223   free (h);
224 }
225 
226 
227 int
gsl_histogram2d_set_ranges_uniform(gsl_histogram2d * h,double xmin,double xmax,double ymin,double ymax)228 gsl_histogram2d_set_ranges_uniform (gsl_histogram2d * h,
229                                     double xmin, double xmax,
230                                     double ymin, double ymax)
231 {
232   size_t i;
233   const size_t nx = h->nx, ny = h->ny;
234 
235   if (xmin >= xmax)
236     {
237       GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
238     }
239 
240   if (ymin >= ymax)
241     {
242       GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
243     }
244 
245   /* initialize ranges */
246 
247   make_uniform (h->xrange, nx, xmin, xmax);
248   make_uniform (h->yrange, ny, ymin, ymax);
249 
250   /* clear contents */
251 
252   for (i = 0; i < nx * ny; i++)
253     {
254       h->bin[i] = 0;
255     }
256 
257   return GSL_SUCCESS;
258 }
259 
260 int
gsl_histogram2d_set_ranges(gsl_histogram2d * h,const double xrange[],size_t xsize,const double yrange[],size_t ysize)261 gsl_histogram2d_set_ranges (gsl_histogram2d * h,
262                             const double xrange[], size_t xsize,
263                             const double yrange[], size_t ysize)
264 {
265   size_t i;
266   const size_t nx = h->nx, ny = h->ny;
267 
268   if (xsize != (nx + 1))
269     {
270       GSL_ERROR_VAL ("size of xrange must match size of histogram",
271                      GSL_EINVAL, 0);
272     }
273 
274   if (ysize != (ny + 1))
275     {
276       GSL_ERROR_VAL ("size of yrange must match size of histogram",
277                      GSL_EINVAL, 0);
278     }
279 
280   /* initialize ranges */
281 
282   for (i = 0; i <= nx; i++)
283     {
284       h->xrange[i] = xrange[i];
285     }
286 
287   for (i = 0; i <= ny; i++)
288     {
289       h->yrange[i] = yrange[i];
290     }
291 
292   /* clear contents */
293 
294   for (i = 0; i < nx * ny; i++)
295     {
296       h->bin[i] = 0;
297     }
298 
299   return GSL_SUCCESS;
300 }
301