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>
26 gsl_histogram_alloc (size_t n)
32 GSL_ERROR_VAL ("histogram length n must be positive integer",
36 h = (gsl_histogram *) malloc (sizeof (gsl_histogram));
40 GSL_ERROR_VAL ("failed to allocate space for histogram struct",
44 h->range = (double *) malloc ((n + 1) * sizeof (double));
48 free (h); /* exception in constructor, avoid memory leak */
50 GSL_ERROR_VAL ("failed to allocate space for histogram ranges",
54 h->bin = (double *) malloc (n * sizeof (double));
59 free (h); /* exception in constructor, avoid memory leak */
61 GSL_ERROR_VAL ("failed to allocate space for histogram bins",
71 make_uniform (double range[], size_t n, double xmin, double xmax)
75 for (i = 0; i <= n; i++)
77 double f1 = ((double) (n-i) / (double) n);
78 double f2 = ((double) i / (double) n);
79 range[i] = f1 * xmin + f2 * xmax;
84 gsl_histogram_calloc_uniform (const size_t n, const double xmin,
91 GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
94 h = gsl_histogram_calloc (n);
101 make_uniform (h->range, n, xmin, xmax);
107 gsl_histogram_calloc (size_t n)
109 gsl_histogram * h = gsl_histogram_alloc (n);
119 for (i = 0; i < n + 1; i++)
124 for (i = 0; i < n; i++)
137 gsl_histogram_free (gsl_histogram * h)
144 /* These initialization functions suggested by Achim Gaedke */
147 gsl_histogram_set_ranges_uniform (gsl_histogram * h, double xmin, double xmax)
150 const size_t n = h->n;
154 GSL_ERROR ("xmin must be less than xmax", GSL_EINVAL);
157 /* initialize ranges */
159 make_uniform (h->range, n, xmin, xmax);
163 for (i = 0; i < n; i++)
172 gsl_histogram_set_ranges (gsl_histogram * h, const double range[], size_t size)
175 const size_t n = h->n;
179 GSL_ERROR ("size of range must match size of histogram", GSL_EINVAL);
182 /* initialize ranges */
184 for (i = 0; i <= n; i++)
186 h->range[i] = range[i];
191 for (i = 0; i < n; i++)