From d44d432baa142fdbe17f9d3abeab2c7629e199b8 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Mon, 6 Oct 2014 16:11:26 +0200 Subject: [PATCH] Re-enable products with triangular views of sparse matrices: we simply have to treat them as a sparse matrix. --- Eigen/src/Core/TriangularMatrix.h | 8 ++++--- .../ConservativeSparseSparseProduct.h | 2 ++ Eigen/src/SparseCore/SparseDenseProduct.h | 12 +++++++++- Eigen/src/SparseCore/SparseProduct.h | 13 +++++++++++ test/sparse_product.cpp | 22 +++++++++++++++---- 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/Eigen/src/Core/TriangularMatrix.h b/Eigen/src/Core/TriangularMatrix.h index 36bbd46e1f..263d54485e 100644 --- a/Eigen/src/Core/TriangularMatrix.h +++ b/Eigen/src/Core/TriangularMatrix.h @@ -247,7 +247,7 @@ template class TriangularView inline const AdjointReturnType adjoint() const { return AdjointReturnType(m_matrix.adjoint()); } - typedef TriangularView,TransposeMode> TransposeReturnType; + typedef TriangularView TransposeReturnType; /** \sa MatrixBase::transpose() */ EIGEN_DEVICE_FUNC inline TransposeReturnType transpose() @@ -255,11 +255,13 @@ template class TriangularView EIGEN_STATIC_ASSERT_LVALUE(MatrixType) return TransposeReturnType(m_matrix.const_cast_derived().transpose()); } + + typedef TriangularView ConstTransposeReturnType; /** \sa MatrixBase::transpose() const */ EIGEN_DEVICE_FUNC - inline const TransposeReturnType transpose() const + inline const ConstTransposeReturnType transpose() const { - return TransposeReturnType(m_matrix.transpose()); + return ConstTransposeReturnType(m_matrix.transpose()); } template diff --git a/Eigen/src/SparseCore/ConservativeSparseSparseProduct.h b/Eigen/src/SparseCore/ConservativeSparseSparseProduct.h index 19d9eaa429..a30522ff7e 100644 --- a/Eigen/src/SparseCore/ConservativeSparseSparseProduct.h +++ b/Eigen/src/SparseCore/ConservativeSparseSparseProduct.h @@ -141,6 +141,8 @@ struct conservative_sparse_sparse_product_selector ColMajorMatrixAux; typedef typename sparse_eval::type ColMajorMatrix; + // If the result is tall and thin (in the extreme case a column vector) + // then it is faster to sort the coefficients inplace instead of transposing twice. // FIXME, the following heuristic is probably not very good. if(lhs.rows()>=rhs.cols()) { diff --git a/Eigen/src/SparseCore/SparseDenseProduct.h b/Eigen/src/SparseCore/SparseDenseProduct.h index ed3cb19902..5aea11425f 100644 --- a/Eigen/src/SparseCore/SparseDenseProduct.h +++ b/Eigen/src/SparseCore/SparseDenseProduct.h @@ -146,6 +146,11 @@ struct generic_product_impl } }; +template +struct generic_product_impl + : generic_product_impl +{}; + template struct generic_product_impl { @@ -158,12 +163,17 @@ struct generic_product_impl RhsNested rhsNested(rhs); dst.setZero(); - // transpoe everything + // transpose everything Transpose dstT(dst); internal::sparse_time_dense_product(rhsNested.transpose(), lhsNested.transpose(), dstT, typename Dest::Scalar(1)); } }; +template +struct generic_product_impl + : generic_product_impl +{}; + template struct sparse_dense_outer_product_evaluator { diff --git a/Eigen/src/SparseCore/SparseProduct.h b/Eigen/src/SparseCore/SparseProduct.h index ae707376ae..c62386ed1c 100644 --- a/Eigen/src/SparseCore/SparseProduct.h +++ b/Eigen/src/SparseCore/SparseProduct.h @@ -33,6 +33,7 @@ SparseMatrixBase::operator*(const SparseMatrixBase &other namespace internal { +// sparse * sparse template struct generic_product_impl { @@ -48,6 +49,18 @@ struct generic_product_impl } }; +// sparse * sparse-triangular +template +struct generic_product_impl + : public generic_product_impl +{}; + +// sparse-triangular * sparse +template +struct generic_product_impl + : public generic_product_impl +{}; + template struct evaluator > > : public evaluator::PlainObject>::type diff --git a/test/sparse_product.cpp b/test/sparse_product.cpp index fa9be54407..366e272749 100644 --- a/test/sparse_product.cpp +++ b/test/sparse_product.cpp @@ -194,7 +194,7 @@ template void sparse_product() VERIFY_IS_APPROX(d3=d1*m2.transpose(), refM3=d1*refM2.transpose()); } - // test self adjoint products + // test self-adjoint and traingular-view products { DenseMatrix b = DenseMatrix::Random(rows, rows); DenseMatrix x = DenseMatrix::Random(rows, rows); @@ -202,9 +202,12 @@ template void sparse_product() DenseMatrix refUp = DenseMatrix::Zero(rows, rows); DenseMatrix refLo = DenseMatrix::Zero(rows, rows); DenseMatrix refS = DenseMatrix::Zero(rows, rows); + DenseMatrix refA = DenseMatrix::Zero(rows, rows); SparseMatrixType mUp(rows, rows); SparseMatrixType mLo(rows, rows); SparseMatrixType mS(rows, rows); + SparseMatrixType mA(rows, rows); + initSparse(density, refA, mA); do { initSparse(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular); } while (refUp.isZero()); @@ -224,19 +227,30 @@ template void sparse_product() VERIFY_IS_APPROX(mS, refS); VERIFY_IS_APPROX(x=mS*b, refX=refS*b); + // sparse selfadjointView with dense matrices VERIFY_IS_APPROX(x=mUp.template selfadjointView()*b, refX=refS*b); VERIFY_IS_APPROX(x=mLo.template selfadjointView()*b, refX=refS*b); VERIFY_IS_APPROX(x=mS.template selfadjointView()*b, refX=refS*b); - // sparse selfadjointView * sparse + // sparse selfadjointView with sparse matrices SparseMatrixType mSres(rows,rows); VERIFY_IS_APPROX(mSres = mLo.template selfadjointView()*mS, refX = refLo.template selfadjointView()*refS); - // sparse * sparse selfadjointview VERIFY_IS_APPROX(mSres = mS * mLo.template selfadjointView(), refX = refS * refLo.template selfadjointView()); + + // sparse triangularView with dense matrices + VERIFY_IS_APPROX(x=mA.template triangularView()*b, refX=refA.template triangularView()*b); + VERIFY_IS_APPROX(x=mA.template triangularView()*b, refX=refA.template triangularView()*b); + VERIFY_IS_APPROX(x=b*mA.template triangularView(), refX=b*refA.template triangularView()); + VERIFY_IS_APPROX(x=b*mA.template triangularView(), refX=b*refA.template triangularView()); + + // sparse triangularView with sparse matrices + VERIFY_IS_APPROX(mSres = mA.template triangularView()*mS, refX = refA.template triangularView()*refS); + VERIFY_IS_APPROX(mSres = mS * mA.template triangularView(), refX = refS * refA.template triangularView()); + VERIFY_IS_APPROX(mSres = mA.template triangularView()*mS, refX = refA.template triangularView()*refS); + VERIFY_IS_APPROX(mSres = mS * mA.template triangularView(), refX = refS * refA.template triangularView()); } - } // New test for Bug in SparseTimeDenseProduct