fn_accu.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA and the authors listed below
00002 // http://nicta.com.au
00003 // 
00004 // Authors:
00005 // - Conrad Sanderson (conradsand at ieee dot org)
00006 // 
00007 // This file is part of the Armadillo C++ library.
00008 // It is provided without any warranty of fitness
00009 // for any purpose. You can redistribute this file
00010 // and/or modify it under the terms of the GNU
00011 // Lesser General Public License (LGPL) as published
00012 // by the Free Software Foundation, either version 3
00013 // of the License or (at your option) any later version.
00014 // (see http://www.opensource.org/licenses for more info)
00015 
00016 
00017 //! \addtogroup fn_accu
00018 //! @{
00019 
00020 
00021 
00022 template<typename T1>
00023 arma_hot
00024 inline
00025 typename T1::elem_type
00026 accu_unwrap(const Base<typename T1::elem_type,T1>& X)
00027   {
00028   arma_extra_debug_sigprint();
00029   
00030   typedef typename T1::elem_type eT;
00031   
00032   const unwrap<T1>   tmp(X.get_ref());
00033   const Mat<eT>& A = tmp.M;
00034   
00035   const eT* A_mem = A.memptr();
00036   const u32 N     = A.n_elem;
00037   
00038   eT val1 = eT(0);
00039   eT val2 = eT(0);
00040   
00041   u32 i,j;
00042   
00043   for(i=0, j=1; j<N; i+=2, j+=2)
00044     {
00045     val1 += A_mem[i];
00046     val2 += A_mem[j];
00047     }
00048   
00049   if(i < N)
00050     {
00051     val1 += A_mem[i];
00052     }
00053   
00054   return val1 + val2;
00055   }
00056 
00057 
00058 
00059 template<typename T1>
00060 arma_hot
00061 inline
00062 typename T1::elem_type
00063 accu_proxy(const Base<typename T1::elem_type,T1>& X)
00064   {
00065   arma_extra_debug_sigprint();
00066   
00067   typedef typename T1::elem_type eT;
00068   
00069   const Proxy<T1> A(X.get_ref());
00070   
00071   const u32 N = A.n_elem;
00072   
00073   eT val = eT(0);
00074   
00075   for(u32 i=0; i<N; ++i)
00076     {
00077     val += A[i];
00078     }
00079   
00080   return val;
00081   }
00082 
00083 
00084 
00085 //! accumulate the elements of a matrix
00086 template<typename T1>
00087 arma_inline
00088 arma_warn_unused
00089 typename T1::elem_type
00090 accu(const Base<typename T1::elem_type,T1>& X)
00091   {
00092   arma_extra_debug_sigprint();
00093   
00094   return (is_Mat<T1>::value == true) ? accu_unwrap(X) : accu_proxy(X);
00095   }
00096 
00097 
00098 
00099 //! explicit handling of Hamming norm (also known as zero norm)
00100 template<typename T1>
00101 arma_inline
00102 arma_warn_unused
00103 u32
00104 accu(const mtOp<u32,T1,op_rel_noteq>& X)
00105   {
00106   arma_extra_debug_sigprint();
00107   
00108   typedef typename T1::elem_type eT;
00109   
00110   const Proxy<T1> A(X.m);
00111   
00112   const u32 n_elem = A.n_elem;
00113   const eT  val    = X.aux;
00114   
00115   u32 n_nonzero = 0;
00116   for(u32 i=0; i<n_elem; ++i)
00117     {
00118     if(A[i] != val)
00119       {
00120       ++n_nonzero;
00121       }
00122     }
00123   
00124   return n_nonzero;
00125   }
00126 
00127 
00128 
00129 //! accumulate the elements of a cube
00130 template<typename T1>
00131 arma_hot
00132 arma_warn_unused
00133 inline
00134 typename T1::elem_type
00135 accu(const BaseCube<typename T1::elem_type,T1>& X)
00136   {
00137   arma_extra_debug_sigprint();
00138   
00139   typedef typename T1::elem_type eT;
00140   
00141   const ProxyCube<T1> A(X.get_ref());
00142   
00143   const u32 n_elem = A.n_elem;
00144   
00145   eT val = eT(0);
00146   
00147   for(u32 i=0; i<n_elem; ++i)
00148     {
00149     val += A[i];
00150     }
00151   
00152   return val;
00153   }
00154 
00155 
00156 
00157 //! accumulate the elements of a diagview
00158 template<typename eT>
00159 arma_pure
00160 arma_warn_unused
00161 inline
00162 eT
00163 accu(const diagview<eT>& X)
00164   {
00165   arma_extra_debug_sigprint();  
00166   
00167   const u32 n_elem = X.n_elem;
00168   eT val = eT(0);
00169   
00170   for(u32 i=0; i<n_elem; ++i)
00171     {
00172     val += X[i];
00173     }
00174   
00175   return val;
00176   }
00177 
00178 
00179 
00180 //! accumulate the elements of a subview (submatrix)
00181 template<typename eT>
00182 arma_pure
00183 arma_warn_unused
00184 inline
00185 eT
00186 accu(const subview<eT>& S)
00187   {
00188   arma_extra_debug_sigprint();  
00189   
00190   eT val = eT(0);
00191   
00192   for(u32 col=0; col<S.n_cols; ++col)
00193     {
00194     const eT* coldata = S.colptr(col);
00195     
00196     for(u32 row=0; row<S.n_rows; ++row)
00197       {
00198       val += coldata[row];
00199       }
00200     
00201     }
00202   
00203   return val;
00204   }
00205 
00206 
00207 
00208 //! accumulate the elements of a subview_row
00209 template<typename eT>
00210 arma_pure
00211 arma_warn_unused
00212 inline
00213 eT
00214 accu(const subview_row<eT>& S)
00215   {
00216   arma_extra_debug_sigprint();  
00217   
00218   const Mat<eT>& X = S.m;
00219   
00220   const u32 row       = S.aux_row1;
00221   const u32 start_col = S.aux_col1;
00222   const u32 end_col   = S.aux_col2;
00223   
00224   eT val = eT(0);
00225   
00226   for(u32 col=start_col; col<=end_col; ++col)
00227     {
00228     val += X.at(row,col);
00229     }
00230   
00231   return val;
00232   }
00233 
00234 
00235 
00236 //! accumulate the elements of a subview_col
00237 template<typename eT>
00238 arma_pure
00239 arma_warn_unused
00240 inline
00241 eT
00242 accu(const subview_col<eT>& S)
00243   {
00244   arma_extra_debug_sigprint();
00245   
00246   const eT* S_colptr = S.colptr(0);
00247   const u32 n_rows   = S.n_rows;
00248   
00249   eT val = eT(0);
00250   
00251   for(u32 row=0; row<n_rows; ++row)
00252     {
00253     val += S_colptr[row];
00254     }
00255   
00256   return val;
00257   }
00258 
00259 
00260 
00261 //! @}