Block.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 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_BLOCK_H
12 #define EIGEN_BLOCK_H
13 
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
20 struct traits<Block<XprType, BlockRows, BlockCols, InnerPanel> > : traits<XprType>
21 {
22  typedef typename traits<XprType>::Scalar Scalar;
23  typedef typename traits<XprType>::StorageKind StorageKind;
24  typedef typename traits<XprType>::XprKind XprKind;
25  typedef typename ref_selector<XprType>::type XprTypeNested;
26  typedef std::remove_reference_t<XprTypeNested> XprTypeNested_;
27  enum{
28  MatrixRows = traits<XprType>::RowsAtCompileTime,
29  MatrixCols = traits<XprType>::ColsAtCompileTime,
30  RowsAtCompileTime = MatrixRows == 0 ? 0 : BlockRows,
31  ColsAtCompileTime = MatrixCols == 0 ? 0 : BlockCols,
32  MaxRowsAtCompileTime = BlockRows==0 ? 0
33  : RowsAtCompileTime != Dynamic ? int(RowsAtCompileTime)
34  : int(traits<XprType>::MaxRowsAtCompileTime),
35  MaxColsAtCompileTime = BlockCols==0 ? 0
36  : ColsAtCompileTime != Dynamic ? int(ColsAtCompileTime)
37  : int(traits<XprType>::MaxColsAtCompileTime),
38 
39  XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0,
40  IsRowMajor = (MaxRowsAtCompileTime==1&&MaxColsAtCompileTime!=1) ? 1
41  : (MaxColsAtCompileTime==1&&MaxRowsAtCompileTime!=1) ? 0
42  : XprTypeIsRowMajor,
43  HasSameStorageOrderAsXprType = (IsRowMajor == XprTypeIsRowMajor),
44  InnerSize = IsRowMajor ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
45  InnerStrideAtCompileTime = HasSameStorageOrderAsXprType
46  ? int(inner_stride_at_compile_time<XprType>::ret)
47  : int(outer_stride_at_compile_time<XprType>::ret),
48  OuterStrideAtCompileTime = HasSameStorageOrderAsXprType
49  ? int(outer_stride_at_compile_time<XprType>::ret)
50  : int(inner_stride_at_compile_time<XprType>::ret),
51 
52  // FIXME, this traits is rather specialized for dense object and it needs to be cleaned further
53  FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0,
54  FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0,
55  Flags = (traits<XprType>::Flags & (DirectAccessBit | (InnerPanel?CompressedAccessBit:0))) | FlagsLvalueBit | FlagsRowMajorBit,
56  // FIXME DirectAccessBit should not be handled by expressions
57  //
58  // Alignment is needed by MapBase's assertions
59  // We can sefely set it to false here. Internal alignment errors will be detected by an eigen_internal_assert in the respective evaluator
60  Alignment = 0
61  };
62 };
63 
64 template<typename XprType, int BlockRows=Dynamic, int BlockCols=Dynamic, bool InnerPanel = false,
65  bool HasDirectAccess = internal::has_direct_access<XprType>::ret> class BlockImpl_dense;
66 
67 } // end namespace internal
68 
69 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, typename StorageKind> class BlockImpl;
70 
105 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel> class Block
106  : public BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, typename internal::traits<XprType>::StorageKind>
107 {
109  public:
110  //typedef typename Impl::Base Base;
111  typedef Impl Base;
114 
116 
119  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
120  Block(XprType& xpr, Index i) : Impl(xpr,i)
121  {
122  eigen_assert( (i>=0) && (
123  ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows())
124  ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols())));
125  }
126 
129  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
130  Block(XprType& xpr, Index startRow, Index startCol)
131  : Impl(xpr, startRow, startCol)
132  {
133  EIGEN_STATIC_ASSERT(RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
134  eigen_assert(startRow >= 0 && BlockRows >= 0 && startRow + BlockRows <= xpr.rows()
135  && startCol >= 0 && BlockCols >= 0 && startCol + BlockCols <= xpr.cols());
136  }
137 
140  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
141  Block(XprType& xpr,
142  Index startRow, Index startCol,
143  Index blockRows, Index blockCols)
144  : Impl(xpr, startRow, startCol, blockRows, blockCols)
145  {
146  eigen_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows)
147  && (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols));
148  eigen_assert(startRow >= 0 && blockRows >= 0 && startRow <= xpr.rows() - blockRows
149  && startCol >= 0 && blockCols >= 0 && startCol <= xpr.cols() - blockCols);
150  }
151 };
152 
153 // The generic default implementation for dense block simplu forward to the internal::BlockImpl_dense
154 // that must be specialized for direct and non-direct access...
155 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
156 class BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, Dense>
157  : public internal::BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel>
158 {
159  typedef internal::BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel> Impl;
160  typedef typename XprType::StorageIndex StorageIndex;
161  public:
162  typedef Impl Base;
164  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index i) : Impl(xpr,i) {}
165  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index startRow, Index startCol) : Impl(xpr, startRow, startCol) {}
167  EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
168  : Impl(xpr, startRow, startCol, blockRows, blockCols) {}
169 };
170 
171 namespace internal {
172 
174 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool HasDirectAccess> class BlockImpl_dense
175  : public internal::dense_xpr_base<Block<XprType, BlockRows, BlockCols, InnerPanel> >::type
176 {
177  typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType;
178  typedef typename internal::ref_selector<XprType>::non_const_type XprTypeNested;
179  public:
180 
181  typedef typename internal::dense_xpr_base<BlockType>::type Base;
183  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl_dense)
184 
185  // class InnerIterator; // FIXME apparently never used
186 
187 
190  inline BlockImpl_dense(XprType& xpr, Index i)
191  : m_xpr(xpr),
192  // It is a row if and only if BlockRows==1 and BlockCols==XprType::ColsAtCompileTime,
193  // and it is a column if and only if BlockRows==XprType::RowsAtCompileTime and BlockCols==1,
194  // all other cases are invalid.
195  // The case a 1x1 matrix seems ambiguous, but the result is the same anyway.
196  m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
197  m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
198  m_blockRows(BlockRows==1 ? 1 : xpr.rows()),
199  m_blockCols(BlockCols==1 ? 1 : xpr.cols())
200  {}
201 
205  inline BlockImpl_dense(XprType& xpr, Index startRow, Index startCol)
206  : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol),
207  m_blockRows(BlockRows), m_blockCols(BlockCols)
208  {}
209 
213  inline BlockImpl_dense(XprType& xpr,
214  Index startRow, Index startCol,
215  Index blockRows, Index blockCols)
216  : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol),
217  m_blockRows(blockRows), m_blockCols(blockCols)
218  {}
219 
220  EIGEN_DEVICE_FUNC inline Index rows() const { return m_blockRows.value(); }
221  EIGEN_DEVICE_FUNC inline Index cols() const { return m_blockCols.value(); }
222 
224  inline Scalar& coeffRef(Index rowId, Index colId)
225  {
227  return m_xpr.coeffRef(rowId + m_startRow.value(), colId + m_startCol.value());
228  }
229 
231  inline const Scalar& coeffRef(Index rowId, Index colId) const
232  {
233  return m_xpr.derived().coeffRef(rowId + m_startRow.value(), colId + m_startCol.value());
234  }
235 
237  EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index rowId, Index colId) const
238  {
239  return m_xpr.coeff(rowId + m_startRow.value(), colId + m_startCol.value());
240  }
241 
243  inline Scalar& coeffRef(Index index)
244  {
246  return m_xpr.coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
247  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
248  }
249 
251  inline const Scalar& coeffRef(Index index) const
252  {
253  return m_xpr.coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
254  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
255  }
256 
258  inline const CoeffReturnType coeff(Index index) const
259  {
260  return m_xpr.coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
261  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
262  }
263 
264  template<int LoadMode>
265  EIGEN_DEVICE_FUNC inline PacketScalar packet(Index rowId, Index colId) const
266  {
267  return m_xpr.template packet<Unaligned>(rowId + m_startRow.value(), colId + m_startCol.value());
268  }
269 
270  template<int LoadMode>
271  EIGEN_DEVICE_FUNC inline void writePacket(Index rowId, Index colId, const PacketScalar& val)
272  {
273  m_xpr.template writePacket<Unaligned>(rowId + m_startRow.value(), colId + m_startCol.value(), val);
274  }
275 
276  template<int LoadMode>
277  EIGEN_DEVICE_FUNC inline PacketScalar packet(Index index) const
278  {
279  return m_xpr.template packet<Unaligned>
280  (m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
281  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
282  }
283 
284  template<int LoadMode>
285  EIGEN_DEVICE_FUNC inline void writePacket(Index index, const PacketScalar& val)
286  {
287  m_xpr.template writePacket<Unaligned>
288  (m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
289  m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0), val);
290  }
291 
292  #ifdef EIGEN_PARSED_BY_DOXYGEN
294  EIGEN_DEVICE_FUNC inline const Scalar* data() const;
295  EIGEN_DEVICE_FUNC inline Index innerStride() const;
296  EIGEN_DEVICE_FUNC inline Index outerStride() const;
297  #endif
298 
299  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
300  const internal::remove_all_t<XprTypeNested>& nestedExpression() const
301  {
302  return m_xpr;
303  }
304 
305  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
306  XprType& nestedExpression() { return m_xpr; }
307 
308  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
309  StorageIndex startRow() const EIGEN_NOEXCEPT
310  {
311  return m_startRow.value();
312  }
313 
314  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
315  StorageIndex startCol() const EIGEN_NOEXCEPT
316  {
317  return m_startCol.value();
318  }
319 
320  protected:
321 
322  XprTypeNested m_xpr;
323  const internal::variable_if_dynamic<StorageIndex, (XprType::RowsAtCompileTime == 1 && BlockRows==1) ? 0 : Dynamic> m_startRow;
324  const internal::variable_if_dynamic<StorageIndex, (XprType::ColsAtCompileTime == 1 && BlockCols==1) ? 0 : Dynamic> m_startCol;
325  const internal::variable_if_dynamic<StorageIndex, RowsAtCompileTime> m_blockRows;
326  const internal::variable_if_dynamic<StorageIndex, ColsAtCompileTime> m_blockCols;
327 };
328 
330 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
331 class BlockImpl_dense<XprType,BlockRows,BlockCols, InnerPanel,true>
332  : public MapBase<Block<XprType, BlockRows, BlockCols, InnerPanel> >
333 {
334  typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType;
335  typedef typename internal::ref_selector<XprType>::non_const_type XprTypeNested;
336  enum {
337  XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0
338  };
339 
343  template <typename Scalar>
345  static Scalar* add_to_nullable_pointer(Scalar* base, Index offset)
346  {
347  return base != nullptr ? base+offset : nullptr;
348  }
349 
350  public:
351 
352  typedef MapBase<BlockType> Base;
354  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl_dense)
355 
356 
358  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
359  BlockImpl_dense(XprType& xpr, Index i)
360  : Base((BlockRows == 0 || BlockCols == 0) ? nullptr : add_to_nullable_pointer(xpr.data(),
361  i * ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && (!XprTypeIsRowMajor))
362  || ((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && ( XprTypeIsRowMajor)) ? xpr.innerStride() : xpr.outerStride())),
363  BlockRows==1 ? 1 : xpr.rows(),
364  BlockCols==1 ? 1 : xpr.cols()),
365  m_xpr(xpr),
366  m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
367  m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0)
368  {
369  init();
370  }
371 
374  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
375  BlockImpl_dense(XprType& xpr, Index startRow, Index startCol)
376  : Base((BlockRows == 0 || BlockCols == 0) ? nullptr : add_to_nullable_pointer(xpr.data(),
377  xpr.innerStride()*(XprTypeIsRowMajor?startCol:startRow) + xpr.outerStride()*(XprTypeIsRowMajor?startRow:startCol))),
378  m_xpr(xpr), m_startRow(startRow), m_startCol(startCol)
379  {
380  init();
381  }
382 
385  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
386  BlockImpl_dense(XprType& xpr,
387  Index startRow, Index startCol,
388  Index blockRows, Index blockCols)
389  : Base((blockRows == 0 || blockCols == 0) ? nullptr : add_to_nullable_pointer(xpr.data(),
390  xpr.innerStride()*(XprTypeIsRowMajor?startCol:startRow) + xpr.outerStride()*(XprTypeIsRowMajor?startRow:startCol)),
391  blockRows, blockCols),
392  m_xpr(xpr), m_startRow(startRow), m_startCol(startCol)
393  {
394  init();
395  }
396 
397  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
398  const internal::remove_all_t<XprTypeNested>& nestedExpression() const EIGEN_NOEXCEPT
399  {
400  return m_xpr;
401  }
402 
403  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
404  XprType& nestedExpression() { return m_xpr; }
405 
407  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
408  Index innerStride() const EIGEN_NOEXCEPT
409  {
410  return internal::traits<BlockType>::HasSameStorageOrderAsXprType
411  ? m_xpr.innerStride()
412  : m_xpr.outerStride();
413  }
414 
416  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
417  Index outerStride() const EIGEN_NOEXCEPT
418  {
419  return internal::traits<BlockType>::HasSameStorageOrderAsXprType
420  ? m_xpr.outerStride()
421  : m_xpr.innerStride();
422  }
423 
424  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
425  StorageIndex startRow() const EIGEN_NOEXCEPT { return m_startRow.value(); }
426 
427  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
428  StorageIndex startCol() const EIGEN_NOEXCEPT { return m_startCol.value(); }
429 
430  #ifndef __SUNPRO_CC
431  // FIXME sunstudio is not friendly with the above friend...
432  // META-FIXME there is no 'friend' keyword around here. Is this obsolete?
433  protected:
434  #endif
435 
436  #ifndef EIGEN_PARSED_BY_DOXYGEN
438  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
439  BlockImpl_dense(XprType& xpr, const Scalar* data, Index blockRows, Index blockCols)
440  : Base(data, blockRows, blockCols), m_xpr(xpr)
441  {
442  init();
443  }
444  #endif
445 
446  protected:
447  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
448  void init()
449  {
450  m_outerStride = internal::traits<BlockType>::HasSameStorageOrderAsXprType
451  ? m_xpr.outerStride()
452  : m_xpr.innerStride();
453  }
454 
455  XprTypeNested m_xpr;
456  const internal::variable_if_dynamic<StorageIndex, (XprType::RowsAtCompileTime == 1 && BlockRows==1) ? 0 : Dynamic> m_startRow;
457  const internal::variable_if_dynamic<StorageIndex, (XprType::ColsAtCompileTime == 1 && BlockCols==1) ? 0 : Dynamic> m_startCol;
458  Index m_outerStride;
459 };
460 
461 } // end namespace internal
462 
463 } // end namespace Eigen
464 
465 #endif // EIGEN_BLOCK_H
#define EIGEN_ALWAYS_INLINE
Definition: Macros.h:836
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1122
int data[]
#define EIGEN_STATIC_ASSERT_LVALUE(Derived)
Definition: StaticAssert.h:96
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
internal::BlockImpl_dense< XprType, BlockRows, BlockCols, InnerPanel > Impl
Definition: Block.h:159
BlockImpl(XprType &xpr, Index startRow, Index startCol)
Definition: Block.h:165
BlockImpl(XprType &xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition: Block.h:167
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:107
internal::remove_all_t< XprType > NestedExpression
Definition: Block.h:115
Block(XprType &xpr, Index startRow, Index startCol)
Definition: Block.h:130
Block(XprType &xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition: Block.h:141
Impl Base
Definition: Block.h:111
BlockImpl< XprType, BlockRows, BlockCols, InnerPanel, typename internal::traits< XprType >::StorageKind > Impl
Definition: Block.h:108
const unsigned int DirectAccessBit
Definition: Constants.h:157
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
const unsigned int CompressedAccessBit
Definition: Constants.h:193
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24