diff --git a/include/armadillo_bits/fn_accu.hpp b/include/armadillo_bits/fn_accu.hpp index b69fc322..fabf6c67 100644 --- a/include/armadillo_bits/fn_accu.hpp +++ b/include/armadillo_bits/fn_accu.hpp @@ -845,23 +845,36 @@ accu(const SpBase& expr) const SpProxy P(expr.get_ref()); + const uword N = P.get_n_nonzero(); + + if(N == 0) { return eT(0); } + if(SpProxy::use_iterator == false) { // direct counting - return arrayops::accumulate(P.get_values(), P.get_n_nonzero()); + return arrayops::accumulate(P.get_values(), N); } - else + + if(is_SpSubview::stored_type>::value) { - typename SpProxy::const_iterator_type it = P.begin(); + const SpSubview& sv = reinterpret_cast< const SpSubview& >(P.Q); - const uword P_n_nz = P.get_n_nonzero(); - - eT val = eT(0); - - for(uword i=0; i < P_n_nz; ++i) { val += (*it); ++it; } - - return val; + if(sv.n_rows == sv.m.n_rows) + { + const SpMat& m = sv.m; + const uword col = sv.aux_col1; + + return arrayops::accumulate(&(m.values[ m.col_ptrs[col] ]), N); + } } + + typename SpProxy::const_iterator_type it = P.begin(); + + eT val = eT(0); + + for(uword i=0; i < N; ++i) { val += (*it); ++it; } + + return val; }