Eigen  5.0.1
 
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 <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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_TRANSPOSE_H
13#define EIGEN_TRANSPOSE_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21template <typename MatrixType>
22struct traits<Transpose<MatrixType>> : public traits<MatrixType> {
23 using MatrixTypeNested = typename ref_selector<MatrixType>::type;
24 using MatrixTypeNestedPlain = std::remove_reference_t<MatrixTypeNested>;
25 enum {
26 RowsAtCompileTime = MatrixType::ColsAtCompileTime,
27 ColsAtCompileTime = MatrixType::RowsAtCompileTime,
28 MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
29 MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
30 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
31 Flags0 = traits<MatrixTypeNestedPlain>::Flags & ~(LvalueBit | NestByRefBit),
32 Flags1 = Flags0 | FlagsLvalueBit,
33 Flags = Flags1 ^ RowMajorBit,
34 InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::value,
35 OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::value
36 };
37};
38} // namespace internal
39
40template <typename MatrixType, typename StorageKind>
41class TransposeImpl;
42
56template <typename MatrixType>
57class Transpose : public TransposeImpl<MatrixType, typename internal::traits<MatrixType>::StorageKind> {
58 public:
59 using MatrixTypeNested = typename internal::ref_selector<MatrixType>::non_const_type;
60
61 using Base = typename TransposeImpl<MatrixType, typename internal::traits<MatrixType>::StorageKind>::Base;
62 EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose)
63 using NestedExpression = internal::remove_all_t<MatrixType>;
64
65 EIGEN_DEVICE_FUNC constexpr explicit EIGEN_STRONG_INLINE Transpose(MatrixType& matrix) : m_matrix(matrix) {}
66
67 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
68
69 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_matrix.cols(); }
70 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_matrix.rows(); }
71
73 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const internal::remove_all_t<MatrixTypeNested>& nestedExpression()
74 const {
75 return m_matrix;
76 }
77
79 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE std::remove_reference_t<MatrixTypeNested>& nestedExpression() {
80 return m_matrix;
81 }
82
84 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index nrows, Index ncols) { m_matrix.resize(ncols, nrows); }
85
86 protected:
87 typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
88};
89
90namespace internal {
91
92template <typename MatrixType>
93struct TransposeImpl_base {
94 using type = typename dense_xpr_base<Transpose<MatrixType>>::type;
95};
96
97} // end namespace internal
98
99// Generic API dispatcher
100template <typename XprType, typename StorageKind>
101class TransposeImpl : public internal::generic_xpr_base<Transpose<XprType>>::type {
102 public:
103 using Base = typename internal::generic_xpr_base<Transpose<XprType>>::type;
104};
105
106template <typename MatrixType>
107class TransposeImpl<MatrixType, Dense> : public internal::TransposeImpl_base<MatrixType>::type {
108 public:
109 using Base = typename internal::TransposeImpl_base<MatrixType>::type;
110 using Base::coeffRef;
111 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
112 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
113
114 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Index innerStride() const {
115 return derived().nestedExpression().innerStride();
116 }
117 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Index outerStride() const {
118 return derived().nestedExpression().outerStride();
119 }
120
121 using ScalarWithConstIfNotLvalue = std::conditional_t<internal::is_lvalue<MatrixType>::value, Scalar, const Scalar>;
122
123 template <typename T = MatrixType, typename = internal::void_t<decltype(std::declval<T&>().data())>>
124 EIGEN_DEVICE_FUNC constexpr ScalarWithConstIfNotLvalue* data() {
125 return derived().nestedExpression().data();
126 }
127 template <typename T = MatrixType, typename = internal::void_t<decltype(std::declval<const T&>().data())>>
128 EIGEN_DEVICE_FUNC constexpr const Scalar* data() const {
129 return derived().nestedExpression().data();
130 }
131
132 // FIXME: shall we keep the const version of coeffRef?
133 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeffRef(Index rowId, Index colId) const {
134 return derived().nestedExpression().coeffRef(colId, rowId);
135 }
136
137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeffRef(Index index) const {
138 return derived().nestedExpression().coeffRef(index);
139 }
140
141 protected:
142 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(TransposeImpl)
143};
144
164template <typename Derived>
165EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename DenseBase<Derived>::TransposeReturnType DenseBase<Derived>::transpose() {
166 return TransposeReturnType(derived());
167}
168
174template <typename Derived>
175EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstTransposeReturnType
177 return ConstTransposeReturnType(derived());
178}
179
198 * \sa adjointInPlace(), transpose(), conjugate(), class Transpose, class internal::scalar_conjugate_op */
199template <typename Derived>
200EIGEN_DEVICE_FUNC constexpr const typename MatrixBase<Derived>::AdjointReturnType MatrixBase<Derived>::adjoint() const {
201 return AdjointReturnType(this->transpose());
202}
203
204/***************************************************************************
205 * "in place" transpose implementation
206 ***************************************************************************/
207
208namespace internal {
209
210template <typename MatrixType,
211 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) &&
212 MatrixType::RowsAtCompileTime != Dynamic,
213 bool MatchPacketSize =
214 (int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size)) &&
215 (internal::evaluator<MatrixType>::Flags & PacketAccessBit)>
216struct inplace_transpose_selector;
217
218template <typename MatrixType>
219struct inplace_transpose_selector<MatrixType, true, false> { // square matrix
220 static void run(MatrixType& m) {
221 m.matrix().template triangularView<StrictlyUpper>().swap(
222 m.matrix().transpose().template triangularView<StrictlyUpper>());
223 }
224};
225
226template <typename MatrixType>
227struct inplace_transpose_selector<MatrixType, true, true> { // PacketSize x PacketSize
228 static void run(MatrixType& m) {
229 using Scalar = typename MatrixType::Scalar;
230 using Packet = typename internal::packet_traits<typename MatrixType::Scalar>::type;
231 const Index PacketSize = internal::packet_traits<Scalar>::size;
232 const Index Alignment = internal::evaluator<MatrixType>::Alignment;
233 PacketBlock<Packet> A;
234 for (Index i = 0; i < PacketSize; ++i) A.packet[i] = m.template packetByOuterInner<Alignment>(i, 0);
235 internal::ptranspose(A);
236 for (Index i = 0; i < PacketSize; ++i)
237 m.template writePacket<Alignment>(m.rowIndexByOuterInner(i, 0), m.colIndexByOuterInner(i, 0), A.packet[i]);
238 }
239};
240
241template <typename MatrixType, Index Alignment>
242void BlockedInPlaceTranspose(MatrixType& m) {
243 using Scalar = typename MatrixType::Scalar;
244 using Packet = typename internal::packet_traits<typename MatrixType::Scalar>::type;
245 const Index PacketSize = internal::packet_traits<Scalar>::size;
246 eigen_assert(m.rows() == m.cols());
247 int row_start = 0;
248 for (; row_start + PacketSize <= m.rows(); row_start += PacketSize) {
249 for (int col_start = row_start; col_start + PacketSize <= m.cols(); col_start += PacketSize) {
250 PacketBlock<Packet> A;
251 if (row_start == col_start) {
252 for (Index i = 0; i < PacketSize; ++i)
253 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i, col_start);
254 internal::ptranspose(A);
255 for (Index i = 0; i < PacketSize; ++i)
256 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start),
257 m.colIndexByOuterInner(row_start + i, col_start), A.packet[i]);
258 } else {
259 PacketBlock<Packet> B;
260 for (Index i = 0; i < PacketSize; ++i) {
261 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i, col_start);
262 B.packet[i] = m.template packetByOuterInner<Alignment>(col_start + i, row_start);
263 }
264 internal::ptranspose(A);
265 internal::ptranspose(B);
266 for (Index i = 0; i < PacketSize; ++i) {
267 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start),
268 m.colIndexByOuterInner(row_start + i, col_start), B.packet[i]);
269 m.template writePacket<Alignment>(m.rowIndexByOuterInner(col_start + i, row_start),
270 m.colIndexByOuterInner(col_start + i, row_start), A.packet[i]);
271 }
272 }
273 }
274 }
275 for (Index row = row_start; row < m.rows(); ++row) {
276 m.matrix().row(row).head(row).swap(m.matrix().col(row).head(row).transpose());
277 }
278}
279
280template <typename MatrixType, bool MatchPacketSize>
281struct inplace_transpose_selector<MatrixType, false, MatchPacketSize> { // non square or dynamic matrix
282 static void run(MatrixType& m) {
283 using Scalar = typename MatrixType::Scalar;
284 if (m.rows() == m.cols()) {
285 const Index PacketSize = internal::packet_traits<Scalar>::size;
286 EIGEN_IF_CONSTEXPR (!NumTraits<Scalar>::IsComplex && (internal::evaluator<MatrixType>::Flags & PacketAccessBit)) {
287 if (m.rows() >= PacketSize) {
288 if ((m.rows() % PacketSize) == 0)
289 BlockedInPlaceTranspose<MatrixType, internal::evaluator<MatrixType>::Alignment>(m);
290 else
291 BlockedInPlaceTranspose<MatrixType, Unaligned>(m);
292 return;
293 }
294 }
295 m.matrix().template triangularView<StrictlyUpper>().swap(
296 m.matrix().transpose().template triangularView<StrictlyUpper>());
297 } else {
298 m = m.transpose().eval();
300 }
303} // end namespace internal
304
324template <typename Derived>
325EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::transposeInPlace() {
326 eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic)) &&
327 "transposeInPlace() called on a non-square non-resizable matrix");
328 internal::inplace_transpose_selector<Derived>::run(derived());
329}
330
331/***************************************************************************
332 * "in place" adjoint implementation
333 ***************************************************************************/
334
354template <typename Derived>
355EIGEN_DEVICE_FUNC inline void MatrixBase<Derived>::adjointInPlace() {
356 derived() = adjoint().eval();
357}
358
359#ifndef EIGEN_NO_DEBUG
360
361// The following is to detect aliasing problems in most common cases.
362
363namespace internal {
364
365template <bool DestIsTransposed, typename OtherDerived>
366struct check_transpose_aliasing_compile_time_selector
367 : bool_constant<bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed> {};
368
369template <bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
370struct check_transpose_aliasing_compile_time_selector<DestIsTransposed, CwiseBinaryOp<BinOp, DerivedA, DerivedB>>
371 : bool_constant<bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed ||
372 bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed> {};
373
374template <typename Scalar, bool DestIsTransposed, typename OtherDerived>
375struct check_transpose_aliasing_run_time_selector {
376 EIGEN_DEVICE_FUNC static bool run(const Scalar* dest, const OtherDerived& src) {
377 return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) &&
378 (dest != 0 && dest == (const Scalar*)extract_data(src));
379 }
380};
381
382template <typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
383struct check_transpose_aliasing_run_time_selector<Scalar, DestIsTransposed, CwiseBinaryOp<BinOp, DerivedA, DerivedB>> {
384 EIGEN_DEVICE_FUNC static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp, DerivedA, DerivedB>& src) {
385 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) &&
386 (dest != 0 && dest == (const Scalar*)extract_data(src.lhs()))) ||
387 ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) &&
388 (dest != 0 && dest == (const Scalar*)extract_data(src.rhs())));
389 }
390};
391
392// the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
393// is because when the condition controlling the assert is known at compile time, ICC emits a warning.
394// This is actually a good warning: in expressions that don't have any transposing, the condition is
395// known at compile time to be false, and using that, we can avoid generating the code of the assert again
396// and again for all these expressions that don't need it.
397
398template <typename Derived, typename OtherDerived,
399 bool MightHaveTransposeAliasing =
400 check_transpose_aliasing_compile_time_selector<blas_traits<Derived>::IsTransposed, OtherDerived>::value>
401struct checkTransposeAliasing_impl {
402 EIGEN_DEVICE_FUNC static void run(const Derived& dst, const OtherDerived& other) {
403 eigen_assert(
404 (!check_transpose_aliasing_run_time_selector<typename Derived::Scalar, blas_traits<Derived>::IsTransposed,
405 OtherDerived>::run(extract_data(dst), other)) &&
406 "aliasing detected during transposition, use transposeInPlace() "
407 "or evaluate the rhs into a temporary using .eval()");
408 }
409};
410
411template <typename Derived, typename OtherDerived>
412struct checkTransposeAliasing_impl<Derived, OtherDerived, false> {
413 EIGEN_DEVICE_FUNC static void run(const Derived&, const OtherDerived&) {}
414};
415
416template <typename Dst, typename Src>
417EIGEN_DEVICE_FUNC inline void check_for_aliasing(const Dst& dst, const Src& src) {
418 EIGEN_IF_CONSTEXPR (!Dst::IsVectorAtCompileTime) {
419 if (dst.rows() > 1 && dst.cols() > 1) internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
420 }
421}
422
423} // end namespace internal
424
425#endif // EIGEN_NO_DEBUG
426
427} // end namespace Eigen
428
429#endif // EIGEN_TRANSPOSE_H
TransposeReturnType transpose()
Definition Transpose.h:165
@ ColsAtCompileTime
Definition DenseBase.h:103
@ RowsAtCompileTime
Definition DenseBase.h:97
void transposeInPlace()
Definition Transpose.h:325
void adjointInPlace()
Definition Transpose.h:355
constexpr const AdjointReturnType adjoint() const
Definition Transpose.h:200
constexpr std::remove_reference_t< MatrixTypeNested > & nestedExpression()
Definition Transpose.h:79
constexpr const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition Transpose.h:73
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition Constants.h:542