gsl_histogram__init2d.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* histogram/init2d.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 <math.h>
  22. #include "gsl_errno.h"
  23. #include "gsl_histogram2d.h"
  24. gsl_histogram2d *
  25. gsl_histogram2d_alloc (const size_t nx, const size_t ny)
  26. {
  27. gsl_histogram2d *h;
  28. if (nx == 0)
  29. {
  30. GSL_ERROR_VAL ("histogram2d length nx must be positive integer",
  31. GSL_EDOM, 0);
  32. }
  33. if (ny == 0)
  34. {
  35. GSL_ERROR_VAL ("histogram2d length ny must be positive integer",
  36. GSL_EDOM, 0);
  37. }
  38. h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
  39. if (h == 0)
  40. {
  41. GSL_ERROR_VAL ("failed to allocate space for histogram2d struct",
  42. GSL_ENOMEM, 0);
  43. }
  44. h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
  45. if (h->xrange == 0)
  46. {
  47. free (h); /* exception in constructor, avoid memory leak */
  48. GSL_ERROR_VAL ("failed to allocate space for histogram2d x ranges",
  49. GSL_ENOMEM, 0);
  50. }
  51. h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
  52. if (h->yrange == 0)
  53. {
  54. free (h->xrange);
  55. free (h); /* exception in constructor, avoid memory leak */
  56. GSL_ERROR_VAL ("failed to allocate space for histogram2d y ranges",
  57. GSL_ENOMEM, 0);
  58. }
  59. h->bin = (double *) malloc (nx * ny * sizeof (double));
  60. if (h->bin == 0)
  61. {
  62. free (h->xrange);
  63. free (h->yrange);
  64. free (h); /* exception in constructor, avoid memory leak */
  65. GSL_ERROR_VAL ("failed to allocate space for histogram bins",
  66. GSL_ENOMEM, 0);
  67. }
  68. h->nx = nx;
  69. h->ny = ny;
  70. return h;
  71. }
  72. static void
  73. make_uniform (double range[], size_t n, double xmin, double xmax)
  74. {
  75. size_t i;
  76. for (i = 0; i <= n; i++)
  77. {
  78. double f1 = ((double) (n-i) / (double) n);
  79. double f2 = ((double) i / (double) n);
  80. range[i] = f1 * xmin + f2 * xmax;
  81. }
  82. }
  83. gsl_histogram2d *
  84. gsl_histogram2d_calloc_uniform (const size_t nx, const size_t ny,
  85. const double xmin, const double xmax,
  86. const double ymin, const double ymax)
  87. {
  88. gsl_histogram2d *h;
  89. if (xmin >= xmax)
  90. {
  91. GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
  92. }
  93. if (ymin >= ymax)
  94. {
  95. GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
  96. }
  97. h = gsl_histogram2d_calloc (nx, ny);
  98. if (h == 0)
  99. {
  100. return h;
  101. }
  102. make_uniform (h->xrange, nx, xmin, xmax);
  103. make_uniform (h->yrange, ny, ymin, ymax);
  104. return h;
  105. }
  106. gsl_histogram2d *
  107. gsl_histogram2d_calloc (const size_t nx, const size_t ny)
  108. {
  109. gsl_histogram2d *h;
  110. if (nx == 0)
  111. {
  112. GSL_ERROR_VAL ("histogram2d length nx must be positive integer",
  113. GSL_EDOM, 0);
  114. }
  115. if (ny == 0)
  116. {
  117. GSL_ERROR_VAL ("histogram2d length ny must be positive integer",
  118. GSL_EDOM, 0);
  119. }
  120. h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
  121. if (h == 0)
  122. {
  123. GSL_ERROR_VAL ("failed to allocate space for histogram2d struct",
  124. GSL_ENOMEM, 0);
  125. }
  126. h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
  127. if (h->xrange == 0)
  128. {
  129. free (h); /* exception in constructor, avoid memory leak */
  130. GSL_ERROR_VAL ("failed to allocate space for histogram2d x ranges",
  131. GSL_ENOMEM, 0);
  132. }
  133. h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
  134. if (h->yrange == 0)
  135. {
  136. free (h->xrange);
  137. free (h); /* exception in constructor, avoid memory leak */
  138. GSL_ERROR_VAL ("failed to allocate space for histogram2d y ranges",
  139. GSL_ENOMEM, 0);
  140. }
  141. h->bin = (double *) malloc (nx * ny * sizeof (double));
  142. if (h->bin == 0)
  143. {
  144. free (h->xrange);
  145. free (h->yrange);
  146. free (h); /* exception in constructor, avoid memory leak */
  147. GSL_ERROR_VAL ("failed to allocate space for histogram bins",
  148. GSL_ENOMEM, 0);
  149. }
  150. {
  151. size_t i;
  152. for (i = 0; i < nx + 1; i++)
  153. {
  154. h->xrange[i] = i;
  155. }
  156. for (i = 0; i < ny + 1; i++)
  157. {
  158. h->yrange[i] = i;
  159. }
  160. for (i = 0; i < nx * ny; i++)
  161. {
  162. h->bin[i] = 0;
  163. }
  164. }
  165. h->nx = nx;
  166. h->ny = ny;
  167. return h;
  168. }
  169. void
  170. gsl_histogram2d_free (gsl_histogram2d * h)
  171. {
  172. free (h->xrange);
  173. free (h->yrange);
  174. free (h->bin);
  175. free (h);
  176. }
  177. int
  178. gsl_histogram2d_set_ranges_uniform (gsl_histogram2d * h,
  179. double xmin, double xmax,
  180. double ymin, double ymax)
  181. {
  182. size_t i;
  183. const size_t nx = h->nx, ny = h->ny;
  184. if (xmin >= xmax)
  185. {
  186. GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
  187. }
  188. if (ymin >= ymax)
  189. {
  190. GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
  191. }
  192. /* initialize ranges */
  193. make_uniform (h->xrange, nx, xmin, xmax);
  194. make_uniform (h->yrange, ny, ymin, ymax);
  195. /* clear contents */
  196. for (i = 0; i < nx * ny; i++)
  197. {
  198. h->bin[i] = 0;
  199. }
  200. return GSL_SUCCESS;
  201. }
  202. int
  203. gsl_histogram2d_set_ranges (gsl_histogram2d * h,
  204. const double xrange[], size_t xsize,
  205. const double yrange[], size_t ysize)
  206. {
  207. size_t i;
  208. const size_t nx = h->nx, ny = h->ny;
  209. if (xsize != (nx + 1))
  210. {
  211. GSL_ERROR_VAL ("size of xrange must match size of histogram",
  212. GSL_EINVAL, 0);
  213. }
  214. if (ysize != (ny + 1))
  215. {
  216. GSL_ERROR_VAL ("size of yrange must match size of histogram",
  217. GSL_EINVAL, 0);
  218. }
  219. /* initialize ranges */
  220. for (i = 0; i <= nx; i++)
  221. {
  222. h->xrange[i] = xrange[i];
  223. }
  224. for (i = 0; i <= ny; i++)
  225. {
  226. h->yrange[i] = yrange[i];
  227. }
  228. /* clear contents */
  229. for (i = 0; i < nx * ny; i++)
  230. {
  231. h->bin[i] = 0;
  232. }
  233. return GSL_SUCCESS;
  234. }