gsl_histogram__oper2d.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* gsl_histogram2d_oper.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_oper.c:
  22. * Routine to make operation on 2D histograms.
  23. * Need GSL library and header.
  24. * Contains the routines:
  25. * gsl_histogram2d_same_binning check if two histograms have the same binning
  26. * gsl_histogram2d_add add two histogram
  27. * gsl_histogram2d_sub subctract two histogram
  28. * gsl_histogram2d_mult multiply two histogram
  29. * gsl_histogram2d_div divide two histogram
  30. * gsl_histogram2d_scale scale histogram contents
  31. *
  32. * Author: S. Piccardi
  33. * Jan. 2000
  34. *
  35. ***************************************************************/
  36. #include "gsl__config.h"
  37. #include <stdlib.h>
  38. #include "gsl_errno.h"
  39. #include "gsl_histogram2d.h"
  40. /*
  41. * gsl_histogram2d_same_binning:
  42. * control if two histogram have the
  43. * same binning
  44. */
  45. int
  46. gsl_histogram2d_equal_bins_p (const gsl_histogram2d * h1,
  47. const gsl_histogram2d * h2)
  48. {
  49. if ((h1->nx != h2->nx) || (h1->ny != h2->ny))
  50. {
  51. return 0;
  52. }
  53. {
  54. size_t i;
  55. /* init ranges */
  56. for (i = 0; i <= (h1->nx); i++)
  57. {
  58. if (h1->xrange[i] != h2->xrange[i])
  59. {
  60. return 0;
  61. }
  62. }
  63. for (i = 0; i <= (h1->ny); i++)
  64. {
  65. if (h1->yrange[i] != h2->yrange[i])
  66. {
  67. return 0;
  68. }
  69. }
  70. }
  71. return 1;
  72. }
  73. /*
  74. * gsl_histogram2d_add:
  75. * add two histogram
  76. */
  77. int
  78. gsl_histogram2d_add (gsl_histogram2d * h1, const gsl_histogram2d * h2)
  79. {
  80. size_t i;
  81. if (!gsl_histogram2d_equal_bins_p (h1, h2))
  82. {
  83. GSL_ERROR ("histograms have different binning", GSL_EINVAL);
  84. }
  85. for (i = 0; i < (h1->nx) * (h1->ny); i++)
  86. {
  87. h1->bin[i] += h2->bin[i];
  88. }
  89. return GSL_SUCCESS;
  90. }
  91. /*
  92. * gsl_histogram2d_sub:
  93. * subtract two histogram
  94. */
  95. int
  96. gsl_histogram2d_sub (gsl_histogram2d * h1, const gsl_histogram2d * h2)
  97. {
  98. size_t i;
  99. if (!gsl_histogram2d_equal_bins_p (h1, h2))
  100. {
  101. GSL_ERROR ("histograms have different binning", GSL_EINVAL);
  102. }
  103. for (i = 0; i < (h1->nx) * (h1->ny); i++)
  104. {
  105. h1->bin[i] -= h2->bin[i];
  106. }
  107. return GSL_SUCCESS;
  108. }
  109. /*
  110. * gsl_histogram2d_mult:
  111. * multiply two histogram
  112. */
  113. int
  114. gsl_histogram2d_mul (gsl_histogram2d * h1, const gsl_histogram2d * h2)
  115. {
  116. size_t i;
  117. if (!gsl_histogram2d_equal_bins_p (h1, h2))
  118. {
  119. GSL_ERROR ("histograms have different binning", GSL_EINVAL);
  120. }
  121. for (i = 0; i < (h1->nx) * (h1->ny); i++)
  122. {
  123. h1->bin[i] *= h2->bin[i];
  124. }
  125. return GSL_SUCCESS;
  126. }
  127. /*
  128. * gsl_histogram2d_div:
  129. * divide two histogram
  130. */
  131. int
  132. gsl_histogram2d_div (gsl_histogram2d * h1, const gsl_histogram2d * h2)
  133. {
  134. size_t i;
  135. if (!gsl_histogram2d_equal_bins_p (h1, h2))
  136. {
  137. GSL_ERROR ("histograms have different binning", GSL_EINVAL);
  138. }
  139. for (i = 0; i < (h1->nx) * (h1->ny); i++)
  140. {
  141. h1->bin[i] /= h2->bin[i];
  142. }
  143. return GSL_SUCCESS;
  144. }
  145. /*
  146. * gsl_histogram2d_scale:
  147. * scale a histogram by a numeric factor
  148. */
  149. int
  150. gsl_histogram2d_scale (gsl_histogram2d * h, double scale)
  151. {
  152. size_t i;
  153. for (i = 0; i < (h->nx) * (h->ny); i++)
  154. {
  155. h->bin[i] *= scale;
  156. }
  157. return GSL_SUCCESS;
  158. }
  159. /*
  160. * gsl_histogram2d_shift:
  161. * shift a histogram by a numeric offset
  162. */
  163. int
  164. gsl_histogram2d_shift (gsl_histogram2d * h, double shift)
  165. {
  166. size_t i;
  167. for (i = 0; i < (h->nx) * (h->ny); i++)
  168. {
  169. h->bin[i] += shift;
  170. }
  171. return GSL_SUCCESS;
  172. }