Blame histogram/calloc_range.c

Packit 67cb25
/* gsl_histogram_calloc_range.c
Packit 67cb25
 * Copyright (C) 2000  Simone Piccardi
Packit 67cb25
 *
Packit 67cb25
 * This library is free software; you can redistribute it and/or
Packit 67cb25
 * modify it under the terms of the GNU General Public License as
Packit 67cb25
 * published by the Free Software Foundation; either version 3 of the
Packit 67cb25
 * License, or (at your option) any later version.
Packit 67cb25
 *
Packit 67cb25
 * This program is distributed in the hope that it will be useful,
Packit 67cb25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 *
Packit 67cb25
 * You should have received a copy of the GNU General Public License along
Packit 67cb25
 * with this library; if not, write to the Free Software Foundation, Inc.,
Packit 67cb25
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
/***************************************************************
Packit 67cb25
 *
Packit 67cb25
 * File gsl_histogram_calloc_range.c: 
Packit 67cb25
 * Routine to create a variable binning histogram providing 
Packit 67cb25
 * an input range vector. Need GSL library and header.
Packit 67cb25
 * Do range check and allocate the histogram data. 
Packit 67cb25
 *
Packit 67cb25
 * Author: S. Piccardi
Packit 67cb25
 * Jan. 2000
Packit 67cb25
 *
Packit 67cb25
 ***************************************************************/
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include <gsl/gsl_histogram.h>
Packit 67cb25
Packit 67cb25
gsl_histogram *
Packit 67cb25
gsl_histogram_calloc_range (size_t n, double *range)
Packit 67cb25
{
Packit 67cb25
  size_t i;
Packit 67cb25
  gsl_histogram *h;
Packit 67cb25
Packit 67cb25
  /* check arguments */
Packit 67cb25
Packit 67cb25
  if (n == 0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR_VAL ("histogram length n must be positive integer",
Packit 67cb25
                        GSL_EDOM, 0);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* check ranges */
Packit 67cb25
Packit 67cb25
  for (i = 0; i < n; i++)
Packit 67cb25
    {
Packit 67cb25
      if (range[i] >= range[i + 1])
Packit 67cb25
        {
Packit 67cb25
          GSL_ERROR_VAL ("histogram bin extremes  must be "
Packit 67cb25
                            "in increasing order", GSL_EDOM, 0);
Packit 67cb25
        }
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* Allocate histogram  */
Packit 67cb25
Packit 67cb25
  h = (gsl_histogram *) malloc (sizeof (gsl_histogram));
Packit 67cb25
Packit 67cb25
  if (h == 0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for histogram struct",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  h->range = (double *) malloc ((n + 1) * sizeof (double));
Packit 67cb25
Packit 67cb25
  if (h->range == 0)
Packit 67cb25
    {
Packit 67cb25
      /* exception in constructor, avoid memory leak */
Packit 67cb25
      free (h);
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for histogram ranges",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  h->bin = (double *) malloc (n * sizeof (double));
Packit 67cb25
Packit 67cb25
  if (h->bin == 0)
Packit 67cb25
    {
Packit 67cb25
      /* exception in constructor, avoid memory leak */
Packit 67cb25
      free (h->range);
Packit 67cb25
      free (h);
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for histogram bins",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* initialize ranges */
Packit 67cb25
Packit 67cb25
  for (i = 0; i <= n; i++)
Packit 67cb25
    {
Packit 67cb25
      h->range[i] = range[i];
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* clear contents */
Packit 67cb25
Packit 67cb25
  for (i = 0; i < n; i++)
Packit 67cb25
    {
Packit 67cb25
      h->bin[i] = 0;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  h->n = n;
Packit 67cb25
Packit 67cb25
  return h;
Packit 67cb25
}