// Copyright (C) 2008-2015 National ICT Australia (NICTA) // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. // ------------------------------------------------------------------- // // Written by Conrad Sanderson - http://conradsanderson.id.au // Written by Ryan Curtin //! \addtogroup operator_schur //! @{ // operator %, which we define it to do a schur product (element-wise multiplication) //! element-wise multiplication of user-accessible Armadillo objects with same element type template arma_inline typename enable_if2 < is_arma_type::value && is_arma_type::value && is_same_type::value, const eGlue >::result operator% ( const T1& X, const T2& Y ) { arma_extra_debug_sigprint(); return eGlue(X, Y); } //! element-wise multiplication of user-accessible Armadillo objects with different element types template inline typename enable_if2 < (is_arma_type::value && is_arma_type::value && (is_same_type::no)), const mtGlue::result, T1, T2, glue_mixed_schur> >::result operator% ( const T1& X, const T2& Y ) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT1; typedef typename T2::elem_type eT2; typedef typename promote_type::result out_eT; promote_type::check(); return mtGlue( X, Y ); } //! element-wise multiplication of two sparse matrices template inline typename enable_if2 < (is_arma_sparse_type::value && is_arma_sparse_type::value && is_same_type::value), SpMat >::result operator% ( const SpBase& x, const SpBase& y ) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const SpProxy pa(x.get_ref()); const SpProxy pb(y.get_ref()); arma_debug_assert_same_size(pa.get_n_rows(), pa.get_n_cols(), pb.get_n_rows(), pb.get_n_cols(), "element-wise multiplication"); SpMat result(pa.get_n_rows(), pa.get_n_cols()); if( (pa.get_n_nonzero() != 0) && (pb.get_n_nonzero() != 0) ) { // Resize memory to correct size. result.mem_resize(n_unique(x, y, op_n_unique_mul())); // Now iterate across both matrices. typename SpProxy::const_iterator_type x_it = pa.begin(); typename SpProxy::const_iterator_type y_it = pb.begin(); typename SpProxy::const_iterator_type x_end = pa.end(); typename SpProxy::const_iterator_type y_end = pb.end(); uword cur_val = 0; while((x_it != x_end) || (y_it != y_end)) { if(x_it == y_it) { const eT val = (*x_it) * (*y_it); if (val != eT(0)) { access::rw(result.values[cur_val]) = val; access::rw(result.row_indices[cur_val]) = x_it.row(); ++access::rw(result.col_ptrs[x_it.col() + 1]); ++cur_val; } ++x_it; ++y_it; } else { const uword x_it_row = x_it.row(); const uword x_it_col = x_it.col(); const uword y_it_row = y_it.row(); const uword y_it_col = y_it.col(); if((x_it_col < y_it_col) || ((x_it_col == y_it_col) && (x_it_row < y_it_row))) // if y is closer to the end { ++x_it; } else { ++y_it; } } } // Fix column pointers to be cumulative. for(uword c = 1; c <= result.n_cols; ++c) { access::rw(result.col_ptrs[c]) += result.col_ptrs[c - 1]; } } return result; } //! element-wise multiplication of one dense and one sparse object template inline typename enable_if2 < (is_arma_type::value && is_arma_sparse_type::value && is_same_type::value), SpMat >::result operator% ( const T1& x, const T2& y ) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const Proxy pa(x); const SpProxy pb(y); arma_debug_assert_same_size(pa.get_n_rows(), pa.get_n_cols(), pb.get_n_rows(), pb.get_n_cols(), "element-wise multiplication"); SpMat result(pa.get_n_rows(), pa.get_n_cols()); // count new size uword new_n_nonzero = 0; typename SpProxy::const_iterator_type it = pb.begin(); typename SpProxy::const_iterator_type it_end = pb.end(); while(it != it_end) { if( ((*it) * pa.at(it.row(), it.col())) != eT(0) ) { ++new_n_nonzero; } ++it; } // Resize memory accordingly. result.mem_resize(new_n_nonzero); uword cur_val = 0; typename SpProxy::const_iterator_type it2 = pb.begin(); while(it2 != it_end) { const uword it2_row = it2.row(); const uword it2_col = it2.col(); const eT val = (*it2) * pa.at(it2_row, it2_col); if(val != eT(0)) { access::rw(result.values[cur_val]) = val; access::rw(result.row_indices[cur_val]) = it2_row; ++access::rw(result.col_ptrs[it2_col + 1]); ++cur_val; } ++it2; } // Fix column pointers. for(uword c = 1; c <= result.n_cols; ++c) { access::rw(result.col_ptrs[c]) += result.col_ptrs[c - 1]; } return result; } //! element-wise multiplication of one sparse and one dense object template inline typename enable_if2 < (is_arma_sparse_type::value && is_arma_type::value && is_same_type::value), SpMat >::result operator% ( const T1& x, const T2& y ) { arma_extra_debug_sigprint(); // This operation is commutative. return (y % x); } template inline Mat operator% ( const subview_each1& X, const Base& Y ) { arma_extra_debug_sigprint(); return subview_each1_aux::operator_schur(X, Y.get_ref()); } template arma_inline Mat operator% ( const Base& X, const subview_each1& Y ) { arma_extra_debug_sigprint(); return subview_each1_aux::operator_schur(Y, X.get_ref()); // NOTE: swapped order } template inline Mat operator% ( const subview_each2& X, const Base& Y ) { arma_extra_debug_sigprint(); return subview_each2_aux::operator_schur(X, Y.get_ref()); } template arma_inline Mat operator% ( const Base& X, const subview_each2& Y ) { arma_extra_debug_sigprint(); return subview_each2_aux::operator_schur(Y, X.get_ref()); // NOTE: swapped order } //! @}