gsl_histogram__init.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* histogram/init.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. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include "gsl_errno.h"
  22. #include "gsl_histogram.h"
  23. gsl_histogram *
  24. gsl_histogram_alloc (size_t n)
  25. {
  26. gsl_histogram *h;
  27. if (n == 0)
  28. {
  29. GSL_ERROR_VAL ("histogram length n must be positive integer",
  30. GSL_EDOM, 0);
  31. }
  32. h = (gsl_histogram *) malloc (sizeof (gsl_histogram));
  33. if (h == 0)
  34. {
  35. GSL_ERROR_VAL ("failed to allocate space for histogram struct",
  36. GSL_ENOMEM, 0);
  37. }
  38. h->range = (double *) malloc ((n + 1) * sizeof (double));
  39. if (h->range == 0)
  40. {
  41. free (h); /* exception in constructor, avoid memory leak */
  42. GSL_ERROR_VAL ("failed to allocate space for histogram ranges",
  43. GSL_ENOMEM, 0);
  44. }
  45. h->bin = (double *) malloc (n * sizeof (double));
  46. if (h->bin == 0)
  47. {
  48. free (h->range);
  49. free (h); /* exception in constructor, avoid memory leak */
  50. GSL_ERROR_VAL ("failed to allocate space for histogram bins",
  51. GSL_ENOMEM, 0);
  52. }
  53. h->n = n;
  54. return h;
  55. }
  56. static void
  57. make_uniform (double range[], size_t n, double xmin, double xmax)
  58. {
  59. size_t i;
  60. for (i = 0; i <= n; i++)
  61. {
  62. double f1 = ((double) (n-i) / (double) n);
  63. double f2 = ((double) i / (double) n);
  64. range[i] = f1 * xmin + f2 * xmax;
  65. }
  66. }
  67. gsl_histogram *
  68. gsl_histogram_calloc_uniform (const size_t n, const double xmin,
  69. const double xmax)
  70. {
  71. gsl_histogram *h;
  72. if (xmin >= xmax)
  73. {
  74. GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
  75. }
  76. h = gsl_histogram_calloc (n);
  77. if (h == 0)
  78. {
  79. return h;
  80. }
  81. make_uniform (h->range, n, xmin, xmax);
  82. return h;
  83. }
  84. gsl_histogram *
  85. gsl_histogram_calloc (size_t n)
  86. {
  87. gsl_histogram * h = gsl_histogram_alloc (n);
  88. if (h == 0)
  89. {
  90. return h;
  91. }
  92. {
  93. size_t i;
  94. for (i = 0; i < n + 1; i++)
  95. {
  96. h->range[i] = i;
  97. }
  98. for (i = 0; i < n; i++)
  99. {
  100. h->bin[i] = 0;
  101. }
  102. }
  103. h->n = n;
  104. return h;
  105. }
  106. void
  107. gsl_histogram_free (gsl_histogram * h)
  108. {
  109. free (h->range);
  110. free (h->bin);
  111. free (h);
  112. }
  113. /* These initialization functions suggested by Achim Gaedke */
  114. int
  115. gsl_histogram_set_ranges_uniform (gsl_histogram * h, double xmin, double xmax)
  116. {
  117. size_t i;
  118. const size_t n = h->n;
  119. if (xmin >= xmax)
  120. {
  121. GSL_ERROR ("xmin must be less than xmax", GSL_EINVAL);
  122. }
  123. /* initialize ranges */
  124. make_uniform (h->range, n, xmin, xmax);
  125. /* clear contents */
  126. for (i = 0; i < n; i++)
  127. {
  128. h->bin[i] = 0;
  129. }
  130. return GSL_SUCCESS;
  131. }
  132. int
  133. gsl_histogram_set_ranges (gsl_histogram * h, const double range[], size_t size)
  134. {
  135. size_t i;
  136. const size_t n = h->n;
  137. if (size != (n+1))
  138. {
  139. GSL_ERROR ("size of range must match size of histogram", GSL_EINVAL);
  140. }
  141. /* initialize ranges */
  142. for (i = 0; i <= n; i++)
  143. {
  144. h->range[i] = range[i];
  145. }
  146. /* clear contents */
  147. for (i = 0; i < n; i++)
  148. {
  149. h->bin[i] = 0;
  150. }
  151. return GSL_SUCCESS;
  152. }