op_min_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_min::direct_min(const eT* const X, const uword n_elem)
00024   {
00025   arma_extra_debug_sigprint();
00026   
00027   eT min_val = priv::most_pos<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 < min_val)
00037       {
00038       min_val = X_i;
00039       }
00040     
00041     if(X_j < min_val)
00042       {
00043       min_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 < min_val)
00053       {
00054       min_val = X_i;
00055       }
00056     }
00057   
00058   return min_val;
00059   }
00060 
00061 
00062 
00063 template<typename eT>
00064 inline 
00065 eT
00066 op_min::direct_min(const eT* const X, const uword n_elem, uword& index_of_min_val)
00067   {
00068   arma_extra_debug_sigprint();
00069   
00070   eT min_val = priv::most_pos<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 < min_val)
00082       {
00083       min_val    = X_i;
00084       best_index = i;
00085       }
00086     
00087     if(X_j < min_val)
00088       {
00089       min_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 < min_val)
00100       {
00101       min_val    = X_i;
00102       best_index = i;
00103       }
00104     }
00105   
00106   index_of_min_val = best_index;
00107   
00108   return min_val;
00109   }  
00110 
00111 
00112 
00113 template<typename eT>
00114 inline 
00115 eT
00116 op_min::direct_min(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 min_val = priv::most_pos<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 < min_val)
00129       {
00130       min_val = tmp_val;
00131       }
00132     }
00133   
00134   return min_val;
00135   }
00136 
00137 
00138 
00139 template<typename eT>
00140 inline 
00141 eT
00142 op_min::direct_min(const subview<eT>& X)
00143   {
00144   arma_extra_debug_sigprint();
00145   
00146   const uword X_n_elem = X.n_elem;
00147   
00148   eT min_val = priv::most_pos<eT>();
00149   
00150   for(uword i=0; i<X_n_elem; ++i)
00151     {
00152     eT tmp_val = X[i];
00153     
00154     if(tmp_val < min_val)
00155       {
00156       min_val = tmp_val;
00157       }
00158     }
00159   
00160   return min_val;
00161   }
00162 
00163 
00164 
00165 template<typename eT>
00166 inline 
00167 eT
00168 op_min::direct_min(const diagview<eT>& X)
00169   {
00170   arma_extra_debug_sigprint();
00171   
00172   const uword X_n_elem = X.n_elem;
00173   
00174   eT min_val = priv::most_pos<eT>();;
00175   
00176   for(uword i=0; i<X_n_elem; ++i)
00177     {
00178     eT tmp_val = X[i];
00179     
00180     if(tmp_val < min_val)
00181       {
00182       min_val = tmp_val;
00183       }
00184     }
00185   
00186   return min_val;
00187   }
00188 
00189 
00190 
00195 template<typename T1>
00196 inline void op_min::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_min>& in)
00197   {
00198   arma_extra_debug_sigprint();
00199   
00200   typedef typename T1::elem_type eT;
00201   
00202   const unwrap_check<T1> tmp(in.m, out);
00203   const Mat<eT>&     X = tmp.M;
00204   
00205   const uword dim = in.aux_uword_a;
00206   arma_debug_check( (dim > 1), "min(): incorrect usage. dim must be 0 or 1");
00207   
00208   const uword X_n_rows = X.n_rows;
00209   const uword X_n_cols = X.n_cols;
00210   
00211   if(dim == 0)  // min in each column
00212     {
00213     arma_extra_debug_print("op_min::apply(), dim = 0");
00214     
00215     arma_debug_check( (X_n_rows == 0), "min(): given object has zero rows" );
00216 
00217     out.set_size(1, X_n_cols);
00218     
00219     eT* out_mem = out.memptr();
00220     
00221     for(uword col=0; col<X_n_cols; ++col)
00222       {
00223       out_mem[col] = op_min::direct_min( X.colptr(col), X_n_rows );
00224       }
00225     }
00226   else
00227   if(dim == 1)  // min in each row
00228     {
00229     arma_extra_debug_print("op_min::apply(), dim = 1");
00230     
00231     arma_debug_check( (X_n_cols == 0), "min(): given object has zero columns" );
00232 
00233     out.set_size(X_n_rows, 1);
00234     
00235     eT* out_mem = out.memptr();
00236     
00237     for(uword row=0; row<X_n_rows; ++row)
00238       {
00239       out_mem[row] = op_min::direct_min( X, row );
00240       }
00241     }
00242   }
00243 
00244 
00245 
00246 template<typename T>
00247 inline 
00248 std::complex<T>
00249 op_min::direct_min(const std::complex<T>* const X, const uword n_elem)
00250   {
00251   arma_extra_debug_sigprint();
00252   
00253   uword index   = 0;
00254   T   min_val = priv::most_pos<T>();
00255   
00256   for(uword i=0; i<n_elem; ++i)
00257     {
00258     const T tmp_val = std::abs(X[i]);
00259     
00260     if(tmp_val < min_val)
00261       {
00262       min_val = tmp_val;
00263       index   = i;
00264       }
00265     }
00266   
00267   return X[index];
00268   }
00269 
00270 
00271 
00272 template<typename T>
00273 inline 
00274 std::complex<T>
00275 op_min::direct_min(const std::complex<T>* const X, const uword n_elem, uword& index_of_min_val)
00276   {
00277   arma_extra_debug_sigprint();
00278   
00279   uword index   = 0;
00280   T   min_val = priv::most_pos<T>();
00281   
00282   for(uword i=0; i<n_elem; ++i)
00283     {
00284     const T tmp_val = std::abs(X[i]);
00285     
00286     if(tmp_val < min_val)
00287       {
00288       min_val = tmp_val;
00289       index   = i;
00290       }
00291     }
00292   
00293   index_of_min_val = index;
00294   
00295   return X[index];
00296   }
00297 
00298 
00299 
00300 template<typename T>
00301 inline 
00302 std::complex<T>
00303 op_min::direct_min(const Mat< std::complex<T> >& X, const uword row)
00304   {
00305   arma_extra_debug_sigprint();
00306   
00307   const uword X_n_cols = X.n_cols;
00308   
00309   uword index   = 0;
00310   T   min_val = priv::most_pos<T>();
00311   
00312   for(uword col=0; col<X_n_cols; ++col)
00313     {
00314     const T tmp_val = std::abs(X.at(row,col));
00315     
00316     if(tmp_val < min_val)
00317       {
00318       min_val = tmp_val;
00319       index   = col;
00320       }
00321     }
00322   
00323   return X.at(row,index);
00324   }
00325 
00326 
00327 
00328 template<typename T>
00329 inline 
00330 std::complex<T>
00331 op_min::direct_min(const subview< std::complex<T> >& X)
00332   {
00333   arma_extra_debug_sigprint();
00334   
00335   const uword X_n_elem = X.n_elem;
00336         uword index    = 0;
00337         T   min_val  = priv::most_pos<T>();
00338   
00339   for(uword i=0; i<X_n_elem; ++i)
00340     {
00341     const T tmp_val = std::abs(X[i]);
00342     
00343     if(tmp_val < min_val)
00344       {
00345       min_val = tmp_val;
00346       index   = i;
00347       }
00348     }
00349   
00350   return X[index];
00351   }
00352 
00353 
00354 
00355 template<typename T>
00356 inline 
00357 std::complex<T>
00358 op_min::direct_min(const diagview< std::complex<T> >& X)
00359   {
00360   arma_extra_debug_sigprint();
00361   
00362   const uword X_n_elem = X.n_elem;
00363         uword index    = 0;
00364         T   min_val  = priv::most_pos<T>();
00365   
00366   for(uword i=0; i<X_n_elem; ++i)
00367     {
00368     const T tmp_val = std::abs(X[i]);
00369     
00370     if(tmp_val < min_val)
00371       {
00372       min_val = tmp_val;
00373       index   = i;
00374       }
00375     }
00376   
00377   return X[index];
00378   }
00379 
00380 
00381 


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