Eigen  5.0.1-dev+master
 
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 // The start alignment carries over to every column only if the outer stride is a multiple of the packet size.
233 const Index Alignment = internal::outer_stride_at_compile_time<MatrixType>::value % PacketSize == 0
234 ? int(internal::evaluator<MatrixType>::Alignment)
235 : int(Unaligned);
236 PacketBlock<Packet> A;
237 for (Index i = 0; i < PacketSize; ++i) A.packet[i] = m.template packetByOuterInner<Alignment>(i, 0);
238 internal::ptranspose(A);
239 for (Index i = 0; i < PacketSize; ++i)
240 m.template writePacket<Alignment>(m.rowIndexByOuterInner(i, 0), m.colIndexByOuterInner(i, 0), A.packet[i]);
241 }
242};
243
244template <typename MatrixType, Index Alignment>
245void BlockedInPlaceTranspose(MatrixType& m) {
246 using Scalar = typename MatrixType::Scalar;
247 using Packet = typename internal::packet_traits<typename MatrixType::Scalar>::type;
248 const Index PacketSize = internal::packet_traits<Scalar>::size;
249 EIGEN_INTERNAL_ASSERT_SQUARE(m);
250 Index row_start = 0;
251 for (; row_start + PacketSize <= m.rows(); row_start += PacketSize) {
252 for (Index col_start = row_start; col_start + PacketSize <= m.cols(); col_start += PacketSize) {
253 PacketBlock<Packet> A;
254 if (row_start == col_start) {
255 for (Index i = 0; i < PacketSize; ++i)
256 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i, col_start);
257 internal::ptranspose(A);
258 for (Index i = 0; i < PacketSize; ++i)
259 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start),
260 m.colIndexByOuterInner(row_start + i, col_start), A.packet[i]);
261 } else {
262 PacketBlock<Packet> B;
263 for (Index i = 0; i < PacketSize; ++i) {
264 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i, col_start);
265 B.packet[i] = m.template packetByOuterInner<Alignment>(col_start + i, row_start);
266 }
267 internal::ptranspose(A);
268 internal::ptranspose(B);
269 for (Index i = 0; i < PacketSize; ++i) {
270 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start),
271 m.colIndexByOuterInner(row_start + i, col_start), B.packet[i]);
272 m.template writePacket<Alignment>(m.rowIndexByOuterInner(col_start + i, row_start),
273 m.colIndexByOuterInner(col_start + i, row_start), A.packet[i]);
274 }
275 }
276 }
277 }
278 // Blocks rather than row()/col(): with a zero-sized fixed dimension those are not vectors and head() would not
279 // compile.
280 constexpr int TailSize = MatrixType::RowsAtCompileTime == 0 || MatrixType::ColsAtCompileTime == 0 ? Dynamic : 1;
281 for (Index row = row_start; row < m.rows(); ++row) {
282 m.matrix()
283 .template block<TailSize, Dynamic>(row, 0, 1, row)
284 .swap(m.matrix().template block<Dynamic, TailSize>(0, row, row, 1).transpose());
285 }
286}
287
288template <typename MatrixType, bool MatchPacketSize>
289struct inplace_transpose_selector<MatrixType, false, MatchPacketSize> { // non square or dynamic matrix
290 static void run(MatrixType& m) {
291 using Scalar = typename MatrixType::Scalar;
292 if (m.rows() == m.cols()) {
293 const Index PacketSize = internal::packet_traits<Scalar>::size;
294 EIGEN_IF_CONSTEXPR (!NumTraits<Scalar>::IsComplex && (internal::evaluator<MatrixType>::Flags & PacketAccessBit)) {
295 if (m.rows() >= PacketSize) {
296 if ((m.rows() % PacketSize) == 0 && (m.outerStride() % PacketSize) == 0)
297 BlockedInPlaceTranspose<MatrixType, internal::evaluator<MatrixType>::Alignment>(m);
298 else
299 BlockedInPlaceTranspose<MatrixType, Unaligned>(m);
300 return;
301 }
303 m.matrix().template triangularView<StrictlyUpper>().swap(
304 m.matrix().transpose().template triangularView<StrictlyUpper>());
305 } else {
306 m = m.transpose().eval();
307 }
308 }
309};
310
311} // end namespace internal
312
332template <typename Derived>
333EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::transposeInPlace() {
334 using ShapeRelation = std::conditional_t<RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic,
335 internal::dimension_relation<1, 1>, internal::square_dimensions<Derived>>;
336 EIGEN_INTERNAL_SHAPE_ASSERT(ShapeRelation::Proven,
337 (rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic)) &&
338 "transposeInPlace() called on a non-square non-resizable matrix");
339 internal::inplace_transpose_selector<Derived>::run(derived());
340}
341
342/***************************************************************************
343 * "in place" adjoint implementation
344 ***************************************************************************/
345
365template <typename Derived>
366EIGEN_DEVICE_FUNC inline void MatrixBase<Derived>::adjointInPlace() {
367 derived() = adjoint().eval();
368}
369
370#ifndef EIGEN_NO_DEBUG
371
372// The following is to detect aliasing problems in most common cases.
373
374namespace internal {
375
376template <bool DestIsTransposed, typename OtherDerived>
377struct check_transpose_aliasing_compile_time_selector
378 : bool_constant<bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed> {};
379
380template <bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
381struct check_transpose_aliasing_compile_time_selector<DestIsTransposed, CwiseBinaryOp<BinOp, DerivedA, DerivedB>>
382 : bool_constant<bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed ||
383 bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed> {};
384
385template <typename Scalar, bool DestIsTransposed, typename OtherDerived>
386struct check_transpose_aliasing_run_time_selector {
387 EIGEN_DEVICE_FUNC static bool run(const Scalar* dest, const OtherDerived& src) {
388 return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) &&
389 (dest != 0 && dest == (const Scalar*)extract_data(src));
390 }
391};
392
393template <typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
394struct check_transpose_aliasing_run_time_selector<Scalar, DestIsTransposed, CwiseBinaryOp<BinOp, DerivedA, DerivedB>> {
395 EIGEN_DEVICE_FUNC static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp, DerivedA, DerivedB>& src) {
396 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) &&
397 (dest != 0 && dest == (const Scalar*)extract_data(src.lhs()))) ||
398 ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) &&
399 (dest != 0 && dest == (const Scalar*)extract_data(src.rhs())));
400 }
401};
402
403// the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
404// is because when the condition controlling the assert is known at compile time, ICC emits a warning.
405// This is actually a good warning: in expressions that don't have any transposing, the condition is
406// known at compile time to be false, and using that, we can avoid generating the code of the assert again
407// and again for all these expressions that don't need it.
408
409template <typename Derived, typename OtherDerived,
410 bool MightHaveTransposeAliasing =
411 check_transpose_aliasing_compile_time_selector<blas_traits<Derived>::IsTransposed, OtherDerived>::value>
412struct checkTransposeAliasing_impl {
413 EIGEN_DEVICE_FUNC static void run(const Derived& dst, const OtherDerived& other) {
414 eigen_assert(
415 (!check_transpose_aliasing_run_time_selector<typename Derived::Scalar, blas_traits<Derived>::IsTransposed,
416 OtherDerived>::run(extract_data(dst), other)) &&
417 "aliasing detected during transposition, use transposeInPlace() "
418 "or evaluate the rhs into a temporary using .eval()");
419 }
420};
421
422template <typename Derived, typename OtherDerived>
423struct checkTransposeAliasing_impl<Derived, OtherDerived, false> {
424 EIGEN_DEVICE_FUNC static void run(const Derived&, const OtherDerived&) {}
425};
426
427template <typename Dst, typename Src>
428EIGEN_DEVICE_FUNC inline void check_for_aliasing(const Dst& dst, const Src& src) {
429 EIGEN_IF_CONSTEXPR (!Dst::IsVectorAtCompileTime) {
430 if (dst.rows() > 1 && dst.cols() > 1) internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
431 }
432}
433
434} // end namespace internal
435
436#endif // EIGEN_NO_DEBUG
437
438} // end namespace Eigen
439
440#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:333
void adjointInPlace()
Definition Transpose.h:366
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
@ Unaligned
Definition Constants.h:236
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