G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BlockTranspose.h
Go to the documentation of this file.
1 
15 namespace Eigen {
16 
25 namespace internal {
26 template<typename MatrixType,int NumBlocks>
27 struct traits<BlockTranspose<MatrixType,NumBlocks> >
28  : traits<MatrixType>
29 {
30  typedef typename MatrixType::Scalar Scalar;
31  typedef typename traits<MatrixType>::StorageKind StorageKind;
32  typedef typename traits<MatrixType>::XprKind XprKind;
33  typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
34  typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
35  enum {
36  RowsAtCompileTime = NumBlocks==Dynamic || int(MatrixType::RowsAtCompileTime)==Dynamic
37  ? Dynamic
38  : MatrixType::ColsAtCompileTime / NumBlocks,
39  ColsAtCompileTime = NumBlocks==Dynamic || int(MatrixType::ColsAtCompileTime)==Dynamic
40  ? Dynamic
41  : NumBlocks * MatrixType::RowsAtCompileTime,
42  //FIXME we don't propagate the max sizes !!!
43  MaxRowsAtCompileTime = RowsAtCompileTime,
44  MaxColsAtCompileTime = ColsAtCompileTime,
45  IsRowMajor = MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1 ? 1
46  : MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1 ? 0
47  : (MatrixType::Flags & RowMajorBit) ? 1 : 0,
48 
49  // FIXME enable DirectAccess with negative strides?
50  Flags = IsRowMajor ? RowMajorBit : 0
51  };
52 };
53 }
54 
55 template<typename MatrixType,int NumBlocks> class BlockTranspose
56  : public internal::dense_xpr_base< BlockTranspose<MatrixType,NumBlocks> >::type
57 {
58  typedef typename internal::traits<BlockTranspose>::MatrixTypeNested MatrixTypeNested;
59  typedef typename internal::traits<BlockTranspose>::_MatrixTypeNested _MatrixTypeNested;
60  public:
61 
62  typedef typename internal::dense_xpr_base<BlockTranspose>::type Base;
63  EIGEN_DENSE_PUBLIC_INTERFACE(BlockTranspose)
64  typedef typename internal::remove_all<MatrixType>::type NestedExpression;
65 
66  template<typename OriginalMatrixType>
67  EIGEN_DEVICE_FUNC
68  inline explicit BlockTranspose(const OriginalMatrixType& a_matrix)
69  : m_matrix(a_matrix), m_numBlocks(NumBlocks)
70  {
71  EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
72  THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
73  eigen_assert(NumBlocks!=Dynamic);
74  }
75 
76  template<typename OriginalMatrixType>
77  EIGEN_DEVICE_FUNC
78  inline BlockTranspose(const OriginalMatrixType& a_matrix, Index numBlocks)
79  : m_matrix(a_matrix), m_numBlocks(numBlocks)
80  {
81  EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
82  THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
83  }
84 
85  EIGEN_DEVICE_FUNC
86  inline Index rows() const { return m_matrix.cols() / m_numBlocks.value(); }
87  // return m_rows;
88 
89  EIGEN_DEVICE_FUNC
90  inline Index cols() const { return m_matrix.rows() * m_numBlocks.value(); }
91 
92  EIGEN_DEVICE_FUNC
93  inline Index numBlocks() const { return m_numBlocks.value(); }
94  // return m_matrix.cols() / rows();
95 
96  /*
97  inline Scalar coeff(Index rowId, Index colId) const
98  {
99  const Index r = colId % m_matrix.rows(); // actual row
100  const Index c = (colId/m_matrix.rows()) * rows() + rowId; // actual col
101  return m_matrix.coeff(r, c);
102  }
103  template<int LoadMode>
104  inline PacketScalar packet(Index rowId, Index colId) const
105  {
106  const Index r = colId % m_matrix.rows(); // actual row
107  const Index c = (colId/m_matrix.rows()) * rows() + rowId; // actual col
108  return m_matrix.template packet<LoadMode>(r, c);
109  }
110  */
111 
112  EIGEN_DEVICE_FUNC
113  const _MatrixTypeNested& nestedExpression() const
114  {
115  return m_matrix;
116  }
117 
118  protected:
119  MatrixTypeNested m_matrix;
120  const internal::variable_if_dynamic<Index, NumBlocks> m_numBlocks;
121 };
122 
123 namespace internal {
124 template<typename ArgType, int NumBlocks>
125 struct unary_evaluator<BlockTranspose<ArgType, NumBlocks> >
126  : evaluator_base<BlockTranspose<ArgType, NumBlocks> >
127 {
128  typedef BlockTranspose<ArgType, NumBlocks> XprType;
129  typedef typename XprType::CoeffReturnType CoeffReturnType;
130  enum {
131  Factor = (NumBlocks==Dynamic) ? Dynamic : NumBlocks*NumBlocks
132  };
133  typedef typename internal::nested_eval<ArgType,Factor>::type ArgTypeNested;
134  typedef typename internal::remove_all<ArgTypeNested>::type ArgTypeNestedCleaned;
135 
136  enum {
137  CoeffReadCost = evaluator<ArgTypeNestedCleaned>::CoeffReadCost,
138  LinearAccessMask = XprType::IsVectorAtCompileTime ? LinearAccessBit : 0,
139  Flags = (evaluator<ArgTypeNestedCleaned>::Flags & (HereditaryBits|LinearAccessMask) & ~RowMajorBit) | (traits<XprType>::Flags & RowMajorBit),
140 
141  Alignment = evaluator<ArgTypeNestedCleaned>::Alignment
142  };
143 
144  EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& xpr)
145  : m_arg(xpr.nestedExpression()),
146  m_argImpl(m_arg),
147  m_rows(xpr.nestedExpression().rows()),
148  m_cols(xpr.nestedExpression().cols()),
149  m_str(m_cols.value() / xpr.numBlocks())
150  {}
151 
152  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
153  CoeffReturnType coeff(Index rowId, Index colId) const
154  {
155  const Index r = colId % m_rows.value(); // actual row
156  const Index c = (colId/m_rows.value()) * m_str + rowId; // actual col
157  return m_argImpl.coeff(r, c);
158  }
159 
160  template<int LoadMode, typename PacketType>
161  EIGEN_STRONG_INLINE
162  PacketType packet(Index rowId, Index colId) const
163  {
164  const Index r = colId % m_rows.value(); // actual row
165  const Index c = (colId/m_rows.value()) * m_str + rowId; // actual col
166  return m_argImpl.template packet<LoadMode>(r, c);
167  }
168 
169 protected:
170  ArgTypeNested m_arg;
171  evaluator<ArgTypeNestedCleaned> m_argImpl;
172  const variable_if_dynamic<Index, ArgType::RowsAtCompileTime> m_rows;
173  const variable_if_dynamic<Index, ArgType::ColsAtCompileTime> m_cols;
174  const Index m_str ;
175 };
176 
177 } // namespace internal
178 
182 template<typename Derived>
183 const typename MatrixBase<Derived>::BlockTransposeReturnType
184 MatrixBase<Derived>::blockTranspose(Index numBlocks) const
185 {
186  return BlockTranspose<Derived,Dynamic>(derived(),numBlocks);
187 }
188 
189 
190 }
Expression of block-wise transposition of a tiled matrix.
Definition: BlockTranspose.h:55