Eigen  3.2.10
SparseBlock.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSE_BLOCK_H
11 #define EIGEN_SPARSE_BLOCK_H
12 
13 namespace Eigen {
14 
15 template<typename XprType, int BlockRows, int BlockCols>
16 class BlockImpl<XprType,BlockRows,BlockCols,true,Sparse>
17  : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,true> >
18 {
19 public:
20  typedef Block<XprType, BlockRows, BlockCols, true> BlockType;
21  enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
22 protected:
23  typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
24  enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
25 public:
26  EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
27 
28  class InnerIterator: public XprType::InnerIterator
29  {
30  typedef typename BlockImpl::Index Index;
31  public:
32  inline InnerIterator(const Block<XprType, BlockRows, BlockCols, true>& xpr, Index outer)
33  : XprType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
34  {}
35  inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
36  inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
37  protected:
38  Index m_outer;
39  };
40  class ReverseInnerIterator: public XprType::ReverseInnerIterator
41  {
42  typedef typename BlockImpl::Index Index;
43  public:
44  inline ReverseInnerIterator(const BlockType& xpr, Index outer)
45  : XprType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
46  {}
47  inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
48  inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
49  protected:
50  Index m_outer;
51  };
52 
53  inline BlockImpl(const XprType& xpr, int i)
54  : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
55  {}
56 
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)
59  {}
60 
61  inline const Scalar coeff(int row, int col) const
62  {
63  return m_matrix.coeff(row + IsRowMajor ? m_outerStart : 0, col +IsRowMajor ? 0 : m_outerStart);
64  }
65 
66  inline const Scalar coeff(int index) const
67  {
68  return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
69  }
70 
71  EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
72  EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
73 
74  protected:
75 
76  typename XprType::Nested m_matrix;
77  Index m_outerStart;
78  const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
79 
80  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
81  private:
82  Index nonZeros() const;
83 };
84 
85 
86 /***************************************************************************
87 * specialisation for SparseMatrix
88 ***************************************************************************/
89 
90 template<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols>
91 class BlockImpl<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse>
92  : public SparseMatrixBase<Block<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
93 {
94  typedef SparseMatrix<_Scalar, _Options, _Index> SparseMatrixType;
95  typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested;
96  typedef Block<const SparseMatrixType, BlockRows, BlockCols, true> ConstBlockType;
97 public:
98  typedef Block<SparseMatrixType, BlockRows, BlockCols, true> BlockType;
99  enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
100  EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
101 protected:
102  enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
103 public:
104 
105  class InnerIterator: public SparseMatrixType::InnerIterator
106  {
107  public:
108  inline InnerIterator(const BlockType& xpr, Index outer)
109  : SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
110  {}
111  inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
112  inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
113  protected:
114  Index m_outer;
115  };
116  class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator
117  {
118  public:
119  inline ReverseInnerIterator(const BlockType& xpr, Index outer)
120  : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
121  {}
122  inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
123  inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
124  protected:
125  Index m_outer;
126  };
127 
128  inline BlockImpl(const SparseMatrixType& xpr, int i)
129  : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
130  {}
131 
132  inline BlockImpl(const SparseMatrixType& xpr, int startRow, int startCol, int blockRows, int blockCols)
133  : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
134  {}
135 
136  template<typename OtherDerived>
137  inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other)
138  {
139  typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _NestedMatrixType;
140  _NestedMatrixType& matrix = const_cast<_NestedMatrixType&>(m_matrix);;
141  // This assignement is slow if this vector set is not empty
142  // and/or it is not at the end of the nonzeros of the underlying matrix.
143 
144  // 1 - eval to a temporary to avoid transposition and/or aliasing issues
145  SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, Index> tmp(other);
146 
147  // 2 - let's check whether there is enough allocated memory
148  Index nnz = tmp.nonZeros();
149  Index start = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block
150  Index end = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]; // ending posiiton of the current block
151  Index block_size = end - start; // available room in the current block
152  Index tail_size = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
153 
154  Index free_size = m_matrix.isCompressed()
155  ? Index(matrix.data().allocatedSize()) + block_size
156  : block_size;
157 
158  if(nnz>free_size)
159  {
160  // realloc manually to reduce copies
161  typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz);
162 
163  std::memcpy(newdata.valuePtr(), m_matrix.data().valuePtr(), start*sizeof(Scalar));
164  std::memcpy(newdata.indexPtr(), m_matrix.data().indexPtr(), start*sizeof(Index));
165 
166  std::memcpy(newdata.valuePtr() + start, tmp.data().valuePtr(), nnz*sizeof(Scalar));
167  std::memcpy(newdata.indexPtr() + start, tmp.data().indexPtr(), nnz*sizeof(Index));
168 
169  std::memcpy(newdata.valuePtr()+start+nnz, matrix.data().valuePtr()+end, tail_size*sizeof(Scalar));
170  std::memcpy(newdata.indexPtr()+start+nnz, matrix.data().indexPtr()+end, tail_size*sizeof(Index));
171 
172  newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
173 
174  matrix.data().swap(newdata);
175  }
176  else
177  {
178  // no need to realloc, simply copy the tail at its respective position and insert tmp
179  matrix.data().resize(start + nnz + tail_size);
180 
181  std::memmove(matrix.data().valuePtr()+start+nnz, matrix.data().valuePtr()+end, tail_size*sizeof(Scalar));
182  std::memmove(matrix.data().indexPtr()+start+nnz, matrix.data().indexPtr()+end, tail_size*sizeof(Index));
183 
184  std::memcpy(matrix.data().valuePtr()+start, tmp.data().valuePtr(), nnz*sizeof(Scalar));
185  std::memcpy(matrix.data().indexPtr()+start, tmp.data().indexPtr(), nnz*sizeof(Index));
186  }
187 
188  // update innerNonZeros
189  if(!m_matrix.isCompressed())
190  for(Index j=0; j<m_outerSize.value(); ++j)
191  matrix.innerNonZeroPtr()[m_outerStart+j] = tmp.innerVector(j).nonZeros();
192 
193  // update outer index pointers
194  Index p = start;
195  for(Index k=0; k<m_outerSize.value(); ++k)
196  {
197  matrix.outerIndexPtr()[m_outerStart+k] = p;
198  p += tmp.innerVector(k).nonZeros();
199  }
200  std::ptrdiff_t offset = nnz - block_size;
201  for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
202  {
203  matrix.outerIndexPtr()[k] += offset;
204  }
205 
206  return derived();
207  }
208 
209  inline BlockType& operator=(const BlockType& other)
210  {
211  return operator=<BlockType>(other);
212  }
213 
214  inline const Scalar* valuePtr() const
215  { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
216  inline Scalar* valuePtr()
217  { return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
218 
219  inline const Index* innerIndexPtr() const
220  { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
221  inline Index* innerIndexPtr()
222  { return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
223 
224  inline const Index* outerIndexPtr() const
225  { return m_matrix.outerIndexPtr() + m_outerStart; }
226  inline Index* outerIndexPtr()
227  { return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; }
228 
229  Index nonZeros() const
230  {
231  if(m_matrix.isCompressed())
232  return std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()])
233  - std::size_t(m_matrix.outerIndexPtr()[m_outerStart]);
234  else if(m_outerSize.value()==0)
235  return 0;
236  else
237  return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum();
238  }
239 
240  inline Scalar& coeffRef(int row, int col)
241  {
242  return m_matrix.const_cast_derived().coeffRef(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
243  }
244 
245  inline const Scalar coeff(int row, int col) const
246  {
247  return m_matrix.coeff(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
248  }
249 
250  inline const Scalar coeff(int index) const
251  {
252  return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
253  }
254 
255  const Scalar& lastCoeff() const
256  {
257  EIGEN_STATIC_ASSERT_VECTOR_ONLY(BlockImpl);
258  eigen_assert(nonZeros()>0);
259  if(m_matrix.isCompressed())
260  return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
261  else
262  return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
263  }
264 
265  EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
266  EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
267 
268  protected:
269 
270  typename SparseMatrixType::Nested m_matrix;
271  Index m_outerStart;
272  const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
273 
274 };
275 
276 
277 template<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols>
278 class BlockImpl<const SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse>
279  : public SparseMatrixBase<Block<const SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
280 {
281  typedef SparseMatrix<_Scalar, _Options, _Index> SparseMatrixType;
282  typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested;
283 public:
284  typedef Block<const SparseMatrixType, BlockRows, BlockCols, true> BlockType;
285  enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
286  EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
287 protected:
288  enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
289 public:
290 
291  class InnerIterator: public SparseMatrixType::InnerIterator
292  {
293  public:
294  inline InnerIterator(const BlockType& xpr, Index outer)
295  : SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
296  {}
297  inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
298  inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
299  protected:
300  Index m_outer;
301  };
302  class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator
303  {
304  public:
305  inline ReverseInnerIterator(const BlockType& xpr, Index outer)
306  : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
307  {}
308  inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
309  inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
310  protected:
311  Index m_outer;
312  };
313 
314  inline BlockImpl(const SparseMatrixType& xpr, int i)
315  : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
316  {}
317 
318  inline BlockImpl(const SparseMatrixType& xpr, int startRow, int startCol, int blockRows, int blockCols)
319  : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
320  {}
321 
322  inline const Scalar* valuePtr() const
323  { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
324 
325  inline const Index* innerIndexPtr() const
326  { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
327 
328  inline const Index* outerIndexPtr() const
329  { return m_matrix.outerIndexPtr() + m_outerStart; }
330 
331  Index nonZeros() const
332  {
333  if(m_matrix.isCompressed())
334  return std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()])
335  - std::size_t(m_matrix.outerIndexPtr()[m_outerStart]);
336  else if(m_outerSize.value()==0)
337  return 0;
338  else
339  return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum();
340  }
341 
342  inline const Scalar coeff(int row, int col) const
343  {
344  return m_matrix.coeff(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
345  }
346 
347  inline const Scalar coeff(int index) const
348  {
349  return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
350  }
351 
352  const Scalar& lastCoeff() const
353  {
354  EIGEN_STATIC_ASSERT_VECTOR_ONLY(BlockImpl);
355  eigen_assert(nonZeros()>0);
356  if(m_matrix.isCompressed())
357  return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
358  else
359  return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
360  }
361 
362  EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
363  EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
364 
365  protected:
366 
367  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
368 
369  typename SparseMatrixType::Nested m_matrix;
370  Index m_outerStart;
371  const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
372 };
373 
374 //----------
375 
379 template<typename Derived>
380 typename SparseMatrixBase<Derived>::InnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer)
381 { return InnerVectorReturnType(derived(), outer); }
382 
386 template<typename Derived>
388 { return ConstInnerVectorReturnType(derived(), outer); }
389 
393 template<typename Derived>
395 SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize)
396 {
397  return Block<Derived,Dynamic,Dynamic,true>(derived(),
398  IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
399  IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
400 
401 }
402 
406 template<typename Derived>
408 SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize) const
409 {
411  IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
412  IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
413 
414 }
415 
416 namespace internal {
417 
418 template< typename XprType, int BlockRows, int BlockCols, bool InnerPanel,
419  bool OuterVector = (BlockCols==1 && XprType::IsRowMajor) || (BlockRows==1 && !XprType::IsRowMajor)>
420 class GenericSparseBlockInnerIteratorImpl;
421 
422 }
423 
427 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
428 class BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse>
429  : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,InnerPanel> >, internal::no_assignment_operator
430 {
431  typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
432  public:
434  enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
435  EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
436 
437 
439  inline BlockImpl(const XprType& xpr, int i)
440  : m_matrix(xpr),
441  m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
442  m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
443  m_blockRows(BlockRows==1 ? 1 : xpr.rows()),
444  m_blockCols(BlockCols==1 ? 1 : xpr.cols())
445  {}
446 
449  inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
450  : m_matrix(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(blockRows), m_blockCols(blockCols)
451  {}
452 
453  inline int rows() const { return m_blockRows.value(); }
454  inline int cols() const { return m_blockCols.value(); }
455 
456  inline Scalar& coeffRef(int row, int col)
457  {
458  return m_matrix.const_cast_derived()
459  .coeffRef(row + m_startRow.value(), col + m_startCol.value());
460  }
461 
462  inline const Scalar coeff(int row, int col) const
463  {
464  return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
465  }
466 
467  inline Scalar& coeffRef(int index)
468  {
469  return m_matrix.const_cast_derived()
470  .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
471  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
472  }
473 
474  inline const Scalar coeff(int index) const
475  {
476  return m_matrix
477  .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
478  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
479  }
480 
481  inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
482 
483  typedef internal::GenericSparseBlockInnerIteratorImpl<XprType,BlockRows,BlockCols,InnerPanel> InnerIterator;
484 
485  class ReverseInnerIterator : public _MatrixTypeNested::ReverseInnerIterator
486  {
487  typedef typename _MatrixTypeNested::ReverseInnerIterator Base;
488  const BlockType& m_block;
489  Index m_begin;
490  public:
491 
492  EIGEN_STRONG_INLINE ReverseInnerIterator(const BlockType& block, Index outer)
493  : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
494  m_block(block),
495  m_begin(IsRowMajor ? block.m_startCol.value() : block.m_startRow.value())
496  {
497  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())) )
498  Base::operator--();
499  }
500 
501  inline Index index() const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
502  inline Index outer() const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
503  inline Index row() const { return Base::row() - m_block.m_startRow.value(); }
504  inline Index col() const { return Base::col() - m_block.m_startCol.value(); }
505 
506  inline operator bool() const { return Base::operator bool() && Base::index() >= m_begin; }
507  };
508  protected:
509  friend class internal::GenericSparseBlockInnerIteratorImpl<XprType,BlockRows,BlockCols,InnerPanel>;
510  friend class ReverseInnerIterator;
511 
512  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
513 
514  typename XprType::Nested m_matrix;
515  const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
516  const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
517  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows;
518  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols;
519  private:
520  Index nonZeros() const;
521 };
522 
523 namespace internal {
524  template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
525  class GenericSparseBlockInnerIteratorImpl<XprType,BlockRows,BlockCols,InnerPanel,false> : public internal::remove_all<typename XprType::Nested>::type::InnerIterator
526  {
527  public:
529  enum {
530  IsRowMajor = BlockType::IsRowMajor
531  };
532  typedef typename BlockType::Index Index;
533  protected:
534  typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
535  typedef typename _MatrixTypeNested::InnerIterator Base;
536  const BlockType& m_block;
537  Index m_end;
538  public:
539 
540  EIGEN_STRONG_INLINE GenericSparseBlockInnerIteratorImpl(const BlockType& block, Index outer)
541  : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
542  m_block(block),
543  m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value())
544  {
545  while( (Base::operator bool()) && (Base::index() < (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value())) )
546  Base::operator++();
547  }
548 
549  inline Index index() const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
550  inline Index outer() const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
551  inline Index row() const { return Base::row() - m_block.m_startRow.value(); }
552  inline Index col() const { return Base::col() - m_block.m_startCol.value(); }
553 
554  inline operator bool() const { return Base::operator bool() && Base::index() < m_end; }
555  };
556 
557  // Row vector of a column-major sparse matrix or column of a row-major one.
558  template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
559  class GenericSparseBlockInnerIteratorImpl<XprType,BlockRows,BlockCols,InnerPanel,true>
560  {
561  public:
563  enum {
564  IsRowMajor = BlockType::IsRowMajor
565  };
566  typedef typename BlockType::Index Index;
567  typedef typename BlockType::Scalar Scalar;
568  protected:
569  typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
570  const BlockType& m_block;
571  Index m_outerPos;
572  Index m_innerIndex;
573  Scalar m_value;
574  Index m_end;
575  public:
576 
577  EIGEN_STRONG_INLINE GenericSparseBlockInnerIteratorImpl(const BlockType& block, Index outer = 0)
578  :
579  m_block(block),
580  m_outerPos( (IsRowMajor ? block.m_startCol.value() : block.m_startRow.value()) - 1), // -1 so that operator++ finds the first non-zero entry
581  m_innerIndex(IsRowMajor ? block.m_startRow.value() : block.m_startCol.value()),
582  m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value())
583  {
584  EIGEN_UNUSED_VARIABLE(outer);
585  eigen_assert(outer==0);
586 
587  ++(*this);
588  }
589 
590  inline Index index() const { return m_outerPos - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
591  inline Index outer() const { return 0; }
592  inline Index row() const { return IsRowMajor ? 0 : index(); }
593  inline Index col() const { return IsRowMajor ? index() : 0; }
594 
595  inline Scalar value() const { return m_value; }
596 
597  inline GenericSparseBlockInnerIteratorImpl& operator++()
598  {
599  // search next non-zero entry
600  while(m_outerPos<m_end)
601  {
602  m_outerPos++;
603  typename XprType::InnerIterator it(m_block.m_matrix, m_outerPos);
604  // search for the key m_innerIndex in the current outer-vector
605  while(it && it.index() < m_innerIndex) ++it;
606  if(it && it.index()==m_innerIndex)
607  {
608  m_value = it.value();
609  break;
610  }
611  }
612  return *this;
613  }
614 
615  inline operator bool() const { return m_outerPos < m_end; }
616  };
617 
618 } // end namespace internal
619 
620 
621 } // end namespace Eigen
622 
623 #endif // EIGEN_SPARSE_BLOCK_H
BlockImpl(const XprType &xpr, int startRow, int startCol, int blockRows, int blockCols)
Definition: SparseBlock.h:449
Definition: LDLT.h:16
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:239
Definition: Eigen_Colamd.h:50
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:102
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:380
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:395