op_max_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2009 NICTA
00002 // 
00003 // Authors:
00004 // - Conrad Sanderson (conradsand at ieee dot org)
00005 // 
00006 // This file is part of the Armadillo C++ library.
00007 // It is provided without any warranty of fitness
00008 // for any purpose. You can redistribute this file
00009 // and/or modify it under the terms of the GNU
00010 // Lesser General Public License (LGPL) as published
00011 // by the Free Software Foundation, either version 3
00012 // of the License or (at your option) any later version.
00013 // (see http://www.opensource.org/licenses for more info)
00014 
00015 
00016 //! \addtogroup op_max
00017 //! @{
00018 
00019 
00020 //! find the maximum value in an array
00021 template<typename eT>
00022 inline 
00023 eT
00024 op_max::direct_max(const eT* const X, const u32 n_elem)
00025   {
00026   arma_extra_debug_sigprint();
00027   
00028   eT max_val = X[0];
00029   
00030   for(u32 i=1; i<n_elem; ++i)
00031     {
00032     const eT tmp_val = X[i];
00033     
00034     if(tmp_val > max_val)
00035       {
00036       max_val = tmp_val;
00037       }
00038     }
00039   
00040   return max_val;
00041   }
00042 
00043 
00044 
00045 //! find the maximum value in a subview
00046 template<typename eT>
00047 inline 
00048 eT
00049 op_max::direct_max(const subview<eT>& X)
00050   {
00051   arma_extra_debug_sigprint();
00052   
00053   eT max_val = X[0];
00054   
00055   for(u32 i=1; i<X.n_elem; ++i)
00056     {
00057     eT tmp_val = X[i];
00058     
00059     if(tmp_val > max_val)
00060       {
00061       max_val = tmp_val;
00062       }
00063     }
00064   
00065   return max_val;
00066   }
00067 
00068 
00069 
00070 //! find the maximum value in a diagview
00071 template<typename eT>
00072 inline 
00073 eT
00074 op_max::direct_max(const diagview<eT>& X)
00075   {
00076   arma_extra_debug_sigprint();
00077   
00078   eT max_val = X[0];
00079   
00080   for(u32 i=1; i<X.n_elem; ++i)
00081     {
00082     eT tmp_val = X[i];
00083     
00084     if(tmp_val > max_val)
00085       {
00086       max_val = tmp_val;
00087       }
00088     }
00089   
00090   return max_val;
00091   }
00092 
00093 
00094 
00095 //! \brief
00096 //! For each row or for each column, find the maximum value.
00097 //! The result is stored in a dense matrix that has either one column or one row.
00098 //! The dimension, for which the maxima are found, is set via the max() function.
00099 template<typename T1>
00100 inline
00101 void
00102 op_max::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_max>& in)
00103   {
00104   arma_extra_debug_sigprint();
00105   
00106   typedef typename T1::elem_type eT;
00107   
00108   const unwrap_check<T1> tmp(in.m, out);
00109   const Mat<eT>& X = tmp.M;
00110   
00111   arma_debug_check( (X.n_elem == 0), "op_max::apply(): given matrix has no elements" );
00112   
00113   const u32 dim = in.aux_u32_a;
00114   arma_debug_check( (dim > 1), "op_max::apply(): incorrect usage. dim must be 0 or 1");
00115   
00116   
00117   if(dim == 0)
00118     {
00119     arma_extra_debug_print("op_max::apply(), dim = 0");
00120     
00121     out.set_size(1, X.n_cols);
00122     
00123     for(u32 col=0; col<X.n_cols; ++col)
00124       {
00125       out[col] = op_max::direct_max( X.colptr(col), X.n_rows );
00126       }
00127     }
00128   else
00129   if(dim == 1)
00130     {
00131     arma_extra_debug_print("op_max::apply(), dim = 1");
00132     
00133     out.set_size(X.n_rows, 1);
00134     
00135     for(u32 row=0; row<X.n_rows; ++row)
00136       {
00137       eT max_val = X.at(row,0);
00138       
00139       for(u32 col=1; col<X.n_cols; ++col)
00140         {
00141         const eT tmp_val = X.at(row,col);
00142         
00143         if(tmp_val > max_val)
00144           {
00145           max_val = tmp_val;
00146           }
00147         }
00148       
00149       out[row] = max_val;
00150       
00151       }
00152     
00153     }
00154   
00155   }
00156 
00157 
00158 
00159 //! Find the maximum value in an array (version for complex numbers)
00160 template<typename T>
00161 inline 
00162 std::complex<T>
00163 op_max::direct_max(const std::complex<T>* const X, const u32 n_elem)
00164   {
00165   arma_extra_debug_sigprint();
00166   
00167   u32 index   = 0;
00168   T   max_val = std::abs(X[index]);
00169   
00170   for(u32 i=1; i<n_elem; ++i)
00171     {
00172     const T tmp_val = std::abs(X[i]);
00173     
00174     if(tmp_val > max_val)
00175       {
00176       max_val = tmp_val;
00177       index   = i;
00178       }
00179     }
00180   
00181   return X[index];
00182   }
00183 
00184 
00185 
00186 //! Find the maximum value in a subview (version for complex numbers)
00187 template<typename T>
00188 inline 
00189 std::complex<T>
00190 op_max::direct_max(const subview< std::complex<T> >& X)
00191   {
00192   arma_extra_debug_sigprint();
00193   
00194   u32 index   = 0;
00195   T   max_val = std::abs(X[index]);
00196   
00197   for(u32 i=1; i<X.n_elem; ++i)
00198     {
00199     const T tmp_val = std::abs(X[i]);
00200     
00201     if(tmp_val > max_val)
00202       {
00203       max_val = tmp_val;
00204       index   = i;
00205       }
00206     }
00207   
00208   return X[index];
00209   }
00210 
00211 
00212 
00213 //! Find the maximum value in a diagview (version for complex numbers)
00214 template<typename T>
00215 inline 
00216 std::complex<T>
00217 op_max::direct_max(const diagview< std::complex<T> >& X)
00218   {
00219   arma_extra_debug_sigprint();
00220   
00221   u32 index   = 0;
00222   T   max_val = std::abs(X[index]);
00223   
00224   for(u32 i=1; i<X.n_elem; ++i)
00225     {
00226     const T tmp_val = std::abs(X[i]);
00227     
00228     if(tmp_val > max_val)
00229       {
00230       max_val = tmp_val;
00231       index   = i;
00232       }
00233     }
00234   
00235   return X[index];
00236   }
00237 
00238 
00239 
00240 //! Implementation for complex numbers
00241 template<typename T, typename T1>
00242 inline void op_max::apply(Mat< std::complex<T> >& out, const Op<T1,op_max>& in)
00243   {
00244   arma_extra_debug_sigprint();
00245   
00246   typedef typename std::complex<T> eT;
00247   isnt_same_type<eT, typename T1::elem_type>::check();
00248   
00249   const unwrap_check<T1> tmp(in.m, out);
00250   const Mat<eT>& X = tmp.M;
00251   
00252   arma_debug_check( (X.n_elem == 0), "op_max::apply(): given matrix has no elements" );
00253   
00254   const u32 dim = in.aux_u32_a;
00255   arma_debug_check( (dim > 1), "op_max::apply(): incorrect usage. dim must be 0 or 1");
00256   
00257   
00258   if(dim == 0)  // column-wise max
00259     {
00260     arma_extra_debug_print("op_max::apply(), dim = 0");
00261     
00262     out.set_size(1, X.n_cols);
00263     
00264     for(u32 col=0; col<X.n_cols; ++col)
00265       {
00266       out[col] = op_max::direct_max( X.colptr(col), X.n_rows );
00267       }
00268     }
00269   else
00270   if(dim == 1)  // row-wise max
00271     {
00272     arma_extra_debug_print("op_max::apply(), dim = 1");
00273     
00274     out.set_size(X.n_rows, 1);
00275     
00276     for(u32 row=0; row<X.n_rows; ++row)
00277       {
00278       u32 index   = 0;
00279       T   max_val = std::abs(X.at(row,index));
00280       
00281       for(u32 col=1; col<X.n_cols; ++col)
00282         {
00283         const T tmp_val = std::abs(X.at(row,col));
00284         
00285         if(tmp_val > max_val)
00286           {
00287           max_val = tmp_val;
00288           index   = col;
00289           }
00290         }
00291       
00292       out[row] = X.at(row,index);
00293       }
00294     
00295     }
00296   
00297   }
00298 
00299 
00300 
00301 //! @}