| 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 | /* |
| 42 | * gsl_histogram_same_binning: |
| 43 | * control if two histograms have the |
| 44 | * same binning |
| 45 | */ |
| 46 | |
| 47 | int |
| 48 | gsl_histogram_equal_bins_p (const gsl_histogram * h1, const gsl_histogram * h2) |
| 49 | { |
| 50 | if (h1->n != h2->n) |
| 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | { |
| 56 | size_t i; |
| 57 | /* init ranges */ |
| 58 | |
| 59 | for (i = 0; i <= h1->n; i++) |
| 60 | { |
| 61 | if (h1->range[i] != h2->range[i]) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * gsl_histogram_add: |
| 73 | * add two histograms |
| 74 | */ |
| 75 | int |
| 76 | gsl_histogram_add (gsl_histogram * h1, const gsl_histogram * h2) |
| 77 | { |
| 78 | size_t i; |
| 79 | |
| 80 | if (!gsl_histogram_equal_bins_p (h1, h2)) |
| 81 | { |
| 82 | GSL_ERROR ("histograms have different binning", GSL_EINVAL); |
| 83 | } |
| 84 | |
| 85 | for (i = 0; i < h1->n; i++) |
| 86 | { |
| 87 | h1->bin[i] += h2->bin[i]; |
| 88 | } |
| 89 | |
| 90 | return GSL_SUCCESS; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * gsl_histogram_sub: |
| 95 | * subtract two histograms |
| 96 | */ |
| 97 | |
| 98 | int |
| 99 | gsl_histogram_sub (gsl_histogram * h1, const gsl_histogram * h2) |
| 100 | { |
| 101 | size_t i; |
| 102 | |
| 103 | if (!gsl_histogram_equal_bins_p (h1, h2)) |
| 104 | { |
| 105 | GSL_ERROR ("histograms have different binning", GSL_EINVAL); |
| 106 | } |
| 107 | |
| 108 | for (i = 0; i < h1->n; i++) |
| 109 | { |
| 110 | h1->bin[i] -= h2->bin[i]; |
| 111 | } |
| 112 | |
| 113 | return GSL_SUCCESS; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * gsl_histogram_mult: |
| 119 | * multiply two histograms |
| 120 | */ |
| 121 | |
| 122 | int |
| 123 | gsl_histogram_mul (gsl_histogram * h1, const gsl_histogram * h2) |
| 124 | { |
| 125 | size_t i; |
| 126 | |
| 127 | if (!gsl_histogram_equal_bins_p (h1, h2)) |
| 128 | { |
| 129 | GSL_ERROR ("histograms have different binning", GSL_EINVAL); |
| 130 | } |
| 131 | |
| 132 | for (i = 0; i < h1->n; i++) |
| 133 | { |
| 134 | h1->bin[i] *= h2->bin[i]; |
| 135 | } |
| 136 | |
| 137 | return GSL_SUCCESS; |
| 138 | } |
| 139 | /* |
| 140 | * gsl_histogram_div: |
| 141 | * divide two histograms |
| 142 | */ |
| 143 | int |
| 144 | gsl_histogram_div (gsl_histogram * h1, const gsl_histogram * h2) |
| 145 | { |
| 146 | size_t i; |
| 147 | |
| 148 | if (!gsl_histogram_equal_bins_p (h1, h2)) |
| 149 | { |
| 150 | GSL_ERROR ("histograms have different binning", GSL_EINVAL); |
| 151 | } |
| 152 | |
| 153 | for (i = 0; i < h1->n; i++) |
| 154 | { |
| 155 | h1->bin[i] /= h2->bin[i]; |
| 156 | } |
| 157 | |
| 158 | return GSL_SUCCESS; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * gsl_histogram_scale: |
| 163 | * scale a histogram by a numeric factor |
| 164 | */ |
| 165 | |
| 166 | int |
| 167 | gsl_histogram_scale (gsl_histogram * h, double scale) |
| 168 | { |
| 169 | size_t i; |
| 170 | |
| 171 | for (i = 0; i < h->n; i++) |
| 172 | { |
| 173 | h->bin[i] *= scale; |
| 174 | } |
| 175 | |
| 176 | return GSL_SUCCESS; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * gsl_histogram_shift: |
| 181 | * shift a histogram by a numeric offset |
| 182 | */ |
| 183 | |
| 184 | int |
| 185 | gsl_histogram_shift (gsl_histogram * h, double shift) |
| 186 | { |
| 187 | size_t i; |
| 188 | |
| 189 | for (i = 0; i < h->n; i++) |
| 190 | { |
| 191 | h->bin[i] += shift; |
| 192 | } |
| 193 | |
| 194 | return GSL_SUCCESS; |
| 195 | } |