11 #ifndef EIGEN_TRANSPOSE_H
12 #define EIGEN_TRANSPOSE_H
17 template<
typename MatrixType>
21 typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedPlain;
23 RowsAtCompileTime = MatrixType::ColsAtCompileTime,
24 ColsAtCompileTime = MatrixType::RowsAtCompileTime,
25 MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
26 MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
29 Flags1 = Flags0 | FlagsLvalueBit,
37 template<
typename MatrixType,
typename StorageKind>
class TransposeImpl;
53 :
public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
61 typedef typename internal::remove_all<MatrixType>::type NestedExpression;
64 explicit EIGEN_STRONG_INLINE
Transpose(MatrixType& matrix) : m_matrix(matrix) {}
66 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Transpose)
68 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
69 Index rows()
const {
return m_matrix.cols(); }
70 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
71 Index cols()
const {
return m_matrix.rows(); }
74 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
75 const typename internal::remove_all<MatrixTypeNested>::type&
79 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
80 typename internal::remove_reference<MatrixTypeNested>::type&
84 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
86 m_matrix.resize(ncols,nrows);
90 typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
95 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
101 template<
typename MatrixType>
110 template<
typename XprType,
typename StorageKind>
123 typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
124 using Base::coeffRef;
128 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
129 Index innerStride()
const {
return derived().nestedExpression().innerStride(); }
130 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
131 Index outerStride()
const {
return derived().nestedExpression().outerStride(); }
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
142 const Scalar* data()
const {
return derived().nestedExpression().data(); }
145 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
146 const Scalar& coeffRef(
Index rowId,
Index colId)
const
148 return derived().nestedExpression().coeffRef(colId, rowId);
151 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
152 const Scalar& coeffRef(
Index index)
const
154 return derived().nestedExpression().coeffRef(index);
157 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(
TransposeImpl)
179 template<
typename Derived>
180 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
192 template<
typename Derived>
193 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
219 template<
typename Derived>
232 template<
typename MatrixType,
233 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=
Dynamic,
234 bool MatchPacketSize =
237 struct inplace_transpose_selector;
239 template<
typename MatrixType>
241 static void run(MatrixType& m) {
242 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose().template triangularView<StrictlyUpper>());
246 template<
typename MatrixType>
247 struct inplace_transpose_selector<MatrixType,true,true> {
248 static void run(MatrixType& m) {
249 typedef typename MatrixType::Scalar Scalar;
250 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type
Packet;
254 for (
Index i=0; i<PacketSize; ++i)
255 A.packet[i] = m.template packetByOuterInner<Alignment>(i,0);
256 internal::ptranspose(A);
257 for (
Index i=0; i<PacketSize; ++i)
258 m.template writePacket<Alignment>(m.rowIndexByOuterInner(i,0), m.colIndexByOuterInner(i,0), A.packet[i]);
263 template <
typename MatrixType, Index Alignment>
264 void BlockedInPlaceTranspose(MatrixType& m) {
265 typedef typename MatrixType::Scalar Scalar;
266 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
268 eigen_assert(m.rows() == m.cols());
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) {
272 PacketBlock<Packet> A;
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);
276 internal::ptranspose(A);
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]);
280 PacketBlock<Packet> B;
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);
285 internal::ptranspose(A);
286 internal::ptranspose(B);
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]);
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());
300 template<
typename MatrixType,
bool MatchPacketSize>
301 struct inplace_transpose_selector<MatrixType,false,MatchPacketSize> {
302 static void run(MatrixType& m) {
303 typedef typename MatrixType::Scalar Scalar;
304 if (m.rows() == m.cols()) {
307 if ((m.rows() % PacketSize) == 0)
308 BlockedInPlaceTranspose<MatrixType,internal::evaluator<MatrixType>::Alignment>(m);
310 BlockedInPlaceTranspose<MatrixType,Unaligned>(m);
313 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose().template triangularView<StrictlyUpper>());
316 m = m.transpose().eval();
343 template<
typename Derived>
346 eigen_assert((rows() == cols() || (RowsAtCompileTime ==
Dynamic && ColsAtCompileTime ==
Dynamic))
347 &&
"transposeInPlace() called on a non-square non-resizable matrix");
374 template<
typename Derived>
377 derived() = adjoint().eval();
380 #ifndef EIGEN_NO_DEBUG
386 template<
bool DestIsTransposed,
typename OtherDerived>
392 template<
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
400 template<
typename Scalar,
bool DestIsTransposed,
typename OtherDerived>
403 static bool run(
const Scalar* dest,
const OtherDerived& src)
409 template<
typename Scalar,
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
425 template<
typename Derived,
typename OtherDerived,
426 bool MightHaveTransposeAliasing
432 static void run(
const Derived& dst,
const OtherDerived& other)
436 ::run(extract_data(dst), other))
437 &&
"aliasing detected during transposition, use transposeInPlace() "
438 "or evaluate the rhs into a temporary using .eval()");
443 template<
typename Derived,
typename OtherDerived>
446 static void run(
const Derived&,
const OtherDerived&)
451 template<
typename Dst,
typename Src>
452 void check_for_aliasing(
const Dst &dst,
const Src &src)
454 if((!Dst::IsVectorAtCompileTime) && dst.rows()>1 && dst.cols()>1)
460 #endif // EIGEN_NO_DEBUG
464 #endif // EIGEN_TRANSPOSE_H