gsl_histogram__calloc_range.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* gsl_histogram_calloc_range.c
  2. * Copyright (C) 2000 Simone Piccardi
  3. *
  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.
  8. *
  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.
  13. *
  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.
  18. */
  19. /***************************************************************
  20. *
  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.
  25. *
  26. * Author: S. Piccardi
  27. * Jan. 2000
  28. *
  29. ***************************************************************/
  30. #include "gsl__config.h"
  31. #include <stdlib.h>
  32. #include "gsl_errno.h"
  33. #include "gsl_histogram.h"
  34. gsl_histogram *
  35. gsl_histogram_calloc_range (size_t n, double *range)
  36. {
  37. size_t i;
  38. gsl_histogram *h;
  39. /* check arguments */
  40. if (n == 0)
  41. {
  42. GSL_ERROR_VAL ("histogram length n must be positive integer",
  43. GSL_EDOM, 0);
  44. }
  45. /* check ranges */
  46. for (i = 0; i < n; i++)
  47. {
  48. if (range[i] >= range[i + 1])
  49. {
  50. GSL_ERROR_VAL ("histogram bin extremes must be "
  51. "in increasing order", GSL_EDOM, 0);
  52. }
  53. }
  54. /* Allocate histogram */
  55. h = (gsl_histogram *) malloc (sizeof (gsl_histogram));
  56. if (h == 0)
  57. {
  58. GSL_ERROR_VAL ("failed to allocate space for histogram struct",
  59. GSL_ENOMEM, 0);
  60. }
  61. h->range = (double *) malloc ((n + 1) * sizeof (double));
  62. if (h->range == 0)
  63. {
  64. /* exception in constructor, avoid memory leak */
  65. free (h);
  66. GSL_ERROR_VAL ("failed to allocate space for histogram ranges",
  67. GSL_ENOMEM, 0);
  68. }
  69. h->bin = (double *) malloc (n * sizeof (double));
  70. if (h->bin == 0)
  71. {
  72. /* exception in constructor, avoid memory leak */
  73. free (h->range);
  74. free (h);
  75. GSL_ERROR_VAL ("failed to allocate space for histogram bins",
  76. GSL_ENOMEM, 0);
  77. }
  78. /* initialize ranges */
  79. for (i = 0; i <= n; i++)
  80. {
  81. h->range[i] = range[i];
  82. }
  83. /* clear contents */
  84. for (i = 0; i < n; i++)
  85. {
  86. h->bin[i] = 0;
  87. }
  88. h->n = n;
  89. return h;
  90. }