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
00291 template<typename Derived>
00292 inline void DenseBase<Derived>::transposeInPlace()
00293 {
00294 eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
00295 && "transposeInPlace() called on a non-square non-resizable matrix");
00296 internal::inplace_transpose_selector<Derived>::run(derived());
00297 }
00298
00299
00300
00301
00302
00322 template<typename Derived>
00323 inline void MatrixBase<Derived>::adjointInPlace()
00324 {
00325 derived() = adjoint().eval();
00326 }
00327
00328 #ifndef EIGEN_NO_DEBUG
00329
00330
00331
00332 namespace internal {
00333
00334 template<typename BinOp,typename NestedXpr,typename Rhs>
00335 struct blas_traits<SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> >
00336 : blas_traits<NestedXpr>
00337 {
00338 typedef SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> XprType;
00339 static inline const XprType extract(const XprType& x) { return x; }
00340 };
00341
00342 template<bool DestIsTransposed, typename OtherDerived>
00343 struct check_transpose_aliasing_compile_time_selector
00344 {
00345 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
00346 };
00347
00348 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
00349 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
00350 {
00351 enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
00352 || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
00353 };
00354 };
00355
00356 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
00357 struct check_transpose_aliasing_run_time_selector
00358 {
00359 static bool run(const Scalar* dest, const OtherDerived& src)
00360 {
00361 return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src));
00362 }
00363 };
00364
00365 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
00366 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
00367 {
00368 static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
00369 {
00370 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.lhs())))
00371 || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.rhs())));
00372 }
00373 };
00374
00375
00376
00377
00378
00379
00380
00381 template<typename Derived, typename OtherDerived,
00382 bool MightHaveTransposeAliasing
00383 = check_transpose_aliasing_compile_time_selector
00384 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
00385 >
00386 struct checkTransposeAliasing_impl
00387 {
00388 static void run(const Derived& dst, const OtherDerived& other)
00389 {
00390 eigen_assert((!check_transpose_aliasing_run_time_selector
00391 <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
00392 ::run(extract_data(dst), other))
00393 && "aliasing detected during transposition, use transposeInPlace() "
00394 "or evaluate the rhs into a temporary using .eval()");
00395
00396 }
00397 };
00398
00399 template<typename Derived, typename OtherDerived>
00400 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
00401 {
00402 static void run(const Derived&, const OtherDerived&)
00403 {
00404 }
00405 };
00406
00407 }
00408
00409 template<typename Derived>
00410 template<typename OtherDerived>
00411 void DenseBase<Derived>::checkTransposeAliasing(const OtherDerived& other) const
00412 {
00413 internal::checkTransposeAliasing_impl<Derived, OtherDerived>::run(derived(), other);
00414 }
00415 #endif
00416
00417 }
00418
00419 #endif // EIGEN_TRANSPOSE_H