11 #ifndef EIGEN_TRANSPOSE_H
12 #define EIGEN_TRANSPOSE_H
31 template<
typename MatrixType>
32 struct traits<Transpose<MatrixType> > : traits<MatrixType>
34 typedef typename MatrixType::Scalar Scalar;
35 typedef typename nested<MatrixType>::type MatrixTypeNested;
36 typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedPlain;
37 typedef typename traits<MatrixType>::StorageKind StorageKind;
38 typedef typename traits<MatrixType>::XprKind XprKind;
40 RowsAtCompileTime = MatrixType::ColsAtCompileTime,
41 ColsAtCompileTime = MatrixType::RowsAtCompileTime,
42 MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
43 MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
44 FlagsLvalueBit = is_lvalue<MatrixType>::value ?
LvalueBit : 0,
45 Flags0 = MatrixTypeNestedPlain::Flags & ~(
LvalueBit | NestByRefBit),
46 Flags1 = Flags0 | FlagsLvalueBit,
48 CoeffReadCost = MatrixTypeNestedPlain::CoeffReadCost,
49 InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
50 OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
55 template<
typename MatrixType,
typename StorageKind>
class TransposeImpl;
58 :
public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
62 typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
65 inline Transpose(MatrixType& matrix) : m_matrix(matrix) {}
67 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Transpose)
69 inline Index rows()
const {
return m_matrix.cols(); }
70 inline Index cols()
const {
return m_matrix.rows(); }
73 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;
100 template<
typename MatrixType>
class TransposeImpl<MatrixType,Dense>
101 :
public internal::TransposeImpl_base<MatrixType>::type
105 typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
106 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
108 inline Index innerStride()
const {
return derived().nestedExpression().innerStride(); }
109 inline Index outerStride()
const {
return derived().nestedExpression().outerStride(); }
111 typedef typename internal::conditional<
112 internal::is_lvalue<MatrixType>::value,
115 >::type ScalarWithConstIfNotLvalue;
117 inline ScalarWithConstIfNotLvalue* data() {
return derived().nestedExpression().data(); }
118 inline const Scalar* data()
const {
return derived().nestedExpression().data(); }
120 inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
122 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
123 return derived().nestedExpression().const_cast_derived().coeffRef(col, row);
126 inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
128 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
129 return derived().nestedExpression().const_cast_derived().coeffRef(index);
132 inline const Scalar& coeffRef(Index row, Index col)
const
134 return derived().nestedExpression().coeffRef(col, row);
137 inline const Scalar& coeffRef(Index index)
const
139 return derived().nestedExpression().coeffRef(index);
142 inline CoeffReturnType coeff(Index row, Index col)
const
144 return derived().nestedExpression().coeff(col, row);
147 inline CoeffReturnType coeff(Index index)
const
149 return derived().nestedExpression().coeff(index);
152 template<
int LoadMode>
153 inline const PacketScalar packet(Index row, Index col)
const
155 return derived().nestedExpression().template packet<LoadMode>(col, row);
158 template<
int LoadMode>
159 inline void writePacket(Index row, Index col,
const PacketScalar& x)
161 derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(col, row, x);
164 template<
int LoadMode>
165 inline const PacketScalar packet(Index index)
const
167 return derived().nestedExpression().template packet<LoadMode>(index);
170 template<
int LoadMode>
171 inline void writePacket(Index index,
const PacketScalar& x)
173 derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(index, x);
196 template<
typename Derived>
197 inline Transpose<Derived>
208 template<
typename Derived>
234 template<
typename Derived>
238 return this->transpose();
248 template<
typename MatrixType,
249 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=
Dynamic>
250 struct inplace_transpose_selector;
252 template<
typename MatrixType>
253 struct inplace_transpose_selector<MatrixType,true> {
254 static void run(MatrixType& m) {
255 m.template triangularView<StrictlyUpper>().swap(m.transpose());
259 template<
typename MatrixType>
260 struct inplace_transpose_selector<MatrixType,false> {
261 static void run(MatrixType& m) {
262 if (m.rows()==m.cols())
263 m.template triangularView<StrictlyUpper>().swap(m.transpose());
265 m = m.transpose().eval();
289 template<
typename Derived>
292 internal::inplace_transpose_selector<Derived>::run(derived());
317 template<
typename Derived>
320 derived() = adjoint().eval();
323 #ifndef EIGEN_NO_DEBUG
329 template<
typename BinOp,
typename NestedXpr,
typename Rhs>
330 struct blas_traits<SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> >
331 : blas_traits<NestedXpr>
333 typedef SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> XprType;
334 static inline const XprType extract(
const XprType& x) {
return x; }
337 template<
bool DestIsTransposed,
typename OtherDerived>
338 struct check_transpose_aliasing_compile_time_selector
340 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
343 template<
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
344 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
346 enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
347 || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
351 template<
typename Scalar,
bool DestIsTransposed,
typename OtherDerived>
352 struct check_transpose_aliasing_run_time_selector
354 static bool run(
const Scalar* dest,
const OtherDerived& src)
356 return (
bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src));
360 template<
typename Scalar,
bool DestIsTransposed,
typename BinOp,
typename DerivedA,
typename DerivedB>
361 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
363 static bool run(
const Scalar* dest,
const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
365 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src.lhs())))
366 || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src.rhs())));
376 template<
typename Derived,
typename OtherDerived,
377 bool MightHaveTransposeAliasing
378 = check_transpose_aliasing_compile_time_selector
379 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
381 struct checkTransposeAliasing_impl
383 static void run(
const Derived& dst,
const OtherDerived& other)
385 eigen_assert((!check_transpose_aliasing_run_time_selector
386 <
typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
387 ::run(extract_data(dst), other))
388 &&
"aliasing detected during tranposition, use transposeInPlace() "
389 "or evaluate the rhs into a temporary using .eval()");
394 template<
typename Derived,
typename OtherDerived>
395 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
397 static void run(
const Derived&,
const OtherDerived&)
404 template<
typename Derived>
405 template<
typename OtherDerived>
406 void DenseBase<Derived>::checkTransposeAliasing(
const OtherDerived& other)
const
408 internal::checkTransposeAliasing_impl<Derived, OtherDerived>::run(derived(), other);
414 #endif // EIGEN_TRANSPOSE_H