From d4ec48575e94ec469ef9fbf005a0a6e67571f528 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Mon, 9 Feb 2015 11:14:36 +0100 Subject: [PATCH] Make Block inherit SparseCompressedBase in the case of an inner-panels and fix valuePtr() innerIndexPtr() --- Eigen/src/Core/Block.h | 2 +- Eigen/src/SparseCore/SparseBlock.h | 17 ++++++++++++----- test/sparse_ref.cpp | 8 +++++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Eigen/src/Core/Block.h b/Eigen/src/Core/Block.h index 9cf9d5432..5f6307206 100644 --- a/Eigen/src/Core/Block.h +++ b/Eigen/src/Core/Block.h @@ -87,7 +87,7 @@ struct traits > : traits::value ? LvalueBit : 0, FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0, - Flags = (traits::Flags & DirectAccessBit) | FlagsLvalueBit | FlagsRowMajorBit + Flags = (traits::Flags & (DirectAccessBit | (InnerPanel?CompressedAccessBit:0))) | FlagsLvalueBit | FlagsRowMajorBit // FIXME DirectAccessBit should not be handled by expressions }; }; diff --git a/Eigen/src/SparseCore/SparseBlock.h b/Eigen/src/SparseCore/SparseBlock.h index 9e4da2057..6b8ade5aa 100644 --- a/Eigen/src/SparseCore/SparseBlock.h +++ b/Eigen/src/SparseCore/SparseBlock.h @@ -74,7 +74,7 @@ namespace internal { template class sparse_matrix_block_impl - : public SparseMatrixBase > + : public SparseCompressedBase > { typedef typename internal::remove_all::type _MatrixTypeNested; typedef Block BlockType; @@ -172,19 +172,24 @@ public: } inline const Scalar* valuePtr() const - { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; } + { return m_matrix.valuePtr(); } inline Scalar* valuePtr() - { return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; } + { return m_matrix.const_cast_derived().valuePtr(); } inline const Index* innerIndexPtr() const - { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; } + { return m_matrix.innerIndexPtr(); } inline Index* innerIndexPtr() - { return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; } + { return m_matrix.const_cast_derived().innerIndexPtr(); } inline const Index* outerIndexPtr() const { return m_matrix.outerIndexPtr() + m_outerStart; } inline Index* outerIndexPtr() { return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; } + + inline const Index* innerNonZeroPtr() const + { return isCompressed() ? 0 : m_matrix.innerNonZeroPtr(); } + inline Index* innerNonZeroPtr() + { return isCompressed() ? 0 : m_matrix.const_cast_derived().innerNonZeroPtr(); } Index nonZeros() const { @@ -196,6 +201,8 @@ public: else return Map >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum(); } + + bool isCompressed() const { return m_matrix.innerNonZeroPtr()==0; } const Scalar& lastCoeff() const { diff --git a/test/sparse_ref.cpp b/test/sparse_ref.cpp index 27700f827..e7380ba21 100644 --- a/test/sparse_ref.cpp +++ b/test/sparse_ref.cpp @@ -51,8 +51,8 @@ void call_ref() { // SparseVector > ca = VectorXcf::Random(10).sparseView(); // SparseVector a = VectorXf::Random(10).sparseView(); - SparseMatrix A = MatrixXf::Random(10,10).sparseView(); - SparseMatrix B = MatrixXf::Random(10,10).sparseView(); + SparseMatrix A = MatrixXf::Random(10,10).sparseView(0.5,1); + SparseMatrix B = MatrixXf::Random(10,10).sparseView(0.5,1); const SparseMatrix& Ac(A); Block > Ab(A,0,1, 3,3); const Block > Abc(A,0,1,3,3); @@ -82,7 +82,9 @@ void call_ref() // VERIFY_EVALUATION_COUNT( call_ref_1(Arc, Arc), 0); // does not compile on purpose VERIFY_EVALUATION_COUNT( call_ref_2(Arc, Arc), 0); - VERIFY_EVALUATION_COUNT( call_ref_2(A.middleCols(1,3), A.middleCols(1,3)), 1); // should be 0 + VERIFY_EVALUATION_COUNT( call_ref_2(A.middleCols(1,3), A.middleCols(1,3)), 0); + + VERIFY_EVALUATION_COUNT( call_ref_2(A.col(2), A.col(2)), 0); VERIFY_EVALUATION_COUNT( call_ref_2(A.block(1,1,3,3), A.block(1,1,3,3)), 1); // should be 0 (allocate starts/nnz only) }