From 11b492e993f4272d86fc4019014b47b09a57a2ce Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Wed, 14 Dec 2016 17:53:47 +0100 Subject: [PATCH] bug #1358: fix compilation for sparse += sparse.selfadjointView(); --- Eigen/src/SparseCore/SparseCwiseBinaryOp.h | 16 ++++++++++ Eigen/src/SparseCore/SparseMatrixBase.h | 5 +++ Eigen/src/SparseCore/SparseSelfAdjointView.h | 33 ++++++++++++++++++-- test/sparse_basic.cpp | 8 +++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Eigen/src/SparseCore/SparseCwiseBinaryOp.h b/Eigen/src/SparseCore/SparseCwiseBinaryOp.h index 0a9bdeac2c..c1ddd1ac17 100644 --- a/Eigen/src/SparseCore/SparseCwiseBinaryOp.h +++ b/Eigen/src/SparseCore/SparseCwiseBinaryOp.h @@ -621,6 +621,22 @@ protected: * Implementation of SparseMatrixBase and SparseCwise functions/operators ***************************************************************************/ +template +template +Derived& SparseMatrixBase::operator+=(const EigenBase &other) +{ + call_assignment(derived(), other.derived(), internal::add_assign_op()); + return derived(); +} + +template +template +Derived& SparseMatrixBase::operator-=(const EigenBase &other) +{ + call_assignment(derived(), other.derived(), internal::assign_op()); + return derived(); +} + template template EIGEN_STRONG_INLINE Derived & diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 813accce10..6087da5c4d 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -265,6 +265,11 @@ template class SparseMatrixBase template Derived& operator-=(const DiagonalBase& other); + template + Derived& operator+=(const EigenBase &other); + template + Derived& operator-=(const EigenBase &other); + Derived& operator*=(const Scalar& other); Derived& operator/=(const Scalar& other); diff --git a/Eigen/src/SparseCore/SparseSelfAdjointView.h b/Eigen/src/SparseCore/SparseSelfAdjointView.h index d31d9babf0..9e39be738d 100644 --- a/Eigen/src/SparseCore/SparseSelfAdjointView.h +++ b/Eigen/src/SparseCore/SparseSelfAdjointView.h @@ -222,14 +222,43 @@ template< typename DstXprType, typename SrcXprType, typename Functor> struct Assignment { typedef typename DstXprType::StorageIndex StorageIndex; + typedef internal::assign_op AssignOpType; + template - static void run(SparseMatrix &dst, const SrcXprType &src, const internal::assign_op &/*func*/) + static void run(SparseMatrix &dst, const SrcXprType &src, const AssignOpType&/*func*/) { internal::permute_symm_to_fullsymm(src.matrix(), dst); } + + // FIXME: the handling of += and -= in sparse matrices should be cleanup so that next two overloads could be reduced to: + template + static void run(SparseMatrix &dst, const SrcXprType &src, const AssignFunc& func) + { + SparseMatrix tmp(src.rows(),src.cols()); + run(tmp, src, AssignOpType()); + call_assignment_no_alias_no_transpose(dst, tmp, func); + } + + template + static void run(SparseMatrix &dst, const SrcXprType &src, + const internal::add_assign_op& /* func */) + { + SparseMatrix tmp(src.rows(),src.cols()); + run(tmp, src, AssignOpType()); + dst += tmp; + } + + template + static void run(SparseMatrix &dst, const SrcXprType &src, + const internal::sub_assign_op& /* func */) + { + SparseMatrix tmp(src.rows(),src.cols()); + run(tmp, src, AssignOpType()); + dst -= tmp; + } template - static void run(DynamicSparseMatrix& dst, const SrcXprType &src, const internal::assign_op &/*func*/) + static void run(DynamicSparseMatrix& dst, const SrcXprType &src, const AssignOpType&/*func*/) { // TODO directly evaluate into dst; SparseMatrix tmp(dst.rows(),dst.cols()); diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp index 2a3117b2b3..4d864bbd0c 100644 --- a/test/sparse_basic.cpp +++ b/test/sparse_basic.cpp @@ -431,6 +431,14 @@ template void sparse_basic(const SparseMatrixType& re m3 = m2.template selfadjointView(); VERIFY_IS_APPROX(m3, refMat3); + refMat3 += refMat2.template selfadjointView(); + m3 += m2.template selfadjointView(); + VERIFY_IS_APPROX(m3, refMat3); + + refMat3 -= refMat2.template selfadjointView(); + m3 -= m2.template selfadjointView(); + VERIFY_IS_APPROX(m3, refMat3); + // selfadjointView only works for square matrices: SparseMatrixType m4(rows, rows+1); VERIFY_RAISES_ASSERT(m4.template selfadjointView());