$search
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 00020 template<typename T1> 00021 arma_hot 00022 inline 00023 typename T1::elem_type 00024 accu(const Base<typename T1::elem_type,T1>& X) 00025 { 00026 arma_extra_debug_sigprint(); 00027 00028 typedef typename T1::elem_type eT; 00029 typedef typename Proxy<T1>::ea_type ea_type; 00030 00031 const Proxy<T1> A(X.get_ref()); 00032 00033 if(Proxy<T1>::prefer_at_accessor == false) 00034 { 00035 ea_type P = A.get_ea(); 00036 const uword n_elem = A.get_n_elem(); 00037 00038 eT val1 = eT(0); 00039 eT val2 = eT(0); 00040 00041 uword i,j; 00042 00043 for(i=0, j=1; j<n_elem; i+=2, j+=2) 00044 { 00045 val1 += P[i]; 00046 val2 += P[j]; 00047 } 00048 00049 if(i < n_elem) 00050 { 00051 val1 += P[i]; 00052 } 00053 00054 return val1 + val2; 00055 } 00056 else 00057 { 00058 const uword n_rows = A.get_n_rows(); 00059 const uword n_cols = A.get_n_cols(); 00060 00061 eT val = eT(0); 00062 00063 for(uword col=0; col<n_cols; ++col) 00064 { 00065 for(uword row=0; row<n_rows; ++row) 00066 { 00067 val += A.at(row,col); 00068 } 00069 } 00070 00071 return val; 00072 } 00073 } 00074 00075 00076 00078 template<typename T1> 00079 arma_inline 00080 arma_warn_unused 00081 uword 00082 accu(const mtOp<uword,T1,op_rel_noteq>& X) 00083 { 00084 arma_extra_debug_sigprint(); 00085 00086 typedef typename T1::elem_type eT; 00087 00088 const Proxy<T1> A(X.m); 00089 00090 const uword n_elem = A.get_n_elem(); 00091 const eT val = X.aux; 00092 00093 uword n_nonzero = 0; 00094 for(uword i=0; i<n_elem; ++i) 00095 { 00096 if(A[i] != val) 00097 { 00098 ++n_nonzero; 00099 } 00100 } 00101 00102 return n_nonzero; 00103 } 00104 00105 00106 00108 template<typename T1> 00109 arma_hot 00110 arma_warn_unused 00111 inline 00112 typename T1::elem_type 00113 accu(const BaseCube<typename T1::elem_type,T1>& X) 00114 { 00115 arma_extra_debug_sigprint(); 00116 00117 typedef typename T1::elem_type eT; 00118 typedef typename ProxyCube<T1>::ea_type ea_type; 00119 00120 const ProxyCube<T1> A(X.get_ref()); 00121 00122 if(ProxyCube<T1>::prefer_at_accessor == false) 00123 { 00124 00125 ea_type P = A.get_ea(); 00126 const uword n_elem = A.get_n_elem(); 00127 00128 eT val1 = eT(0); 00129 eT val2 = eT(0); 00130 00131 uword i,j; 00132 00133 for(i=0, j=1; j<n_elem; i+=2, j+=2) 00134 { 00135 val1 += P[i]; 00136 val2 += P[j]; 00137 } 00138 00139 if(i < n_elem) 00140 { 00141 val1 += P[i]; 00142 } 00143 00144 return val1 + val2; 00145 } 00146 else 00147 { 00148 const uword n_rows = A.get_n_rows(); 00149 const uword n_cols = A.get_n_cols(); 00150 const uword n_slices = A.get_n_slices(); 00151 00152 eT val = eT(0); 00153 00154 for(uword slice=0; slice<n_slices; ++slice) 00155 { 00156 for(uword col=0; col<n_cols; ++col) 00157 { 00158 for(uword row=0; row<n_rows; ++row) 00159 { 00160 val += A.at(row,col,slice); 00161 } 00162 } 00163 } 00164 00165 return val; 00166 } 00167 } 00168 00169 00170 00172 template<typename eT> 00173 arma_pure 00174 arma_warn_unused 00175 inline 00176 eT 00177 accu(const diagview<eT>& X) 00178 { 00179 arma_extra_debug_sigprint(); 00180 00181 const uword n_elem = X.n_elem; 00182 00183 eT val = eT(0); 00184 00185 for(uword i=0; i<n_elem; ++i) 00186 { 00187 val += X[i]; 00188 } 00189 00190 return val; 00191 } 00192 00193 00194 00196 template<typename eT> 00197 arma_pure 00198 arma_warn_unused 00199 inline 00200 eT 00201 accu(const subview<eT>& S) 00202 { 00203 arma_extra_debug_sigprint(); 00204 00205 const uword S_n_rows = S.n_rows; 00206 const uword S_n_cols = S.n_cols; 00207 const uword S_n_elem = S.n_elem; 00208 00209 eT val = eT(0); 00210 00211 if(S_n_elem > 0) 00212 { 00213 for(uword col=0; col<S_n_cols; ++col) 00214 { 00215 val += arrayops::accumulate( S.colptr(col), S_n_rows ); 00216 } 00217 } 00218 00219 return val; 00220 } 00221 00222 00223 00225 template<typename eT> 00226 arma_pure 00227 arma_warn_unused 00228 inline 00229 eT 00230 accu(const subview_row<eT>& S) 00231 { 00232 arma_extra_debug_sigprint(); 00233 00234 const Mat<eT>& X = S.m; 00235 00236 const uword n_elem = S.n_elem; 00237 const uword row = S.aux_row1; 00238 const uword start_col = S.aux_col1; 00239 const uword end_col_p1 = start_col + S.n_cols; 00240 00241 eT val = eT(0); 00242 00243 if(n_elem > 0) 00244 { 00245 uword i,j; 00246 00247 for(i=start_col, j=start_col+1; j < end_col_p1; i+=2, j+=2) 00248 { 00249 val += X.at(row,i); 00250 val += X.at(row,j); 00251 } 00252 00253 if(i < end_col_p1) 00254 { 00255 val += X.at(row,i); 00256 } 00257 } 00258 00259 return val; 00260 } 00261 00262 00263 00265 template<typename eT> 00266 arma_pure 00267 arma_warn_unused 00268 inline 00269 eT 00270 accu(const subview_col<eT>& S) 00271 { 00272 arma_extra_debug_sigprint(); 00273 00274 return (S.n_elem > 0) ? arrayops::accumulate( S.colptr(0), S.n_rows ) : eT(0); 00275 } 00276 00277 00278