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 License along
15  * with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 /***************************************************************
19  *
20  * File gsl_histogram_oper.c:
21  * Routine to make operation on histograms.
22  * Need GSL library and header.
23  * Contains the routines:
24  * gsl_histogram_same_binning check if two histograms have the same binning
25  * gsl_histogram_add          add two histograms
26  * gsl_histogram_sub          subctract two histograms
27  * gsl_histogram_mult         multiply two histograms
28  * gsl_histogram_div          divide two histograms
29  * gsl_histogram_scale        scale histogram contents
30  *
31  * Author: S. Piccardi
32  * Jan. 2000
33  *
34  ***************************************************************/
35 #include <config.h>
36 #include <stdlib.h>
37 #include <gsl/gsl_errno.h>
38 #include <gsl/gsl_histogram.h>
39 
40 /*
41  * gsl_histogram_same_binning:
42  * control if two histograms have the
43  * same binning
44  */
45 
46 int
gsl_histogram_equal_bins_p(const gsl_histogram * h1,const gsl_histogram * h2)47 gsl_histogram_equal_bins_p (const gsl_histogram * h1, const gsl_histogram * h2)
48 {
49   if (h1->n != h2->n)
50     {
51       return 0;
52     }
53 
54   {
55     size_t i;
56     /* init ranges */
57 
58     for (i = 0; i <= h1->n; i++)
59       {
60         if (h1->range[i] != h2->range[i])
61           {
62             return 0;
63           }
64       }
65   }
66 
67   return 1;
68 }
69 
70 /*
71  * gsl_histogram_add:
72  * add two histograms
73  */
74 int
gsl_histogram_add(gsl_histogram * h1,const gsl_histogram * h2)75 gsl_histogram_add (gsl_histogram * h1, const gsl_histogram * h2)
76 {
77   size_t i;
78 
79   if (!gsl_histogram_equal_bins_p (h1, h2))
80     {
81       GSL_ERROR ("histograms have different binning", GSL_EINVAL);
82     }
83 
84   for (i = 0; i < h1->n; i++)
85     {
86       h1->bin[i] += h2->bin[i];
87     }
88 
89   return GSL_SUCCESS;
90 }
91 
92 /*
93  * gsl_histogram_sub:
94  * subtract two histograms
95  */
96 
97 int
gsl_histogram_sub(gsl_histogram * h1,const gsl_histogram * h2)98 gsl_histogram_sub (gsl_histogram * h1, const gsl_histogram * h2)
99 {
100   size_t i;
101 
102   if (!gsl_histogram_equal_bins_p (h1, h2))
103     {
104       GSL_ERROR ("histograms have different binning", GSL_EINVAL);
105     }
106 
107   for (i = 0; i < h1->n; i++)
108     {
109       h1->bin[i] -= h2->bin[i];
110     }
111 
112   return GSL_SUCCESS;
113 
114 }
115 
116 /*
117  * gsl_histogram_mult:
118  * multiply two histograms
119  */
120 
121 int
gsl_histogram_mul(gsl_histogram * h1,const gsl_histogram * h2)122 gsl_histogram_mul (gsl_histogram * h1, const gsl_histogram * h2)
123 {
124   size_t i;
125 
126   if (!gsl_histogram_equal_bins_p (h1, h2))
127     {
128       GSL_ERROR ("histograms have different binning", GSL_EINVAL);
129     }
130 
131   for (i = 0; i < h1->n; i++)
132     {
133       h1->bin[i] *= h2->bin[i];
134     }
135 
136   return GSL_SUCCESS;
137 }
138 /*
139  * gsl_histogram_div:
140  * divide two histograms
141  */
142 int
gsl_histogram_div(gsl_histogram * h1,const gsl_histogram * h2)143 gsl_histogram_div (gsl_histogram * h1, const gsl_histogram * h2)
144 {
145   size_t i;
146 
147   if (!gsl_histogram_equal_bins_p (h1, h2))
148     {
149       GSL_ERROR ("histograms have different binning", GSL_EINVAL);
150     }
151 
152   for (i = 0; i < h1->n; i++)
153     {
154       h1->bin[i] /= h2->bin[i];
155     }
156 
157   return GSL_SUCCESS;
158 }
159 
160 /*
161  * gsl_histogram_scale:
162  * scale a histogram by a numeric factor
163  */
164 
165 int
gsl_histogram_scale(gsl_histogram * h,double scale)166 gsl_histogram_scale (gsl_histogram * h, double scale)
167 {
168   size_t i;
169 
170   for (i = 0; i < h->n; i++)
171     {
172       h->bin[i] *= scale;
173     }
174 
175   return GSL_SUCCESS;
176 }
177 
178 /*
179  * gsl_histogram_shift:
180  * shift a histogram by a numeric offset
181  */
182 
183 int
gsl_histogram_shift(gsl_histogram * h,double shift)184 gsl_histogram_shift (gsl_histogram * h, double shift)
185 {
186   size_t i;
187 
188   for (i = 0; i < h->n; i++)
189     {
190       h->bin[i] += shift;
191     }
192 
193   return GSL_SUCCESS;
194 }
195