IT++ Logo

min_max.h

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

Generated on Sat Feb 26 2011 16:06:31 for IT++ by Doxygen 1.7.3