gsl_histogram__oper.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* gsl_histogram_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_histogram_oper.c:
  22. * Routine to make operation on histograms.
  23. * Need GSL library and header.
  24. * Contains the routines:
  25. * gsl_histogram_same_binning check if two histograms have the same binning
  26. * gsl_histogram_add add two histograms
  27. * gsl_histogram_sub subctract two histograms
  28. * gsl_histogram_mult multiply two histograms
  29. * gsl_histogram_div divide two histograms
  30. * gsl_histogram_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_histogram.h"
  40. /*
  41. * gsl_histogram_same_binning:
  42. * control if two histograms have the
  43. * same binning
  44. */
  45. int
  46. gsl_histogram_equal_bins_p (const gsl_histogram * h1, const gsl_histogram * h2)
  47. {
  48. if (h1->n != h2->n)
  49. {
  50. return 0;
  51. }
  52. {
  53. size_t i;
  54. /* init ranges */
  55. for (i = 0; i <= h1->n; i++)
  56. {
  57. if (h1->range[i] != h2->range[i])
  58. {
  59. return 0;
  60. }
  61. }
  62. }
  63. return 1;
  64. }
  65. /*
  66. * gsl_histogram_add:
  67. * add two histograms
  68. */
  69. int
  70. gsl_histogram_add (gsl_histogram * h1, const gsl_histogram * h2)
  71. {
  72. size_t i;
  73. if (!gsl_histogram_equal_bins_p (h1, h2))
  74. {
  75. GSL_ERROR ("histograms have different binning", GSL_EINVAL);
  76. }
  77. for (i = 0; i < h1->n; i++)
  78. {
  79. h1->bin[i] += h2->bin[i];
  80. }
  81. return GSL_SUCCESS;
  82. }
  83. /*
  84. * gsl_histogram_sub:
  85. * subtract two histograms
  86. */
  87. int
  88. gsl_histogram_sub (gsl_histogram * h1, const gsl_histogram * h2)
  89. {
  90. size_t i;
  91. if (!gsl_histogram_equal_bins_p (h1, h2))
  92. {
  93. GSL_ERROR ("histograms have different binning", GSL_EINVAL);
  94. }
  95. for (i = 0; i < h1->n; i++)
  96. {
  97. h1->bin[i] -= h2->bin[i];
  98. }
  99. return GSL_SUCCESS;
  100. }
  101. /*
  102. * gsl_histogram_mult:
  103. * multiply two histograms
  104. */
  105. int
  106. gsl_histogram_mul (gsl_histogram * h1, const gsl_histogram * h2)
  107. {
  108. size_t i;
  109. if (!gsl_histogram_equal_bins_p (h1, h2))
  110. {
  111. GSL_ERROR ("histograms have different binning", GSL_EINVAL);
  112. }
  113. for (i = 0; i < h1->n; i++)
  114. {
  115. h1->bin[i] *= h2->bin[i];
  116. }
  117. return GSL_SUCCESS;
  118. }
  119. /*
  120. * gsl_histogram_div:
  121. * divide two histograms
  122. */
  123. int
  124. gsl_histogram_div (gsl_histogram * h1, const gsl_histogram * h2)
  125. {
  126. size_t i;
  127. if (!gsl_histogram_equal_bins_p (h1, h2))
  128. {
  129. GSL_ERROR ("histograms have different binning", GSL_EINVAL);
  130. }
  131. for (i = 0; i < h1->n; i++)
  132. {
  133. h1->bin[i] /= h2->bin[i];
  134. }
  135. return GSL_SUCCESS;
  136. }
  137. /*
  138. * gsl_histogram_scale:
  139. * scale a histogram by a numeric factor
  140. */
  141. int
  142. gsl_histogram_scale (gsl_histogram * h, double scale)
  143. {
  144. size_t i;
  145. for (i = 0; i < h->n; i++)
  146. {
  147. h->bin[i] *= scale;
  148. }
  149. return GSL_SUCCESS;
  150. }
  151. /*
  152. * gsl_histogram_shift:
  153. * shift a histogram by a numeric offset
  154. */
  155. int
  156. gsl_histogram_shift (gsl_histogram * h, double shift)
  157. {
  158. size_t i;
  159. for (i = 0; i < h->n; i++)
  160. {
  161. h->bin[i] += shift;
  162. }
  163. return GSL_SUCCESS;
  164. }