WPILibC++ 2023.4.3
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
14namespace Eigen {
15
16namespace internal {
17template<typename MatrixType>
18struct 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,
29 Flags1 = Flags0 | FlagsLvalueBit,
30 Flags = Flags1 ^ RowMajorBit,
32 OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
33 };
34};
35}
36
37template<typename MatrixType, typename StorageKind> class TransposeImpl;
38
39/** \class Transpose
40 * \ingroup Core_Module
41 *
42 * \brief Expression of the transpose of a matrix
43 *
44 * \tparam MatrixType the type of the object of which we are taking the transpose
45 *
46 * This class represents an expression of the transpose of a matrix.
47 * It is the return type of MatrixBase::transpose() and MatrixBase::adjoint()
48 * and most of the time this is the only way it is used.
49 *
50 * \sa MatrixBase::transpose(), MatrixBase::adjoint()
51 */
52template<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
64 explicit EIGEN_STRONG_INLINE Transpose(MatrixType& matrix) : m_matrix(matrix) {}
65
67
69 Index rows() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
71 Index cols() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
72
73 /** \returns the nested expression */
76 nestedExpression() const { return m_matrix; }
77
78 /** \returns the nested expression */
82
83 /** \internal */
85 void resize(Index nrows, Index ncols) {
86 m_matrix.resize(ncols,nrows);
87 }
88
89 protected:
91};
92
93namespace internal {
94
95template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
97{
99};
100
101template<typename MatrixType>
102struct TransposeImpl_base<MatrixType, false>
103{
105};
106
107} // end namespace internal
108
109// Generic API dispatcher
110template<typename XprType, typename StorageKind>
112 : public internal::generic_xpr_base<Transpose<XprType> >::type
113{
114public:
116};
117
118template<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
160/** \returns an expression of the transpose of *this.
161 *
162 * Example: \include MatrixBase_transpose.cpp
163 * Output: \verbinclude MatrixBase_transpose.out
164 *
165 * \warning If you want to replace a matrix by its own transpose, do \b NOT do this:
166 * \code
167 * m = m.transpose(); // bug!!! caused by aliasing effect
168 * \endcode
169 * Instead, use the transposeInPlace() method:
170 * \code
171 * m.transposeInPlace();
172 * \endcode
173 * which gives Eigen good opportunities for optimization, or alternatively you can also do:
174 * \code
175 * m = m.transpose().eval();
176 * \endcode
177 *
178 * \sa transposeInPlace(), adjoint() */
179template<typename Derived>
181Transpose<Derived>
183{
184 return TransposeReturnType(derived());
185}
186
187/** This is the const version of transpose().
188 *
189 * Make sure you read the warning for transpose() !
190 *
191 * \sa transposeInPlace(), adjoint() */
192template<typename Derived>
196{
197 return ConstTransposeReturnType(derived());
198}
199
200/** \returns an expression of the adjoint (i.e. conjugate transpose) of *this.
201 *
202 * Example: \include MatrixBase_adjoint.cpp
203 * Output: \verbinclude MatrixBase_adjoint.out
204 *
205 * \warning If you want to replace a matrix by its own adjoint, do \b NOT do this:
206 * \code
207 * m = m.adjoint(); // bug!!! caused by aliasing effect
208 * \endcode
209 * Instead, use the adjointInPlace() method:
210 * \code
211 * m.adjointInPlace();
212 * \endcode
213 * which gives Eigen good opportunities for optimization, or alternatively you can also do:
214 * \code
215 * m = m.adjoint().eval();
216 * \endcode
217 *
218 * \sa adjointInPlace(), transpose(), conjugate(), class Transpose, class internal::scalar_conjugate_op */
219template<typename Derived>
222{
223 return AdjointReturnType(this->transpose());
224}
225
226/***************************************************************************
227* "in place" transpose implementation
228***************************************************************************/
229
230namespace internal {
231
232template<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))
238
239template<typename MatrixType>
240struct 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
246template<typename MatrixType>
247struct inplace_transpose_selector<MatrixType,true,true> { // PacketSize x PacketSize
248 static void run(MatrixType& m) {
249 typedef typename MatrixType::Scalar Scalar;
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
263template <typename MatrixType, Index Alignment>
264void BlockedInPlaceTranspose(MatrixType& m) {
265 typedef typename MatrixType::Scalar Scalar;
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 {
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
300template<typename MatrixType,bool MatchPacketSize>
301struct 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()) {
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
324/** This is the "in place" version of transpose(): it replaces \c *this by its own transpose.
325 * Thus, doing
326 * \code
327 * m.transposeInPlace();
328 * \endcode
329 * has the same effect on m as doing
330 * \code
331 * m = m.transpose().eval();
332 * \endcode
333 * and is faster and also safer because in the latter line of code, forgetting the eval() results
334 * in a bug caused by \ref TopicAliasing "aliasing".
335 *
336 * Notice however that this method is only useful if you want to replace a matrix by its own transpose.
337 * If you just need the transpose of a matrix, use transpose().
338 *
339 * \note if the matrix is not square, then \c *this must be a resizable matrix.
340 * This excludes (non-square) fixed-size matrices, block-expressions and maps.
341 *
342 * \sa transpose(), adjoint(), adjointInPlace() */
343template<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
355/** This is the "in place" version of adjoint(): it replaces \c *this by its own transpose.
356 * Thus, doing
357 * \code
358 * m.adjointInPlace();
359 * \endcode
360 * has the same effect on m as doing
361 * \code
362 * m = m.adjoint().eval();
363 * \endcode
364 * and is faster and also safer because in the latter line of code, forgetting the eval() results
365 * in a bug caused by aliasing.
366 *
367 * Notice however that this method is only useful if you want to replace a matrix by its own adjoint.
368 * If you just need the adjoint of a matrix, use adjoint().
369 *
370 * \note if the matrix is not square, then \c *this must be a resizable matrix.
371 * This excludes (non-square) fixed-size matrices, block-expressions and maps.
372 *
373 * \sa transpose(), adjoint(), transposeInPlace() */
374template<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
384namespace internal {
385
386template<bool DestIsTransposed, typename OtherDerived>
388{
389 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
390};
391
392template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
393struct 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
400template<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
409template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
410struct 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
425template<typename Derived, typename OtherDerived,
426 bool MightHaveTransposeAliasing
427 = check_transpose_aliasing_compile_time_selector
428 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
429 >
431{
432 static void run(const Derived& dst, const OtherDerived& other)
433 {
435 <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
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
443template<typename Derived, typename OtherDerived>
444struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
445{
446 static void run(const Derived&, const OtherDerived&)
447 {
448 }
449};
450
451template<typename Dst, typename Src>
452void 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_DEVICE_FUNC EIGEN_STRONG_INLINE RowXpr row(Index i)
This is the const version of row(). *‍/.
Definition: BlockMethods.h:1118
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Just a side note.
Definition: Macros.h:1274
#define EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(Derived)
Definition: Macros.h:1257
#define EIGEN_NOEXCEPT
Definition: Macros.h:1428
#define EIGEN_CONSTEXPR
Definition: Macros.h:797
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:986
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1293
#define eigen_assert(x)
Definition: Macros.h:1047
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1241
#define EIGEN_STRONG_INLINE
Definition: Macros.h:927
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:84
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const _RhsNested & rhs() const
Definition: CwiseBinaryOp.h:135
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const _LhsNested & lhs() const
Definition: CwiseBinaryOp.h:132
EIGEN_DEVICE_FUNC void transposeInPlace()
This is the "in place" version of transpose(): it replaces *this by its own transpose.
Definition: Transpose.h:344
EIGEN_DEVICE_FUNC TransposeReturnType transpose()
Definition: Transpose.h:182
EIGEN_DEVICE_FUNC const AdjointReturnType adjoint() const
Definition: Transpose.h:221
EIGEN_DEVICE_FUNC void adjointInPlace()
This is the "in place" version of adjoint(): it replaces *this by its own transpose.
Definition: Transpose.h:375
Expression of the transpose of a matrix.
Definition: Transpose.h:54
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Transpose.h:69
TransposeImpl< MatrixType, typenameinternal::traits< MatrixType >::StorageKind >::Base Base
Definition: Transpose.h:59
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Transpose.h:71
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::remove_reference< MatrixTypeNested >::type & nestedExpression()
Definition: Transpose.h:81
internal::remove_all< MatrixType >::type NestedExpression
Definition: Transpose.h:61
internal::ref_selector< MatrixType >::non_const_type m_matrix
Definition: Transpose.h:90
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: Transpose.h:76
internal::ref_selector< MatrixType >::non_const_type MatrixTypeNested
Definition: Transpose.h:57
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index nrows, Index ncols)
Definition: Transpose.h:85
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ScalarWithConstIfNotLvalue * data()
Definition: Transpose.h:140
internal::conditional< internal::is_lvalue< MatrixType >::value, Scalar, constScalar >::type ScalarWithConstIfNotLvalue
Definition: Transpose.h:137
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outerStride() const
Definition: Transpose.h:131
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeffRef(Index index) const
Definition: Transpose.h:152
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
Definition: Transpose.h:142
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeffRef(Index rowId, Index colId) const
Definition: Transpose.h:146
internal::TransposeImpl_base< MatrixType >::type Base
Definition: Transpose.h:123
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index innerStride() const
Definition: Transpose.h:129
Definition: Transpose.h:113
internal::generic_xpr_base< Transpose< XprType > >::type Base
Definition: Transpose.h:115
type
Definition: core.h:575
const unsigned int PacketAccessBit
Short version: means the expression might be vectorized.
Definition: Constants.h:94
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition: Constants.h:144
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:66
void check_for_aliasing(const Dst &dst, const Src &src)
Definition: Transpose.h:452
EIGEN_DEVICE_FUNC void ptranspose(PacketBlock< Packet4f, 4 > &kernel)
Definition: PacketMath.h:1124
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE const T::Scalar * extract_data(const T &m)
Definition: BlasUtil.h:533
void BlockedInPlaceTranspose(MatrixType &m)
Definition: Transpose.h:264
Namespace containing all symbols from the Eigen library.
Definition: MatrixExponential.h:16
const unsigned int NestByRefBit
Definition: Constants.h:169
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time,...
Definition: Constants.h:22
GHC_FS_API uintmax_t remove_all(const path &p, std::error_code &ec) noexcept
Definition: filesystem.hpp:4732
Definition: Eigen_Colamd.h:50
The type used to identify a dense storage.
Definition: Constants.h:507
Holds information about the various numeric (i.e.
Definition: NumTraits.h:233
Definition: GenericPacketMath.h:1014
Packet packet[N]
Definition: GenericPacketMath.h:1015
dense_xpr_base< Transpose< MatrixType > >::type type
Definition: Transpose.h:104
Definition: Transpose.h:97
dense_xpr_base< Transpose< MatrixType > >::type type
Definition: Transpose.h:98
Definition: BlasUtil.h:403
@ IsTransposed
Definition: BlasUtil.h:409
static bool run(const Scalar *dest, const CwiseBinaryOp< BinOp, DerivedA, DerivedB > &src)
Definition: Transpose.h:412
static bool run(const Scalar *dest, const OtherDerived &src)
Definition: Transpose.h:403
static void run(const Derived &, const OtherDerived &)
Definition: Transpose.h:446
static void run(const Derived &dst, const OtherDerived &other)
Definition: Transpose.h:432
Definition: Meta.h:109
Definition: XprHelper.h:484
Definition: CoreEvaluators.h:91
Definition: XprHelper.h:501
Definition: DenseCoeffsBase.h:659
static void run(MatrixType &m)
Definition: Transpose.h:241
static void run(MatrixType &m)
Definition: Transpose.h:248
Definition: XprHelper.h:660
Definition: DenseCoeffsBase.h:671
Definition: GenericPacketMath.h:107
T type
Definition: GenericPacketMath.h:108
Definition: XprHelper.h:417
T type
Definition: Meta.h:126
T type
Definition: Meta.h:114
remove_reference< MatrixTypeNested >::type MatrixTypeNestedPlain
Definition: Transpose.h:21
ref_selector< MatrixType >::type MatrixTypeNested
Definition: Transpose.h:20
Definition: ForwardDeclarations.h:17
Definition: Meta.h:96