11 #ifndef EIGEN_TRANSPOSE_H
12 #define EIGEN_TRANSPOSE_H
31 template<
typename MatrixType>
32 struct traits<Transpose<MatrixType> > :
public traits<MatrixType>
34 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
35 typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedPlain;
37 RowsAtCompileTime = MatrixType::ColsAtCompileTime,
38 ColsAtCompileTime = MatrixType::RowsAtCompileTime,
39 MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
40 MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
41 FlagsLvalueBit = is_lvalue<MatrixType>::value ?
LvalueBit : 0,
42 Flags0 = MatrixTypeNestedPlain::Flags & ~(
LvalueBit | NestByRefBit),
43 Flags1 = Flags0 | FlagsLvalueBit,
45 InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
46 OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
51 template<
typename MatrixType,
typename StorageKind>
class TransposeImpl;
54 :
public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
58 typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
60 typedef typename internal::remove_all<MatrixType>::type NestedExpression;
63 explicit inline Transpose(MatrixType& matrix) : m_matrix(matrix) {}
65 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
67 EIGEN_DEVICE_FUNC
inline Index rows()
const {
return m_matrix.cols(); }
68 EIGEN_DEVICE_FUNC
inline Index cols()
const {
return m_matrix.rows(); }
72 const typename internal::remove_all<typename MatrixType::Nested>::type&
77 typename internal::remove_all<typename MatrixType::Nested>::type&
81 typename MatrixType::Nested m_matrix;
86 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
87 struct TransposeImpl_base
89 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
92 template<
typename MatrixType>
93 struct TransposeImpl_base<MatrixType, false>
95 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
101 template<
typename XprType,
typename StorageKind>
103 :
public internal::generic_xpr_base<Transpose<XprType> >::type
106 typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
109 template<
typename MatrixType>
class TransposeImpl<MatrixType,Dense>
110 :
public internal::TransposeImpl_base<MatrixType>::type
114 typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
115 using Base::coeffRef;
116 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
117 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
119 EIGEN_DEVICE_FUNC inline Index innerStride()
const {
return derived().nestedExpression().innerStride(); }
120 EIGEN_DEVICE_FUNC
inline Index outerStride()
const {
return derived().nestedExpression().outerStride(); }
122 typedef typename internal::conditional<
123 internal::is_lvalue<MatrixType>::value,
126 >::type ScalarWithConstIfNotLvalue;
128 EIGEN_DEVICE_FUNC
inline ScalarWithConstIfNotLvalue* data() {
return derived().nestedExpression().data(); }
129 EIGEN_DEVICE_FUNC
inline const Scalar* data()
const {
return derived().nestedExpression().data(); }
133 inline const Scalar& coeffRef(Index rowId, Index colId)
const
135 return derived().nestedExpression().coeffRef(colId, rowId);
139 inline const Scalar& coeffRef(Index index)
const
141 return derived().nestedExpression().coeffRef(index);
164 template<
typename Derived>
165 inline Transpose<Derived>
176 template<
typename Derived>
202 template<
typename Derived>
206 return AdjointReturnType(this->transpose());
215 template<
typename MatrixType,
216 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic,
217 bool MatchPacketSize =
218 (
int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size))
220 struct inplace_transpose_selector;
222 template<
typename MatrixType>
223 struct inplace_transpose_selector<MatrixType,true,false> {
224 static void run(MatrixType& m) {
225 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
230 template<
typename MatrixType>
231 struct inplace_transpose_selector<MatrixType,true,true> {
232 static void run(MatrixType& m) {
233 typedef typename MatrixType::Scalar Scalar;
234 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
235 const Index PacketSize = internal::packet_traits<Scalar>::size;
236 const Index Alignment = internal::evaluator<MatrixType>::Alignment;
237 PacketBlock<Packet> A;
238 for (Index i=0; i<PacketSize; ++i)
239 A.packet[i] = m.template packetByOuterInner<Alignment>(i,0);
240 internal::ptranspose(A);
241 for (Index i=0; i<PacketSize; ++i)
242 m.template writePacket<Alignment>(m.rowIndexByOuterInner(i,0), m.colIndexByOuterInner(i,0), A.packet[i]);
246 template<
typename MatrixType,
bool MatchPacketSize>
247 struct inplace_transpose_selector<MatrixType,false,MatchPacketSize> {
248 static void run(MatrixType& m) {
249 if (m.rows()==m.cols())
250 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
252 m = m.transpose().eval();
277 template<
typename Derived>
280 eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
281 &&
"transposeInPlace() called on a non-square non-resizable matrix");
282 internal::inplace_transpose_selector<Derived>::run(derived());
308 template<
typename Derived>
311 derived() = adjoint().eval();
314 #ifndef EIGEN_NO_DEBUG
320 template<
bool DestIsTransposed,
typename OtherDerived>
321 struct check_transpose_aliasing_compile_time_selector
323 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
326 template<
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
327 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
329 enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
330 || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
334 template<
typename Scalar,
bool DestIsTransposed,
typename OtherDerived>
335 struct check_transpose_aliasing_run_time_selector
337 static bool run(
const Scalar* dest,
const OtherDerived& src)
339 return (
bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(
const Scalar*)extract_data(src));
343 template<
typename Scalar,
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
344 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
346 static bool run(
const Scalar* dest,
const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
348 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(
const Scalar*)extract_data(src.lhs())))
349 || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(
const Scalar*)extract_data(src.rhs())));
359 template<
typename Derived,
typename OtherDerived,
360 bool MightHaveTransposeAliasing
361 = check_transpose_aliasing_compile_time_selector
362 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
364 struct checkTransposeAliasing_impl
366 static void run(
const Derived& dst,
const OtherDerived& other)
368 eigen_assert((!check_transpose_aliasing_run_time_selector
369 <
typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
370 ::run(extract_data(dst), other))
371 &&
"aliasing detected during transposition, use transposeInPlace() "
372 "or evaluate the rhs into a temporary using .eval()");
377 template<
typename Derived,
typename OtherDerived>
378 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
380 static void run(
const Derived&,
const OtherDerived&)
385 template<
typename Dst,
typename Src>
386 void check_for_aliasing(
const Dst &dst,
const Src &src)
388 internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
393 #endif // EIGEN_NO_DEBUG
397 #endif // EIGEN_TRANSPOSE_H
Expression of the transpose of a matrix.
Definition: Transpose.h:53
const unsigned int LvalueBit
Definition: Constants.h:130
const unsigned int RowMajorBit
Definition: Constants.h:53
internal::remove_all< typename MatrixType::Nested >::type & nestedExpression()
Definition: Transpose.h:78
TransposeReturnType transpose()
Definition: Transpose.h:166
const unsigned int PacketAccessBit
Definition: Constants.h:80
void adjointInPlace()
Definition: Transpose.h:309
Definition: Eigen_Colamd.h:54
void transposeInPlace()
Definition: Transpose.h:278
const AdjointReturnType adjoint() const
Definition: Transpose.h:204
const internal::remove_all< typename MatrixType::Nested >::type & nestedExpression() const
Definition: Transpose.h:73
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48