SparseBlock.h
Go to the documentation of this file.
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 {
21 public:
23 protected:
24  enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
25 public:
27 
28  class InnerIterator: public XprType::InnerIterator
29  {
30  typedef typename BlockImpl::Index Index;
31  public:
32  inline InnerIterator(const BlockType& 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  EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
62  EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
63 
64  protected:
65 
66  typename XprType::Nested m_matrix;
69 };
70 
71 
72 /***************************************************************************
73 * specialisation for SparseMatrix
74 ***************************************************************************/
75 
76 template<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols>
77 class BlockImpl<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse>
78  : public SparseMatrixBase<Block<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
79 {
83 public:
86 protected:
87  enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
88 public:
89 
91  {
92  public:
93  inline InnerIterator(const BlockType& xpr, Index outer)
94  : SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
95  {}
96  inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
97  inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
98  protected:
99  Index m_outer;
100  };
101  class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator
102  {
103  public:
104  inline ReverseInnerIterator(const BlockType& xpr, Index outer)
105  : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
106  {}
107  inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
108  inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
109  protected:
111  };
112 
113  inline BlockImpl(const SparseMatrixType& xpr, int i)
114  : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
115  {}
116 
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)
119  {}
120 
121  template<typename OtherDerived>
122  inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other)
123  {
124  typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _NestedMatrixType;
125  _NestedMatrixType& matrix = const_cast<_NestedMatrixType&>(m_matrix);;
126  // This assignement is slow if this vector set is not empty
127  // and/or it is not at the end of the nonzeros of the underlying matrix.
128 
129  // 1 - eval to a temporary to avoid transposition and/or aliasing issues
131 
132  // 2 - let's check whether there is enough allocated memory
133  Index nnz = tmp.nonZeros();
134  Index start = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block
135  Index end = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]; // ending posiiton of the current block
136  Index block_size = end - start; // available room in the current block
137  Index tail_size = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
138 
139  Index free_size = m_matrix.isCompressed()
140  ? Index(matrix.data().allocatedSize()) + block_size
141  : block_size;
142 
143  if(nnz>free_size)
144  {
145  // realloc manually to reduce copies
146  typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz);
147 
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));
150 
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));
153 
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));
156 
157  newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
158 
159  matrix.data().swap(newdata);
160  }
161  else
162  {
163  // no need to realloc, simply copy the tail at its respective position and insert tmp
164  matrix.data().resize(start + nnz + tail_size);
165 
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));
168 
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));
171  }
172 
173  // update innerNonZeros
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();
177 
178  // update outer index pointers
179  Index p = start;
180  for(Index k=0; k<m_outerSize.value(); ++k)
181  {
182  matrix.outerIndexPtr()[m_outerStart+k] = p;
183  p += tmp.innerVector(k).nonZeros();
184  }
185  std::ptrdiff_t offset = nnz - block_size;
186  for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
187  {
188  matrix.outerIndexPtr()[k] += offset;
189  }
190 
191  return derived();
192  }
193 
194  inline BlockType& operator=(const BlockType& other)
195  {
196  return operator=<BlockType>(other);
197  }
198 
199  inline const Scalar* valuePtr() const
200  { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
201  inline Scalar* valuePtr()
202  { return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
203 
204  inline const Index* innerIndexPtr() const
205  { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
207  { return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
208 
209  inline const Index* outerIndexPtr() const
210  { return m_matrix.outerIndexPtr() + m_outerStart; }
212  { return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; }
213 
214  Index nonZeros() const
215  {
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)
220  return 0;
221  else
222  return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum();
223  }
224 
225  const Scalar& lastCoeff() const
226  {
228  eigen_assert(nonZeros()>0);
229  if(m_matrix.isCompressed())
230  return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
231  else
232  return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
233  }
234 
235  EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
236  EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
237 
238  protected:
239 
240  typename SparseMatrixType::Nested m_matrix;
243 
244 };
245 
246 //----------
247 
251 template<typename Derived>
253 { return InnerVectorReturnType(derived(), outer); }
254 
258 template<typename Derived>
260 { return ConstInnerVectorReturnType(derived(), outer); }
261 
265 template<typename Derived>
267 {
268  return Block<Derived,Dynamic,Dynamic,true>(derived(),
269  IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
270  IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
271 
272 }
273 
277 template<typename Derived>
279 {
281  IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
282  IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
283 
284 }
285 
289 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
290 class BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse>
291  : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,InnerPanel> >, internal::no_assignment_operator
292 {
295 public:
298 
299 
301  inline BlockImpl(const XprType& xpr, int i)
302  : m_matrix(xpr),
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())
307  {}
308 
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)
313  {}
314 
315  inline int rows() const { return m_blockRows.value(); }
316  inline int cols() const { return m_blockCols.value(); }
317 
318  inline Scalar& coeffRef(int row, int col)
319  {
320  return m_matrix.const_cast_derived()
321  .coeffRef(row + m_startRow.value(), col + m_startCol.value());
322  }
323 
324  inline const Scalar coeff(int row, int col) const
325  {
326  return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
327  }
328 
329  inline Scalar& coeffRef(int index)
330  {
331  return m_matrix.const_cast_derived()
332  .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
333  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
334  }
335 
336  inline const Scalar coeff(int index) const
337  {
338  return m_matrix
339  .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
340  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
341  }
342 
343  inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
344 
345  class InnerIterator : public _MatrixTypeNested::InnerIterator
346  {
347  typedef typename _MatrixTypeNested::InnerIterator Base;
350  public:
351 
353  : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
354  m_block(block),
355  m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value())
356  {
357  while( (Base::operator bool()) && (Base::index() < (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value())) )
358  Base::operator++();
359  }
360 
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()); }
363  inline Index row() const { return Base::row() - m_block.m_startRow.value(); }
364  inline Index col() const { return Base::col() - m_block.m_startCol.value(); }
365 
366  inline operator bool() const { return Base::operator bool() && Base::index() < m_end; }
367  };
368  class ReverseInnerIterator : public _MatrixTypeNested::ReverseInnerIterator
369  {
370  typedef typename _MatrixTypeNested::ReverseInnerIterator Base;
372  Index m_begin;
373  public:
374 
376  : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
377  m_block(block),
378  m_begin(IsRowMajor ? block.m_startCol.value() : block.m_startRow.value())
379  {
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())) )
381  Base::operator--();
382  }
383 
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(); }
388 
389  inline operator bool() const { return Base::operator bool() && Base::index() >= m_begin; }
390  };
391  protected:
392  friend class InnerIterator;
393  friend class ReverseInnerIterator;
394 
395  typename XprType::Nested m_matrix;
400 
401 };
402 
403 } // end namespace Eigen
404 
405 #endif // EIGEN_SPARSE_BLOCK_H
EIGEN_STRONG_INLINE ReverseInnerIterator(const BlockType &block, Index outer)
Definition: SparseBlock.h:375
Block< Derived, Dynamic, Dynamic, true > innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:266
#define EIGEN_STRONG_INLINE
A versatible sparse matrix representation.
Definition: SparseMatrix.h:85
#define inline
Definition: Types.h:109
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:104
EIGEN_STRONG_INLINE InnerIterator(const BlockType &block, Index outer)
Definition: SparseBlock.h:352
const internal::variable_if_dynamic< Index, RowsAtCompileTime > m_blockRows
Definition: SparseBlock.h:398
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
Block< Derived > block(Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition: BlockMethods.h:56
Index nonZeros() const
Definition: SparseMatrix.h:246
Block< XprType, BlockRows, BlockCols, true > BlockType
Definition: SparseBlock.h:20
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
Definition: SparseUtil.h:62
internal::remove_all< typename SparseMatrixType::Nested >::type _MatrixTypeNested
Definition: SparseBlock.h:81
Base class of any sparse matrices or sparse expressions.
internal::remove_all< typename XprType::Nested >::type _MatrixTypeNested
Definition: SparseBlock.h:19
internal::traits< Block< SparseMatrix< _Scalar, _Options, _Index >, BlockRows, BlockCols, true > >::Scalar Scalar
internal::remove_all< typename XprType::Nested >::type _MatrixTypeNested
Definition: SparseBlock.h:293
internal::traits< Derived >::Index Index
Definition: EigenBase.h:31
const internal::variable_if_dynamic< Index, ColsAtCompileTime > m_blockCols
Definition: SparseBlock.h:399
const internal::variable_if_dynamic< Index, OuterSize > m_outerSize
Definition: SparseBlock.h:68
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.
Definition: Core/Block.h:102
RowXpr row(Index i)
Definition: BlockMethods.h:725
BlockImpl(const SparseMatrixType &xpr, int startRow, int startCol, int blockRows, int blockCols)
Definition: SparseBlock.h:117
BlockImpl(const XprType &xpr, int startRow, int startCol, int blockRows, int blockCols)
Definition: SparseBlock.h:311
Block< XprType, BlockRows, BlockCols, InnerPanel > BlockType
Definition: SparseBlock.h:294
BlockImpl(const XprType &xpr, int startRow, int startCol, int blockRows, int blockCols)
Definition: SparseBlock.h:57
const internal::variable_if_dynamic< Index, XprType::RowsAtCompileTime==1?0:Dynamic > m_startRow
Definition: SparseBlock.h:396
ColXpr col(Index i)
Definition: BlockMethods.h:708
const internal::variable_if_dynamic< Index, XprType::ColsAtCompileTime==1?0:Dynamic > m_startCol
Definition: SparseBlock.h:397
#define eigen_assert(x)
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:126


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:07