| 1 | /*! |
|---|
| 2 | * \file |
|---|
| 3 | * \brief Minimum and maximum functions on vectors and matrices |
|---|
| 4 | * \author Tony Ottosson, Johan Bergman and Adam Piatyszek |
|---|
| 5 | * |
|---|
| 6 | * ------------------------------------------------------------------------- |
|---|
| 7 | * |
|---|
| 8 | * IT++ - C++ library of mathematical, signal processing, speech processing, |
|---|
| 9 | * and communications classes and functions |
|---|
| 10 | * |
|---|
| 11 | * Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) |
|---|
| 12 | * |
|---|
| 13 | * This program is free software; you can redistribute it and/or modify |
|---|
| 14 | * it under the terms of the GNU General Public License as published by |
|---|
| 15 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 16 | * (at your option) any later version. |
|---|
| 17 | * |
|---|
| 18 | * This program is distributed in the hope that it will be useful, |
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 21 | * GNU General Public License for more details. |
|---|
| 22 | * |
|---|
| 23 | * You should have received a copy of the GNU General Public License |
|---|
| 24 | * along with this program; if not, write to the Free Software |
|---|
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 26 | * |
|---|
| 27 | * ------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #ifndef MIN_MAX_H |
|---|
| 31 | #define MIN_MAX_H |
|---|
| 32 | |
|---|
| 33 | #include <itpp/base/mat.h> |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | namespace itpp { |
|---|
| 37 | |
|---|
| 38 | /*! |
|---|
| 39 | * \addtogroup miscfunc |
|---|
| 40 | * @{ |
|---|
| 41 | */ |
|---|
| 42 | |
|---|
| 43 | //! Maximum value of vector |
|---|
| 44 | template<class T> |
|---|
| 45 | T max(const Vec<T> &v) |
|---|
| 46 | { |
|---|
| 47 | T maxdata = v(0); |
|---|
| 48 | for (int i = 1; i < v.length(); i++) |
|---|
| 49 | if (v(i) > maxdata) |
|---|
| 50 | maxdata = v(i); |
|---|
| 51 | return maxdata; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | //! Maximum value of vector, also returns the index position of max value |
|---|
| 55 | template<class T> |
|---|
| 56 | T max(const Vec<T> &v, int& index) |
|---|
| 57 | { |
|---|
| 58 | T maxdata = v(0); |
|---|
| 59 | index = 0; |
|---|
| 60 | for (int i = 1; i < v.length(); i++) |
|---|
| 61 | if (v(i) > maxdata) { |
|---|
| 62 | maxdata = v(i); |
|---|
| 63 | index = i; |
|---|
| 64 | } |
|---|
| 65 | return maxdata; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /*! |
|---|
| 69 | * Maximum values over each row/column in the matrix \c m |
|---|
| 70 | * |
|---|
| 71 | * <tt>max(m) = max(m, 1)</tt> returns a vector where the elements are |
|---|
| 72 | * maximum over each column, whereas <tt>max(m, 2)</tt> returns a vector |
|---|
| 73 | * where the elements are maximum over each row. |
|---|
| 74 | */ |
|---|
| 75 | template<class T> |
|---|
| 76 | Vec<T> max(const Mat<T> &m, int dim = 1) |
|---|
| 77 | { |
|---|
| 78 | it_assert((dim == 1) || (dim == 2), "max(): dimension need to be 1 or 2"); |
|---|
| 79 | Vec<T> out; |
|---|
| 80 | if (dim == 1) { |
|---|
| 81 | out.set_size(m.cols(), false); |
|---|
| 82 | for (int i = 0; i < m.cols(); i++) |
|---|
| 83 | out(i) = max(m.get_col(i)); |
|---|
| 84 | } |
|---|
| 85 | else { |
|---|
| 86 | out.set_size(m.rows(), false); |
|---|
| 87 | for (int i = 0; i < m.rows(); i++) |
|---|
| 88 | out(i) = max(m.get_row(i)); |
|---|
| 89 | } |
|---|
| 90 | return out; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /*! |
|---|
| 94 | * Maximum values over each row/column in the matrix \c m |
|---|
| 95 | * |
|---|
| 96 | * <tt>max(m) = max(m, 1)</tt> returns a vector where the elements are |
|---|
| 97 | * maximum over each column, whereas <tt>max(m, 2)</tt> returns a vector |
|---|
| 98 | * where the elements are maximum over each row. |
|---|
| 99 | * |
|---|
| 100 | * Also returns a vector of indices with positions of maximum value within |
|---|
| 101 | * a column/row. |
|---|
| 102 | */ |
|---|
| 103 | template<class T> |
|---|
| 104 | Vec<T> max(const Mat<T> &m, ivec &index, int dim = 1) |
|---|
| 105 | { |
|---|
| 106 | it_assert((dim == 1) || (dim == 2), "max(): dimension need to be 1 or 2"); |
|---|
| 107 | Vec<T> out; |
|---|
| 108 | if (dim == 1) { |
|---|
| 109 | out.set_size(m.cols(), false); |
|---|
| 110 | index.set_size(m.cols(), false); |
|---|
| 111 | for (int i = 0; i < m.cols(); i++) |
|---|
| 112 | out(i) = max(m.get_col(i), index(i)); |
|---|
| 113 | } |
|---|
| 114 | else { |
|---|
| 115 | out.set_size(m.rows(), false); |
|---|
| 116 | index.set_size(m.rows(), false); |
|---|
| 117 | for (int i = 0; i < m.rows(); i++) |
|---|
| 118 | out(i) = max(m.get_row(i), index(i)); |
|---|
| 119 | } |
|---|
| 120 | return out; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | //! Minimum value of vector |
|---|
| 124 | template<class T> |
|---|
| 125 | T min(const Vec<T> &in) |
|---|
| 126 | { |
|---|
| 127 | T mindata = in[0]; |
|---|
| 128 | for (int i = 1; i < in.length(); i++) |
|---|
| 129 | if (in[i] < mindata) |
|---|
| 130 | mindata = in[i]; |
|---|
| 131 | return mindata; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | //! Minimum value of vector, also returns the index position of min value |
|---|
| 135 | template<class T> |
|---|
| 136 | T min(const Vec<T> &in, int& index) |
|---|
| 137 | { |
|---|
| 138 | T mindata = in[0]; |
|---|
| 139 | index = 0; |
|---|
| 140 | for (int i = 1; i < in.length(); i++) |
|---|
| 141 | if (in[i] < mindata) { |
|---|
| 142 | mindata = in[i]; |
|---|
| 143 | index = i; |
|---|
| 144 | } |
|---|
| 145 | return mindata; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | /*! |
|---|
| 150 | * Minimum values over each row/column in the matrix \c m |
|---|
| 151 | * |
|---|
| 152 | * <tt>min(m) = min(m, 1)</tt> returns a vector where the elements are |
|---|
| 153 | * minimum over each column, whereas <tt>min(m, 2)</tt> returns a vector |
|---|
| 154 | * where the elements are minimum over each row. |
|---|
| 155 | */ |
|---|
| 156 | template<class T> |
|---|
| 157 | Vec<T> min(const Mat<T> &m, int dim=1) |
|---|
| 158 | { |
|---|
| 159 | it_assert((dim == 1) || (dim == 2), "min(): dimension need to be 1 or 2"); |
|---|
| 160 | Vec<T> out; |
|---|
| 161 | if (dim == 1) { |
|---|
| 162 | out.set_size(m.cols(), false); |
|---|
| 163 | for (int i = 0; i < m.cols(); i++) |
|---|
| 164 | out(i) = min(m.get_col(i)); |
|---|
| 165 | } |
|---|
| 166 | else { |
|---|
| 167 | out.set_size(m.rows(), false); |
|---|
| 168 | for (int i = 0; i < m.rows(); i++) |
|---|
| 169 | out(i) = min(m.get_row(i)); |
|---|
| 170 | } |
|---|
| 171 | return out; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | /*! |
|---|
| 176 | * Minimum values over each row/column in the matrix \c m |
|---|
| 177 | * |
|---|
| 178 | * <tt>min(m) = min(m, 1)</tt> returns a vector where the elements are |
|---|
| 179 | * minimum over each column, whereas <tt>min(m, 2)</tt> returns a vector |
|---|
| 180 | * where the elements are minimum over each row. |
|---|
| 181 | * |
|---|
| 182 | * Also returns a vector of indices with positions of minimum value within |
|---|
| 183 | * a column/row. |
|---|
| 184 | */ |
|---|
| 185 | template<class T> |
|---|
| 186 | Vec<T> min(const Mat<T> &m, ivec &index, int dim=1) |
|---|
| 187 | { |
|---|
| 188 | it_assert((dim == 1) || (dim == 2), "min(): dimension need to be 1 or 2"); |
|---|
| 189 | Vec<T> out; |
|---|
| 190 | if (dim == 1) { |
|---|
| 191 | out.set_size(m.cols(), false); |
|---|
| 192 | index.set_size(m.cols(), false); |
|---|
| 193 | for (int i = 0; i < m.cols(); i++) |
|---|
| 194 | out(i) = min(m.get_col(i), index(i)); |
|---|
| 195 | } |
|---|
| 196 | else { |
|---|
| 197 | out.set_size(m.rows(), false); |
|---|
| 198 | index.set_size(m.rows(), false); |
|---|
| 199 | for (int i = 0; i < m.rows(); i++) |
|---|
| 200 | out(i) = min(m.get_row(i), index(i)); |
|---|
| 201 | } |
|---|
| 202 | return out; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | //! Return the postion of the maximum element in the vector |
|---|
| 207 | template<class T> |
|---|
| 208 | int max_index(const Vec<T> &in) |
|---|
| 209 | { |
|---|
| 210 | int maxindex = 0; |
|---|
| 211 | for (int i = 1; i < in.length(); i++) |
|---|
| 212 | if (in[i] > in[maxindex]) |
|---|
| 213 | maxindex = i; |
|---|
| 214 | return maxindex; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | //! Return the postion of the maximum element in the matrix |
|---|
| 218 | template<class T> |
|---|
| 219 | void max_index(const Mat<T> &m, int &row, int &col) |
|---|
| 220 | { |
|---|
| 221 | T maxdata = m(0, 0); |
|---|
| 222 | row = col = 0; |
|---|
| 223 | for (int i = 0; i < m.rows(); i++) |
|---|
| 224 | for (int j = 0; j < m.cols(); j++) |
|---|
| 225 | if (m(i, j) > maxdata) { |
|---|
| 226 | row = i; |
|---|
| 227 | col = j; |
|---|
| 228 | maxdata = m(i, j); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | //! Return the postion of the minimum element in the vector |
|---|
| 233 | template<class T> |
|---|
| 234 | int min_index(const Vec<T> &in) |
|---|
| 235 | { |
|---|
| 236 | int minindex = 0; |
|---|
| 237 | for (int i = 1; i < in.length(); i++) |
|---|
| 238 | if (in[i] < in[minindex]) |
|---|
| 239 | minindex = i; |
|---|
| 240 | return minindex; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | //! Return the postion of the minimum element in the matrix |
|---|
| 244 | template<class T> |
|---|
| 245 | void min_index(const Mat<T> &m, int &row, int &col) |
|---|
| 246 | { |
|---|
| 247 | T mindata = m(0, 0); |
|---|
| 248 | row = col = 0; |
|---|
| 249 | for (int i = 0; i < m.rows(); i++) |
|---|
| 250 | for (int j = 0; j < m.cols(); j++) |
|---|
| 251 | if (m(i, j) < mindata) { |
|---|
| 252 | row = i; |
|---|
| 253 | col = j; |
|---|
| 254 | mindata = m(i, j); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /*! |
|---|
| 259 | * @} |
|---|
| 260 | */ |
|---|
| 261 | |
|---|
| 262 | } //namespace itpp |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | #endif /* MIN_MAX_H */ |
|---|