00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef EIGEN_TRANSPOSE_H
00012 #define EIGEN_TRANSPOSE_H
00013
00014 namespace Eigen {
00015
00030 namespace internal {
00031 template<typename MatrixType>
00032 struct traits<Transpose<MatrixType> > : traits<MatrixType>
00033 {
00034 typedef typename MatrixType::Scalar Scalar;
00035 typedef typename nested<MatrixType>::type MatrixTypeNested;
00036 typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedPlain;
00037 typedef typename traits<MatrixType>::StorageKind StorageKind;
00038 typedef typename traits<MatrixType>::XprKind XprKind;
00039 enum {
00040 RowsAtCompileTime = MatrixType::ColsAtCompileTime,
00041 ColsAtCompileTime = MatrixType::RowsAtCompileTime,
00042 MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
00043 MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
00044 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
00045 Flags0 = MatrixTypeNestedPlain::Flags & ~(LvalueBit | NestByRefBit),
00046 Flags1 = Flags0 | FlagsLvalueBit,
00047 Flags = Flags1 ^ RowMajorBit,
00048 CoeffReadCost = MatrixTypeNestedPlain::CoeffReadCost,
00049 InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
00050 OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
00051 };
00052 };
00053 }
00054
00055 template<typename MatrixType, typename StorageKind> class TransposeImpl;
00056
00057 template<typename MatrixType> class Transpose
00058 : public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
00059 {
00060 public:
00061
00062 typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
00063 EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose)
00064
00065 inline Transpose(MatrixType& a_matrix) : m_matrix(a_matrix) {}
00066
00067 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
00068
00069 inline Index rows() const { return m_matrix.cols(); }
00070 inline Index cols() const { return m_matrix.rows(); }
00071
00073 const typename internal::remove_all<typename MatrixType::Nested>::type&
00074 nestedExpression() const { return m_matrix; }
00075
00077 typename internal::remove_all<typename MatrixType::Nested>::type&
00078 nestedExpression() { return m_matrix.const_cast_derived(); }
00079
00080 protected:
00081 typename MatrixType::Nested m_matrix;
00082 };
00083
00084 namespace internal {
00085
00086 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
00087 struct TransposeImpl_base
00088 {
00089 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
00090 };
00091
00092 template<typename MatrixType>
00093 struct TransposeImpl_base<MatrixType, false>
00094 {
00095 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
00096 };
00097
00098 }
00099
00100 template<typename MatrixType> class TransposeImpl<MatrixType,Dense>
00101 : public internal::TransposeImpl_base<MatrixType>::type
00102 {
00103 public:
00104
00105 typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
00106 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
00107 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
00108
00109 inline Index innerStride() const { return derived().nestedExpression().innerStride(); }
00110 inline Index outerStride() const { return derived().nestedExpression().outerStride(); }
00111
00112 typedef typename internal::conditional<
00113 internal::is_lvalue<MatrixType>::value,
00114 Scalar,
00115 const Scalar
00116 >::type ScalarWithConstIfNotLvalue;
00117
00118 inline ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
00119 inline const Scalar* data() const { return derived().nestedExpression().data(); }
00120
00121 inline ScalarWithConstIfNotLvalue& coeffRef(Index rowId, Index colId)
00122 {
00123 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00124 return derived().nestedExpression().const_cast_derived().coeffRef(colId, rowId);
00125 }
00126
00127 inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
00128 {
00129 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00130 return derived().nestedExpression().const_cast_derived().coeffRef(index);
00131 }
00132
00133 inline const Scalar& coeffRef(Index rowId, Index colId) const
00134 {
00135 return derived().nestedExpression().coeffRef(colId, rowId);
00136 }
00137
00138 inline const Scalar& coeffRef(Index index) const
00139 {
00140 return derived().nestedExpression().coeffRef(index);
00141 }
00142
00143 inline CoeffReturnType coeff(Index rowId, Index colId) const
00144 {
00145 return derived().nestedExpression().coeff(colId, rowId);
00146 }
00147
00148 inline CoeffReturnType coeff(Index index) const
00149 {
00150 return derived().nestedExpression().coeff(index);
00151 }
00152
00153 template<int LoadMode>
00154 inline const PacketScalar packet(Index rowId, Index colId) const
00155 {
00156 return derived().nestedExpression().template packet<LoadMode>(colId, rowId);
00157 }
00158
00159 template<int LoadMode>
00160 inline void writePacket(Index rowId, Index colId, const PacketScalar& x)
00161 {
00162 derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(colId, rowId, x);
00163 }
00164
00165 template<int LoadMode>
00166 inline const PacketScalar packet(Index index) const
00167 {
00168 return derived().nestedExpression().template packet<LoadMode>(index);
00169 }
00170
00171 template<int LoadMode>
00172 inline void writePacket(Index index, const PacketScalar& x)
00173 {
00174 derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(index, x);
00175 }
00176 };
00177
00197 template<typename Derived>
00198 inline Transpose<Derived>
00199 DenseBase<Derived>::transpose()
00200 {
00201 return derived();
00202 }
00203
00209 template<typename Derived>
00210 inline typename DenseBase<Derived>::ConstTransposeReturnType
00211 DenseBase<Derived>::transpose() const
00212 {
00213 return ConstTransposeReturnType(derived());
00214 }
00215
00235 template<typename Derived>
00236 inline const typename MatrixBase<Derived>::AdjointReturnType
00237 MatrixBase<Derived>::adjoint() const
00238 {
00239 return this->transpose();
00240
00241 }
00242
00243
00244
00245
00246
00247 namespace internal {
00248
00249 template<typename MatrixType,
00250 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic>
00251 struct inplace_transpose_selector;
00252
00253 template<typename MatrixType>
00254 struct inplace_transpose_selector<MatrixType,true> {
00255 static void run(MatrixType& m) {
00256 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
00257 }
00258 };
00259
00260 template<typename MatrixType>
00261 struct inplace_transpose_selector<MatrixType,false> {
00262 static void run(MatrixType& m) {
00263 if (m.rows()==m.cols())
00264 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
00265 else
00266 m = m.transpose().eval();
00267 }
00268 };
00269
00270 }
00271
00290 template<typename Derived>
00291 inline void DenseBase<Derived>::transposeInPlace()
00292 {
00293 eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
00294 && "transposeInPlace() called on a non-square non-resizable matrix");
00295 internal::inplace_transpose_selector<Derived>::run(derived());
00296 }
00297
00298
00299
00300
00301
00320 template<typename Derived>
00321 inline void MatrixBase<Derived>::adjointInPlace()
00322 {
00323 derived() = adjoint().eval();
00324 }
00325
00326 #ifndef EIGEN_NO_DEBUG
00327
00328
00329
00330 namespace internal {
00331
00332 template<typename BinOp,typename NestedXpr,typename Rhs>
00333 struct blas_traits<SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> >
00334 : blas_traits<NestedXpr>
00335 {
00336 typedef SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> XprType;
00337 static inline const XprType extract(const XprType& x) { return x; }
00338 };
00339
00340 template<bool DestIsTransposed, typename OtherDerived>
00341 struct check_transpose_aliasing_compile_time_selector
00342 {
00343 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
00344 };
00345
00346 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
00347 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
00348 {
00349 enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
00350 || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
00351 };
00352 };
00353
00354 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
00355 struct check_transpose_aliasing_run_time_selector
00356 {
00357 static bool run(const Scalar* dest, const OtherDerived& src)
00358 {
00359 return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src));
00360 }
00361 };
00362
00363 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
00364 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
00365 {
00366 static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
00367 {
00368 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.lhs())))
00369 || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.rhs())));
00370 }
00371 };
00372
00373
00374
00375
00376
00377
00378
00379 template<typename Derived, typename OtherDerived,
00380 bool MightHaveTransposeAliasing
00381 = check_transpose_aliasing_compile_time_selector
00382 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
00383 >
00384 struct checkTransposeAliasing_impl
00385 {
00386 static void run(const Derived& dst, const OtherDerived& other)
00387 {
00388 eigen_assert((!check_transpose_aliasing_run_time_selector
00389 <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
00390 ::run(extract_data(dst), other))
00391 && "aliasing detected during transposition, use transposeInPlace() "
00392 "or evaluate the rhs into a temporary using .eval()");
00393
00394 }
00395 };
00396
00397 template<typename Derived, typename OtherDerived>
00398 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
00399 {
00400 static void run(const Derived&, const OtherDerived&)
00401 {
00402 }
00403 };
00404
00405 }
00406
00407 template<typename Derived>
00408 template<typename OtherDerived>
00409 void DenseBase<Derived>::checkTransposeAliasing(const OtherDerived& other) const
00410 {
00411 internal::checkTransposeAliasing_impl<Derived, OtherDerived>::run(derived(), other);
00412 }
00413 #endif
00414
00415 }
00416
00417 #endif // EIGEN_TRANSPOSE_H