If the sparsity of X is a subset of the sparsity of W, then you can wrote your own function doing the addition in-place:
namespace Eigen {
template<typename Dst, typename Src>
void inplace_sparse_add(Dst &dst, const Src &src)
{
EIGEN_STATIC_ASSERT( ((internal::evaluator<Dst>::Flags&RowMajorBit) == (internal::evaluator<Src>::Flags&RowMajorBit)),
THE_STORAGE_ORDER_OF_BOTH_SIDES_MUST_MATCH);
using internal::evaluator;
evaluator<Dst> dst_eval(dst);
evaluator<Src> src_eval(src);
assert(dst.rows()==src.rows() && dst.cols()==src.cols());
for (Index j=0; j<src.outerSize(); ++j)
{
typename evaluator<Dst>::InnerIterator dst_it(dst_eval, j);
typename evaluator<Src>::InnerIterator src_it(src_eval, j);
while(src_it)
{
while(dst_it && dst_it.index()!=src_it.index())
++dst_it;
assert(dst_it);
dst_it.valueRef() += src_it.value();
++src_it;
}
}
}
}
Here is a usage example:
int main()
{
int n = 10;
MatrixXd R = MatrixXd::Random(n,n);
SparseMatrix<double, RowMajor> A = R.sparseView(0.25,1), B = 0.5*R.sparseView(0.65,1);
cout << A.toDense() << "\n\n" << B.toDense() << "\n\n";
inplace_sparse_add(A, B);
cout << A.toDense() << "\n\n";
auto Ai = A.row(2);
inplace_sparse_add(Ai, B.row(2));
cout << A.toDense() << "\n\n";
}
W.row(i)andX.row(j)have the same sparsity patterns?? If yes, then this could indeed be optimized. - ggaelWshould cover whatever sparse rows inX, which means, in terms of sparsity patterns, each single row ofXshould be a subset of any row inW. - avocadoWis a dense matrix, the aboveW.row(i) += X.row(j)could still be optimized further? Also I wonder if eigen'sSparseMatrixelem-wise addition is less performant than ordinary c++ impl using stlhashmapor vector? - avocadoMatrixXd) then this should be fine. If W is a sparse matrix but you are sure thatW.row(i) += X.row(j)can be done in-place without any reallocation/copies, then this could be optimized within Eigen itself by (1) extending the API to let Eigen knows and (2) writing the respective in-place evaluation code. Meanwhile, you could write your own inplace evaluation. I'll post an exemple. - ggael