Eigen  3.3.90 (mercurial changeset b7a5520e097f)
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 
82  EIGEN_DEVICE_FUNC
83  void resize(Index nrows, Index ncols) {
84  m_matrix.resize(ncols,nrows);
85  }
86 
87  protected:
88  typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
89 };
90 
91 namespace internal {
92 
93 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
94 struct TransposeImpl_base
95 {
96  typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
97 };
98 
99 template<typename MatrixType>
100 struct TransposeImpl_base<MatrixType, false>
101 {
102  typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
103 };
104 
105 } // end namespace internal
106 
107 // Generic API dispatcher
108 template<typename XprType, typename StorageKind>
109 class TransposeImpl
110  : public internal::generic_xpr_base<Transpose<XprType> >::type
111 {
112 public:
113  typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
114 };
115 
116 template<typename MatrixType> class TransposeImpl<MatrixType,Dense>
117  : public internal::TransposeImpl_base<MatrixType>::type
118 {
119  public:
120 
121  typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
122  using Base::coeffRef;
123  EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
124  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
125 
126  EIGEN_DEVICE_FUNC inline Index innerStride() const { return derived().nestedExpression().innerStride(); }
127  EIGEN_DEVICE_FUNC inline Index outerStride() const { return derived().nestedExpression().outerStride(); }
128 
129  typedef typename internal::conditional<
130  internal::is_lvalue<MatrixType>::value,
131  Scalar,
132  const Scalar
133  >::type ScalarWithConstIfNotLvalue;
134 
135  EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
136  EIGEN_DEVICE_FUNC inline const Scalar* data() const { return derived().nestedExpression().data(); }
137 
138  // FIXME: shall we keep the const version of coeffRef?
139  EIGEN_DEVICE_FUNC
140  inline const Scalar& coeffRef(Index rowId, Index colId) const
141  {
142  return derived().nestedExpression().coeffRef(colId, rowId);
143  }
144 
145  EIGEN_DEVICE_FUNC
146  inline const Scalar& coeffRef(Index index) const
147  {
148  return derived().nestedExpression().coeffRef(index);
149  }
150 };
151 
171 template<typename Derived>
172 EIGEN_DEVICE_FUNC inline Transpose<Derived>
174 {
175  return TransposeReturnType(derived());
176 }
177 
183 template<typename Derived>
184 EIGEN_DEVICE_FUNC inline typename DenseBase<Derived>::ConstTransposeReturnType
186 {
187  return ConstTransposeReturnType(derived());
188 }
189 
209 template<typename Derived>
210 EIGEN_DEVICE_FUNC inline const typename MatrixBase<Derived>::AdjointReturnType
212 {
213  return AdjointReturnType(this->transpose());
214 }
215 
216 /***************************************************************************
217 * "in place" transpose implementation
218 ***************************************************************************/
219 
220 namespace internal {
221 
222 template<typename MatrixType,
223  bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic,
224  bool MatchPacketSize =
225  (int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size))
226  && (internal::evaluator<MatrixType>::Flags&PacketAccessBit) >
227 struct inplace_transpose_selector;
228 
229 template<typename MatrixType>
230 struct inplace_transpose_selector<MatrixType,true,false> { // square matrix
231  static void run(MatrixType& m) {
232  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
233  }
234 };
235 
236 // TODO: vectorized path is currently limited to LargestPacketSize x LargestPacketSize cases only.
237 template<typename MatrixType>
238 struct inplace_transpose_selector<MatrixType,true,true> { // PacketSize x PacketSize
239  static void run(MatrixType& m) {
240  typedef typename MatrixType::Scalar Scalar;
241  typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
242  const Index PacketSize = internal::packet_traits<Scalar>::size;
243  const Index Alignment = internal::evaluator<MatrixType>::Alignment;
244  PacketBlock<Packet> A;
245  for (Index i=0; i<PacketSize; ++i)
246  A.packet[i] = m.template packetByOuterInner<Alignment>(i,0);
247  internal::ptranspose(A);
248  for (Index i=0; i<PacketSize; ++i)
249  m.template writePacket<Alignment>(m.rowIndexByOuterInner(i,0), m.colIndexByOuterInner(i,0), A.packet[i]);
250  }
251 };
252 
253 template<typename MatrixType,bool MatchPacketSize>
254 struct inplace_transpose_selector<MatrixType,false,MatchPacketSize> { // non square matrix
255  static void run(MatrixType& m) {
256  if (m.rows()==m.cols())
257  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
258  else
259  m = m.transpose().eval();
260  }
261 };
262 
263 } // end namespace internal
264 
284 template<typename Derived>
285 EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::transposeInPlace()
286 {
287  eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
288  && "transposeInPlace() called on a non-square non-resizable matrix");
289  internal::inplace_transpose_selector<Derived>::run(derived());
290 }
291 
292 /***************************************************************************
293 * "in place" adjoint implementation
294 ***************************************************************************/
295 
315 template<typename Derived>
316 EIGEN_DEVICE_FUNC inline void MatrixBase<Derived>::adjointInPlace()
317 {
318  derived() = adjoint().eval();
319 }
320 
321 #ifndef EIGEN_NO_DEBUG
322 
323 // The following is to detect aliasing problems in most common cases.
324 
325 namespace internal {
326 
327 template<bool DestIsTransposed, typename OtherDerived>
328 struct check_transpose_aliasing_compile_time_selector
329 {
330  enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
331 };
332 
333 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
334 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
335 {
336  enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
337  || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
338  };
339 };
340 
341 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
342 struct check_transpose_aliasing_run_time_selector
343 {
344  static bool run(const Scalar* dest, const OtherDerived& src)
345  {
346  return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src));
347  }
348 };
349 
350 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
351 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
352 {
353  static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
354  {
355  return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.lhs())))
356  || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.rhs())));
357  }
358 };
359 
360 // the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
361 // is because when the condition controlling the assert is known at compile time, ICC emits a warning.
362 // This is actually a good warning: in expressions that don't have any transposing, the condition is
363 // known at compile time to be false, and using that, we can avoid generating the code of the assert again
364 // and again for all these expressions that don't need it.
365 
366 template<typename Derived, typename OtherDerived,
367  bool MightHaveTransposeAliasing
368  = check_transpose_aliasing_compile_time_selector
369  <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
370  >
371 struct checkTransposeAliasing_impl
372 {
373  static void run(const Derived& dst, const OtherDerived& other)
374  {
375  eigen_assert((!check_transpose_aliasing_run_time_selector
376  <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
377  ::run(extract_data(dst), other))
378  && "aliasing detected during transposition, use transposeInPlace() "
379  "or evaluate the rhs into a temporary using .eval()");
380 
381  }
382 };
383 
384 template<typename Derived, typename OtherDerived>
385 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
386 {
387  static void run(const Derived&, const OtherDerived&)
388  {
389  }
390 };
391 
392 template<typename Dst, typename Src>
393 void check_for_aliasing(const Dst &dst, const Src &src)
394 {
395  internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
396 }
397 
398 } // end namespace internal
399 
400 #endif // EIGEN_NO_DEBUG
401 
402 } // end namespace Eigen
403 
404 #endif // EIGEN_TRANSPOSE_H
internal::remove_reference< MatrixTypeNested >::type & nestedExpression()
Definition: Transpose.h:79
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
const _LhsNested & lhs() const
Definition: CwiseBinaryOp.h:132
const AdjointReturnType adjoint() const
Definition: Transpose.h:211
Expression of the transpose of a matrix.
Definition: Transpose.h:52
const unsigned int LvalueBit
Definition: Constants.h:143
Namespace containing all symbols from the Eigen library.
Definition: Core:340
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
const unsigned int RowMajorBit
Definition: Constants.h:65
TransposeReturnType transpose()
Definition: Transpose.h:173
const unsigned int PacketAccessBit
Definition: Constants.h:93
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: Meta.h:33
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: Transpose.h:74
void adjointInPlace()
Definition: Transpose.h:316
Definition: Eigen_Colamd.h:50
const int Dynamic
Definition: Constants.h:21
const _RhsNested & rhs() const
Definition: CwiseBinaryOp.h:135
void transposeInPlace()
Definition: Transpose.h:285
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48