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 <config.h>
21 #include <stdlib.h>
22 #include <math.h>
23 #include <gsl/gsl_errno.h>
24 #include <gsl/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   RETURN_IF_NULL (h);
221   free (h->xrange);
222   free (h->yrange);
223   free (h->bin);
224   free (h);
225 }
226 
227 
228 int
gsl_histogram2d_set_ranges_uniform(gsl_histogram2d * h,double xmin,double xmax,double ymin,double ymax)229 gsl_histogram2d_set_ranges_uniform (gsl_histogram2d * h,
230                                     double xmin, double xmax,
231                                     double ymin, double ymax)
232 {
233   size_t i;
234   const size_t nx = h->nx, ny = h->ny;
235 
236   if (xmin >= xmax)
237     {
238       GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
239     }
240 
241   if (ymin >= ymax)
242     {
243       GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
244     }
245 
246   /* initialize ranges */
247 
248   make_uniform (h->xrange, nx, xmin, xmax);
249   make_uniform (h->yrange, ny, ymin, ymax);
250 
251   /* clear contents */
252 
253   for (i = 0; i < nx * ny; i++)
254     {
255       h->bin[i] = 0;
256     }
257 
258   return GSL_SUCCESS;
259 }
260 
261 int
gsl_histogram2d_set_ranges(gsl_histogram2d * h,const double xrange[],size_t xsize,const double yrange[],size_t ysize)262 gsl_histogram2d_set_ranges (gsl_histogram2d * h,
263                             const double xrange[], size_t xsize,
264                             const double yrange[], size_t ysize)
265 {
266   size_t i;
267   const size_t nx = h->nx, ny = h->ny;
268 
269   if (xsize != (nx + 1))
270     {
271       GSL_ERROR_VAL ("size of xrange must match size of histogram",
272                      GSL_EINVAL, 0);
273     }
274 
275   if (ysize != (ny + 1))
276     {
277       GSL_ERROR_VAL ("size of yrange must match size of histogram",
278                      GSL_EINVAL, 0);
279     }
280 
281   /* initialize ranges */
282 
283   for (i = 0; i <= nx; i++)
284     {
285       h->xrange[i] = xrange[i];
286     }
287 
288   for (i = 0; i <= ny; i++)
289     {
290       h->yrange[i] = yrange[i];
291     }
292 
293   /* clear contents */
294 
295   for (i = 0; i < nx * ny; i++)
296     {
297       h->bin[i] = 0;
298     }
299 
300   return GSL_SUCCESS;
301 }
302