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-2010 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 
30 namespace internal {
31 template<typename MatrixType>
32 struct traits<Transpose<MatrixType> > : traits<MatrixType>
33 {
34  typedef typename MatrixType::Scalar Scalar;
39  enum {
40  RowsAtCompileTime = MatrixType::ColsAtCompileTime,
41  ColsAtCompileTime = MatrixType::RowsAtCompileTime,
42  MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
43  MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
44  FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
45  Flags0 = MatrixTypeNestedPlain::Flags & ~(LvalueBit | NestByRefBit),
46  Flags1 = Flags0 | FlagsLvalueBit,
47  Flags = Flags1 ^ RowMajorBit,
48  CoeffReadCost = MatrixTypeNestedPlain::CoeffReadCost,
51  };
52 };
53 }
54 
55 template<typename MatrixType, typename StorageKind> class TransposeImpl;
56 
57 template<typename MatrixType> class Transpose
58  : public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
59 {
60  public:
61 
64 
65  inline Transpose(MatrixType& a_matrix) : m_matrix(a_matrix) {}
66 
68 
69  inline Index rows() const { return m_matrix.cols(); }
70  inline Index cols() const { return m_matrix.rows(); }
71 
74  nestedExpression() const { return m_matrix; }
75 
78  nestedExpression() { return m_matrix.const_cast_derived(); }
79 
80  protected:
81  typename MatrixType::Nested m_matrix;
82 };
83 
84 namespace internal {
85 
86 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
88 {
90 };
91 
92 template<typename MatrixType>
93 struct TransposeImpl_base<MatrixType, false>
94 {
96 };
97 
98 } // end namespace internal
99 
100 template<typename MatrixType> class TransposeImpl<MatrixType,Dense>
101  : public internal::TransposeImpl_base<MatrixType>::type
102 {
103  public:
104 
108 
109  inline Index innerStride() const { return derived().nestedExpression().innerStride(); }
110  inline Index outerStride() const { return derived().nestedExpression().outerStride(); }
111 
112  typedef typename internal::conditional<
114  Scalar,
115  const Scalar
117 
118  inline ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
119  inline const Scalar* data() const { return derived().nestedExpression().data(); }
120 
121  inline ScalarWithConstIfNotLvalue& coeffRef(Index rowId, Index colId)
122  {
123  EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
124  return derived().nestedExpression().const_cast_derived().coeffRef(colId, rowId);
125  }
126 
127  inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
128  {
129  EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
130  return derived().nestedExpression().const_cast_derived().coeffRef(index);
131  }
132 
133  inline const Scalar& coeffRef(Index rowId, Index colId) const
134  {
135  return derived().nestedExpression().coeffRef(colId, rowId);
136  }
137 
138  inline const Scalar& coeffRef(Index index) const
139  {
140  return derived().nestedExpression().coeffRef(index);
141  }
142 
143  inline CoeffReturnType coeff(Index rowId, Index colId) const
144  {
145  return derived().nestedExpression().coeff(colId, rowId);
146  }
147 
148  inline CoeffReturnType coeff(Index index) const
149  {
150  return derived().nestedExpression().coeff(index);
151  }
152 
153  template<int LoadMode>
154  inline const PacketScalar packet(Index rowId, Index colId) const
155  {
156  return derived().nestedExpression().template packet<LoadMode>(colId, rowId);
157  }
158 
159  template<int LoadMode>
160  inline void writePacket(Index rowId, Index colId, const PacketScalar& x)
161  {
162  derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(colId, rowId, x);
163  }
164 
165  template<int LoadMode>
166  inline const PacketScalar packet(Index index) const
167  {
168  return derived().nestedExpression().template packet<LoadMode>(index);
169  }
170 
171  template<int LoadMode>
172  inline void writePacket(Index index, const PacketScalar& x)
173  {
174  derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(index, x);
175  }
176 };
177 
197 template<typename Derived>
198 inline Transpose<Derived>
200 {
201  return derived();
202 }
203 
209 template<typename Derived>
212 {
213  return ConstTransposeReturnType(derived());
214 }
215 
235 template<typename Derived>
236 inline const typename MatrixBase<Derived>::AdjointReturnType
238 {
239  return this->transpose(); // in the complex case, the .conjugate() is be implicit here
240  // due to implicit conversion to return type
241 }
242 
243 /***************************************************************************
244 * "in place" transpose implementation
245 ***************************************************************************/
246 
247 namespace internal {
248 
249 template<typename MatrixType,
250  bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic>
252 
253 template<typename MatrixType>
255  static void run(MatrixType& m) {
256  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
257  }
258 };
259 
260 template<typename MatrixType>
261 struct inplace_transpose_selector<MatrixType,false> { // non square matrix
262  static void run(MatrixType& m) {
263  if (m.rows()==m.cols())
264  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
265  else
266  m = m.transpose().eval();
267  }
268 };
269 
270 } // end namespace internal
271 
290 template<typename Derived>
292 {
293  eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
294  && "transposeInPlace() called on a non-square non-resizable matrix");
296 }
297 
298 /***************************************************************************
299 * "in place" adjoint implementation
300 ***************************************************************************/
301 
320 template<typename Derived>
322 {
323  derived() = adjoint().eval();
324 }
325 
326 #ifndef EIGEN_NO_DEBUG
327 
328 // The following is to detect aliasing problems in most common cases.
329 
330 namespace internal {
331 
332 template<typename BinOp,typename NestedXpr,typename Rhs>
335 {
337  static inline const XprType extract(const XprType& x) { return x; }
338 };
339 
340 template<bool DestIsTransposed, typename OtherDerived>
342 {
343  enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
344 };
345 
346 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
347 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
348 {
349  enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
350  || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
351  };
352 };
353 
354 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
356 {
357  static bool run(const Scalar* dest, const OtherDerived& src)
358  {
359  return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src));
360  }
361 };
362 
363 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
364 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
365 {
366  static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
367  {
368  return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.lhs())))
369  || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.rhs())));
370  }
371 };
372 
373 // the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
374 // is because when the condition controlling the assert is known at compile time, ICC emits a warning.
375 // This is actually a good warning: in expressions that don't have any transposing, the condition is
376 // known at compile time to be false, and using that, we can avoid generating the code of the assert again
377 // and again for all these expressions that don't need it.
378 
379 template<typename Derived, typename OtherDerived,
380  bool MightHaveTransposeAliasing
382  <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
383  >
385 {
386  static void run(const Derived& dst, const OtherDerived& other)
387  {
389  <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
390  ::run(extract_data(dst), other))
391  && "aliasing detected during transposition, use transposeInPlace() "
392  "or evaluate the rhs into a temporary using .eval()");
393 
394  }
395 };
396 
397 template<typename Derived, typename OtherDerived>
398 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
399 {
400  static void run(const Derived&, const OtherDerived&)
401  {
402  }
403 };
404 
405 } // end namespace internal
406 
407 template<typename Derived>
408 template<typename OtherDerived>
409 void DenseBase<Derived>::checkTransposeAliasing(const OtherDerived& other) const
410 {
412 }
413 #endif
414 
415 } // end namespace Eigen
416 
417 #endif // EIGEN_TRANSPOSE_H
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
const _RhsNested & rhs() const
ScalarWithConstIfNotLvalue * data()
Definition: Transpose.h:118
const PacketScalar packet(Index rowId, Index colId) const
Definition: Transpose.h:154
static bool run(const Scalar *dest, const CwiseBinaryOp< BinOp, DerivedA, DerivedB > &src)
Definition: Transpose.h:366
internal::conditional< internal::is_lvalue< MatrixType >::value, Scalar, const Scalar >::type ScalarWithConstIfNotLvalue
Definition: Transpose.h:116
#define inline
Definition: Types.h:109
remove_reference< MatrixTypeNested >::type MatrixTypeNestedPlain
Definition: Transpose.h:36
Expression of the transpose of a matrix.
Definition: Transpose.h:57
traits< MatrixType >::XprKind XprKind
Definition: Transpose.h:38
const unsigned int LvalueBit
Index cols() const
Definition: Transpose.h:70
internal::TransposeImpl_base< MatrixType >::type Base
Definition: Transpose.h:105
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
const PacketScalar packet(Index index) const
Definition: Transpose.h:166
void checkTransposeAliasing(const OtherDerived &other) const
Definition: Transpose.h:409
void writePacket(Index rowId, Index colId, const PacketScalar &x)
Definition: Transpose.h:160
const unsigned int RowMajorBit
internal::remove_all< typename MatrixType::Nested >::type & nestedExpression()
Definition: Transpose.h:78
CoeffReturnType coeff(Index index) const
Definition: Transpose.h:148
Eigen::Transpose< Derived > transpose()
Definition: Transpose.h:199
#define EIGEN_STATIC_ASSERT_LVALUE(Derived)
Definition: StaticAssert.h:191
ScalarWithConstIfNotLvalue & coeffRef(Index index)
Definition: Transpose.h:127
dense_xpr_base< Transpose< MatrixType > >::type type
Definition: Transpose.h:89
dense_xpr_base< Transpose< MatrixType > >::type type
Definition: Transpose.h:95
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Generic expression where a coefficient-wise binary operator is applied to two expressions.
const Scalar & coeffRef(Index rowId, Index colId) const
Definition: Transpose.h:133
MatrixType::Nested m_matrix
Definition: Transpose.h:81
static void run(const Derived &, const OtherDerived &)
Definition: Transpose.h:400
traits< MatrixType >::StorageKind StorageKind
Definition: Transpose.h:37
void adjointInPlace()
Definition: Transpose.h:321
ScalarWithConstIfNotLvalue & coeffRef(Index rowId, Index colId)
Definition: Transpose.h:121
const _LhsNested & lhs() const
const unsigned int NestByRefBit
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
CoeffReturnType coeff(Index rowId, Index colId) const
Definition: Transpose.h:143
const T::Scalar * extract_data(const T &m)
Definition: BlasUtil.h:255
void transposeInPlace()
Definition: Transpose.h:291
TransposeImpl< MatrixType, typename internal::traits< MatrixType >::StorageKind >::Base Base
Definition: Transpose.h:62
#define eigen_assert(x)
const AdjointReturnType adjoint() const
Definition: Transpose.h:237
static void run(const Derived &dst, const OtherDerived &other)
Definition: Transpose.h:386
const internal::remove_all< typename MatrixType::Nested >::type & nestedExpression() const
Definition: Transpose.h:74
const Scalar & coeffRef(Index index) const
Definition: Transpose.h:138
nested< MatrixType >::type MatrixTypeNested
Definition: Transpose.h:35
void writePacket(Index index, const PacketScalar &x)
Definition: Transpose.h:172
static bool run(const Scalar *dest, const OtherDerived &src)
Definition: Transpose.h:357


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:13