Fix sparse block

This commit is contained in:
Gael Guennebaud 2013-09-07 00:00:13 +02:00
parent ed78a76161
commit 7fa007e8bf

View File

@ -1,410 +1,410 @@
// This file is part of Eigen, a lightweight C++ template library // This file is part of Eigen, a lightweight C++ template library
// for linear algebra. // for linear algebra.
// //
// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr> // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
// //
// This Source Code Form is subject to the terms of the Mozilla // This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed // Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef EIGEN_SPARSE_BLOCK_H #ifndef EIGEN_SPARSE_BLOCK_H
#define EIGEN_SPARSE_BLOCK_H #define EIGEN_SPARSE_BLOCK_H
namespace Eigen { namespace Eigen {
template<typename XprType, int BlockRows, int BlockCols> template<typename XprType, int BlockRows, int BlockCols>
class BlockImpl<XprType,BlockRows,BlockCols,true,Sparse> class BlockImpl<XprType,BlockRows,BlockCols,true,Sparse>
: public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,true> > : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,true> >
{ {
typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested; typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
typedef Block<XprType, BlockRows, BlockCols, true> BlockType; typedef Block<XprType, BlockRows, BlockCols, true> BlockType;
public: public:
enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor }; enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
protected: protected:
enum { OuterSize = IsRowMajor ? BlockRows : BlockCols }; enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
public: public:
EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType) EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
class InnerIterator: public XprType::InnerIterator class InnerIterator: public XprType::InnerIterator
{ {
typedef typename BlockImpl::Index Index; typedef typename BlockImpl::Index Index;
public: public:
inline InnerIterator(const BlockType& xpr, Index outer) inline InnerIterator(const BlockType& xpr, Index outer)
: XprType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer) : XprType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
{} {}
inline Index row() const { return IsRowMajor ? m_outer : this->index(); } inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
inline Index col() const { return IsRowMajor ? this->index() : m_outer; } inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
protected: protected:
Index m_outer; Index m_outer;
}; };
class ReverseInnerIterator: public XprType::ReverseInnerIterator class ReverseInnerIterator: public XprType::ReverseInnerIterator
{ {
typedef typename BlockImpl::Index Index; typedef typename BlockImpl::Index Index;
public: public:
inline ReverseInnerIterator(const BlockType& xpr, Index outer) inline ReverseInnerIterator(const BlockType& xpr, Index outer)
: XprType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer) : XprType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
{} {}
inline Index row() const { return IsRowMajor ? m_outer : this->index(); } inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
inline Index col() const { return IsRowMajor ? this->index() : m_outer; } inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
protected: protected:
Index m_outer; Index m_outer;
}; };
inline BlockImpl(const XprType& xpr, int i) inline BlockImpl(const XprType& xpr, int i)
: m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize) : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
{} {}
inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols) inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
: m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols) : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
{} {}
EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); } EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); } EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
protected: protected:
typename XprType::Nested m_matrix; typename XprType::Nested m_matrix;
Index m_outerStart; Index m_outerStart;
const internal::variable_if_dynamic<Index, OuterSize> m_outerSize; const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
public: public:
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
}; };
/*************************************************************************** /***************************************************************************
* specialization for SparseMatrix * specialization for SparseMatrix
***************************************************************************/ ***************************************************************************/
template<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols> template<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols>
class BlockImpl<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse> class BlockImpl<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse>
: public SparseMatrixBase<Block<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> > : public SparseMatrixBase<Block<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
{ {
typedef SparseMatrix<_Scalar, _Options, _Index> SparseMatrixType; typedef SparseMatrix<_Scalar, _Options, _Index> SparseMatrixType;
typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested; typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested;
typedef Block<SparseMatrixType, BlockRows, BlockCols, true> BlockType; typedef Block<SparseMatrixType, BlockRows, BlockCols, true> BlockType;
public: public:
enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor }; enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType) EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
protected: protected:
enum { OuterSize = IsRowMajor ? BlockRows : BlockCols }; enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
public: public:
class InnerIterator: public SparseMatrixType::InnerIterator class InnerIterator: public SparseMatrixType::InnerIterator
{ {
public: public:
inline InnerIterator(const BlockType& xpr, Index outer) inline InnerIterator(const BlockType& xpr, Index outer)
: SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer) : SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
{} {}
inline Index row() const { return IsRowMajor ? m_outer : this->index(); } inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
inline Index col() const { return IsRowMajor ? this->index() : m_outer; } inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
protected: protected:
Index m_outer; Index m_outer;
}; };
class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator
{ {
public: public:
inline ReverseInnerIterator(const BlockType& xpr, Index outer) inline ReverseInnerIterator(const BlockType& xpr, Index outer)
: SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer) : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
{} {}
inline Index row() const { return IsRowMajor ? m_outer : this->index(); } inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
inline Index col() const { return IsRowMajor ? this->index() : m_outer; } inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
protected: protected:
Index m_outer; Index m_outer;
}; };
inline BlockImpl(const SparseMatrixType& xpr, int i) inline BlockImpl(const SparseMatrixType& xpr, int i)
: m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize) : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
{} {}
inline BlockImpl(const SparseMatrixType& xpr, int startRow, int startCol, int blockRows, int blockCols) inline BlockImpl(const SparseMatrixType& xpr, int startRow, int startCol, int blockRows, int blockCols)
: m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols) : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
{} {}
template<typename OtherDerived> template<typename OtherDerived>
inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other) inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other)
{ {
typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _NestedMatrixType; typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _NestedMatrixType;
_NestedMatrixType& matrix = const_cast<_NestedMatrixType&>(m_matrix);; _NestedMatrixType& matrix = const_cast<_NestedMatrixType&>(m_matrix);;
// This assignment is slow if this vector set is not empty // This assignment is slow if this vector set is not empty
// and/or it is not at the end of the nonzeros of the underlying matrix. // and/or it is not at the end of the nonzeros of the underlying matrix.
// 1 - eval to a temporary to avoid transposition and/or aliasing issues // 1 - eval to a temporary to avoid transposition and/or aliasing issues
SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, Index> tmp(other); SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, Index> tmp(other);
// 2 - let's check whether there is enough allocated memory // 2 - let's check whether there is enough allocated memory
Index nnz = tmp.nonZeros(); Index nnz = tmp.nonZeros();
Index start = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block Index start = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block
Index end = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]; // ending position of the current block Index end = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]; // ending position of the current block
Index block_size = end - start; // available room in the current block Index block_size = end - start; // available room in the current block
Index tail_size = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end; Index tail_size = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
Index free_size = m_matrix.isCompressed() Index free_size = m_matrix.isCompressed()
? Index(matrix.data().allocatedSize()) + block_size ? Index(matrix.data().allocatedSize()) + block_size
: block_size; : block_size;
if(nnz>free_size) if(nnz>free_size)
{ {
// realloc manually to reduce copies // realloc manually to reduce copies
typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz); typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz);
internal::smart_copy(&m_matrix.data().value(0), &m_matrix.data().value(0) + start, &newdata.value(0)); internal::smart_copy(&m_matrix.data().value(0), &m_matrix.data().value(0) + start, &newdata.value(0));
internal::smart_copy(&m_matrix.data().index(0), &m_matrix.data().index(0) + start, &newdata.index(0)); internal::smart_copy(&m_matrix.data().index(0), &m_matrix.data().index(0) + start, &newdata.index(0));
internal::smart_copy(&tmp.data().value(0), &tmp.data().value(0) + nnz, &newdata.value(start)); internal::smart_copy(&tmp.data().value(0), &tmp.data().value(0) + nnz, &newdata.value(start));
internal::smart_copy(&tmp.data().index(0), &tmp.data().index(0) + nnz, &newdata.index(start)); internal::smart_copy(&tmp.data().index(0), &tmp.data().index(0) + nnz, &newdata.index(start));
internal::smart_copy(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &newdata.value(start+nnz)); internal::smart_copy(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &newdata.value(start+nnz));
internal::smart_copy(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &newdata.index(start+nnz)); internal::smart_copy(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &newdata.index(start+nnz));
newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz); newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
matrix.data().swap(newdata); matrix.data().swap(newdata);
} }
else else
{ {
// no need to realloc, simply copy the tail at its respective position and insert tmp // no need to realloc, simply copy the tail at its respective position and insert tmp
matrix.data().resize(start + nnz + tail_size); matrix.data().resize(start + nnz + tail_size);
internal::smart_memmove(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &matrix.data().value(start + nnz)); internal::smart_memmove(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &matrix.data().value(start + nnz));
internal::smart_memmove(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &matrix.data().index(start + nnz)); internal::smart_memmove(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &matrix.data().index(start + nnz));
internal::smart_copy(&tmp.data().value(0), &tmp.data().value(0) + nnz, &matrix.value(start)); internal::smart_copy(&tmp.data().value(0), &tmp.data().value(0) + nnz, &matrix.data().value(start));
internal::smart_copy(&tmp.data().index(0), &tmp.data().index(0) + nnz, &matrix.index(start)); internal::smart_copy(&tmp.data().index(0), &tmp.data().index(0) + nnz, &matrix.data().index(start));
} }
// update innerNonZeros // update innerNonZeros
if(!m_matrix.isCompressed()) if(!m_matrix.isCompressed())
for(Index j=0; j<m_outerSize.value(); ++j) for(Index j=0; j<m_outerSize.value(); ++j)
matrix.innerNonZeroPtr()[m_outerStart+j] = tmp.innerVector(j).nonZeros(); matrix.innerNonZeroPtr()[m_outerStart+j] = tmp.innerVector(j).nonZeros();
// update outer index pointers // update outer index pointers
Index p = start; Index p = start;
for(Index k=0; k<m_outerSize.value(); ++k) for(Index k=0; k<m_outerSize.value(); ++k)
{ {
matrix.outerIndexPtr()[m_outerStart+k] = p; matrix.outerIndexPtr()[m_outerStart+k] = p;
p += tmp.innerVector(k).nonZeros(); p += tmp.innerVector(k).nonZeros();
} }
std::ptrdiff_t offset = nnz - block_size; std::ptrdiff_t offset = nnz - block_size;
for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k) for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
{ {
matrix.outerIndexPtr()[k] += offset; matrix.outerIndexPtr()[k] += offset;
} }
return derived(); return derived();
} }
inline BlockType& operator=(const BlockType& other) inline BlockType& operator=(const BlockType& other)
{ {
return operator=<BlockType>(other); return operator=<BlockType>(other);
} }
inline const Scalar* valuePtr() const inline const Scalar* valuePtr() const
{ return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; } { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
inline Scalar* valuePtr() inline Scalar* valuePtr()
{ return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; } { return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
inline const Index* innerIndexPtr() const inline const Index* innerIndexPtr() const
{ return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; } { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
inline Index* innerIndexPtr() inline Index* innerIndexPtr()
{ return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; } { return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
inline const Index* outerIndexPtr() const inline const Index* outerIndexPtr() const
{ return m_matrix.outerIndexPtr() + m_outerStart; } { return m_matrix.outerIndexPtr() + m_outerStart; }
inline Index* outerIndexPtr() inline Index* outerIndexPtr()
{ return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; } { return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; }
Index nonZeros() const Index nonZeros() const
{ {
if(m_matrix.isCompressed()) if(m_matrix.isCompressed())
return std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]) return std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()])
- std::size_t(m_matrix.outerIndexPtr()[m_outerStart]); - std::size_t(m_matrix.outerIndexPtr()[m_outerStart]);
else if(m_outerSize.value()==0) else if(m_outerSize.value()==0)
return 0; return 0;
else else
return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum(); return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum();
} }
const Scalar& lastCoeff() const const Scalar& lastCoeff() const
{ {
EIGEN_STATIC_ASSERT_VECTOR_ONLY(BlockImpl); EIGEN_STATIC_ASSERT_VECTOR_ONLY(BlockImpl);
eigen_assert(nonZeros()>0); eigen_assert(nonZeros()>0);
if(m_matrix.isCompressed()) if(m_matrix.isCompressed())
return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1]; return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
else else
return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1]; return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
} }
EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); } EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); } EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
protected: protected:
typename SparseMatrixType::Nested m_matrix; typename SparseMatrixType::Nested m_matrix;
Index m_outerStart; Index m_outerStart;
const internal::variable_if_dynamic<Index, OuterSize> m_outerSize; const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
}; };
//---------- //----------
/** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this /** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
* is col-major (resp. row-major). * is col-major (resp. row-major).
*/ */
template<typename Derived> template<typename Derived>
typename SparseMatrixBase<Derived>::InnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer) typename SparseMatrixBase<Derived>::InnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer)
{ return InnerVectorReturnType(derived(), outer); } { return InnerVectorReturnType(derived(), outer); }
/** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this /** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
* is col-major (resp. row-major). Read-only. * is col-major (resp. row-major). Read-only.
*/ */
template<typename Derived> template<typename Derived>
const typename SparseMatrixBase<Derived>::ConstInnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer) const const typename SparseMatrixBase<Derived>::ConstInnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer) const
{ return ConstInnerVectorReturnType(derived(), outer); } { return ConstInnerVectorReturnType(derived(), outer); }
/** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this /** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
* is col-major (resp. row-major). * is col-major (resp. row-major).
*/ */
template<typename Derived> template<typename Derived>
Block<Derived,Dynamic,Dynamic,true> SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize) Block<Derived,Dynamic,Dynamic,true> SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize)
{ {
return Block<Derived,Dynamic,Dynamic,true>(derived(), return Block<Derived,Dynamic,Dynamic,true>(derived(),
IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart, IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize); IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
} }
/** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this /** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
* is col-major (resp. row-major). Read-only. * is col-major (resp. row-major). Read-only.
*/ */
template<typename Derived> template<typename Derived>
const Block<const Derived,Dynamic,Dynamic,true> SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize) const const Block<const Derived,Dynamic,Dynamic,true> SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize) const
{ {
return Block<const Derived,Dynamic,Dynamic,true>(derived(), return Block<const Derived,Dynamic,Dynamic,true>(derived(),
IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart, IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize); IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
} }
/** Generic implementation of sparse Block expression. /** Generic implementation of sparse Block expression.
* Real-only. * Real-only.
*/ */
template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel> template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
class BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse> class BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse>
: public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,InnerPanel> >, internal::no_assignment_operator : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,InnerPanel> >, internal::no_assignment_operator
{ {
typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested; typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType; typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType;
public: public:
enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor }; enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType) EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
/** Column or Row constructor /** Column or Row constructor
*/ */
inline BlockImpl(const XprType& xpr, int i) inline BlockImpl(const XprType& xpr, int i)
: m_matrix(xpr), : m_matrix(xpr),
m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0), m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0), m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
m_blockRows(xpr.rows()), m_blockRows(xpr.rows()),
m_blockCols(xpr.cols()) m_blockCols(xpr.cols())
{} {}
/** Dynamic-size constructor /** Dynamic-size constructor
*/ */
inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols) inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
: m_matrix(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(blockRows), m_blockCols(blockCols) : m_matrix(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(blockRows), m_blockCols(blockCols)
{} {}
inline int rows() const { return m_blockRows.value(); } inline int rows() const { return m_blockRows.value(); }
inline int cols() const { return m_blockCols.value(); } inline int cols() const { return m_blockCols.value(); }
inline Scalar& coeffRef(int row, int col) inline Scalar& coeffRef(int row, int col)
{ {
return m_matrix.const_cast_derived() return m_matrix.const_cast_derived()
.coeffRef(row + m_startRow.value(), col + m_startCol.value()); .coeffRef(row + m_startRow.value(), col + m_startCol.value());
} }
inline const Scalar coeff(int row, int col) const inline const Scalar coeff(int row, int col) const
{ {
return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value()); return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
} }
inline Scalar& coeffRef(int index) inline Scalar& coeffRef(int index)
{ {
return m_matrix.const_cast_derived() return m_matrix.const_cast_derived()
.coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index), .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0)); m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
} }
inline const Scalar coeff(int index) const inline const Scalar coeff(int index) const
{ {
return m_matrix return m_matrix
.coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index), .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0)); m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
} }
inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; } inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
class InnerIterator : public _MatrixTypeNested::InnerIterator class InnerIterator : public _MatrixTypeNested::InnerIterator
{ {
typedef typename _MatrixTypeNested::InnerIterator Base; typedef typename _MatrixTypeNested::InnerIterator Base;
const BlockType& m_block; const BlockType& m_block;
Index m_end; Index m_end;
public: public:
EIGEN_STRONG_INLINE InnerIterator(const BlockType& block, Index outer) EIGEN_STRONG_INLINE InnerIterator(const BlockType& block, Index outer)
: Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())), : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
m_block(block), m_block(block),
m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value()) m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value())
{ {
while( (Base::operator bool()) && (Base::index() < (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value())) ) while( (Base::operator bool()) && (Base::index() < (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value())) )
Base::operator++(); Base::operator++();
} }
inline Index index() const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); } inline Index index() const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
inline Index outer() const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); } inline Index outer() const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
inline Index row() const { return Base::row() - m_block.m_startRow.value(); } inline Index row() const { return Base::row() - m_block.m_startRow.value(); }
inline Index col() const { return Base::col() - m_block.m_startCol.value(); } inline Index col() const { return Base::col() - m_block.m_startCol.value(); }
inline operator bool() const { return Base::operator bool() && Base::index() < m_end; } inline operator bool() const { return Base::operator bool() && Base::index() < m_end; }
}; };
class ReverseInnerIterator : public _MatrixTypeNested::ReverseInnerIterator class ReverseInnerIterator : public _MatrixTypeNested::ReverseInnerIterator
{ {
typedef typename _MatrixTypeNested::ReverseInnerIterator Base; typedef typename _MatrixTypeNested::ReverseInnerIterator Base;
const BlockType& m_block; const BlockType& m_block;
Index m_begin; Index m_begin;
public: public:
EIGEN_STRONG_INLINE ReverseInnerIterator(const BlockType& block, Index outer) EIGEN_STRONG_INLINE ReverseInnerIterator(const BlockType& block, Index outer)
: Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())), : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
m_block(block), m_block(block),
m_begin(IsRowMajor ? block.m_startCol.value() : block.m_startRow.value()) m_begin(IsRowMajor ? block.m_startCol.value() : block.m_startRow.value())
{ {
while( (Base::operator bool()) && (Base::index() >= (IsRowMajor ? m_block.m_startCol.value()+block.m_blockCols.value() : m_block.m_startRow.value()+block.m_blockRows.value())) ) while( (Base::operator bool()) && (Base::index() >= (IsRowMajor ? m_block.m_startCol.value()+block.m_blockCols.value() : m_block.m_startRow.value()+block.m_blockRows.value())) )
Base::operator--(); Base::operator--();
} }
inline Index index() const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); } inline Index index() const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
inline Index outer() const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); } inline Index outer() const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
inline Index row() const { return Base::row() - m_block.m_startRow.value(); } inline Index row() const { return Base::row() - m_block.m_startRow.value(); }
inline Index col() const { return Base::col() - m_block.m_startCol.value(); } inline Index col() const { return Base::col() - m_block.m_startCol.value(); }
inline operator bool() const { return Base::operator bool() && Base::index() >= m_begin; } inline operator bool() const { return Base::operator bool() && Base::index() >= m_begin; }
}; };
protected: protected:
friend class InnerIterator; friend class InnerIterator;
friend class ReverseInnerIterator; friend class ReverseInnerIterator;
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
typename XprType::Nested m_matrix; typename XprType::Nested m_matrix;
const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow; const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol; const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows; const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows;
const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols; const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols;
}; };
} // end namespace Eigen } // end namespace Eigen
#endif // EIGEN_SPARSE_BLOCK_H #endif // EIGEN_SPARSE_BLOCK_H