Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
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#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19template<typename MatrixType>
20struct traits<Transpose<MatrixType> > : public traits<MatrixType>
21{
22 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
23 typedef typename remove_reference<MatrixTypeNested>::type 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
39template<typename MatrixType, typename StorageKind> class TransposeImpl;
40
54template<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
61 typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
62 EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose)
63 typedef typename internal::remove_all<MatrixType>::type NestedExpression;
64
65 EIGEN_DEVICE_FUNC
66 explicit EIGEN_STRONG_INLINE Transpose(MatrixType& matrix) : m_matrix(matrix) {}
67
68 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
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
77 const typename internal::remove_all<MatrixTypeNested>::type&
78 nestedExpression() const { return m_matrix; }
79
81 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
82 typename internal::remove_reference<MatrixTypeNested>::type&
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
95namespace internal {
96
97template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
98struct TransposeImpl_base
99{
100 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
101};
102
103template<typename MatrixType>
104struct 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
112template<typename XprType, typename StorageKind>
113class TransposeImpl
114 : public internal::generic_xpr_base<Transpose<XprType> >::type
115{
116public:
117 typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
118};
119
120template<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;
127 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
128 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
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 typename internal::conditional<
136 internal::is_lvalue<MatrixType>::value,
137 Scalar,
138 const Scalar
139 >::type ScalarWithConstIfNotLvalue;
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:
159 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(TransposeImpl)
160};
161
181template<typename Derived>
182EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
183Transpose<Derived>
185{
186 return TransposeReturnType(derived());
187}
188
194template<typename Derived>
195EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
198{
199 return ConstTransposeReturnType(derived());
200}
201
221template<typename Derived>
222EIGEN_DEVICE_FUNC inline const typename MatrixBase<Derived>::AdjointReturnType
224{
225 return AdjointReturnType(this->transpose());
226}
227
228/***************************************************************************
229* "in place" transpose implementation
230***************************************************************************/
231
232namespace internal {
233
234template<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) >
239struct inplace_transpose_selector;
240
241template<typename MatrixType>
242struct 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
248template<typename MatrixType>
249struct 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);
258 internal::ptranspose(A);
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
265template <typename MatrixType, Index Alignment>
266void BlockedInPlaceTranspose(MatrixType& m) {
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);
278 internal::ptranspose(A);
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 }
287 internal::ptranspose(A);
288 internal::ptranspose(B);
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
302template<typename MatrixType,bool MatchPacketSize>
303struct 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>());
317 } else {
318 m = m.transpose().eval();
320 }
322
323
324} // end namespace internal
325
345template<typename Derived>
346EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::transposeInPlace()
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/***************************************************************************
354* "in place" adjoint implementation
355***************************************************************************/
356
376template<typename Derived>
377EIGEN_DEVICE_FUNC inline void MatrixBase<Derived>::adjointInPlace()
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
386namespace internal {
387
388template<bool DestIsTransposed, typename OtherDerived>
389struct check_transpose_aliasing_compile_time_selector
390{
391 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
392};
393
394template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
395struct 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
402template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
403struct check_transpose_aliasing_run_time_selector
404{
405 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
411template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
412struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
413{
414 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
427template<typename Derived, typename OtherDerived,
428 bool MightHaveTransposeAliasing
429 = check_transpose_aliasing_compile_time_selector
430 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
431 >
432struct checkTransposeAliasing_impl
433{
434 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
445template<typename Derived, typename OtherDerived>
446struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
447{
448 static void run(const Derived&, const OtherDerived&)
449 {
450 }
451};
452
453template<typename Dst, typename Src>
454void 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
void transposeInPlace()
Definition: Transpose.h:346
TransposeReturnType transpose()
Definition: Transpose.h:184
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:67
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
Expression of the transpose of a matrix.
Definition: Transpose.h:56
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: Transpose.h:78
internal::remove_reference< MatrixTypeNested >::type & nestedExpression()
Definition: Transpose.h:83
const unsigned int PacketAccessBit
Definition: Constants.h:96
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24