Transpose.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) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2009-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
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_TRANSPOSE_H
12 #define EIGEN_TRANSPOSE_H
13 
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template<typename MatrixType>
20 struct traits<Transpose<MatrixType> > : public traits<MatrixType>
21 {
22  typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
23  typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNestedPlain;
24  enum {
25  RowsAtCompileTime = MatrixType::ColsAtCompileTime,
26  ColsAtCompileTime = MatrixType::RowsAtCompileTime,
27  MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
28  MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
29  FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
30  Flags0 = traits<MatrixTypeNestedPlain>::Flags & ~(LvalueBit | NestByRefBit),
31  Flags1 = Flags0 | FlagsLvalueBit,
32  Flags = Flags1 ^ RowMajorBit,
33  InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
34  OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
35  };
36 };
37 }
38 
39 template<typename MatrixType, typename StorageKind> class TransposeImpl;
40 
54 template<typename MatrixType> class Transpose
55  : public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
56 {
57  public:
58 
59  typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
60 
64 
66  explicit EIGEN_STRONG_INLINE Transpose(MatrixType& matrix) : m_matrix(matrix) {}
67 
69 
70  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
71  Index rows() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
72  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
73  Index cols() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
74 
76  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
78  nestedExpression() const { return m_matrix; }
79 
81  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
82  std::remove_reference_t<MatrixTypeNested>&
83  nestedExpression() { return m_matrix; }
84 
86  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
87  void resize(Index nrows, Index ncols) {
88  m_matrix.resize(ncols,nrows);
89  }
90 
91  protected:
92  typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
93 };
94 
95 namespace internal {
96 
97 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
98 struct TransposeImpl_base
99 {
100  typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
101 };
102 
103 template<typename MatrixType>
104 struct TransposeImpl_base<MatrixType, false>
105 {
106  typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
107 };
108 
109 } // end namespace internal
110 
111 // Generic API dispatcher
112 template<typename XprType, typename StorageKind>
114  : public internal::generic_xpr_base<Transpose<XprType> >::type
115 {
116 public:
117  typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
118 };
119 
120 template<typename MatrixType> class TransposeImpl<MatrixType,Dense>
121  : public internal::TransposeImpl_base<MatrixType>::type
122 {
123  public:
124 
125  typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
126  using Base::coeffRef;
129 
130  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
131  Index innerStride() const { return derived().nestedExpression().innerStride(); }
132  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
133  Index outerStride() const { return derived().nestedExpression().outerStride(); }
134 
135  typedef std::conditional_t<
136  internal::is_lvalue<MatrixType>::value,
137  Scalar,
138  const Scalar
140 
141  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
142  ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
143  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
144  const Scalar* data() const { return derived().nestedExpression().data(); }
145 
146  // FIXME: shall we keep the const version of coeffRef?
147  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
148  const Scalar& coeffRef(Index rowId, Index colId) const
149  {
150  return derived().nestedExpression().coeffRef(colId, rowId);
151  }
152 
153  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
154  const Scalar& coeffRef(Index index) const
155  {
156  return derived().nestedExpression().coeffRef(index);
157  }
158  protected:
160 };
161 
181 template<typename Derived>
182 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
185 {
186  return TransposeReturnType(derived());
187 }
188 
194 template<typename Derived>
195 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
198 {
199  return ConstTransposeReturnType(derived());
200 }
201 
221 template<typename Derived>
224 {
225  return AdjointReturnType(this->transpose());
226 }
227 
228 
232 namespace internal {
233 
234 template<typename MatrixType,
235  bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic,
236  bool MatchPacketSize =
237  (int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size))
238  && (internal::evaluator<MatrixType>::Flags&PacketAccessBit) >
239 struct inplace_transpose_selector;
240 
241 template<typename MatrixType>
242 struct inplace_transpose_selector<MatrixType,true,false> { // square matrix
243  static void run(MatrixType& m) {
244  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose().template triangularView<StrictlyUpper>());
245  }
246 };
247 
248 template<typename MatrixType>
249 struct inplace_transpose_selector<MatrixType,true,true> { // PacketSize x PacketSize
250  static void run(MatrixType& m) {
251  typedef typename MatrixType::Scalar Scalar;
252  typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
253  const Index PacketSize = internal::packet_traits<Scalar>::size;
254  const Index Alignment = internal::evaluator<MatrixType>::Alignment;
255  PacketBlock<Packet> A;
256  for (Index i=0; i<PacketSize; ++i)
257  A.packet[i] = m.template packetByOuterInner<Alignment>(i,0);
259  for (Index i=0; i<PacketSize; ++i)
260  m.template writePacket<Alignment>(m.rowIndexByOuterInner(i,0), m.colIndexByOuterInner(i,0), A.packet[i]);
261  }
262 };
263 
264 
265 template <typename MatrixType, Index Alignment>
267  typedef typename MatrixType::Scalar Scalar;
268  typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
269  const Index PacketSize = internal::packet_traits<Scalar>::size;
270  eigen_assert(m.rows() == m.cols());
271  int row_start = 0;
272  for (; row_start + PacketSize <= m.rows(); row_start += PacketSize) {
273  for (int col_start = row_start; col_start + PacketSize <= m.cols(); col_start += PacketSize) {
274  PacketBlock<Packet> A;
275  if (row_start == col_start) {
276  for (Index i=0; i<PacketSize; ++i)
277  A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i,col_start);
279  for (Index i=0; i<PacketSize; ++i)
280  m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start), m.colIndexByOuterInner(row_start + i,col_start), A.packet[i]);
281  } else {
282  PacketBlock<Packet> B;
283  for (Index i=0; i<PacketSize; ++i) {
284  A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i,col_start);
285  B.packet[i] = m.template packetByOuterInner<Alignment>(col_start + i, row_start);
286  }
289  for (Index i=0; i<PacketSize; ++i) {
290  m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start), m.colIndexByOuterInner(row_start + i,col_start), B.packet[i]);
291  m.template writePacket<Alignment>(m.rowIndexByOuterInner(col_start + i, row_start), m.colIndexByOuterInner(col_start + i,row_start), A.packet[i]);
292  }
293  }
294  }
295  }
296  for (Index row = row_start; row < m.rows(); ++row) {
297  m.matrix().row(row).head(row).swap(
298  m.matrix().col(row).head(row).transpose());
299  }
300 }
301 
302 template<typename MatrixType,bool MatchPacketSize>
303 struct inplace_transpose_selector<MatrixType,false,MatchPacketSize> { // non square or dynamic matrix
304  static void run(MatrixType& m) {
305  typedef typename MatrixType::Scalar Scalar;
306  if (m.rows() == m.cols()) {
307  const Index PacketSize = internal::packet_traits<Scalar>::size;
308  if (!NumTraits<Scalar>::IsComplex && m.rows() >= PacketSize) {
309  if ((m.rows() % PacketSize) == 0)
310  BlockedInPlaceTranspose<MatrixType,internal::evaluator<MatrixType>::Alignment>(m);
311  else
312  BlockedInPlaceTranspose<MatrixType,Unaligned>(m);
313  }
314  else {
315  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose().template triangularView<StrictlyUpper>());
316  }
317  } else {
318  m = m.transpose().eval();
319  }
320  }
321 };
322 
323 
324 } // end namespace internal
325 
345 template<typename Derived>
347 {
348  eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
349  && "transposeInPlace() called on a non-square non-resizable matrix");
350  internal::inplace_transpose_selector<Derived>::run(derived());
351 }
352 
353 
376 template<typename Derived>
378 {
379  derived() = adjoint().eval();
380 }
381 
382 #ifndef EIGEN_NO_DEBUG
383 
384 // The following is to detect aliasing problems in most common cases.
385 
386 namespace internal {
387 
388 template<bool DestIsTransposed, typename OtherDerived>
389 struct check_transpose_aliasing_compile_time_selector
390 {
391  enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
392 };
393 
394 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
395 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
396 {
397  enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
398  || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
399  };
400 };
401 
402 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
403 struct check_transpose_aliasing_run_time_selector
404 {
405  EIGEN_DEVICE_FUNC static bool run(const Scalar* dest, const OtherDerived& src)
406  {
407  return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src));
408  }
409 };
410 
411 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
412 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
413 {
414  EIGEN_DEVICE_FUNC static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
415  {
416  return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.lhs())))
417  || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.rhs())));
418  }
419 };
420 
421 // the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
422 // is because when the condition controlling the assert is known at compile time, ICC emits a warning.
423 // This is actually a good warning: in expressions that don't have any transposing, the condition is
424 // known at compile time to be false, and using that, we can avoid generating the code of the assert again
425 // and again for all these expressions that don't need it.
426 
427 template<typename Derived, typename OtherDerived,
428  bool MightHaveTransposeAliasing
429  = check_transpose_aliasing_compile_time_selector
430  <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
431  >
432 struct checkTransposeAliasing_impl
433 {
434  EIGEN_DEVICE_FUNC static void run(const Derived& dst, const OtherDerived& other)
435  {
436  eigen_assert((!check_transpose_aliasing_run_time_selector
437  <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
438  ::run(extract_data(dst), other))
439  && "aliasing detected during transposition, use transposeInPlace() "
440  "or evaluate the rhs into a temporary using .eval()");
441 
442  }
443 };
444 
445 template<typename Derived, typename OtherDerived>
446 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
447 {
448  EIGEN_DEVICE_FUNC static void run(const Derived&, const OtherDerived&)
449  {
450  }
451 };
452 
453 template<typename Dst, typename Src>
454 EIGEN_DEVICE_FUNC inline void check_for_aliasing(const Dst &dst, const Src &src)
455 {
456  if((!Dst::IsVectorAtCompileTime) && dst.rows()>1 && dst.cols()>1)
457  internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
458 }
459 
460 } // end namespace internal
461 
462 #endif // EIGEN_NO_DEBUG
463 
464 } // end namespace Eigen
465 
466 #endif // EIGEN_TRANSPOSE_H
Matrix3f m
RowXpr row(Index i)
This is the const version of row(). *‍/.
MatrixXcf A
MatrixXf B
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(Derived)
Definition: Macros.h:1133
#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
Matrix< float, 1, Dynamic > MatrixType
TransposeReturnType transpose()
Definition: Transpose.h:184
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
Transpose< Derived > TransposeReturnType
Definition: DenseBase.h:315
void transposeInPlace()
Definition: Transpose.h:346
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
void adjointInPlace()
Definition: Transpose.h:377
const AdjointReturnType adjoint() const
Definition: Transpose.h:223
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
ScalarWithConstIfNotLvalue * data()
Definition: Transpose.h:142
const Scalar & coeffRef(Index rowId, Index colId) const
Definition: Transpose.h:148
const Scalar & coeffRef(Index index) const
Definition: Transpose.h:154
std::conditional_t< internal::is_lvalue< MatrixType >::value, Scalar, const Scalar > ScalarWithConstIfNotLvalue
Definition: Transpose.h:139
internal::TransposeImpl_base< MatrixType >::type Base
Definition: Transpose.h:125
internal::generic_xpr_base< Transpose< XprType > >::type Base
Definition: Transpose.h:117
Expression of the transpose of a matrix.
Definition: Transpose.h:56
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Transpose.h:73
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition: Transpose.h:78
TransposeImpl< MatrixType, typename internal::traits< MatrixType >::StorageKind >::Base Base
Definition: Transpose.h:61
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Transpose.h:71
std::remove_reference_t< MatrixTypeNested > & nestedExpression()
Definition: Transpose.h:83
void resize(Index nrows, Index ncols)
Definition: Transpose.h:87
internal::ref_selector< MatrixType >::non_const_type m_matrix
Definition: Transpose.h:92
internal::remove_all_t< MatrixType > NestedExpression
Definition: Transpose.h:63
internal::ref_selector< MatrixType >::non_const_type MatrixTypeNested
Definition: Transpose.h:59
const unsigned int PacketAccessBit
Definition: Constants.h:96
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
void check_for_aliasing(const Dst &dst, const Src &src)
Definition: Transpose.h:454
void ptranspose(PacketBlock< Packet2cf, 2 > &kernel)
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
EIGEN_ALWAYS_INLINE const T::Scalar * extract_data(const T &m)
Definition: BlasUtil.h:593
void BlockedInPlaceTranspose(MatrixType &m)
Definition: Transpose.h:266
: InteropHeaders
Definition: Core:139
const unsigned int NestByRefBit
Definition: Constants.h:171
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
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231