Eigen  3.2.93
Transpose.h
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 namespace Eigen {
15 
16 namespace internal {
17 template<typename MatrixType>
18 struct traits<Transpose<MatrixType> > : public traits<MatrixType>
19 {
20  typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
21  typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedPlain;
22  enum {
23  RowsAtCompileTime = MatrixType::ColsAtCompileTime,
24  ColsAtCompileTime = MatrixType::RowsAtCompileTime,
25  MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
26  MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
27  FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
28  Flags0 = traits<MatrixTypeNestedPlain>::Flags & ~(LvalueBit | NestByRefBit),
29  Flags1 = Flags0 | FlagsLvalueBit,
30  Flags = Flags1 ^ RowMajorBit,
31  InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
32  OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
33  };
34 };
35 }
36 
37 template<typename MatrixType, typename StorageKind> class TransposeImpl;
38 
52 template<typename MatrixType> class Transpose
53  : public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
54 {
55  public:
56 
57  typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
58 
59  typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
60  EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose)
61  typedef typename internal::remove_all<MatrixType>::type NestedExpression;
62 
63  EIGEN_DEVICE_FUNC
64  explicit inline Transpose(MatrixType& matrix) : m_matrix(matrix) {}
65 
66  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
67 
68  EIGEN_DEVICE_FUNC inline Index rows() const { return m_matrix.cols(); }
69  EIGEN_DEVICE_FUNC inline Index cols() const { return m_matrix.rows(); }
70 
72  EIGEN_DEVICE_FUNC
73  const typename internal::remove_all<MatrixTypeNested>::type&
74  nestedExpression() const { return m_matrix; }
75 
77  EIGEN_DEVICE_FUNC
78  typename internal::remove_reference<MatrixTypeNested>::type&
79  nestedExpression() { return m_matrix; }
80 
81  protected:
82  typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
83 };
84 
85 namespace internal {
86 
87 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
88 struct TransposeImpl_base
89 {
90  typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
91 };
92 
93 template<typename MatrixType>
94 struct TransposeImpl_base<MatrixType, false>
95 {
96  typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
97 };
98 
99 } // end namespace internal
100 
101 // Generic API dispatcher
102 template<typename XprType, typename StorageKind>
103 class TransposeImpl
104  : public internal::generic_xpr_base<Transpose<XprType> >::type
105 {
106 public:
107  typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
108 };
109 
110 template<typename MatrixType> class TransposeImpl<MatrixType,Dense>
111  : public internal::TransposeImpl_base<MatrixType>::type
112 {
113  public:
114 
115  typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
116  using Base::coeffRef;
117  EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
118  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
119 
120  EIGEN_DEVICE_FUNC inline Index innerStride() const { return derived().nestedExpression().innerStride(); }
121  EIGEN_DEVICE_FUNC inline Index outerStride() const { return derived().nestedExpression().outerStride(); }
122 
123  typedef typename internal::conditional<
124  internal::is_lvalue<MatrixType>::value,
125  Scalar,
126  const Scalar
127  >::type ScalarWithConstIfNotLvalue;
128 
129  EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
130  EIGEN_DEVICE_FUNC inline const Scalar* data() const { return derived().nestedExpression().data(); }
131 
132  // FIXME: shall we keep the const version of coeffRef?
133  EIGEN_DEVICE_FUNC
134  inline const Scalar& coeffRef(Index rowId, Index colId) const
135  {
136  return derived().nestedExpression().coeffRef(colId, rowId);
137  }
138 
139  EIGEN_DEVICE_FUNC
140  inline const Scalar& coeffRef(Index index) const
141  {
142  return derived().nestedExpression().coeffRef(index);
143  }
144 };
145 
165 template<typename Derived>
166 inline Transpose<Derived>
168 {
169  return TransposeReturnType(derived());
170 }
171 
177 template<typename Derived>
180 {
181  return ConstTransposeReturnType(derived());
182 }
183 
203 template<typename Derived>
204 inline const typename MatrixBase<Derived>::AdjointReturnType
206 {
207  return AdjointReturnType(this->transpose());
208 }
209 
210 /***************************************************************************
211 * "in place" transpose implementation
212 ***************************************************************************/
213 
214 namespace internal {
215 
216 template<typename MatrixType,
217  bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic,
218  bool MatchPacketSize =
219  (int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size))
220  && (internal::evaluator<MatrixType>::Flags&PacketAccessBit) >
221 struct inplace_transpose_selector;
222 
223 template<typename MatrixType>
224 struct inplace_transpose_selector<MatrixType,true,false> { // square matrix
225  static void run(MatrixType& m) {
226  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
227  }
228 };
229 
230 // TODO: vectorized path is currently limited to LargestPacketSize x LargestPacketSize cases only.
231 template<typename MatrixType>
232 struct inplace_transpose_selector<MatrixType,true,true> { // PacketSize x PacketSize
233  static void run(MatrixType& m) {
234  typedef typename MatrixType::Scalar Scalar;
235  typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
236  const Index PacketSize = internal::packet_traits<Scalar>::size;
237  const Index Alignment = internal::evaluator<MatrixType>::Alignment;
238  PacketBlock<Packet> A;
239  for (Index i=0; i<PacketSize; ++i)
240  A.packet[i] = m.template packetByOuterInner<Alignment>(i,0);
241  internal::ptranspose(A);
242  for (Index i=0; i<PacketSize; ++i)
243  m.template writePacket<Alignment>(m.rowIndexByOuterInner(i,0), m.colIndexByOuterInner(i,0), A.packet[i]);
244  }
245 };
246 
247 template<typename MatrixType,bool MatchPacketSize>
248 struct inplace_transpose_selector<MatrixType,false,MatchPacketSize> { // non square matrix
249  static void run(MatrixType& m) {
250  if (m.rows()==m.cols())
251  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
252  else
253  m = m.transpose().eval();
254  }
255 };
256 
257 } // end namespace internal
258 
278 template<typename Derived>
280 {
281  eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
282  && "transposeInPlace() called on a non-square non-resizable matrix");
283  internal::inplace_transpose_selector<Derived>::run(derived());
284 }
285 
286 /***************************************************************************
287 * "in place" adjoint implementation
288 ***************************************************************************/
289 
309 template<typename Derived>
311 {
312  derived() = adjoint().eval();
313 }
314 
315 #ifndef EIGEN_NO_DEBUG
316 
317 // The following is to detect aliasing problems in most common cases.
318 
319 namespace internal {
320 
321 template<bool DestIsTransposed, typename OtherDerived>
322 struct check_transpose_aliasing_compile_time_selector
323 {
324  enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
325 };
326 
327 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
328 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
329 {
330  enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
331  || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
332  };
333 };
334 
335 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
336 struct check_transpose_aliasing_run_time_selector
337 {
338  static bool run(const Scalar* dest, const OtherDerived& src)
339  {
340  return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src));
341  }
342 };
343 
344 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
345 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
346 {
347  static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
348  {
349  return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.lhs())))
350  || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.rhs())));
351  }
352 };
353 
354 // the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
355 // is because when the condition controlling the assert is known at compile time, ICC emits a warning.
356 // This is actually a good warning: in expressions that don't have any transposing, the condition is
357 // known at compile time to be false, and using that, we can avoid generating the code of the assert again
358 // and again for all these expressions that don't need it.
359 
360 template<typename Derived, typename OtherDerived,
361  bool MightHaveTransposeAliasing
362  = check_transpose_aliasing_compile_time_selector
363  <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
364  >
365 struct checkTransposeAliasing_impl
366 {
367  static void run(const Derived& dst, const OtherDerived& other)
368  {
369  eigen_assert((!check_transpose_aliasing_run_time_selector
370  <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
371  ::run(extract_data(dst), other))
372  && "aliasing detected during transposition, use transposeInPlace() "
373  "or evaluate the rhs into a temporary using .eval()");
374 
375  }
376 };
377 
378 template<typename Derived, typename OtherDerived>
379 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
380 {
381  static void run(const Derived&, const OtherDerived&)
382  {
383  }
384 };
385 
386 template<typename Dst, typename Src>
387 void check_for_aliasing(const Dst &dst, const Src &src)
388 {
389  internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
390 }
391 
392 } // end namespace internal
393 
394 #endif // EIGEN_NO_DEBUG
395 
396 } // end namespace Eigen
397 
398 #endif // EIGEN_TRANSPOSE_H
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: Transpose.h:74
internal::remove_reference< MatrixTypeNested >::type & nestedExpression()
Definition: Transpose.h:79
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
Expression of the transpose of a matrix.
Definition: Transpose.h:52
const unsigned int LvalueBit
Definition: Constants.h:139
Namespace containing all symbols from the Eigen library.
Definition: Core:271
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
const unsigned int RowMajorBit
Definition: Constants.h:61
TransposeReturnType transpose()
Definition: Transpose.h:167
const unsigned int PacketAccessBit
Definition: Constants.h:89
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
void adjointInPlace()
Definition: Transpose.h:310
const _LhsNested & lhs() const
Definition: CwiseBinaryOp.h:131
Definition: Eigen_Colamd.h:50
const int Dynamic
Definition: Constants.h:21
void transposeInPlace()
Definition: Transpose.h:279
const _RhsNested & rhs() const
Definition: CwiseBinaryOp.h:134
const AdjointReturnType adjoint() const
Definition: Transpose.h:205
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48