1 /* gsl_histogram_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_histogram_calloc_range.c:
22 * Routine to create a variable binning histogram providing
23 * an input range vector. 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_histogram.h>
36 gsl_histogram_calloc_range (size_t n, double *range)
45 GSL_ERROR_VAL ("histogram length n must be positive integer",
51 for (i = 0; i < n; i++)
53 if (range[i] >= range[i + 1])
55 GSL_ERROR_VAL ("histogram bin extremes must be "
56 "in increasing order", GSL_EDOM, 0);
60 /* Allocate histogram */
62 h = (gsl_histogram *) malloc (sizeof (gsl_histogram));
66 GSL_ERROR_VAL ("failed to allocate space for histogram struct",
70 h->range = (double *) malloc ((n + 1) * sizeof (double));
74 /* exception in constructor, avoid memory leak */
76 GSL_ERROR_VAL ("failed to allocate space for histogram ranges",
80 h->bin = (double *) malloc (n * sizeof (double));
84 /* exception in constructor, avoid memory leak */
87 GSL_ERROR_VAL ("failed to allocate space for histogram bins",
91 /* initialize ranges */
93 for (i = 0; i <= n; i++)
95 h->range[i] = range[i];
100 for (i = 0; i < n; i++)