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 namespace Eigen {
15 
16 namespace internal {
17 template<typename MatrixType>
18 struct traits<Transpose<MatrixType> > : public traits<MatrixType>
19 {
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,
29  Flags1 = Flags0 | FlagsLvalueBit,
30  Flags = Flags1 ^ RowMajorBit,
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 
58 
61  typedef typename internal::remove_all<MatrixType>::type NestedExpression;
62 
65 
67 
69  Index rows() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
71  Index cols() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
72 
76  nestedExpression() const { return m_matrix; }
77 
81  nestedExpression() { return m_matrix; }
82 
85  void resize(Index nrows, Index ncols) {
86  m_matrix.resize(ncols,nrows);
87  }
88 
89  protected:
91 };
92 
93 namespace internal {
94 
97 {
99 };
100 
101 template<typename MatrixType>
103 {
105 };
106 
107 } // end namespace internal
108 
109 // Generic API dispatcher
110 template<typename XprType, typename StorageKind>
111 class TransposeImpl
112  : public internal::generic_xpr_base<Transpose<XprType> >::type
113 {
114 public:
116 };
117 
118 template<typename MatrixType> class TransposeImpl<MatrixType,Dense>
119  : public internal::TransposeImpl_base<MatrixType>::type
120 {
121  public:
122 
124  using Base::coeffRef;
127 
129  Index innerStride() const { return derived().nestedExpression().innerStride(); }
131  Index outerStride() const { return derived().nestedExpression().outerStride(); }
132 
133  typedef typename internal::conditional<
135  Scalar,
136  const Scalar
138 
140  ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
142  const Scalar* data() const { return derived().nestedExpression().data(); }
143 
144  // FIXME: shall we keep the const version of coeffRef?
146  const Scalar& coeffRef(Index rowId, Index colId) const
147  {
148  return derived().nestedExpression().coeffRef(colId, rowId);
149  }
150 
152  const Scalar& coeffRef(Index index) const
153  {
154  return derived().nestedExpression().coeffRef(index);
155  }
156  protected:
158 };
159 
179 template<typename Derived>
181 Transpose<Derived>
183 {
184  return TransposeReturnType(derived());
185 }
186 
192 template<typename Derived>
196 {
197  return ConstTransposeReturnType(derived());
198 }
199 
219 template<typename Derived>
222 {
223  return AdjointReturnType(this->transpose());
224 }
225 
226 /***************************************************************************
227 * "in place" transpose implementation
228 ***************************************************************************/
229 
230 namespace internal {
231 
232 template<typename MatrixType,
233  bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic,
234  bool MatchPacketSize =
235  (int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size))
237 struct inplace_transpose_selector;
238 
239 template<typename MatrixType>
240 struct inplace_transpose_selector<MatrixType,true,false> { // square matrix
241  static void run(MatrixType& m) {
242  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose().template triangularView<StrictlyUpper>());
243  }
244 };
245 
246 template<typename MatrixType>
247 struct inplace_transpose_selector<MatrixType,true,true> { // PacketSize x PacketSize
248  static void run(MatrixType& m) {
249  typedef typename MatrixType::Scalar Scalar;
251  const Index PacketSize = internal::packet_traits<Scalar>::size;
254  for (Index i=0; i<PacketSize; ++i)
255  A.packet[i] = m.template packetByOuterInner<Alignment>(i,0);
257  for (Index i=0; i<PacketSize; ++i)
258  m.template writePacket<Alignment>(m.rowIndexByOuterInner(i,0), m.colIndexByOuterInner(i,0), A.packet[i]);
259  }
260 };
261 
262 
263 template <typename MatrixType, Index Alignment>
265  typedef typename MatrixType::Scalar Scalar;
267  const Index PacketSize = internal::packet_traits<Scalar>::size;
268  eigen_assert(m.rows() == m.cols());
269  int row_start = 0;
270  for (; row_start + PacketSize <= m.rows(); row_start += PacketSize) {
271  for (int col_start = row_start; col_start + PacketSize <= m.cols(); col_start += PacketSize) {
273  if (row_start == col_start) {
274  for (Index i=0; i<PacketSize; ++i)
275  A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i,col_start);
277  for (Index i=0; i<PacketSize; ++i)
278  m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start), m.colIndexByOuterInner(row_start + i,col_start), A.packet[i]);
279  } else {
280  PacketBlock<Packet> B;
281  for (Index i=0; i<PacketSize; ++i) {
282  A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i,col_start);
283  B.packet[i] = m.template packetByOuterInner<Alignment>(col_start + i, row_start);
284  }
287  for (Index i=0; i<PacketSize; ++i) {
288  m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start), m.colIndexByOuterInner(row_start + i,col_start), B.packet[i]);
289  m.template writePacket<Alignment>(m.rowIndexByOuterInner(col_start + i, row_start), m.colIndexByOuterInner(col_start + i,row_start), A.packet[i]);
290  }
291  }
292  }
293  }
294  for (Index row = row_start; row < m.rows(); ++row) {
295  m.matrix().row(row).head(row).swap(
296  m.matrix().col(row).head(row).transpose());
297  }
298 }
299 
300 template<typename MatrixType,bool MatchPacketSize>
301 struct inplace_transpose_selector<MatrixType,false,MatchPacketSize> { // non square or dynamic matrix
302  static void run(MatrixType& m) {
303  typedef typename MatrixType::Scalar Scalar;
304  if (m.rows() == m.cols()) {
305  const Index PacketSize = internal::packet_traits<Scalar>::size;
306  if (!NumTraits<Scalar>::IsComplex && m.rows() >= PacketSize) {
307  if ((m.rows() % PacketSize) == 0)
308  BlockedInPlaceTranspose<MatrixType,internal::evaluator<MatrixType>::Alignment>(m);
309  else
310  BlockedInPlaceTranspose<MatrixType,Unaligned>(m);
311  }
312  else {
313  m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose().template triangularView<StrictlyUpper>());
314  }
315  } else {
316  m = m.transpose().eval();
317  }
318  }
319 };
320 
321 
322 } // end namespace internal
323 
343 template<typename Derived>
345 {
346  eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
347  && "transposeInPlace() called on a non-square non-resizable matrix");
349 }
350 
351 /***************************************************************************
352 * "in place" adjoint implementation
353 ***************************************************************************/
354 
374 template<typename Derived>
376 {
377  derived() = adjoint().eval();
378 }
379 
380 #ifndef EIGEN_NO_DEBUG
381 
382 // The following is to detect aliasing problems in most common cases.
383 
384 namespace internal {
385 
386 template<bool DestIsTransposed, typename OtherDerived>
388 {
389  enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
390 };
391 
392 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
393 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
394 {
395  enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
396  || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
397  };
398 };
399 
400 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
402 {
403  static bool run(const Scalar* dest, const OtherDerived& src)
404  {
405  return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src));
406  }
407 };
408 
409 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
410 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
411 {
412  static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
413  {
414  return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.lhs())))
415  || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.rhs())));
416  }
417 };
418 
419 // the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
420 // is because when the condition controlling the assert is known at compile time, ICC emits a warning.
421 // This is actually a good warning: in expressions that don't have any transposing, the condition is
422 // known at compile time to be false, and using that, we can avoid generating the code of the assert again
423 // and again for all these expressions that don't need it.
424 
425 template<typename Derived, typename OtherDerived,
426  bool MightHaveTransposeAliasing
427  = check_transpose_aliasing_compile_time_selector
429  >
431 {
432  static void run(const Derived& dst, const OtherDerived& other)
433  {
436  ::run(extract_data(dst), other))
437  && "aliasing detected during transposition, use transposeInPlace() "
438  "or evaluate the rhs into a temporary using .eval()");
439 
440  }
441 };
442 
443 template<typename Derived, typename OtherDerived>
444 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
445 {
446  static void run(const Derived&, const OtherDerived&)
447  {
448  }
449 };
450 
451 template<typename Dst, typename Src>
452 void check_for_aliasing(const Dst &dst, const Src &src)
453 {
454  if((!Dst::IsVectorAtCompileTime) && dst.rows()>1 && dst.cols()>1)
456 }
457 
458 } // end namespace internal
459 
460 #endif // EIGEN_NO_DEBUG
461 
462 } // end namespace Eigen
463 
464 #endif // EIGEN_TRANSPOSE_H
Eigen::internal::blas_traits::IsTransposed
@ IsTransposed
Definition: BlasUtil.h:409
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::internal::TransposeImpl_base
Definition: Transpose.h:96
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::TransposeImpl< MatrixType, Dense >::coeffRef
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId) const
Definition: Transpose.h:146
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::TransposeImpl
Definition: Transpose.h:37
Eigen::CwiseBinaryOp
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR
#define EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(Derived)
Definition: Macros.h:1247
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Packet
internal::packet_traits< Scalar >::type Packet
Definition: benchmark-blocking-sizes.cpp:54
Eigen::internal::PacketBlock
Definition: GenericPacketMath.h:1014
Eigen::internal::dense_xpr_base
Definition: XprHelper.h:483
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::internal::check_for_aliasing
void check_for_aliasing(const Dst &dst, const Src &src)
Definition: Transpose.h:452
Eigen::internal::is_lvalue
Definition: XprHelper.h:659
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
ret
DenseIndex ret
Definition: level1_cplx_impl.h:44
Eigen::internal::packet_traits
Definition: GenericPacketMath.h:106
B
Definition: test_numpy_dtypes.cpp:299
EIGEN_CONSTEXPR
#define EIGEN_CONSTEXPR
Definition: Macros.h:787
Eigen::internal::BlockedInPlaceTranspose
void BlockedInPlaceTranspose(MatrixType &m)
Definition: Transpose.h:266
type
Definition: pytypes.h:1525
EIGEN_DENSE_PUBLIC_INTERFACE
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1283
Eigen::Transpose::m_matrix
internal::ref_selector< MatrixType >::non_const_type m_matrix
Definition: Transpose.h:90
Eigen::TransposeImpl< MatrixType, Dense >::innerStride
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index innerStride() const
Definition: Transpose.h:129
Eigen::internal::check_transpose_aliasing_run_time_selector
Definition: Transpose.h:401
Eigen::Transpose::nestedExpression
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::remove_reference< MatrixTypeNested >::type & nestedExpression()
Definition: Transpose.h:81
Eigen::Transpose::cols
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Transpose.h:71
Eigen::internal::check_transpose_aliasing_run_time_selector< Scalar, DestIsTransposed, CwiseBinaryOp< BinOp, DerivedA, DerivedB > >::run
static bool run(const Scalar *dest, const CwiseBinaryOp< BinOp, DerivedA, DerivedB > &src)
Definition: Transpose.h:412
Eigen::internal::traits< Transpose< MatrixType > >::MatrixTypeNestedPlain
remove_reference< MatrixTypeNested >::type MatrixTypeNestedPlain
Definition: Transpose.h:21
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
Eigen::Transpose
Expression of the transpose of a matrix.
Definition: Transpose.h:52
Eigen::PacketAccessBit
const unsigned int PacketAccessBit
Definition: Constants.h:94
Eigen::internal::check_transpose_aliasing_compile_time_selector::ret
@ ret
Definition: Transpose.h:389
Eigen::Transpose::Base
TransposeImpl< MatrixType, typename internal::traits< MatrixType >::StorageKind >::Base Base
Definition: Transpose.h:59
Eigen::internal::check_transpose_aliasing_run_time_selector::run
static bool run(const Scalar *dest, const OtherDerived &src)
Definition: Transpose.h:403
Eigen::TransposeImpl< MatrixType, Dense >::ScalarWithConstIfNotLvalue
internal::conditional< internal::is_lvalue< MatrixType >::value, Scalar, const Scalar >::type ScalarWithConstIfNotLvalue
Definition: Transpose.h:137
Eigen::internal::true_type
Definition: Meta.h:96
EIGEN_GENERIC_PUBLIC_INTERFACE
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1264
A
Definition: test_numpy_dtypes.cpp:298
Eigen::TransposeImpl::Base
internal::generic_xpr_base< Transpose< XprType > >::type Base
Definition: Transpose.h:115
Eigen::LvalueBit
const unsigned int LvalueBit
Definition: Constants.h:144
Eigen::internal::TransposeImpl_base< MatrixType, false >::type
dense_xpr_base< Transpose< MatrixType > >::type type
Definition: Transpose.h:104
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::TransposeImpl< MatrixType, Dense >::Base
internal::TransposeImpl_base< MatrixType >::type Base
Definition: Transpose.h:123
Eigen::internal::ptranspose
EIGEN_STRONG_INLINE void ptranspose(PacketBlock< Packet2cf, 2 > &kernel)
Definition: AltiVec/Complex.h:224
Eigen::internal::generic_xpr_base
Definition: XprHelper.h:501
Eigen::internal::traits< Transpose< MatrixType > >::MatrixTypeNested
ref_selector< MatrixType >::type MatrixTypeNested
Definition: Transpose.h:20
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::internal::outer_stride_at_compile_time
Definition: DenseCoeffsBase.h:670
Eigen::internal::check_transpose_aliasing_compile_time_selector
Definition: Transpose.h:387
gtsam.examples.DogLegOptimizerExample.run
def run(args)
Definition: DogLegOptimizerExample.py:21
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::Triplet< double >
Eigen::TransposeImpl< MatrixType, Dense >::data
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar * data() const
Definition: Transpose.h:142
Eigen::internal::evaluator
Definition: CoreEvaluators.h:90
Eigen::Transpose::resize
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index nrows, Index ncols)
Definition: Transpose.h:85
Eigen::internal::ref_selector
Definition: XprHelper.h:416
Eigen::CwiseBinaryOp::rhs
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE _RhsNested & rhs() const
Definition: CwiseBinaryOp.h:135
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
Eigen::MatrixBase::adjoint
const EIGEN_DEVICE_FUNC AdjointReturnType adjoint() const
Definition: Transpose.h:221
Eigen::internal::checkTransposeAliasing_impl
Definition: Transpose.h:430
Eigen::MatrixBase::AdjointReturnType
internal::conditional< NumTraits< Scalar >::IsComplex, CwiseUnaryOp< internal::scalar_conjugate_op< Scalar >, ConstTransposeReturnType >, ConstTransposeReturnType >::type AdjointReturnType
Definition: MatrixBase.h:113
Eigen::internal::inner_stride_at_compile_time
Definition: DenseCoeffsBase.h:658
Eigen::internal::checkTransposeAliasing_impl< Derived, OtherDerived, false >::run
static void run(const Derived &, const OtherDerived &)
Definition: Transpose.h:446
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::DenseBase::ConstTransposeReturnType
internal::add_const< Transpose< const Derived > >::type ConstTransposeReturnType
Definition: DenseBase.h:327
Eigen::internal::extract_data
EIGEN_DEVICE_FUNC const EIGEN_ALWAYS_INLINE T::Scalar * extract_data(const T &m)
Definition: BlasUtil.h:533
row
m row(1)
Eigen::Transpose::rows
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Transpose.h:69
Eigen::internal::conditional
Definition: Meta.h:109
Eigen::Transpose::MatrixTypeNested
internal::ref_selector< MatrixType >::non_const_type MatrixTypeNested
Definition: Transpose.h:57
Eigen::TransposeImpl< MatrixType, Dense >::outerStride
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outerStride() const
Definition: Transpose.h:131
Eigen::MatrixBase::adjointInPlace
EIGEN_DEVICE_FUNC void adjointInPlace()
Definition: Transpose.h:375
Eigen::DenseBase::transpose
EIGEN_DEVICE_FUNC TransposeReturnType transpose()
Definition: Transpose.h:182
Eigen::internal::blas_traits
Definition: BlasUtil.h:402
EIGEN_NOEXCEPT
#define EIGEN_NOEXCEPT
Definition: Macros.h:1418
Eigen::Transpose::nestedExpression
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: Transpose.h:76
internal
Definition: BandTriangularSolver.h:13
Eigen::internal::inplace_transpose_selector
Definition: Transpose.h:239
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::TransposeImpl< MatrixType, Dense >::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ScalarWithConstIfNotLvalue * data()
Definition: Transpose.h:140
Eigen::NestByRefBit
const unsigned int NestByRefBit
Definition: Constants.h:169
Eigen::internal::TransposeImpl_base::type
dense_xpr_base< Transpose< MatrixType > >::type type
Definition: Transpose.h:98
Eigen::DenseBase::transposeInPlace
EIGEN_DEVICE_FUNC void transposeInPlace()
Definition: Transpose.h:344
EIGEN_INHERIT_ASSIGNMENT_OPERATORS
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1231
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
adjoint
void adjoint(const MatrixType &m)
Definition: adjoint.cpp:67
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::CwiseBinaryOp::lhs
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE _LhsNested & lhs() const
Definition: CwiseBinaryOp.h:132
Eigen::internal::checkTransposeAliasing_impl::run
static void run(const Derived &dst, const OtherDerived &other)
Definition: Transpose.h:432
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::TransposeImpl< MatrixType, Dense >::coeffRef
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar & coeffRef(Index index) const
Definition: Transpose.h:152
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::Dense
Definition: Constants.h:507


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:18:03