11#ifndef EIGEN_TRANSPOSE_H
12#define EIGEN_TRANSPOSE_H
15#include "./InternalHeaderCheck.h"
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;
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,
33 InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
34 OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
39template <
typename MatrixType,
typename StorageKind>
55template <
typename MatrixType>
56class Transpose :
public TransposeImpl<MatrixType, typename internal::traits<MatrixType>::StorageKind> {
58 typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
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;
64 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE Transpose(MatrixType& matrix) : m_matrix(matrix) {}
66 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
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(); }
72 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const internal::remove_all_t<MatrixTypeNested>&
nestedExpression()
const {
77 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::remove_reference_t<MatrixTypeNested>&
nestedExpression() {
82 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void resize(
Index nrows,
Index ncols) { m_matrix.resize(ncols, nrows); }
85 typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
90template <typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
91struct TransposeImpl_base {
92 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
95template <
typename MatrixType>
96struct TransposeImpl_base<MatrixType, false> {
97 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
103template <
typename XprType,
typename StorageKind>
104class TransposeImpl :
public internal::generic_xpr_base<Transpose<XprType> >::type {
106 typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
109template <
typename MatrixType>
110class TransposeImpl<
MatrixType,
Dense> :
public internal::TransposeImpl_base<MatrixType>::type {
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)
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(); }
120 typedef std::conditional_t<internal::is_lvalue<MatrixType>::value, Scalar,
const Scalar> ScalarWithConstIfNotLvalue;
122 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
constexpr ScalarWithConstIfNotLvalue* data() {
123 return derived().nestedExpression().data();
125 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
constexpr const Scalar* data()
const {
126 return derived().nestedExpression().data();
130 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeffRef(
Index rowId,
Index colId)
const {
131 return derived().nestedExpression().coeffRef(colId, rowId);
134 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeffRef(
Index index)
const {
135 return derived().nestedExpression().coeffRef(index);
139 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(TransposeImpl)
161template <
typename Derived>
163 return TransposeReturnType(derived());
171template <
typename Derived>
172EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const typename DenseBase<Derived>::ConstTransposeReturnType
174 return ConstTransposeReturnType(derived());
196template <
typename Derived>
208 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) &&
209 MatrixType::RowsAtCompileTime !=
Dynamic,
210 bool MatchPacketSize =
211 (
int(MatrixType::RowsAtCompileTime) ==
int(internal::packet_traits<typename MatrixType::Scalar>::size)) &&
213struct inplace_transpose_selector;
215template <
typename MatrixType>
216struct inplace_transpose_selector<MatrixType, true, false> {
218 m.
matrix().template triangularView<StrictlyUpper>().swap(
219 m.
matrix().transpose().template triangularView<StrictlyUpper>());
223template <
typename MatrixType>
224struct inplace_transpose_selector<MatrixType, true, true> {
225 static void run(MatrixType& m) {
226 typedef typename MatrixType::Scalar Scalar;
227 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
228 const Index PacketSize = internal::packet_traits<Scalar>::size;
229 const Index Alignment = internal::evaluator<MatrixType>::Alignment;
230 PacketBlock<Packet> A;
231 for (
Index i = 0; i < PacketSize; ++i) A.packet[i] = m.template packetByOuterInner<Alignment>(i, 0);
232 internal::ptranspose(A);
233 for (
Index i = 0; i < PacketSize; ++i)
234 m.template writePacket<Alignment>(m.rowIndexByOuterInner(i, 0), m.colIndexByOuterInner(i, 0), A.packet[i]);
238template <
typename MatrixType, Index Alignment>
239void BlockedInPlaceTranspose(MatrixType& m) {
240 typedef typename MatrixType::Scalar Scalar;
241 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
242 const Index PacketSize = internal::packet_traits<Scalar>::size;
243 eigen_assert(m.rows() == m.cols());
245 for (; row_start + PacketSize <= m.rows(); row_start += PacketSize) {
246 for (
int col_start = row_start; col_start + PacketSize <= m.cols(); col_start += PacketSize) {
247 PacketBlock<Packet> A;
248 if (row_start == col_start) {
249 for (
Index i = 0; i < PacketSize; ++i)
250 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i, col_start);
251 internal::ptranspose(A);
252 for (
Index i = 0; i < PacketSize; ++i)
253 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start),
254 m.colIndexByOuterInner(row_start + i, col_start), A.packet[i]);
256 PacketBlock<Packet> B;
257 for (
Index i = 0; i < PacketSize; ++i) {
258 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i, col_start);
259 B.packet[i] = m.template packetByOuterInner<Alignment>(col_start + i, row_start);
261 internal::ptranspose(A);
262 internal::ptranspose(B);
263 for (
Index i = 0; i < PacketSize; ++i) {
264 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start),
265 m.colIndexByOuterInner(row_start + i, col_start), B.packet[i]);
266 m.template writePacket<Alignment>(m.rowIndexByOuterInner(col_start + i, row_start),
267 m.colIndexByOuterInner(col_start + i, row_start), A.packet[i]);
272 for (
Index row = row_start; row < m.rows(); ++row) {
273 m.matrix().row(row).head(row).swap(m.matrix().col(row).head(row).transpose());
277template <
typename MatrixType,
bool MatchPacketSize>
278struct inplace_transpose_selector<MatrixType, false, MatchPacketSize> {
279 static void run(MatrixType& m) {
280 typedef typename MatrixType::Scalar Scalar;
281 if (m.rows() == m.cols()) {
282 const Index PacketSize = internal::packet_traits<Scalar>::size;
283 if (!NumTraits<Scalar>::IsComplex && m.rows() >= PacketSize) {
284 if ((m.rows() % PacketSize) == 0)
285 BlockedInPlaceTranspose<MatrixType, internal::evaluator<MatrixType>::Alignment>(m);
287 BlockedInPlaceTranspose<MatrixType, Unaligned>(m);
289 m.matrix().template triangularView<StrictlyUpper>().swap(
290 m.matrix().transpose().template triangularView<StrictlyUpper>());
293 m = m.transpose().eval();
319template <
typename Derived>
322 "transposeInPlace() called on a non-square non-resizable matrix");
323 internal::inplace_transpose_selector<Derived>::run(derived());
349template <
typename Derived>
354#ifndef EIGEN_NO_DEBUG
360template <
bool DestIsTransposed,
typename OtherDerived>
361struct check_transpose_aliasing_compile_time_selector {
362 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
365template <
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
366struct check_transpose_aliasing_compile_time_selector<DestIsTransposed, CwiseBinaryOp<BinOp, DerivedA, DerivedB> > {
368 ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed ||
369 bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
373template <
typename Scalar,
bool DestIsTransposed,
typename OtherDerived>
374struct check_transpose_aliasing_run_time_selector {
375 EIGEN_DEVICE_FUNC
static bool run(
const Scalar* dest,
const OtherDerived& src) {
376 return (
bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) &&
377 (dest != 0 && dest == (
const Scalar*)extract_data(src));
381template <
typename Scalar,
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
382struct check_transpose_aliasing_run_time_selector<Scalar, DestIsTransposed, CwiseBinaryOp<BinOp, DerivedA, DerivedB> > {
383 EIGEN_DEVICE_FUNC
static bool run(
const Scalar* dest,
const CwiseBinaryOp<BinOp, DerivedA, DerivedB>& src) {
384 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) &&
385 (dest != 0 && dest == (
const Scalar*)extract_data(src.lhs()))) ||
386 ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) &&
387 (dest != 0 && dest == (
const Scalar*)extract_data(src.rhs())));
397template <
typename Derived,
typename OtherDerived,
398 bool MightHaveTransposeAliasing =
399 check_transpose_aliasing_compile_time_selector<blas_traits<Derived>::IsTransposed, OtherDerived>::ret>
400struct checkTransposeAliasing_impl {
401 EIGEN_DEVICE_FUNC
static void run(
const Derived& dst,
const OtherDerived& other) {
403 (!check_transpose_aliasing_run_time_selector<
typename Derived::Scalar, blas_traits<Derived>::IsTransposed,
404 OtherDerived>::run(extract_data(dst), other)) &&
405 "aliasing detected during transposition, use transposeInPlace() "
406 "or evaluate the rhs into a temporary using .eval()");
410template <
typename Derived,
typename OtherDerived>
411struct checkTransposeAliasing_impl<Derived, OtherDerived, false> {
412 EIGEN_DEVICE_FUNC
static void run(
const Derived&,
const OtherDerived&) {}
415template <
typename Dst,
typename Src>
416EIGEN_DEVICE_FUNC
inline void check_for_aliasing(
const Dst& dst,
const Src& src) {
417 if ((!Dst::IsVectorAtCompileTime) && dst.rows() > 1 && dst.cols() > 1)
418 internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
TransposeReturnType transpose()
Definition Transpose.h:162
@ ColsAtCompileTime
Definition DenseBase.h:102
@ RowsAtCompileTime
Definition DenseBase.h:96
void transposeInPlace()
Definition Transpose.h:320
void adjointInPlace()
Definition Transpose.h:350
const AdjointReturnType adjoint() const
Definition Transpose.h:197
Expression of the transpose of a matrix.
Definition ForwardDeclarations.h:100
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
const unsigned int RowMajorBit
Definition Constants.h:70
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:82
const int Dynamic
Definition Constants.h:25
Definition Constants.h:519