IT++ Logo
min_max.h
Go to the documentation of this file.
00001 
00029 #ifndef MIN_MAX_H
00030 #define MIN_MAX_H
00031 
00032 #include <itpp/base/mat.h>
00033 
00034 
00035 namespace itpp
00036 {
00037 
00043 
00044 template<class T>
00045 T max(const Vec<T> &v)
00046 {
00047   T maxdata = v(0);
00048   for (int i = 1; i < v.length(); i++)
00049     if (v(i) > maxdata)
00050       maxdata = v(i);
00051   return maxdata;
00052 }
00053 
00055 template<class T>
00056 T max(const Vec<T> &v, int& index)
00057 {
00058   T maxdata = v(0);
00059   index = 0;
00060   for (int i = 1; i < v.length(); i++)
00061     if (v(i) > maxdata) {
00062       maxdata = v(i);
00063       index = i;
00064     }
00065   return maxdata;
00066 }
00067 
00075 template<class T>
00076 Vec<T> max(const Mat<T> &m, int dim = 1)
00077 {
00078   it_assert((dim == 1) || (dim == 2), "max(): dimension need to be 1 or 2");
00079   Vec<T> out;
00080   if (dim == 1) {
00081     out.set_size(m.cols(), false);
00082     for (int i = 0; i < m.cols(); i++)
00083       out(i) = max(m.get_col(i));
00084   }
00085   else {
00086     out.set_size(m.rows(), false);
00087     for (int i = 0; i < m.rows(); i++)
00088       out(i) = max(m.get_row(i));
00089   }
00090   return out;
00091 }
00092 
00103 template<class T>
00104 Vec<T> max(const Mat<T> &m, ivec &index, int dim = 1)
00105 {
00106   it_assert((dim == 1) || (dim == 2), "max(): dimension need to be 1 or 2");
00107   Vec<T> out;
00108   if (dim == 1) {
00109     out.set_size(m.cols(), false);
00110     index.set_size(m.cols(), false);
00111     for (int i = 0; i < m.cols(); i++)
00112       out(i) = max(m.get_col(i), index(i));
00113   }
00114   else {
00115     out.set_size(m.rows(), false);
00116     index.set_size(m.rows(), false);
00117     for (int i = 0; i < m.rows(); i++)
00118       out(i) = max(m.get_row(i), index(i));
00119   }
00120   return out;
00121 }
00122 
00124 template<class T>
00125 T min(const Vec<T> &in)
00126 {
00127   T mindata = in[0];
00128   for (int i = 1; i < in.length(); i++)
00129     if (in[i] < mindata)
00130       mindata = in[i];
00131   return mindata;
00132 }
00133 
00135 template<class T>
00136 T min(const Vec<T> &in, int& index)
00137 {
00138   T mindata = in[0];
00139   index = 0;
00140   for (int i = 1; i < in.length(); i++)
00141     if (in[i] < mindata) {
00142       mindata = in[i];
00143       index = i;
00144     }
00145   return mindata;
00146 }
00147 
00148 
00156 template<class T>
00157 Vec<T> min(const Mat<T> &m, int dim = 1)
00158 {
00159   it_assert((dim == 1) || (dim == 2), "min(): dimension need to be 1 or 2");
00160   Vec<T> out;
00161   if (dim == 1) {
00162     out.set_size(m.cols(), false);
00163     for (int i = 0; i < m.cols(); i++)
00164       out(i) = min(m.get_col(i));
00165   }
00166   else {
00167     out.set_size(m.rows(), false);
00168     for (int i = 0; i < m.rows(); i++)
00169       out(i) = min(m.get_row(i));
00170   }
00171   return out;
00172 }
00173 
00174 
00185 template<class T>
00186 Vec<T> min(const Mat<T> &m,  ivec &index, int dim = 1)
00187 {
00188   it_assert((dim == 1) || (dim == 2), "min(): dimension need to be 1 or 2");
00189   Vec<T> out;
00190   if (dim == 1) {
00191     out.set_size(m.cols(), false);
00192     index.set_size(m.cols(), false);
00193     for (int i = 0; i < m.cols(); i++)
00194       out(i) = min(m.get_col(i), index(i));
00195   }
00196   else {
00197     out.set_size(m.rows(), false);
00198     index.set_size(m.rows(), false);
00199     for (int i = 0; i < m.rows(); i++)
00200       out(i) = min(m.get_row(i), index(i));
00201   }
00202   return out;
00203 }
00204 
00205 
00207 template<class T>
00208 int max_index(const Vec<T> &in)
00209 {
00210   int maxindex = 0;
00211   for (int i = 1; i < in.length(); i++)
00212     if (in[i] > in[maxindex])
00213       maxindex = i;
00214   return maxindex;
00215 }
00216 
00218 template<class T>
00219 void max_index(const Mat<T> &m, int &row, int &col)
00220 {
00221   T maxdata = m(0, 0);
00222   row = col = 0;
00223   for (int i = 0; i < m.rows(); i++)
00224     for (int j = 0; j < m.cols(); j++)
00225       if (m(i, j) > maxdata) {
00226         row = i;
00227         col = j;
00228         maxdata = m(i, j);
00229       }
00230 }
00231 
00233 template<class T>
00234 int min_index(const Vec<T> &in)
00235 {
00236   int minindex = 0;
00237   for (int i = 1; i < in.length(); i++)
00238     if (in[i] < in[minindex])
00239       minindex = i;
00240   return minindex;
00241 }
00242 
00244 template<class T>
00245 void min_index(const Mat<T> &m, int &row, int &col)
00246 {
00247   T mindata = m(0, 0);
00248   row = col = 0;
00249   for (int i = 0; i < m.rows(); i++)
00250     for (int j = 0; j < m.cols(); j++)
00251       if (m(i, j) < mindata) {
00252         row = i;
00253         col = j;
00254         mindata = m(i, j);
00255       }
00256 }
00257 
00262 } //namespace itpp
00263 
00264 
00265 #endif /* MIN_MAX_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SourceForge Logo

Generated on Sat Jul 9 2011 15:21:30 for IT++ by Doxygen 1.7.4