1 /* gsl_histogram_oper.c
2 * Copyright (C) 2000 Simone Piccardi
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.
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.
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.
19 /***************************************************************
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
35 ***************************************************************/
38 #include <gsl/gsl_errno.h>
39 #include <gsl/gsl_histogram.h>
42 * gsl_histogram_same_binning:
43 * control if two histograms have the
48 gsl_histogram_equal_bins_p (const gsl_histogram * h1, const gsl_histogram * h2)
59 for (i = 0; i <= h1->n; i++)
61 if (h1->range[i] != h2->range[i])
76 gsl_histogram_add (gsl_histogram * h1, const gsl_histogram * h2)
80 if (!gsl_histogram_equal_bins_p (h1, h2))
82 GSL_ERROR ("histograms have different binning", GSL_EINVAL);
85 for (i = 0; i < h1->n; i++)
87 h1->bin[i] += h2->bin[i];
95 * subtract two histograms
99 gsl_histogram_sub (gsl_histogram * h1, const gsl_histogram * h2)
103 if (!gsl_histogram_equal_bins_p (h1, h2))
105 GSL_ERROR ("histograms have different binning", GSL_EINVAL);
108 for (i = 0; i < h1->n; i++)
110 h1->bin[i] -= h2->bin[i];
118 * gsl_histogram_mult:
119 * multiply two histograms
123 gsl_histogram_mul (gsl_histogram * h1, const gsl_histogram * h2)
127 if (!gsl_histogram_equal_bins_p (h1, h2))
129 GSL_ERROR ("histograms have different binning", GSL_EINVAL);
132 for (i = 0; i < h1->n; i++)
134 h1->bin[i] *= h2->bin[i];
141 * divide two histograms
144 gsl_histogram_div (gsl_histogram * h1, const gsl_histogram * h2)
148 if (!gsl_histogram_equal_bins_p (h1, h2))
150 GSL_ERROR ("histograms have different binning", GSL_EINVAL);
153 for (i = 0; i < h1->n; i++)
155 h1->bin[i] /= h2->bin[i];
162 * gsl_histogram_scale:
163 * scale a histogram by a numeric factor
167 gsl_histogram_scale (gsl_histogram * h, double scale)
171 for (i = 0; i < h->n; i++)
180 * gsl_histogram_shift:
181 * shift a histogram by a numeric offset
185 gsl_histogram_shift (gsl_histogram * h, double shift)
189 for (i = 0; i < h->n; i++)