diff --git a/include/armadillo b/include/armadillo index ed4ed3f5..5178a657 100644 --- a/include/armadillo +++ b/include/armadillo @@ -316,6 +316,7 @@ namespace arma #include "armadillo_bits/op_rcond_bones.hpp" #include "armadillo_bits/op_sp_plus_bones.hpp" #include "armadillo_bits/op_sp_minus_bones.hpp" + #include "armadillo_bits/op_sp_sum_bones.hpp" #include "armadillo_bits/op_powmat_bones.hpp" #include "armadillo_bits/op_rank_bones.hpp" #include "armadillo_bits/op_row_as_mat_bones.hpp" @@ -354,7 +355,6 @@ namespace arma #include "armadillo_bits/spop_max_bones.hpp" #include "armadillo_bits/spop_min_bones.hpp" - #include "armadillo_bits/spop_sum_bones.hpp" #include "armadillo_bits/spop_strans_bones.hpp" #include "armadillo_bits/spop_htrans_bones.hpp" #include "armadillo_bits/spop_misc_bones.hpp" @@ -764,6 +764,7 @@ namespace arma #include "armadillo_bits/op_rcond_meat.hpp" #include "armadillo_bits/op_sp_plus_meat.hpp" #include "armadillo_bits/op_sp_minus_meat.hpp" + #include "armadillo_bits/op_sp_sum_meat.hpp" #include "armadillo_bits/op_powmat_meat.hpp" #include "armadillo_bits/op_rank_meat.hpp" #include "armadillo_bits/op_row_as_mat_meat.hpp" @@ -802,7 +803,6 @@ namespace arma #include "armadillo_bits/spop_max_meat.hpp" #include "armadillo_bits/spop_min_meat.hpp" - #include "armadillo_bits/spop_sum_meat.hpp" #include "armadillo_bits/spop_strans_meat.hpp" #include "armadillo_bits/spop_htrans_meat.hpp" #include "armadillo_bits/spop_misc_meat.hpp" diff --git a/include/armadillo_bits/SpMat_bones.hpp b/include/armadillo_bits/SpMat_bones.hpp index 9a91e8bf..59c64ff1 100644 --- a/include/armadillo_bits/SpMat_bones.hpp +++ b/include/armadillo_bits/SpMat_bones.hpp @@ -140,8 +140,7 @@ class SpMat : public SpBase< eT, SpMat > template inline SpMat& operator/=(const Op& expr); template inline SpMat& operator%=(const Op& expr); - //! explicit specification of sparse +/- scalar - template inline explicit SpMat(const SpToDOp& expr); + template inline SpMat(const SpToDOp& expr); //! construction of complex matrix out of two non-complex matrices template diff --git a/include/armadillo_bits/SpMat_meat.hpp b/include/armadillo_bits/SpMat_meat.hpp index 247348d1..8d2dd8a4 100644 --- a/include/armadillo_bits/SpMat_meat.hpp +++ b/include/armadillo_bits/SpMat_meat.hpp @@ -802,12 +802,12 @@ SpMat::SpMat(const SpToDOp& expr) { arma_extra_debug_sigprint_this(this); - typedef typename T1::elem_type T; + typedef typename T1::elem_type T1_eT; // Make sure the type is compatible. - arma_type_check(( is_same_type< eT, T >::no )); + arma_type_check(( is_same_type< eT, T1_eT >::no )); - op_type::apply(*this, expr); + (*this) = Mat(expr); } diff --git a/include/armadillo_bits/SpToDOp_bones.hpp b/include/armadillo_bits/SpToDOp_bones.hpp index b8ae6ccb..bba28122 100644 --- a/include/armadillo_bits/SpToDOp_bones.hpp +++ b/include/armadillo_bits/SpToDOp_bones.hpp @@ -42,10 +42,13 @@ class SpToDOp : public Base< typename T1::elem_type, SpToDOp > inline explicit SpToDOp(const T1& in_m); inline SpToDOp(const T1& in_m, const elem_type in_aux); + inline SpToDOp(const T1& in_m, const uword in_aux_uword_a, const uword in_aux_uword_b); inline ~SpToDOp(); arma_aligned const T1& m; //!< the operand; must be derived from SpBase arma_aligned elem_type aux; //!< auxiliary data, using the element type as used by T1 + arma_aligned uword aux_uword_a; //!< auxiliary data, uword format + arma_aligned uword aux_uword_b; //!< auxiliary data, uword format static constexpr bool is_row = op_type::template traits::is_row; static constexpr bool is_col = op_type::template traits::is_col; diff --git a/include/armadillo_bits/SpToDOp_meat.hpp b/include/armadillo_bits/SpToDOp_meat.hpp index 66ab6405..4e9b501b 100644 --- a/include/armadillo_bits/SpToDOp_meat.hpp +++ b/include/armadillo_bits/SpToDOp_meat.hpp @@ -42,6 +42,18 @@ SpToDOp::SpToDOp(const T1& in_m, const typename T1::elem_type in_au +template +inline +SpToDOp::SpToDOp(const T1& in_m, const uword in_aux_uword_a, const uword in_aux_uword_b) + : m(in_m) + , aux_uword_a(in_aux_uword_a) + , aux_uword_b(in_aux_uword_b) + { + arma_extra_debug_sigprint(); + } + + + template inline SpToDOp::~SpToDOp() diff --git a/include/armadillo_bits/fn_sum.hpp b/include/armadillo_bits/fn_sum.hpp index 0fa89369..292b90a7 100644 --- a/include/armadillo_bits/fn_sum.hpp +++ b/include/armadillo_bits/fn_sum.hpp @@ -115,13 +115,13 @@ typename enable_if2 < is_arma_sparse_type::value && resolves_to_sparse_vector::no, - const SpOp + const SpToDOp >::result sum(const T1& x) { arma_extra_debug_sigprint(); - return SpOp(x, 0, 0); + return SpToDOp(x, 0, 0); } @@ -133,13 +133,13 @@ typename enable_if2 < is_arma_sparse_type::value, - const SpOp + const SpToDOp >::result sum(const T1& x, const uword dim) { arma_extra_debug_sigprint(); - return SpOp(x, dim, 0); + return SpToDOp(x, dim, 0); } diff --git a/include/armadillo_bits/spop_sum_bones.hpp b/include/armadillo_bits/op_sp_sum_bones.hpp similarity index 85% rename from include/armadillo_bits/spop_sum_bones.hpp rename to include/armadillo_bits/op_sp_sum_bones.hpp index 2e4e5582..ee62469d 100644 --- a/include/armadillo_bits/spop_sum_bones.hpp +++ b/include/armadillo_bits/op_sp_sum_bones.hpp @@ -16,18 +16,11 @@ // ------------------------------------------------------------------------ -//! \addtogroup spop_sum -//! @{ - - -class spop_sum +class op_sp_sum : public traits_op_xvec { public: template - inline static void apply(SpMat& out, const SpOp& in); + inline static void apply(Mat& out, const SpToDOp& in); }; - - -//! @} diff --git a/include/armadillo_bits/spop_sum_meat.hpp b/include/armadillo_bits/op_sp_sum_meat.hpp similarity index 75% rename from include/armadillo_bits/spop_sum_meat.hpp rename to include/armadillo_bits/op_sp_sum_meat.hpp index 63badfd9..4f160b3e 100644 --- a/include/armadillo_bits/spop_sum_meat.hpp +++ b/include/armadillo_bits/op_sp_sum_meat.hpp @@ -16,21 +16,18 @@ // ------------------------------------------------------------------------ -//! \addtogroup spop_sum -//! @{ - - template inline void -spop_sum::apply(SpMat& out, const SpOp& in) +op_sp_sum::apply(Mat& out, const SpToDOp& in) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword dim = in.aux_uword_a; + arma_debug_check( (dim > 1), "sum(): parameter 'dim' must be 0 or 1" ); const SpProxy p(in.m); @@ -38,29 +35,24 @@ spop_sum::apply(SpMat& out, const SpOp& in) const uword p_n_rows = p.get_n_rows(); const uword p_n_cols = p.get_n_cols(); - if(p.get_n_nonzero() == 0) - { - if(dim == 0) { out.zeros(1,p_n_cols); } - if(dim == 1) { out.zeros(p_n_rows,1); } - - return; - } + if(dim == 0) { out.zeros(1, p_n_cols); } + if(dim == 1) { out.zeros(p_n_rows, 1); } + if(p.get_n_nonzero() == 0) { return; } + + eT* out_mem = out.memptr(); + if(dim == 0) // find the sum in each column { - Row acc(p_n_cols, arma_zeros_indicator()); - - eT* acc_mem = acc.memptr(); - if(SpProxy::use_iterator) { typename SpProxy::const_iterator_type it = p.begin(); const uword N = p.get_n_nonzero(); - + for(uword i=0; i < N; ++i) { - acc_mem[it.col()] += (*it); + out_mem[it.col()] += (*it); ++it; } } @@ -68,37 +60,25 @@ spop_sum::apply(SpMat& out, const SpOp& in) { for(uword col = 0; col < p_n_cols; ++col) { - acc_mem[col] = arrayops::accumulate + out_mem[col] = arrayops::accumulate ( &p.get_values()[p.get_col_ptrs()[col]], p.get_col_ptrs()[col + 1] - p.get_col_ptrs()[col] ); } } - - out = acc; } else if(dim == 1) // find the sum in each row { - Col acc(p_n_rows, arma_zeros_indicator()); - - eT* acc_mem = acc.memptr(); - typename SpProxy::const_iterator_type it = p.begin(); const uword N = p.get_n_nonzero(); for(uword i=0; i < N; ++i) { - acc_mem[it.row()] += (*it); + out_mem[it.row()] += (*it); ++it; } - - out = acc; } } - - - -//! @}