From 15084cf1ac1f58085cd0635676aa1d28efb268de Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Fri, 29 Jan 2016 22:09:45 +0100 Subject: [PATCH] bug #632: add support for "dense +/- sparse" operations. The current implementation is based on SparseView to make the dense subexpression compatible with the sparse one. --- Eigen/src/SparseCore/SparseCwiseBinaryOp.h | 30 ++++++++++++++++++++++ test/sparse_basic.cpp | 5 ++++ 2 files changed, 35 insertions(+) diff --git a/Eigen/src/SparseCore/SparseCwiseBinaryOp.h b/Eigen/src/SparseCore/SparseCwiseBinaryOp.h index d9420ac63..06c6d0e4d 100644 --- a/Eigen/src/SparseCore/SparseCwiseBinaryOp.h +++ b/Eigen/src/SparseCore/SparseCwiseBinaryOp.h @@ -60,6 +60,8 @@ namespace internal { // Generic "sparse OP sparse" +template struct binary_sparse_evaluator; + template struct binary_evaluator, IteratorBased, IteratorBased> : evaluator_base > @@ -428,6 +430,34 @@ SparseMatrixBase::cwiseProduct(const MatrixBase &other) c return typename CwiseProductDenseReturnType::Type(derived(), other.derived()); } +template +EIGEN_STRONG_INLINE const CwiseBinaryOp, const SparseDerived, const SparseView > +operator+(const SparseMatrixBase &a, const MatrixBase &b) +{ + return a.derived() + SparseView(b.derived()); +} + +template +EIGEN_STRONG_INLINE const CwiseBinaryOp, const SparseView, const SparseDerived> +operator+(const MatrixBase &a, const SparseMatrixBase &b) +{ + return SparseView(a.derived()) + b.derived(); +} + +template +EIGEN_STRONG_INLINE const CwiseBinaryOp, const SparseDerived, const SparseView > +operator-(const SparseMatrixBase &a, const MatrixBase &b) +{ + return a.derived() - SparseView(b.derived()); +} + +template +EIGEN_STRONG_INLINE const CwiseBinaryOp, const SparseView, const SparseDerived> +operator-(const MatrixBase &a, const SparseMatrixBase &b) +{ + return SparseView(a.derived()) - b.derived(); +} + } // end namespace Eigen #endif // EIGEN_SPARSE_CWISE_BINARY_OP_H diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp index d803e7dae..5a5650705 100644 --- a/test/sparse_basic.cpp +++ b/test/sparse_basic.cpp @@ -192,6 +192,11 @@ template void sparse_basic(const SparseMatrixType& re VERIFY_IS_APPROX(refM4.cwiseProduct(m3), refM4.cwiseProduct(refM3)); // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4); + VERIFY_IS_APPROX(refM4 + m3, refM4 + refM3); + VERIFY_IS_APPROX(m3 + refM4, refM3 + refM4); + VERIFY_IS_APPROX(refM4 - m3, refM4 - refM3); + VERIFY_IS_APPROX(m3 - refM4, refM3 - refM4); + // test aliasing VERIFY_IS_APPROX((m1 = -m1), (refM1 = -refM1)); VERIFY_IS_APPROX((m1 = m1.transpose()), (refM1 = refM1.transpose().eval()));