11 #ifndef EIGEN_TRANSPOSE_H
12 #define EIGEN_TRANSPOSE_H
14 #include "./InternalHeaderCheck.h"
19 template<
typename MatrixType>
20 struct 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
39 template<
typename MatrixType,
typename StorageKind>
class TransposeImpl;
55 :
public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
59 typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
61 typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
63 typedef internal::remove_all_t<MatrixType> NestedExpression;
66 explicit EIGEN_STRONG_INLINE
Transpose(MatrixType& matrix) : m_matrix(matrix) {}
68 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Transpose)
70 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
71 Index rows()
const EIGEN_NOEXCEPT {
return m_matrix.cols(); }
72 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
73 Index cols()
const EIGEN_NOEXCEPT {
return m_matrix.rows(); }
76 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
77 const internal::remove_all_t<MatrixTypeNested>&
81 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
82 std::remove_reference_t<MatrixTypeNested>&
86 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
88 m_matrix.resize(ncols,nrows);
92 typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
97 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
98 struct TransposeImpl_base
100 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
103 template<
typename MatrixType>
104 struct TransposeImpl_base<MatrixType, false>
106 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
112 template<
typename XprType,
typename StorageKind>
114 :
public internal::generic_xpr_base<Transpose<XprType> >::type
117 typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
120 template<
typename MatrixType>
class TransposeImpl<MatrixType,Dense>
121 :
public internal::TransposeImpl_base<MatrixType>::type
125 typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
126 using Base::coeffRef;
127 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
128 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
130 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
131 Index innerStride()
const {
return derived().nestedExpression().innerStride(); }
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
133 Index outerStride()
const {
return derived().nestedExpression().outerStride(); }
135 typedef std::conditional_t<
136 internal::is_lvalue<MatrixType>::value,
139 > ScalarWithConstIfNotLvalue;
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
142 ScalarWithConstIfNotLvalue* data() {
return derived().nestedExpression().data(); }
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
144 const Scalar* data()
const {
return derived().nestedExpression().data(); }
147 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
148 const Scalar& coeffRef(
Index rowId,
Index colId)
const
150 return derived().nestedExpression().coeffRef(colId, rowId);
153 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
154 const Scalar& coeffRef(
Index index)
const
156 return derived().nestedExpression().coeffRef(index);
159 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(TransposeImpl)
181 template<
typename Derived>
182 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
183 typename DenseBase<Derived>::TransposeReturnType
194 template<
typename Derived>
195 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
221 template<
typename Derived>
225 return AdjointReturnType(this->transpose());
234 template<
typename MatrixType,
235 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=
Dynamic,
236 bool MatchPacketSize =
237 (
int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size))
239 struct inplace_transpose_selector;
241 template<
typename MatrixType>
242 struct inplace_transpose_selector<MatrixType,true,false> {
243 static void run(MatrixType& m) {
244 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose().template triangularView<StrictlyUpper>());
248 template<
typename MatrixType>
249 struct inplace_transpose_selector<MatrixType,true,true> {
250 static void run(MatrixType& m) {
251 typedef typename MatrixType::Scalar Scalar;
252 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
253 const Index PacketSize = internal::packet_traits<Scalar>::size;
254 const Index Alignment = internal::evaluator<MatrixType>::Alignment;
255 PacketBlock<Packet> A;
256 for (
Index i=0; i<PacketSize; ++i)
257 A.packet[i] = m.template packetByOuterInner<Alignment>(i,0);
258 internal::ptranspose(A);
259 for (
Index i=0; i<PacketSize; ++i)
260 m.template writePacket<Alignment>(m.rowIndexByOuterInner(i,0), m.colIndexByOuterInner(i,0), A.packet[i]);
265 template <
typename MatrixType, Index Alignment>
266 void BlockedInPlaceTranspose(MatrixType& m) {
267 typedef typename MatrixType::Scalar Scalar;
268 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
269 const Index PacketSize = internal::packet_traits<Scalar>::size;
270 eigen_assert(m.rows() == m.cols());
272 for (; row_start + PacketSize <= m.rows(); row_start += PacketSize) {
273 for (
int col_start = row_start; col_start + PacketSize <= m.cols(); col_start += PacketSize) {
274 PacketBlock<Packet> A;
275 if (row_start == col_start) {
276 for (
Index i=0; i<PacketSize; ++i)
277 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i,col_start);
278 internal::ptranspose(A);
279 for (
Index i=0; i<PacketSize; ++i)
280 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start), m.colIndexByOuterInner(row_start + i,col_start), A.packet[i]);
282 PacketBlock<Packet> B;
283 for (
Index i=0; i<PacketSize; ++i) {
284 A.packet[i] = m.template packetByOuterInner<Alignment>(row_start + i,col_start);
285 B.packet[i] = m.template packetByOuterInner<Alignment>(col_start + i, row_start);
287 internal::ptranspose(A);
288 internal::ptranspose(B);
289 for (
Index i=0; i<PacketSize; ++i) {
290 m.template writePacket<Alignment>(m.rowIndexByOuterInner(row_start + i, col_start), m.colIndexByOuterInner(row_start + i,col_start), B.packet[i]);
291 m.template writePacket<Alignment>(m.rowIndexByOuterInner(col_start + i, row_start), m.colIndexByOuterInner(col_start + i,row_start), A.packet[i]);
296 for (
Index row = row_start; row < m.rows(); ++row) {
297 m.matrix().row(row).head(row).swap(
298 m.matrix().col(row).head(row).transpose());
302 template<
typename MatrixType,
bool MatchPacketSize>
303 struct inplace_transpose_selector<MatrixType,false,MatchPacketSize> {
304 static void run(MatrixType& m) {
305 typedef typename MatrixType::Scalar Scalar;
306 if (m.rows() == m.cols()) {
307 const Index PacketSize = internal::packet_traits<Scalar>::size;
308 if (!NumTraits<Scalar>::IsComplex && m.rows() >= PacketSize) {
309 if ((m.rows() % PacketSize) == 0)
310 BlockedInPlaceTranspose<MatrixType,internal::evaluator<MatrixType>::Alignment>(m);
312 BlockedInPlaceTranspose<MatrixType,Unaligned>(m);
315 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose().template triangularView<StrictlyUpper>());
318 m = m.transpose().eval();
345 template<
typename Derived>
348 eigen_assert((rows() == cols() || (RowsAtCompileTime ==
Dynamic && ColsAtCompileTime ==
Dynamic))
349 &&
"transposeInPlace() called on a non-square non-resizable matrix");
350 internal::inplace_transpose_selector<Derived>::run(derived());
376 template<
typename Derived>
379 derived() = adjoint().eval();
382 #ifndef EIGEN_NO_DEBUG
388 template<
bool DestIsTransposed,
typename OtherDerived>
389 struct check_transpose_aliasing_compile_time_selector
391 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
394 template<
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
395 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
397 enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
398 || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
402 template<
typename Scalar,
bool DestIsTransposed,
typename OtherDerived>
403 struct check_transpose_aliasing_run_time_selector
405 static bool run(
const Scalar* dest,
const OtherDerived& src)
407 return (
bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(
const Scalar*)extract_data(src));
411 template<
typename Scalar,
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
412 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
414 static bool run(
const Scalar* dest,
const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
416 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(
const Scalar*)extract_data(src.lhs())))
417 || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(
const Scalar*)extract_data(src.rhs())));
427 template<
typename Derived,
typename OtherDerived,
428 bool MightHaveTransposeAliasing
429 = check_transpose_aliasing_compile_time_selector
430 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
432 struct checkTransposeAliasing_impl
434 static void run(
const Derived& dst,
const OtherDerived& other)
436 eigen_assert((!check_transpose_aliasing_run_time_selector
437 <
typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
438 ::run(extract_data(dst), other))
439 &&
"aliasing detected during transposition, use transposeInPlace() "
440 "or evaluate the rhs into a temporary using .eval()");
445 template<
typename Derived,
typename OtherDerived>
446 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
448 static void run(
const Derived&,
const OtherDerived&)
453 template<
typename Dst,
typename Src>
454 void check_for_aliasing(
const Dst &dst,
const Src &src)
456 if((!Dst::IsVectorAtCompileTime) && dst.rows()>1 && dst.cols()>1)
457 internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
TransposeReturnType transpose()
Definition: Transpose.h:184
void transposeInPlace()
Definition: Transpose.h:346
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
void adjointInPlace()
Definition: Transpose.h:377
const AdjointReturnType adjoint() const
Definition: Transpose.h:223
Expression of the transpose of a matrix.
Definition: Transpose.h:56
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition: Transpose.h:78
std::remove_reference_t< MatrixTypeNested > & nestedExpression()
Definition: Transpose.h:83
const unsigned int PacketAccessBit
Definition: Constants.h:96
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24