11 #ifndef EIGEN_TRANSPOSE_H
12 #define EIGEN_TRANSPOSE_H
17 template<
typename MatrixType>
18 struct traits<Transpose<MatrixType> > :
public traits<MatrixType>
20 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
21 typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedPlain;
23 RowsAtCompileTime = MatrixType::ColsAtCompileTime,
24 ColsAtCompileTime = MatrixType::RowsAtCompileTime,
25 MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
26 MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
27 FlagsLvalueBit = is_lvalue<MatrixType>::value ?
LvalueBit : 0,
28 Flags0 = traits<MatrixTypeNestedPlain>::Flags & ~(
LvalueBit | NestByRefBit),
29 Flags1 = Flags0 | FlagsLvalueBit,
31 InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
32 OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
37 template<
typename MatrixType,
typename StorageKind>
class TransposeImpl;
53 :
public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
57 typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
59 typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
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>
96 struct TransposeImpl_base
98 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
101 template<
typename MatrixType>
102 struct TransposeImpl_base<MatrixType, false>
104 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
110 template<
typename XprType,
typename StorageKind>
112 :
public internal::generic_xpr_base<Transpose<XprType> >::type
115 typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
118 template<
typename MatrixType>
class TransposeImpl<MatrixType,Dense>
119 :
public internal::TransposeImpl_base<MatrixType>::type
123 typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
124 using Base::coeffRef;
125 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
126 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
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(); }
133 typedef typename internal::conditional<
134 internal::is_lvalue<MatrixType>::value,
137 >::type ScalarWithConstIfNotLvalue;
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
140 ScalarWithConstIfNotLvalue* data() {
return derived().nestedExpression().data(); }
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>
223 return AdjointReturnType(this->transpose());
232 template<
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))
237 struct inplace_transpose_selector;
239 template<
typename MatrixType>
240 struct inplace_transpose_selector<MatrixType,true,false> {
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;
251 const Index PacketSize = internal::packet_traits<Scalar>::size;
252 const Index Alignment = internal::evaluator<MatrixType>::Alignment;
253 PacketBlock<Packet> A;
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;
267 const Index PacketSize = internal::packet_traits<Scalar>::size;
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()) {
305 const Index PacketSize = internal::packet_traits<Scalar>::size;
306 if (!NumTraits<Scalar>::IsComplex && m.rows() >= PacketSize) {
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");
348 internal::inplace_transpose_selector<Derived>::run(derived());
374 template<
typename Derived>
377 derived() = adjoint().eval();
380 #ifndef EIGEN_NO_DEBUG
386 template<
bool DestIsTransposed,
typename OtherDerived>
387 struct check_transpose_aliasing_compile_time_selector
389 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
392 template<
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
393 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
395 enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
396 || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
400 template<
typename Scalar,
bool DestIsTransposed,
typename OtherDerived>
401 struct check_transpose_aliasing_run_time_selector
403 static bool run(
const Scalar* dest,
const OtherDerived& src)
405 return (
bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(
const Scalar*)extract_data(src));
409 template<
typename Scalar,
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
410 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
412 static bool run(
const Scalar* dest,
const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
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())));
425 template<
typename Derived,
typename OtherDerived,
426 bool MightHaveTransposeAliasing
427 = check_transpose_aliasing_compile_time_selector
428 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
430 struct checkTransposeAliasing_impl
432 static void run(
const Derived& dst,
const OtherDerived& other)
434 eigen_assert((!check_transpose_aliasing_run_time_selector
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()");
443 template<
typename Derived,
typename OtherDerived>
444 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
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)
455 internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
void transposeInPlace()
Definition: Transpose.h:344
TransposeReturnType transpose()
Definition: Transpose.h:182
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:65
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
void adjointInPlace()
Definition: Transpose.h:375
const AdjointReturnType adjoint() const
Definition: Transpose.h:221
Expression of the transpose of a matrix.
Definition: Transpose.h:54
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: Transpose.h:76
internal::remove_reference< MatrixTypeNested >::type & nestedExpression()
Definition: Transpose.h:81
const unsigned int PacketAccessBit
Definition: Constants.h:94
const unsigned int LvalueBit
Definition: Constants.h:144
const unsigned int RowMajorBit
Definition: Constants.h:66
Namespace containing all symbols from the Eigen library.
Definition: Core:134
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
Definition: Constants.h:22