annotate armadillo-2.4.4/include/armadillo_bits/fn_accu.hpp @ 36:cc18e9a13fe8 slimline

Fix failure to set hasTimestamp on final part
author Chris Cannam
date Wed, 16 May 2012 11:44:39 +0100
parents 8b6102e2a9b0
children
rev   line source
max@0 1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
max@0 2 // Copyright (C) 2008-2011 Conrad Sanderson
max@0 3 //
max@0 4 // This file is part of the Armadillo C++ library.
max@0 5 // It is provided without any warranty of fitness
max@0 6 // for any purpose. You can redistribute this file
max@0 7 // and/or modify it under the terms of the GNU
max@0 8 // Lesser General Public License (LGPL) as published
max@0 9 // by the Free Software Foundation, either version 3
max@0 10 // of the License or (at your option) any later version.
max@0 11 // (see http://www.opensource.org/licenses for more info)
max@0 12
max@0 13
max@0 14 //! \addtogroup fn_accu
max@0 15 //! @{
max@0 16
max@0 17
max@0 18
max@0 19 //! accumulate the elements of a matrix
max@0 20 template<typename T1>
max@0 21 arma_hot
max@0 22 inline
max@0 23 typename T1::elem_type
max@0 24 accu(const Base<typename T1::elem_type,T1>& X)
max@0 25 {
max@0 26 arma_extra_debug_sigprint();
max@0 27
max@0 28 typedef typename T1::elem_type eT;
max@0 29 typedef typename Proxy<T1>::ea_type ea_type;
max@0 30
max@0 31 const Proxy<T1> A(X.get_ref());
max@0 32
max@0 33 if(Proxy<T1>::prefer_at_accessor == false)
max@0 34 {
max@0 35 ea_type P = A.get_ea();
max@0 36 const uword n_elem = A.get_n_elem();
max@0 37
max@0 38 eT val1 = eT(0);
max@0 39 eT val2 = eT(0);
max@0 40
max@0 41 uword i,j;
max@0 42
max@0 43 for(i=0, j=1; j<n_elem; i+=2, j+=2)
max@0 44 {
max@0 45 val1 += P[i];
max@0 46 val2 += P[j];
max@0 47 }
max@0 48
max@0 49 if(i < n_elem)
max@0 50 {
max@0 51 val1 += P[i];
max@0 52 }
max@0 53
max@0 54 return val1 + val2;
max@0 55 }
max@0 56 else
max@0 57 {
max@0 58 const uword n_rows = A.get_n_rows();
max@0 59 const uword n_cols = A.get_n_cols();
max@0 60
max@0 61 eT val = eT(0);
max@0 62
max@0 63 for(uword col=0; col<n_cols; ++col)
max@0 64 {
max@0 65 for(uword row=0; row<n_rows; ++row)
max@0 66 {
max@0 67 val += A.at(row,col);
max@0 68 }
max@0 69 }
max@0 70
max@0 71 return val;
max@0 72 }
max@0 73 }
max@0 74
max@0 75
max@0 76
max@0 77 //! explicit handling of Hamming norm (also known as zero norm)
max@0 78 template<typename T1>
max@0 79 arma_inline
max@0 80 arma_warn_unused
max@0 81 uword
max@0 82 accu(const mtOp<uword,T1,op_rel_noteq>& X)
max@0 83 {
max@0 84 arma_extra_debug_sigprint();
max@0 85
max@0 86 typedef typename T1::elem_type eT;
max@0 87
max@0 88 const Proxy<T1> A(X.m);
max@0 89
max@0 90 const uword n_elem = A.get_n_elem();
max@0 91 const eT val = X.aux;
max@0 92
max@0 93 uword n_nonzero = 0;
max@0 94 for(uword i=0; i<n_elem; ++i)
max@0 95 {
max@0 96 if(A[i] != val)
max@0 97 {
max@0 98 ++n_nonzero;
max@0 99 }
max@0 100 }
max@0 101
max@0 102 return n_nonzero;
max@0 103 }
max@0 104
max@0 105
max@0 106
max@0 107 //! accumulate the elements of a cube
max@0 108 template<typename T1>
max@0 109 arma_hot
max@0 110 arma_warn_unused
max@0 111 inline
max@0 112 typename T1::elem_type
max@0 113 accu(const BaseCube<typename T1::elem_type,T1>& X)
max@0 114 {
max@0 115 arma_extra_debug_sigprint();
max@0 116
max@0 117 typedef typename T1::elem_type eT;
max@0 118 typedef typename ProxyCube<T1>::ea_type ea_type;
max@0 119
max@0 120 const ProxyCube<T1> A(X.get_ref());
max@0 121
max@0 122 if(ProxyCube<T1>::prefer_at_accessor == false)
max@0 123 {
max@0 124
max@0 125 ea_type P = A.get_ea();
max@0 126 const uword n_elem = A.get_n_elem();
max@0 127
max@0 128 eT val1 = eT(0);
max@0 129 eT val2 = eT(0);
max@0 130
max@0 131 uword i,j;
max@0 132
max@0 133 for(i=0, j=1; j<n_elem; i+=2, j+=2)
max@0 134 {
max@0 135 val1 += P[i];
max@0 136 val2 += P[j];
max@0 137 }
max@0 138
max@0 139 if(i < n_elem)
max@0 140 {
max@0 141 val1 += P[i];
max@0 142 }
max@0 143
max@0 144 return val1 + val2;
max@0 145 }
max@0 146 else
max@0 147 {
max@0 148 const uword n_rows = A.get_n_rows();
max@0 149 const uword n_cols = A.get_n_cols();
max@0 150 const uword n_slices = A.get_n_slices();
max@0 151
max@0 152 eT val = eT(0);
max@0 153
max@0 154 for(uword slice=0; slice<n_slices; ++slice)
max@0 155 {
max@0 156 for(uword col=0; col<n_cols; ++col)
max@0 157 {
max@0 158 for(uword row=0; row<n_rows; ++row)
max@0 159 {
max@0 160 val += A.at(row,col,slice);
max@0 161 }
max@0 162 }
max@0 163 }
max@0 164
max@0 165 return val;
max@0 166 }
max@0 167 }
max@0 168
max@0 169
max@0 170
max@0 171 //! accumulate the elements of a diagview
max@0 172 template<typename eT>
max@0 173 arma_pure
max@0 174 arma_warn_unused
max@0 175 inline
max@0 176 eT
max@0 177 accu(const diagview<eT>& X)
max@0 178 {
max@0 179 arma_extra_debug_sigprint();
max@0 180
max@0 181 const uword n_elem = X.n_elem;
max@0 182
max@0 183 eT val = eT(0);
max@0 184
max@0 185 for(uword i=0; i<n_elem; ++i)
max@0 186 {
max@0 187 val += X[i];
max@0 188 }
max@0 189
max@0 190 return val;
max@0 191 }
max@0 192
max@0 193
max@0 194
max@0 195 //! accumulate the elements of a subview (submatrix)
max@0 196 template<typename eT>
max@0 197 arma_pure
max@0 198 arma_warn_unused
max@0 199 inline
max@0 200 eT
max@0 201 accu(const subview<eT>& S)
max@0 202 {
max@0 203 arma_extra_debug_sigprint();
max@0 204
max@0 205 const uword S_n_rows = S.n_rows;
max@0 206 const uword S_n_cols = S.n_cols;
max@0 207 const uword S_n_elem = S.n_elem;
max@0 208
max@0 209 eT val = eT(0);
max@0 210
max@0 211 if(S_n_elem > 0)
max@0 212 {
max@0 213 for(uword col=0; col<S_n_cols; ++col)
max@0 214 {
max@0 215 val += arrayops::accumulate( S.colptr(col), S_n_rows );
max@0 216 }
max@0 217 }
max@0 218
max@0 219 return val;
max@0 220 }
max@0 221
max@0 222
max@0 223
max@0 224 //! accumulate the elements of a subview_row
max@0 225 template<typename eT>
max@0 226 arma_pure
max@0 227 arma_warn_unused
max@0 228 inline
max@0 229 eT
max@0 230 accu(const subview_row<eT>& S)
max@0 231 {
max@0 232 arma_extra_debug_sigprint();
max@0 233
max@0 234 const Mat<eT>& X = S.m;
max@0 235
max@0 236 const uword n_elem = S.n_elem;
max@0 237 const uword row = S.aux_row1;
max@0 238 const uword start_col = S.aux_col1;
max@0 239 const uword end_col_p1 = start_col + S.n_cols;
max@0 240
max@0 241 eT val = eT(0);
max@0 242
max@0 243 if(n_elem > 0)
max@0 244 {
max@0 245 uword i,j;
max@0 246
max@0 247 for(i=start_col, j=start_col+1; j < end_col_p1; i+=2, j+=2)
max@0 248 {
max@0 249 val += X.at(row,i);
max@0 250 val += X.at(row,j);
max@0 251 }
max@0 252
max@0 253 if(i < end_col_p1)
max@0 254 {
max@0 255 val += X.at(row,i);
max@0 256 }
max@0 257 }
max@0 258
max@0 259 return val;
max@0 260 }
max@0 261
max@0 262
max@0 263
max@0 264 //! accumulate the elements of a subview_col
max@0 265 template<typename eT>
max@0 266 arma_pure
max@0 267 arma_warn_unused
max@0 268 inline
max@0 269 eT
max@0 270 accu(const subview_col<eT>& S)
max@0 271 {
max@0 272 arma_extra_debug_sigprint();
max@0 273
max@0 274 return (S.n_elem > 0) ? arrayops::accumulate( S.colptr(0), S.n_rows ) : eT(0);
max@0 275 }
max@0 276
max@0 277
max@0 278
max@0 279 //! @}