op_max_meat.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
00002 // Copyright (C) 2008-2011 Conrad Sanderson
00003 // 
00004 // This file is part of the Armadillo C++ library.
00005 // It is provided without any warranty of fitness
00006 // for any purpose. You can redistribute this file
00007 // and/or modify it under the terms of the GNU
00008 // Lesser General Public License (LGPL) as published
00009 // by the Free Software Foundation, either version 3
00010 // of the License or (at your option) any later version.
00011 // (see http://www.opensource.org/licenses for more info)
00012 
00013 
00016 
00017 
00018 
00019 template<typename eT>
00020 arma_pure
00021 inline
00022 eT
00023 op_max::direct_max(const eT* const X, const uword n_elem)
00024   {
00025   arma_extra_debug_sigprint();
00026   
00027   eT max_val = priv::most_neg<eT>();
00028   
00029   uword i,j;
00030   
00031   for(i=0, j=1; j<n_elem; i+=2, j+=2)
00032     {
00033     const eT X_i = X[i];
00034     const eT X_j = X[j];
00035     
00036     if(X_i > max_val)
00037       {
00038       max_val = X_i;
00039       }
00040     
00041     if(X_j > max_val)
00042       {
00043       max_val = X_j;
00044       }
00045     }
00046   
00047   
00048   if(i < n_elem)
00049     {
00050     const eT X_i = X[i];
00051     
00052     if(X_i > max_val)
00053       {
00054       max_val = X_i;
00055       }
00056     }
00057   
00058   return max_val;
00059   }
00060 
00061 
00062 
00063 template<typename eT>
00064 inline
00065 eT
00066 op_max::direct_max(const eT* const X, const uword n_elem, uword& index_of_max_val)
00067   {
00068   arma_extra_debug_sigprint();
00069   
00070   eT max_val = priv::most_neg<eT>();
00071   
00072   uword best_index = 0;
00073   
00074   uword i,j;
00075   
00076   for(i=0, j=1; j<n_elem; i+=2, j+=2)
00077     {
00078     const eT X_i = X[i];
00079     const eT X_j = X[j];
00080     
00081     if(X_i > max_val)
00082       {
00083       max_val    = X_i;
00084       best_index = i;
00085       }
00086     
00087     if(X_j > max_val)
00088       {
00089       max_val    = X_j;
00090       best_index = j;
00091       }
00092     }
00093   
00094   
00095   if(i < n_elem)
00096     {
00097     const eT X_i = X[i];
00098     
00099     if(X_i > max_val)
00100       {
00101       max_val    = X_i;
00102       best_index = i;
00103       }
00104     }
00105   
00106   index_of_max_val = best_index;
00107   
00108   return max_val;
00109   }
00110 
00111 
00112 
00113 template<typename eT>
00114 inline
00115 eT
00116 op_max::direct_max(const Mat<eT>& X, const uword row)
00117   {
00118   arma_extra_debug_sigprint();
00119   
00120   const uword X_n_cols = X.n_cols;
00121   
00122   eT max_val = priv::most_neg<eT>();
00123   
00124   for(uword col=0; col<X_n_cols; ++col)
00125     {
00126     const eT tmp_val = X.at(row,col);
00127     
00128     if(tmp_val > max_val)
00129       {
00130       max_val = tmp_val;
00131       }
00132     }
00133   
00134   return max_val;
00135   }
00136 
00137 
00138 
00139 template<typename eT>
00140 inline
00141 eT
00142 op_max::direct_max(const subview<eT>& X)
00143   {
00144   arma_extra_debug_sigprint();
00145   
00146   const uword X_n_elem = X.n_elem;
00147   
00148   eT max_val = priv::most_neg<eT>();
00149   
00150   for(uword i=0; i<X_n_elem; ++i)
00151     {
00152     eT tmp_val = X[i];
00153     
00154     if(tmp_val > max_val)
00155       {
00156       max_val = tmp_val;
00157       }
00158     }
00159   
00160   return max_val;
00161   }
00162 
00163 
00164 
00165 template<typename eT>
00166 inline
00167 eT
00168 op_max::direct_max(const diagview<eT>& X)
00169   {
00170   arma_extra_debug_sigprint();
00171   
00172   const uword X_n_elem = X.n_elem;
00173   
00174   eT max_val = priv::most_neg<eT>();
00175   
00176   for(uword i=0; i<X_n_elem; ++i)
00177     {
00178     eT tmp_val = X[i];
00179     
00180     if(tmp_val > max_val)
00181       {
00182       max_val = tmp_val;
00183       }
00184     }
00185   
00186   return max_val;
00187   }
00188 
00189 
00190 
00195 template<typename T1>
00196 inline
00197 void
00198 op_max::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_max>& in)
00199   {
00200   arma_extra_debug_sigprint();
00201   
00202   typedef typename T1::elem_type eT;
00203   
00204   const unwrap_check<T1> tmp(in.m, out);
00205   const Mat<eT>& X     = tmp.M;
00206   
00207   const uword dim = in.aux_uword_a;
00208   arma_debug_check( (dim > 1), "max(): incorrect usage. dim must be 0 or 1");
00209   
00210   const uword X_n_rows = X.n_rows;
00211   const uword X_n_cols = X.n_cols;
00212   
00213   if(dim == 0)
00214     {
00215     arma_extra_debug_print("op_max::apply(), dim = 0");
00216     
00217     arma_debug_check( (X_n_rows == 0), "max(): given object has zero rows" );
00218 
00219     out.set_size(1, X_n_cols);
00220     
00221     eT* out_mem = out.memptr();
00222     
00223     for(uword col=0; col<X_n_cols; ++col)
00224       {
00225       out_mem[col] = op_max::direct_max( X.colptr(col), X_n_rows );
00226       }
00227     }
00228   else
00229   if(dim == 1)
00230     {
00231     arma_extra_debug_print("op_max::apply(), dim = 1");
00232     
00233     arma_debug_check( (X_n_cols == 0), "max(): given object has zero columns" );
00234 
00235     out.set_size(X_n_rows, 1);
00236     
00237     eT* out_mem = out.memptr();
00238     
00239     for(uword row=0; row<X_n_rows; ++row)
00240       {
00241       out_mem[row] = op_max::direct_max( X, row );
00242       }
00243     }
00244   }
00245 
00246 
00247 
00248 template<typename T>
00249 inline
00250 std::complex<T>
00251 op_max::direct_max(const std::complex<T>* const X, const uword n_elem)
00252   {
00253   arma_extra_debug_sigprint();
00254   
00255   uword index   = 0;
00256   T   max_val = priv::most_neg<T>();
00257   
00258   for(uword i=0; i<n_elem; ++i)
00259     {
00260     const T tmp_val = std::abs(X[i]);
00261     
00262     if(tmp_val > max_val)
00263       {
00264       max_val = tmp_val;
00265       index   = i;
00266       }
00267     }
00268   
00269   return X[index];
00270   }
00271 
00272 
00273 
00274 template<typename T>
00275 inline
00276 std::complex<T>
00277 op_max::direct_max(const std::complex<T>* const X, const uword n_elem, uword& index_of_max_val)
00278   {
00279   arma_extra_debug_sigprint();
00280   
00281   uword index   = 0;
00282   T   max_val = priv::most_neg<T>();
00283   
00284   for(uword i=0; i<n_elem; ++i)
00285     {
00286     const T tmp_val = std::abs(X[i]);
00287     
00288     if(tmp_val > max_val)
00289       {
00290       max_val = tmp_val;
00291       index   = i;
00292       }
00293     }
00294   
00295   index_of_max_val = index;
00296   
00297   return X[index];
00298   }
00299 
00300 
00301 
00302 template<typename T>
00303 inline 
00304 std::complex<T>
00305 op_max::direct_max(const Mat< std::complex<T> >& X, const uword row)
00306   {
00307   arma_extra_debug_sigprint();
00308   
00309   const uword X_n_cols = X.n_cols;
00310   
00311   uword index   = 0;
00312   T   max_val = priv::most_neg<T>();
00313   
00314   for(uword col=0; col<X_n_cols; ++col)
00315     {
00316     const T tmp_val = std::abs(X.at(row,col));
00317     
00318     if(tmp_val > max_val)
00319       {
00320       max_val = tmp_val;
00321       index   = col;
00322       }
00323     }
00324   
00325   return X.at(row,index);
00326   }
00327 
00328 
00329 
00330 template<typename T>
00331 inline
00332 std::complex<T>
00333 op_max::direct_max(const subview< std::complex<T> >& X)
00334   {
00335   arma_extra_debug_sigprint();
00336   
00337   const uword X_n_elem = X.n_elem;
00338   
00339   uword index   = 0;
00340   T   max_val = priv::most_neg<T>();
00341   
00342   for(uword i=0; i<X_n_elem; ++i)
00343     {
00344     const T tmp_val = std::abs(X[i]);
00345     
00346     if(tmp_val > max_val)
00347       {
00348       max_val = tmp_val;
00349       index   = i;
00350       }
00351     }
00352   
00353   return X[index];
00354   }
00355 
00356 
00357 
00358 template<typename T>
00359 inline 
00360 std::complex<T>
00361 op_max::direct_max(const diagview< std::complex<T> >& X)
00362   {
00363   arma_extra_debug_sigprint();
00364   
00365   const uword X_n_elem = X.n_elem;
00366   
00367   uword index   = 0;
00368   T   max_val = priv::most_neg<T>();
00369   
00370   for(uword i=0; i<X_n_elem; ++i)
00371     {
00372     const T tmp_val = std::abs(X[i]);
00373     
00374     if(tmp_val > max_val)
00375       {
00376       max_val = tmp_val;
00377       index   = i;
00378       }
00379     }
00380   
00381   return X[index];
00382   }
00383 
00384 
00385 


armadillo_matrix
Author(s): Conrad Sanderson - NICTA (www.nicta.com.au), (Wrapper by Sjoerd van den Dries)
autogenerated on Tue Jan 7 2014 11:42:05