Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
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 <[email protected]>
5// Copyright (C) 2009-2014 Gael Guennebaud <[email protected]>
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// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20template <typename MatrixType>
21struct traits<Transpose<MatrixType> > : public traits<MatrixType> {
22 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
23 typedef std::remove_reference_t<MatrixTypeNested> 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} // namespace internal
38
39template <typename MatrixType, typename StorageKind>
40class TransposeImpl;
41
55template <typename MatrixType>
56class Transpose : public TransposeImpl<MatrixType, typename internal::traits<MatrixType>::StorageKind> {
57 public:
58 typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
59
60 typedef typename TransposeImpl<MatrixType, typename internal::traits<MatrixType>::StorageKind>::Base Base;
61 EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose)
62 typedef internal::remove_all_t<MatrixType> NestedExpression;
63
64 EIGEN_DEVICE_FUNC explicit EIGEN_STRONG_INLINE Transpose(MatrixType& matrix) : m_matrix(matrix) {}
65
66 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
67
68 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
69 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
70
72 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const internal::remove_all_t<MatrixTypeNested>& nestedExpression() const {
73 return m_matrix;
74 }
75
77 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::remove_reference_t<MatrixTypeNested>& nestedExpression() {
78 return m_matrix;
79 }
80
82 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index nrows, Index ncols) { m_matrix.resize(ncols, nrows); }
83
84 protected:
85 typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
86};
87
88namespace internal {
89
90template <typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
91struct TransposeImpl_base {
92 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
93};
94
95template <typename MatrixType>
96struct TransposeImpl_base<MatrixType, false> {
97 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
98};
99
100} // end namespace internal
101
102// Generic API dispatcher
103template <typename XprType, typename StorageKind>
104class TransposeImpl : public internal::generic_xpr_base<Transpose<XprType> >::type {
105 public:
106 typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
107};
108
109template <typename MatrixType>
110class TransposeImpl<MatrixType, Dense> : public internal::TransposeImpl_base<MatrixType>::type {
111 public:
112 typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
113 using Base::coeffRef;
114 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
115 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
116
117 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index innerStride() const { return derived().nestedExpression().innerStride(); }
118 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outerStride() const { return derived().nestedExpression().outerStride(); }
119
120 typedef std::conditional_t<internal::is_lvalue<MatrixType>::value, Scalar, const Scalar> ScalarWithConstIfNotLvalue;
121
122 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ScalarWithConstIfNotLvalue* data() {
123 return derived().nestedExpression().data();
124 }
125 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar* data() const { return derived().nestedExpression().data(); }
126
127 // FIXME: shall we keep the const version of coeffRef?
128 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeffRef(Index rowId, Index colId) const {
129 return derived().nestedExpression().coeffRef(colId, rowId);
130 }
131
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeffRef(Index index) const {
133 return derived().nestedExpression().coeffRef(index);
134 }
135
136 protected:
137 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(TransposeImpl)
138};
139
159template <typename Derived>
160EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename DenseBase<Derived>::TransposeReturnType DenseBase<Derived>::transpose() {
161 return TransposeReturnType(derived());
162}
163
169template <typename Derived>
170EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstTransposeReturnType
172 return ConstTransposeReturnType(derived());
173}
174
194template <typename Derived>
195EIGEN_DEVICE_FUNC inline const typename MatrixBase<Derived>::AdjointReturnType MatrixBase<Derived>::adjoint() const {
196 return AdjointReturnType(this->transpose());
199/***************************************************************************
200 * "in place" transpose implementation
201 ***************************************************************************/
202
203namespace internal {
204
205template <typename MatrixType,
206 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) &&
207 MatrixType::RowsAtCompileTime != Dynamic,
208 bool MatchPacketSize =
209 (int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size)) &&
210 (internal::evaluator<MatrixType>::Flags & PacketAccessBit)>
211struct inplace_transpose_selector;
212
213template <typename MatrixType>
214struct inplace_transpose_selector<MatrixType, true, false> { // square matrix
215 static void run(MatrixType& m) {
216 m.matrix().template triangularView<StrictlyUpper>().swap(
217 m.matrix().transpose().template triangularView<StrictlyUpper>());
218 }
219};
220
221template <typename MatrixType>
222struct inplace_transpose_selector<MatrixType, true, true> { // PacketSize x PacketSize
223 static void run(MatrixType& m) {
224 typedef typename MatrixType::Scalar Scalar;
225 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
226 const Index PacketSize = internal::packet_traits<Scalar>::size;
227 const Index Alignment = internal::evaluator<MatrixType>::Alignment;
228 PacketBlock<Packet> A;
229 for (Index i = 0; i < PacketSize; ++i) A.packet[i] = m.template packetByOuterInner<Alignment>(i, 0);
230 internal::ptranspose(A);
231 for (Index i = 0; i < PacketSize; ++i)
232 m.template writePacket<Alignment>(m.rowIndexByOuterInner(i, 0), m.colIndexByOuterInner(i, 0), A.packet[i]);
233 }
234};
235
236template <typename MatrixType, Index Alignment>
237void BlockedInPlaceTranspose(MatrixType& m) {
238 typedef typename MatrixType::Scalar Scalar;
239 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
240 const Index PacketSize = internal::packet_traits<Scalar>::size;
241 eigen_assert(m.rows() == m.cols());
242 int row_start = 0;
243 for (; row_start + PacketSize <= m.rows(); row_start += PacketSize) {
244 for (int col_start = row_start; col_start + PacketSize <= m.cols(); col_start += PacketSize) {
245 PacketBlock<Packet> A;
246 if (row_start == col_start) {
247 for (Index i = 0; i < PacketSize; ++i)
248 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i, col_start);
249 internal::ptranspose(A);
250 for (Index i = 0; i < PacketSize; ++i)
251 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start),
252 m.colIndexByOuterInner(row_start + i, col_start), A.packet[i]);
253 } else {
254 PacketBlock<Packet> B;
255 for (Index i = 0; i < PacketSize; ++i) {
256 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i, col_start);
257 B.packet[i] = m.template packetByOuterInner<Alignment>(col_start + i, row_start);
258 }
259 internal::ptranspose(A);
260 internal::ptranspose(B);
261 for (Index i = 0; i < PacketSize; ++i) {
262 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start),
263 m.colIndexByOuterInner(row_start + i, col_start), B.packet[i]);
264 m.template writePacket<Alignment>(m.rowIndexByOuterInner(col_start + i, row_start),
265 m.colIndexByOuterInner(col_start + i, row_start), A.packet[i]);
266 }
267 }
268 }
269 }
270 for (Index row = row_start; row < m.rows(); ++row) {
271 m.matrix().row(row).head(row).swap(m.matrix().col(row).head(row).transpose());
272 }
273}
274
275template <typename MatrixType, bool MatchPacketSize>
276struct inplace_transpose_selector<MatrixType, false, MatchPacketSize> { // non square or dynamic matrix
277 static void run(MatrixType& m) {
278 typedef typename MatrixType::Scalar Scalar;
279 if (m.rows() == m.cols()) {
280 const Index PacketSize = internal::packet_traits<Scalar>::size;
281 if (!NumTraits<Scalar>::IsComplex && m.rows() >= PacketSize) {
282 if ((m.rows() % PacketSize) == 0)
283 BlockedInPlaceTranspose<MatrixType, internal::evaluator<MatrixType>::Alignment>(m);
284 else
285 BlockedInPlaceTranspose<MatrixType, Unaligned>(m);
286 } else {
287 m.matrix().template triangularView<StrictlyUpper>().swap(
288 m.matrix().transpose().template triangularView<StrictlyUpper>());
289 }
290 } else {
291 m = m.transpose().eval();
292 }
293 }
294};
295
296} // end namespace internal
297
317template <typename Derived>
318EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::transposeInPlace() {
319 eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic)) &&
320 "transposeInPlace() called on a non-square non-resizable matrix");
321 internal::inplace_transpose_selector<Derived>::run(derived());
322}
323
324/***************************************************************************
325 * "in place" adjoint implementation
326 ***************************************************************************/
327
347template <typename Derived>
348EIGEN_DEVICE_FUNC inline void MatrixBase<Derived>::adjointInPlace() {
349 derived() = adjoint().eval();
350}
351
352#ifndef EIGEN_NO_DEBUG
353
354// The following is to detect aliasing problems in most common cases.
355
356namespace internal {
357
358template <bool DestIsTransposed, typename OtherDerived>
359struct check_transpose_aliasing_compile_time_selector {
360 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
361};
362
363template <bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
364struct check_transpose_aliasing_compile_time_selector<DestIsTransposed, CwiseBinaryOp<BinOp, DerivedA, DerivedB> > {
365 enum {
366 ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed ||
367 bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
368 };
369};
370
371template <typename Scalar, bool DestIsTransposed, typename OtherDerived>
372struct check_transpose_aliasing_run_time_selector {
373 EIGEN_DEVICE_FUNC static bool run(const Scalar* dest, const OtherDerived& src) {
374 return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) &&
375 (dest != 0 && dest == (const Scalar*)extract_data(src));
376 }
377};
378
379template <typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
380struct check_transpose_aliasing_run_time_selector<Scalar, DestIsTransposed, CwiseBinaryOp<BinOp, DerivedA, DerivedB> > {
381 EIGEN_DEVICE_FUNC static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp, DerivedA, DerivedB>& src) {
382 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) &&
383 (dest != 0 && dest == (const Scalar*)extract_data(src.lhs()))) ||
384 ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) &&
385 (dest != 0 && dest == (const Scalar*)extract_data(src.rhs())));
386 }
387};
388
389// the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
390// is because when the condition controlling the assert is known at compile time, ICC emits a warning.
391// This is actually a good warning: in expressions that don't have any transposing, the condition is
392// known at compile time to be false, and using that, we can avoid generating the code of the assert again
393// and again for all these expressions that don't need it.
394
395template <typename Derived, typename OtherDerived,
396 bool MightHaveTransposeAliasing =
397 check_transpose_aliasing_compile_time_selector<blas_traits<Derived>::IsTransposed, OtherDerived>::ret>
398struct checkTransposeAliasing_impl {
399 EIGEN_DEVICE_FUNC static void run(const Derived& dst, const OtherDerived& other) {
400 eigen_assert(
401 (!check_transpose_aliasing_run_time_selector<typename Derived::Scalar, blas_traits<Derived>::IsTransposed,
402 OtherDerived>::run(extract_data(dst), other)) &&
403 "aliasing detected during transposition, use transposeInPlace() "
404 "or evaluate the rhs into a temporary using .eval()");
405 }
406};
407
408template <typename Derived, typename OtherDerived>
409struct checkTransposeAliasing_impl<Derived, OtherDerived, false> {
410 EIGEN_DEVICE_FUNC static void run(const Derived&, const OtherDerived&) {}
411};
412
413template <typename Dst, typename Src>
414EIGEN_DEVICE_FUNC inline void check_for_aliasing(const Dst& dst, const Src& src) {
415 if ((!Dst::IsVectorAtCompileTime) && dst.rows() > 1 && dst.cols() > 1)
416 internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
417}
418
419} // end namespace internal
420
421#endif // EIGEN_NO_DEBUG
422
423} // end namespace Eigen
424
425#endif // EIGEN_TRANSPOSE_H
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:44
internal::traits< Homogeneous< MatrixType, Direction_ > >::Scalar Scalar
Definition DenseBase.h:62
EvalReturnType eval() const
Definition DenseBase.h:379
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
Expression of the transpose of a matrix.
Definition Transpose.h:56
std::remove_reference_t< MatrixTypeNested > & nestedExpression()
Definition Transpose.h:77
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition Transpose.h:72
const unsigned int PacketAccessBit
Definition Constants.h:97
const unsigned int LvalueBit
Definition Constants.h:148
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const int Dynamic
Definition Constants.h:25
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523