gsl_histogram__calloc_range2d.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* gsl_histogram2d_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_histogram2d_calloc_range.c:
  22. * Routine to create a variable binning 2D histogram providing
  23. * the input range vectors. 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_histogram2d.h"
  34. /*
  35. * Routine that create a 2D histogram using the given
  36. * values for X and Y ranges
  37. */
  38. gsl_histogram2d *
  39. gsl_histogram2d_calloc_range (size_t nx, size_t ny,
  40. double *xrange,
  41. double *yrange)
  42. {
  43. size_t i, j;
  44. gsl_histogram2d *h;
  45. /* check arguments */
  46. if (nx == 0)
  47. {
  48. GSL_ERROR_VAL ("histogram length nx must be positive integer",
  49. GSL_EDOM, 0);
  50. }
  51. if (ny == 0)
  52. {
  53. GSL_ERROR_VAL ("histogram length ny must be positive integer",
  54. GSL_EDOM, 0);
  55. }
  56. /* init ranges */
  57. for (i = 0; i < nx; i++)
  58. {
  59. if (xrange[i] >= xrange[i + 1])
  60. {
  61. GSL_ERROR_VAL ("histogram xrange not in increasing order",
  62. GSL_EDOM, 0);
  63. }
  64. }
  65. for (j = 0; j < ny; j++)
  66. {
  67. if (yrange[j] >= yrange[j + 1])
  68. {
  69. GSL_ERROR_VAL ("histogram yrange not in increasing order"
  70. ,GSL_EDOM, 0);
  71. }
  72. }
  73. /* Allocate histogram */
  74. h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
  75. if (h == 0)
  76. {
  77. GSL_ERROR_VAL ("failed to allocate space for histogram struct",
  78. GSL_ENOMEM, 0);
  79. }
  80. h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
  81. if (h->xrange == 0)
  82. {
  83. /* exception in constructor, avoid memory leak */
  84. free (h);
  85. GSL_ERROR_VAL ("failed to allocate space for histogram xrange",
  86. GSL_ENOMEM, 0);
  87. }
  88. h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
  89. if (h->yrange == 0)
  90. {
  91. /* exception in constructor, avoid memory leak */
  92. free (h);
  93. GSL_ERROR_VAL ("failed to allocate space for histogram yrange",
  94. GSL_ENOMEM, 0);
  95. }
  96. h->bin = (double *) malloc (nx * ny * sizeof (double));
  97. if (h->bin == 0)
  98. {
  99. /* exception in constructor, avoid memory leak */
  100. free (h->xrange);
  101. free (h->yrange);
  102. free (h);
  103. GSL_ERROR_VAL ("failed to allocate space for histogram bins",
  104. GSL_ENOMEM, 0);
  105. }
  106. /* init histogram */
  107. /* init ranges */
  108. for (i = 0; i <= nx; i++)
  109. {
  110. h->xrange[i] = xrange[i];
  111. }
  112. for (j = 0; j <= ny; j++)
  113. {
  114. h->yrange[j] = yrange[j];
  115. }
  116. /* clear contents */
  117. for (i = 0; i < nx; i++)
  118. {
  119. for (j = 0; j < ny; j++)
  120. {
  121. h->bin[i * ny + j] = 0;
  122. }
  123. }
  124. h->nx = nx;
  125. h->ny = ny;
  126. return h;
  127. }