10 #ifndef EIGEN_SPARSE_BLOCK_H 11 #define EIGEN_SPARSE_BLOCK_H 15 template<
typename XprType,
int BlockRows,
int BlockCols>
16 class BlockImpl<XprType,BlockRows,BlockCols,true,Sparse>
24 enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
30 typedef typename BlockImpl::Index
Index;
33 : XprType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
35 inline Index
row()
const {
return IsRowMajor ? m_outer : this->index(); }
36 inline Index
col()
const {
return IsRowMajor ? this->index() : m_outer; }
40 class ReverseInnerIterator:
public XprType::ReverseInnerIterator
42 typedef typename BlockImpl::Index
Index;
45 : XprType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
47 inline Index
row()
const {
return IsRowMajor ? m_outer : this->index(); }
48 inline Index
col()
const {
return IsRowMajor ? this->index() : m_outer; }
54 : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
57 inline BlockImpl(
const XprType& xpr,
int startRow,
int startCol,
int blockRows,
int blockCols)
58 : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
76 template<
typename _Scalar,
int _Options,
typename _Index,
int BlockRows,
int BlockCols>
78 :
public SparseMatrixBase<Block<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
87 enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
94 : SparseMatrixType::
InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
96 inline Index
row()
const {
return IsRowMajor ? m_outer : this->index(); }
97 inline Index
col()
const {
return IsRowMajor ? this->index() : m_outer; }
105 : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
107 inline Index row()
const {
return IsRowMajor ? m_outer : this->index(); }
108 inline Index col()
const {
return IsRowMajor ? this->index() : m_outer; }
114 : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
117 inline BlockImpl(
const SparseMatrixType& xpr,
int startRow,
int startCol,
int blockRows,
int blockCols)
118 : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
121 template<
typename OtherDerived>
125 _NestedMatrixType& matrix =
const_cast<_NestedMatrixType&
>(m_matrix);;
134 Index start = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart];
135 Index end = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()];
136 Index block_size = end - start;
137 Index tail_size = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
139 Index free_size = m_matrix.isCompressed()
140 ?
Index(matrix.data().allocatedSize()) + block_size
148 std::memcpy(&newdata.value(0), &m_matrix.data().value(0), start*
sizeof(
Scalar));
149 std::memcpy(&newdata.index(0), &m_matrix.data().index(0), start*
sizeof(
Index));
151 std::memcpy(&newdata.value(start), &tmp.
data().value(0), nnz*
sizeof(
Scalar));
152 std::memcpy(&newdata.index(start), &tmp.
data().index(0), nnz*
sizeof(
Index));
154 std::memcpy(&newdata.value(start+nnz), &matrix.data().value(end), tail_size*
sizeof(
Scalar));
155 std::memcpy(&newdata.index(start+nnz), &matrix.data().index(end), tail_size*
sizeof(
Index));
157 newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
159 matrix.data().swap(newdata);
164 matrix.data().resize(start + nnz + tail_size);
166 std::memmove(&matrix.data().value(start+nnz), &matrix.data().value(end), tail_size*
sizeof(
Scalar));
167 std::memmove(&matrix.data().index(start+nnz), &matrix.data().index(end), tail_size*
sizeof(
Index));
169 std::memcpy(&matrix.data().value(start), &tmp.
data().value(0), nnz*
sizeof(
Scalar));
170 std::memcpy(&matrix.data().index(start), &tmp.
data().index(0), nnz*
sizeof(
Index));
174 if(!m_matrix.isCompressed())
175 for(
Index j=0; j<m_outerSize.value(); ++j)
176 matrix.innerNonZeroPtr()[m_outerStart+j] = tmp.
innerVector(j).nonZeros();
180 for(
Index k=0; k<m_outerSize.value(); ++k)
182 matrix.outerIndexPtr()[m_outerStart+k] = p;
185 std::ptrdiff_t offset = nnz - block_size;
186 for(
Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
188 matrix.outerIndexPtr()[k] += offset;
196 return operator=<BlockType>(other);
200 {
return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
202 {
return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
205 {
return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
207 {
return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
210 {
return m_matrix.outerIndexPtr() + m_outerStart; }
212 {
return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; }
216 if(m_matrix.isCompressed())
217 return std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()])
218 - std::size_t(m_matrix.outerIndexPtr()[m_outerStart]);
219 else if(m_outerSize.value()==0)
229 if(m_matrix.isCompressed())
230 return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
232 return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
251 template<
typename Derived>
258 template<
typename Derived>
265 template<
typename Derived>
269 IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
270 IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
277 template<
typename Derived>
281 IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
282 IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
289 template<
typename XprType,
int BlockRows,
int BlockCols,
bool InnerPanel>
290 class BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse>
303 m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
304 m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
305 m_blockRows(xpr.rows()),
306 m_blockCols(xpr.cols())
311 inline BlockImpl(
const XprType& xpr,
int startRow,
int startCol,
int blockRows,
int blockCols)
312 : m_matrix(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(blockRows), m_blockCols(blockCols)
315 inline int rows()
const {
return m_blockRows.value(); }
316 inline int cols()
const {
return m_blockCols.value(); }
320 return m_matrix.const_cast_derived()
321 .coeffRef(row + m_startRow.value(), col + m_startCol.value());
326 return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
331 return m_matrix.const_cast_derived()
332 .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
333 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
339 .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
340 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
347 typedef typename _MatrixTypeNested::InnerIterator
Base;
353 : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
355 m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value())
357 while( (Base::operator
bool()) && (Base::index() < (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value())) )
361 inline Index index()
const {
return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
362 inline Index outer()
const {
return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
366 inline operator bool()
const {
return Base::operator bool() && Base::index() < m_end; }
368 class ReverseInnerIterator :
public _MatrixTypeNested::ReverseInnerIterator
370 typedef typename _MatrixTypeNested::ReverseInnerIterator
Base;
376 : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
378 m_begin(IsRowMajor ? block.m_startCol.value() : block.m_startRow.value())
380 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())) )
384 inline Index
index()
const {
return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
385 inline Index
outer()
const {
return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
386 inline Index
row()
const {
return Base::row() - m_block.m_startRow.value(); }
387 inline Index
col()
const {
return Base::col() - m_block.m_startCol.value(); }
389 inline operator bool()
const {
return Base::operator bool() && Base::index() >= m_begin; }
393 friend class ReverseInnerIterator;
405 #endif // EIGEN_SPARSE_BLOCK_H
EIGEN_STRONG_INLINE ReverseInnerIterator(const BlockType &block, Index outer)
Block< Derived, Dynamic, Dynamic, true > innerVectors(Index outerStart, Index outerSize)
#define EIGEN_STRONG_INLINE
const internal::variable_if_dynamic< Index, OuterSize > m_outerSize
A versatible sparse matrix representation.
Scalar & coeffRef(int row, int col)
const Index * innerIndexPtr() const
A matrix or vector expression mapping an existing array of data.
Scalar & coeffRef(int index)
EIGEN_STRONG_INLINE InnerIterator(const BlockType &block, Index outer)
const internal::variable_if_dynamic< Index, RowsAtCompileTime > m_blockRows
InnerIterator(const BlockType &xpr, Index outer)
iterative scaling algorithm to equilibrate rows and column norms in matrices
Block< Derived > block(Index startRow, Index startCol, Index blockRows, Index blockCols)
const Scalar * valuePtr() const
EIGEN_STRONG_INLINE Index rows() const
const BlockType & m_block
BlockImpl(const SparseMatrixType &xpr, int i)
BlockType & operator=(const BlockType &other)
const Scalar & lastCoeff() const
Block< XprType, BlockRows, BlockCols, true > BlockType
ReverseInnerIterator(const BlockType &xpr, Index outer)
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
internal::remove_all< typename SparseMatrixType::Nested >::type _MatrixTypeNested
Base class of any sparse matrices or sparse expressions.
BlockType & operator=(const SparseMatrixBase< OtherDerived > &other)
internal::remove_all< typename XprType::Nested >::type _MatrixTypeNested
SparseMatrix< _Scalar, _Options, _Index > SparseMatrixType
internal::traits< Block< SparseMatrix< _Scalar, _Options, _Index >, BlockRows, BlockCols, true > >::Scalar Scalar
ReverseInnerIterator(const BlockType &xpr, Index outer)
internal::remove_all< typename XprType::Nested >::type _MatrixTypeNested
InnerVectorReturnType innerVector(Index outer)
_MatrixTypeNested::ReverseInnerIterator Base
Block< SparseMatrixType, BlockRows, BlockCols, true > BlockType
SparseMatrixType::Nested m_matrix
internal::traits< Derived >::Index Index
const Scalar coeff(int row, int col) const
BlockImpl(const XprType &xpr, int i)
const internal::variable_if_dynamic< Index, ColsAtCompileTime > m_blockCols
const internal::variable_if_dynamic< Index, OuterSize > m_outerSize
const BlockType & m_block
_MatrixTypeNested::InnerIterator Base
An InnerIterator allows to loop over the element of a sparse (or dense) matrix or expression...
Expression of a fixed-size or dynamic-size block.
BlockImpl(const SparseMatrixType &xpr, int startRow, int startCol, int blockRows, int blockCols)
BlockImpl(const XprType &xpr, int startRow, int startCol, int blockRows, int blockCols)
Block< XprType, BlockRows, BlockCols, InnerPanel > BlockType
EIGEN_STRONG_INLINE Index cols() const
InnerIterator(const BlockType &xpr, Index outer)
const Scalar coeff(int index) const
BlockImpl(const XprType &xpr, int startRow, int startCol, int blockRows, int blockCols)
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
EIGEN_STRONG_INLINE Index rows() const
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
const _MatrixTypeNested & nestedExpression() const
const Index * outerIndexPtr() const
EIGEN_STRONG_INLINE Index cols() const