Transpose.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00006 //
00007 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
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& matrix) : m_matrix(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 } // end namespace internal
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 
00108     inline Index innerStride() const { return derived().nestedExpression().innerStride(); }
00109     inline Index outerStride() const { return derived().nestedExpression().outerStride(); }
00110 
00111     typedef typename internal::conditional<
00112                        internal::is_lvalue<MatrixType>::value,
00113                        Scalar,
00114                        const Scalar
00115                      >::type ScalarWithConstIfNotLvalue;
00116 
00117     inline ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
00118     inline const Scalar* data() const { return derived().nestedExpression().data(); }
00119 
00120     inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
00121     {
00122       EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00123       return derived().nestedExpression().const_cast_derived().coeffRef(col, row);
00124     }
00125 
00126     inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
00127     {
00128       EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00129       return derived().nestedExpression().const_cast_derived().coeffRef(index);
00130     }
00131 
00132     inline const Scalar& coeffRef(Index row, Index col) const
00133     {
00134       return derived().nestedExpression().coeffRef(col, row);
00135     }
00136 
00137     inline const Scalar& coeffRef(Index index) const
00138     {
00139       return derived().nestedExpression().coeffRef(index);
00140     }
00141 
00142     inline CoeffReturnType coeff(Index row, Index col) const
00143     {
00144       return derived().nestedExpression().coeff(col, row);
00145     }
00146 
00147     inline CoeffReturnType coeff(Index index) const
00148     {
00149       return derived().nestedExpression().coeff(index);
00150     }
00151 
00152     template<int LoadMode>
00153     inline const PacketScalar packet(Index row, Index col) const
00154     {
00155       return derived().nestedExpression().template packet<LoadMode>(col, row);
00156     }
00157 
00158     template<int LoadMode>
00159     inline void writePacket(Index row, Index col, const PacketScalar& x)
00160     {
00161       derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(col, row, x);
00162     }
00163 
00164     template<int LoadMode>
00165     inline const PacketScalar packet(Index index) const
00166     {
00167       return derived().nestedExpression().template packet<LoadMode>(index);
00168     }
00169 
00170     template<int LoadMode>
00171     inline void writePacket(Index index, const PacketScalar& x)
00172     {
00173       derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(index, x);
00174     }
00175 };
00176 
00196 template<typename Derived>
00197 inline Transpose<Derived>
00198 DenseBase<Derived>::transpose()
00199 {
00200   return derived();
00201 }
00202 
00208 template<typename Derived>
00209 inline const typename DenseBase<Derived>::ConstTransposeReturnType
00210 DenseBase<Derived>::transpose() const
00211 {
00212   return ConstTransposeReturnType(derived());
00213 }
00214 
00234 template<typename Derived>
00235 inline const typename MatrixBase<Derived>::AdjointReturnType
00236 MatrixBase<Derived>::adjoint() const
00237 {
00238   return this->transpose(); // in the complex case, the .conjugate() is be implicit here
00239                             // due to implicit conversion to return type
00240 }
00241 
00242 /***************************************************************************
00243 * "in place" transpose implementation
00244 ***************************************************************************/
00245 
00246 namespace internal {
00247 
00248 template<typename MatrixType,
00249   bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic>
00250 struct inplace_transpose_selector;
00251 
00252 template<typename MatrixType>
00253 struct inplace_transpose_selector<MatrixType,true> { // square matrix
00254   static void run(MatrixType& m) {
00255     m.template triangularView<StrictlyUpper>().swap(m.transpose());
00256   }
00257 };
00258 
00259 template<typename MatrixType>
00260 struct inplace_transpose_selector<MatrixType,false> { // non square matrix
00261   static void run(MatrixType& m) {
00262     if (m.rows()==m.cols())
00263       m.template triangularView<StrictlyUpper>().swap(m.transpose());
00264     else
00265       m = m.transpose().eval();
00266   }
00267 };
00268 
00269 } // end namespace internal
00270 
00289 template<typename Derived>
00290 inline void DenseBase<Derived>::transposeInPlace()
00291 {
00292   internal::inplace_transpose_selector<Derived>::run(derived());
00293 }
00294 
00295 /***************************************************************************
00296 * "in place" adjoint implementation
00297 ***************************************************************************/
00298 
00317 template<typename Derived>
00318 inline void MatrixBase<Derived>::adjointInPlace()
00319 {
00320   derived() = adjoint().eval();
00321 }
00322 
00323 #ifndef EIGEN_NO_DEBUG
00324 
00325 // The following is to detect aliasing problems in most common cases.
00326 
00327 namespace internal {
00328 
00329 template<typename BinOp,typename NestedXpr,typename Rhs>
00330 struct blas_traits<SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> >
00331  : blas_traits<NestedXpr>
00332 {
00333   typedef SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> XprType;
00334   static inline const XprType extract(const XprType& x) { return x; }
00335 };
00336 
00337 template<bool DestIsTransposed, typename OtherDerived>
00338 struct check_transpose_aliasing_compile_time_selector
00339 {
00340   enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
00341 };
00342 
00343 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
00344 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
00345 {
00346   enum { ret =    bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
00347                || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
00348   };
00349 };
00350 
00351 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
00352 struct check_transpose_aliasing_run_time_selector
00353 {
00354   static bool run(const Scalar* dest, const OtherDerived& src)
00355   {
00356     return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src));
00357   }
00358 };
00359 
00360 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
00361 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
00362 {
00363   static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
00364   {
00365     return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src.lhs())))
00366         || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src.rhs())));
00367   }
00368 };
00369 
00370 // the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
00371 // is because when the condition controlling the assert is known at compile time, ICC emits a warning.
00372 // This is actually a good warning: in expressions that don't have any transposing, the condition is
00373 // known at compile time to be false, and using that, we can avoid generating the code of the assert again
00374 // and again for all these expressions that don't need it.
00375 
00376 template<typename Derived, typename OtherDerived,
00377          bool MightHaveTransposeAliasing
00378                  = check_transpose_aliasing_compile_time_selector
00379                      <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
00380         >
00381 struct checkTransposeAliasing_impl
00382 {
00383     static void run(const Derived& dst, const OtherDerived& other)
00384     {
00385         eigen_assert((!check_transpose_aliasing_run_time_selector
00386                       <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
00387                       ::run(extract_data(dst), other))
00388           && "aliasing detected during tranposition, use transposeInPlace() "
00389              "or evaluate the rhs into a temporary using .eval()");
00390 
00391     }
00392 };
00393 
00394 template<typename Derived, typename OtherDerived>
00395 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
00396 {
00397     static void run(const Derived&, const OtherDerived&)
00398     {
00399     }
00400 };
00401 
00402 } // end namespace internal
00403 
00404 template<typename Derived>
00405 template<typename OtherDerived>
00406 void DenseBase<Derived>::checkTransposeAliasing(const OtherDerived& other) const
00407 {
00408     internal::checkTransposeAliasing_impl<Derived, OtherDerived>::run(derived(), other);
00409 }
00410 #endif
00411 
00412 } // end namespace Eigen
00413 
00414 #endif // EIGEN_TRANSPOSE_H


win_eigen
Author(s): Daniel Stonier
autogenerated on Wed Sep 16 2015 07:12:26