Eigen  3.2.92
SparseBlock.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2014 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 // Subset of columns or rows
16 template<typename XprType, int BlockRows, int BlockCols>
17 class BlockImpl<XprType,BlockRows,BlockCols,true,Sparse>
18  : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,true> >
19 {
20  typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
21  typedef Block<XprType, BlockRows, BlockCols, true> BlockType;
22 public:
23  enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
24 protected:
25  enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
26  typedef SparseMatrixBase<BlockType> Base;
27  using Base::convert_index;
28 public:
29  EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
30 
31  inline BlockImpl(const XprType& xpr, Index i)
32  : m_matrix(xpr), m_outerStart(convert_index(i)), m_outerSize(OuterSize)
33  {}
34 
35  inline BlockImpl(const XprType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
36  : m_matrix(xpr), m_outerStart(convert_index(IsRowMajor ? startRow : startCol)), m_outerSize(convert_index(IsRowMajor ? blockRows : blockCols))
37  {}
38 
39  EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
40  EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
41 
42  Index nonZeros() const
43  {
44  typedef internal::evaluator<XprType> EvaluatorType;
45  EvaluatorType matEval(m_matrix);
46  Index nnz = 0;
47  Index end = m_outerStart + m_outerSize.value();
48  for(Index j=m_outerStart; j<end; ++j)
49  for(typename EvaluatorType::InnerIterator it(matEval, j); it; ++it)
50  ++nnz;
51  return nnz;
52  }
53 
54  inline const Scalar coeff(Index row, Index col) const
55  {
56  return m_matrix.coeff(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
57  }
58 
59  inline const Scalar coeff(Index index) const
60  {
61  return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
62  }
63 
64  inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
65  Index startRow() const { return IsRowMajor ? m_outerStart : 0; }
66  Index startCol() const { return IsRowMajor ? 0 : m_outerStart; }
67  Index blockRows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
68  Index blockCols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
69 
70  protected:
71 
72  typename XprType::Nested m_matrix;
73  Index m_outerStart;
74  const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
75 
76  public:
77  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
78 };
79 
80 
81 /***************************************************************************
82 * specialization for SparseMatrix
83 ***************************************************************************/
84 
85 namespace internal {
86 
87 template<typename SparseMatrixType, int BlockRows, int BlockCols>
88 class sparse_matrix_block_impl
89  : public SparseCompressedBase<Block<SparseMatrixType,BlockRows,BlockCols,true> >
90 {
91  typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested;
92  typedef Block<SparseMatrixType, BlockRows, BlockCols, true> BlockType;
93  typedef SparseCompressedBase<Block<SparseMatrixType,BlockRows,BlockCols,true> > Base;
94  using Base::convert_index;
95 public:
96  enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
97  EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
98 protected:
99  typedef typename Base::IndexVector IndexVector;
100  enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
101 public:
102 
103  inline sparse_matrix_block_impl(const SparseMatrixType& xpr, Index i)
104  : m_matrix(xpr), m_outerStart(convert_index(i)), m_outerSize(OuterSize)
105  {}
106 
107  inline sparse_matrix_block_impl(const SparseMatrixType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
108  : m_matrix(xpr), m_outerStart(convert_index(IsRowMajor ? startRow : startCol)), m_outerSize(convert_index(IsRowMajor ? blockRows : blockCols))
109  {}
110 
111  template<typename OtherDerived>
112  inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other)
113  {
114  typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _NestedMatrixType;
115  _NestedMatrixType& matrix = const_cast<_NestedMatrixType&>(m_matrix);;
116  // This assignment is slow if this vector set is not empty
117  // and/or it is not at the end of the nonzeros of the underlying matrix.
118 
119  // 1 - eval to a temporary to avoid transposition and/or aliasing issues
120  Ref<const SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, StorageIndex> > tmp(other.derived());
121  eigen_internal_assert(tmp.outerSize()==m_outerSize.value());
122 
123  // 2 - let's check whether there is enough allocated memory
124  Index nnz = tmp.nonZeros();
125  Index start = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block
126  Index end = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]; // ending position of the current block
127  Index block_size = end - start; // available room in the current block
128  Index tail_size = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
129 
130  Index free_size = m_matrix.isCompressed()
131  ? Index(matrix.data().allocatedSize()) + block_size
132  : block_size;
133 
134  bool update_trailing_pointers = false;
135  if(nnz>free_size)
136  {
137  // realloc manually to reduce copies
138  typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz);
139 
140  internal::smart_copy(&m_matrix.data().value(0), &m_matrix.data().value(0) + start, &newdata.value(0));
141  internal::smart_copy(&m_matrix.data().index(0), &m_matrix.data().index(0) + start, &newdata.index(0));
142 
143  internal::smart_copy(tmp.valuePtr(), tmp.valuePtr() + nnz, &newdata.value(start));
144  internal::smart_copy(tmp.innerIndexPtr(), tmp.innerIndexPtr() + nnz, &newdata.index(start));
145 
146  internal::smart_copy(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &newdata.value(start+nnz));
147  internal::smart_copy(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &newdata.index(start+nnz));
148 
149  newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
150 
151  matrix.data().swap(newdata);
152 
153  update_trailing_pointers = true;
154  }
155  else
156  {
157  if(m_matrix.isCompressed())
158  {
159  // no need to realloc, simply copy the tail at its respective position and insert tmp
160  matrix.data().resize(start + nnz + tail_size);
161 
162  internal::smart_memmove(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &matrix.data().value(start + nnz));
163  internal::smart_memmove(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &matrix.data().index(start + nnz));
164 
165  update_trailing_pointers = true;
166  }
167 
168  internal::smart_copy(tmp.valuePtr(), tmp.valuePtr() + nnz, &matrix.data().value(start));
169  internal::smart_copy(tmp.innerIndexPtr(), tmp.innerIndexPtr() + nnz, &matrix.data().index(start));
170  }
171 
172  // update outer index pointers and innerNonZeros
173  if(IsVectorAtCompileTime)
174  {
175  if(!m_matrix.isCompressed())
176  matrix.innerNonZeroPtr()[m_outerStart] = StorageIndex(nnz);
177  matrix.outerIndexPtr()[m_outerStart] = StorageIndex(start);
178  }
179  else
180  {
181  StorageIndex p = StorageIndex(start);
182  for(Index k=0; k<m_outerSize.value(); ++k)
183  {
184  Index nnz_k = tmp.innerVector(k).nonZeros();
185  if(!m_matrix.isCompressed())
186  matrix.innerNonZeroPtr()[m_outerStart+k] = StorageIndex(nnz_k);
187  matrix.outerIndexPtr()[m_outerStart+k] = p;
188  p += nnz_k;
189  }
190  }
191 
192  if(update_trailing_pointers)
193  {
194  StorageIndex offset = internal::convert_index<StorageIndex>(nnz - block_size);
195  for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
196  {
197  matrix.outerIndexPtr()[k] += offset;
198  }
199  }
200 
201  return derived();
202  }
203 
204  inline BlockType& operator=(const BlockType& other)
205  {
206  return operator=<BlockType>(other);
207  }
208 
209  inline const Scalar* valuePtr() const
210  { return m_matrix.valuePtr(); }
211  inline Scalar* valuePtr()
212  { return m_matrix.const_cast_derived().valuePtr(); }
213 
214  inline const StorageIndex* innerIndexPtr() const
215  { return m_matrix.innerIndexPtr(); }
216  inline StorageIndex* innerIndexPtr()
217  { return m_matrix.const_cast_derived().innerIndexPtr(); }
218 
219  inline const StorageIndex* outerIndexPtr() const
220  { return m_matrix.outerIndexPtr() + m_outerStart; }
221  inline StorageIndex* outerIndexPtr()
222  { return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; }
223 
224  inline const StorageIndex* innerNonZeroPtr() const
225  { return isCompressed() ? 0 : (m_matrix.innerNonZeroPtr()+m_outerStart); }
226  inline StorageIndex* innerNonZeroPtr()
227  { return isCompressed() ? 0 : (m_matrix.const_cast_derived().innerNonZeroPtr()+m_outerStart); }
228 
229  bool isCompressed() const { return m_matrix.innerNonZeroPtr()==0; }
230 
231  inline Scalar& coeffRef(Index row, Index col)
232  {
233  return m_matrix.const_cast_derived().coeffRef(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
234  }
235 
236  inline const Scalar coeff(Index row, Index col) const
237  {
238  return m_matrix.coeff(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
239  }
240 
241  inline const Scalar coeff(Index index) const
242  {
243  return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
244  }
245 
246  const Scalar& lastCoeff() const
247  {
248  EIGEN_STATIC_ASSERT_VECTOR_ONLY(sparse_matrix_block_impl);
249  eigen_assert(Base::nonZeros()>0);
250  if(m_matrix.isCompressed())
251  return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
252  else
253  return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
254  }
255 
256  EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
257  EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
258 
259  inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
260  Index startRow() const { return IsRowMajor ? m_outerStart : 0; }
261  Index startCol() const { return IsRowMajor ? 0 : m_outerStart; }
262  Index blockRows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
263  Index blockCols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
264 
265  protected:
266 
267  typename SparseMatrixType::Nested m_matrix;
268  Index m_outerStart;
269  const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
270 
271 };
272 
273 } // namespace internal
274 
275 template<typename _Scalar, int _Options, typename _StorageIndex, int BlockRows, int BlockCols>
276 class BlockImpl<SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true,Sparse>
277  : public internal::sparse_matrix_block_impl<SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols>
278 {
279 public:
280  typedef _StorageIndex StorageIndex;
281  typedef SparseMatrix<_Scalar, _Options, _StorageIndex> SparseMatrixType;
282  typedef internal::sparse_matrix_block_impl<SparseMatrixType,BlockRows,BlockCols> Base;
283  inline BlockImpl(SparseMatrixType& xpr, Index i)
284  : Base(xpr, i)
285  {}
286 
287  inline BlockImpl(SparseMatrixType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
288  : Base(xpr, startRow, startCol, blockRows, blockCols)
289  {}
290 
291  using Base::operator=;
292 };
293 
294 template<typename _Scalar, int _Options, typename _StorageIndex, int BlockRows, int BlockCols>
295 class BlockImpl<const SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true,Sparse>
296  : public internal::sparse_matrix_block_impl<const SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols>
297 {
298 public:
299  typedef _StorageIndex StorageIndex;
300  typedef const SparseMatrix<_Scalar, _Options, _StorageIndex> SparseMatrixType;
301  typedef internal::sparse_matrix_block_impl<SparseMatrixType,BlockRows,BlockCols> Base;
302  inline BlockImpl(SparseMatrixType& xpr, Index i)
303  : Base(xpr, i)
304  {}
305 
306  inline BlockImpl(SparseMatrixType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
307  : Base(xpr, startRow, startCol, blockRows, blockCols)
308  {}
309 
310  using Base::operator=;
311 private:
312  template<typename Derived> BlockImpl(const SparseMatrixBase<Derived>& xpr, Index i);
313  template<typename Derived> BlockImpl(const SparseMatrixBase<Derived>& xpr);
314 };
315 
316 //----------
317 
321 template<typename Derived>
323 { return InnerVectorReturnType(derived(), outer); }
324 
328 template<typename Derived>
330 { return ConstInnerVectorReturnType(derived(), outer); }
331 
335 template<typename Derived>
338 {
339  return Block<Derived,Dynamic,Dynamic,true>(derived(),
340  IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
341  IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
342 
343 }
344 
348 template<typename Derived>
351 {
353  IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
354  IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
355 
356 }
357 
361 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
362 class BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse>
363  : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,InnerPanel> >, internal::no_assignment_operator
364 {
366  typedef SparseMatrixBase<BlockType> Base;
367  using Base::convert_index;
368 public:
369  enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
370  EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
371 
372  typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
373 
376  inline BlockImpl(const XprType& xpr, Index i)
377  : m_matrix(xpr),
378  m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? convert_index(i) : 0),
379  m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? convert_index(i) : 0),
380  m_blockRows(BlockRows==1 ? 1 : xpr.rows()),
381  m_blockCols(BlockCols==1 ? 1 : xpr.cols())
382  {}
383 
386  inline BlockImpl(const XprType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
387  : m_matrix(xpr), m_startRow(convert_index(startRow)), m_startCol(convert_index(startCol)), m_blockRows(convert_index(blockRows)), m_blockCols(convert_index(blockCols))
388  {}
389 
390  inline Index rows() const { return m_blockRows.value(); }
391  inline Index cols() const { return m_blockCols.value(); }
392 
393  inline Scalar& coeffRef(Index row, Index col)
394  {
395  return m_matrix.const_cast_derived()
396  .coeffRef(row + m_startRow.value(), col + m_startCol.value());
397  }
398 
399  inline const Scalar coeff(Index row, Index col) const
400  {
401  return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
402  }
403 
404  inline Scalar& coeffRef(Index index)
405  {
406  return m_matrix.const_cast_derived()
407  .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
408  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
409  }
410 
411  inline const Scalar coeff(Index index) const
412  {
413  return m_matrix
414  .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
415  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
416  }
417 
418  inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
419  Index startRow() const { return m_startRow.value(); }
420  Index startCol() const { return m_startCol.value(); }
421  Index blockRows() const { return m_blockRows.value(); }
422  Index blockCols() const { return m_blockCols.value(); }
423 
424  protected:
425 // friend class internal::GenericSparseBlockInnerIteratorImpl<XprType,BlockRows,BlockCols,InnerPanel>;
426  friend class ReverseInnerIterator;
427  friend struct internal::unary_evaluator<Block<XprType,BlockRows,BlockCols,InnerPanel>, internal::IteratorBased, Scalar >;
428 
429  Index nonZeros() const { return Dynamic; }
430 
431  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
432 
433  typename XprType::Nested m_matrix;
434  const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
435  const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
436  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows;
437  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols;
438 
439 };
440 
441 namespace internal {
442 
443 template<typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
444 struct unary_evaluator<Block<ArgType,BlockRows,BlockCols,InnerPanel>, IteratorBased >
445  : public evaluator_base<Block<ArgType,BlockRows,BlockCols,InnerPanel> >
446 {
447  class InnerVectorInnerIterator;
448  class OuterVectorInnerIterator;
449  public:
451  typedef typename XprType::StorageIndex StorageIndex;
452  typedef typename XprType::Scalar Scalar;
453 
454  class ReverseInnerIterator;
455 
456  enum {
457  IsRowMajor = XprType::IsRowMajor,
458 
459  OuterVector = (BlockCols==1 && ArgType::IsRowMajor)
460  | // FIXME | instead of || to please GCC 4.4.0 stupid warning "suggest parentheses around &&".
461  // revert to || as soon as not needed anymore.
462  (BlockRows==1 && !ArgType::IsRowMajor),
463 
464  CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
465  Flags = XprType::Flags
466  };
467 
468  typedef typename internal::conditional<OuterVector,OuterVectorInnerIterator,InnerVectorInnerIterator>::type InnerIterator;
469 
470  explicit unary_evaluator(const XprType& op)
471  : m_argImpl(op.nestedExpression()), m_block(op)
472  {}
473 
474  inline Index nonZerosEstimate() const {
475  Index nnz = m_block.nonZeros();
476  if(nnz<0)
477  return m_argImpl.nonZerosEstimate() * m_block.size() / m_block.nestedExpression().size();
478  return nnz;
479  }
480 
481  protected:
482  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
483 
484  evaluator<ArgType> m_argImpl;
485  const XprType &m_block;
486 };
487 
488 template<typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
489 class unary_evaluator<Block<ArgType,BlockRows,BlockCols,InnerPanel>, IteratorBased>::InnerVectorInnerIterator
490  : public EvalIterator
491 {
492  const XprType& m_block;
493  Index m_end;
494 public:
495 
496  EIGEN_STRONG_INLINE InnerVectorInnerIterator(const unary_evaluator& aEval, Index outer)
497  : EvalIterator(aEval.m_argImpl, outer + (IsRowMajor ? aEval.m_block.startRow() : aEval.m_block.startCol())),
498  m_block(aEval.m_block),
499  m_end(IsRowMajor ? aEval.m_block.startCol()+aEval.m_block.blockCols() : aEval.m_block.startRow()+aEval.m_block.blockRows())
500  {
501  while( (EvalIterator::operator bool()) && (EvalIterator::index() < (IsRowMajor ? m_block.startCol() : m_block.startRow())) )
502  EvalIterator::operator++();
503  }
504 
505  inline StorageIndex index() const { return EvalIterator::index() - convert_index<StorageIndex>(IsRowMajor ? m_block.startCol() : m_block.startRow()); }
506  inline Index outer() const { return EvalIterator::outer() - (IsRowMajor ? m_block.startRow() : m_block.startCol()); }
507  inline Index row() const { return EvalIterator::row() - m_block.startRow(); }
508  inline Index col() const { return EvalIterator::col() - m_block.startCol(); }
509 
510  inline operator bool() const { return EvalIterator::operator bool() && EvalIterator::index() < m_end; }
511 };
512 
513 template<typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
514 class unary_evaluator<Block<ArgType,BlockRows,BlockCols,InnerPanel>, IteratorBased>::OuterVectorInnerIterator
515 {
516  const unary_evaluator& m_eval;
517  Index m_outerPos;
518  Index m_innerIndex;
519  Scalar m_value;
520  Index m_end;
521 public:
522 
523  EIGEN_STRONG_INLINE OuterVectorInnerIterator(const unary_evaluator& aEval, Index outer)
524  : m_eval(aEval),
525  m_outerPos( (IsRowMajor ? aEval.m_block.startCol() : aEval.m_block.startRow()) - 1), // -1 so that operator++ finds the first non-zero entry
526  m_innerIndex(IsRowMajor ? aEval.m_block.startRow() : aEval.m_block.startCol()),
527  m_value(0),
528  m_end(IsRowMajor ? aEval.m_block.startCol()+aEval.m_block.blockCols() : aEval.m_block.startRow()+aEval.m_block.blockRows())
529  {
530  EIGEN_UNUSED_VARIABLE(outer);
531  eigen_assert(outer==0);
532 
533  ++(*this);
534  }
535 
536  inline StorageIndex index() const { return convert_index<StorageIndex>(m_outerPos - (IsRowMajor ? m_eval.m_block.startCol() : m_eval.m_block.startRow())); }
537  inline Index outer() const { return 0; }
538  inline Index row() const { return IsRowMajor ? 0 : index(); }
539  inline Index col() const { return IsRowMajor ? index() : 0; }
540 
541  inline Scalar value() const { return m_value; }
542 
543  inline OuterVectorInnerIterator& operator++()
544  {
545  // search next non-zero entry
546  while(++m_outerPos<m_end)
547  {
548  EvalIterator it(m_eval.m_argImpl, m_outerPos);
549  // search for the key m_innerIndex in the current outer-vector
550  while(it && it.index() < m_innerIndex) ++it;
551  if(it && it.index()==m_innerIndex)
552  {
553  m_value = it.value();
554  break;
555  }
556  }
557  return *this;
558  }
559 
560  inline operator bool() const { return m_outerPos < m_end; }
561 };
562 
563 template<typename _Scalar, int _Options, typename _StorageIndex, int BlockRows, int BlockCols>
564 struct unary_evaluator<Block<SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true>, IteratorBased>
565  : evaluator<SparseCompressedBase<Block<SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true> > >
566 {
567  typedef Block<SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true> XprType;
568  typedef evaluator<SparseCompressedBase<XprType> > Base;
569  explicit unary_evaluator(const XprType &xpr) : Base(xpr) {}
570 };
571 
572 template<typename _Scalar, int _Options, typename _StorageIndex, int BlockRows, int BlockCols>
573 struct unary_evaluator<Block<const SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true>, IteratorBased>
574  : evaluator<SparseCompressedBase<Block<const SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true> > >
575 {
576  typedef Block<const SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true> XprType;
577  typedef evaluator<SparseCompressedBase<XprType> > Base;
578  explicit unary_evaluator(const XprType &xpr) : Base(xpr) {}
579 };
580 
581 } // end namespace internal
582 
583 
584 } // end namespace Eigen
585 
586 #endif // EIGEN_SPARSE_BLOCK_H
Definition: LDLT.h:16
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:278
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:322
Definition: Constants.h:493
BlockImpl(const XprType &xpr, Index i)
Definition: SparseBlock.h:376
Definition: Eigen_Colamd.h:54
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:337
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:104
BlockImpl(const XprType &xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition: SparseBlock.h:386