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 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_TRANSPOSE_H
00027 #define EIGEN_TRANSPOSE_H
00028 
00043 namespace internal {
00044 template<typename MatrixType>
00045 struct traits<Transpose<MatrixType> > : traits<MatrixType>
00046 {
00047   typedef typename MatrixType::Scalar Scalar;
00048   typedef typename nested<MatrixType>::type MatrixTypeNested;
00049   typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedPlain;
00050   typedef typename traits<MatrixType>::StorageKind StorageKind;
00051   typedef typename traits<MatrixType>::XprKind XprKind;
00052   enum {
00053     RowsAtCompileTime = MatrixType::ColsAtCompileTime,
00054     ColsAtCompileTime = MatrixType::RowsAtCompileTime,
00055     MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
00056     MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
00057     FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
00058     Flags0 = MatrixTypeNestedPlain::Flags & ~(LvalueBit | NestByRefBit),
00059     Flags1 = Flags0 | FlagsLvalueBit,
00060     Flags = Flags1 ^ RowMajorBit,
00061     CoeffReadCost = MatrixTypeNestedPlain::CoeffReadCost,
00062     InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
00063     OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
00064   };
00065 };
00066 }
00067 
00068 template<typename MatrixType, typename StorageKind> class TransposeImpl;
00069 
00070 template<typename MatrixType> class Transpose
00071   : public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
00072 {
00073   public:
00074 
00075     typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
00076     EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose)
00077 
00078     inline Transpose(MatrixType& matrix) : m_matrix(matrix) {}
00079 
00080     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
00081 
00082     inline Index rows() const { return m_matrix.cols(); }
00083     inline Index cols() const { return m_matrix.rows(); }
00084 
00086     const typename internal::remove_all<typename MatrixType::Nested>::type&
00087     nestedExpression() const { return m_matrix; }
00088 
00090     typename internal::remove_all<typename MatrixType::Nested>::type&
00091     nestedExpression() { return m_matrix.const_cast_derived(); }
00092 
00093   protected:
00094     const typename MatrixType::Nested m_matrix;
00095 };
00096 
00097 namespace internal {
00098 
00099 template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
00100 struct TransposeImpl_base
00101 {
00102   typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
00103 };
00104 
00105 template<typename MatrixType>
00106 struct TransposeImpl_base<MatrixType, false>
00107 {
00108   typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
00109 };
00110 
00111 } // end namespace internal
00112 
00113 template<typename MatrixType> class TransposeImpl<MatrixType,Dense>
00114   : public internal::TransposeImpl_base<MatrixType>::type
00115 {
00116   public:
00117 
00118     typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
00119     EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
00120 
00121     inline Index innerStride() const { return derived().nestedExpression().innerStride(); }
00122     inline Index outerStride() const { return derived().nestedExpression().outerStride(); }
00123 
00124     typedef typename internal::conditional<
00125                        internal::is_lvalue<MatrixType>::value,
00126                        Scalar,
00127                        const Scalar
00128                      >::type ScalarWithConstIfNotLvalue;
00129 
00130     inline ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
00131     inline const Scalar* data() const { return derived().nestedExpression().data(); }
00132 
00133     inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
00134     {
00135       EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00136       return derived().nestedExpression().const_cast_derived().coeffRef(col, row);
00137     }
00138 
00139     inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
00140     {
00141       EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00142       return derived().nestedExpression().const_cast_derived().coeffRef(index);
00143     }
00144 
00145     inline const Scalar& coeffRef(Index row, Index col) const
00146     {
00147       return derived().nestedExpression().coeffRef(col, row);
00148     }
00149 
00150     inline const Scalar& coeffRef(Index index) const
00151     {
00152       return derived().nestedExpression().coeffRef(index);
00153     }
00154 
00155     inline const CoeffReturnType coeff(Index row, Index col) const
00156     {
00157       return derived().nestedExpression().coeff(col, row);
00158     }
00159 
00160     inline const CoeffReturnType coeff(Index index) const
00161     {
00162       return derived().nestedExpression().coeff(index);
00163     }
00164 
00165     template<int LoadMode>
00166     inline const PacketScalar packet(Index row, Index col) const
00167     {
00168       return derived().nestedExpression().template packet<LoadMode>(col, row);
00169     }
00170 
00171     template<int LoadMode>
00172     inline void writePacket(Index row, Index col, const PacketScalar& x)
00173     {
00174       derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(col, row, x);
00175     }
00176 
00177     template<int LoadMode>
00178     inline const PacketScalar packet(Index index) const
00179     {
00180       return derived().nestedExpression().template packet<LoadMode>(index);
00181     }
00182 
00183     template<int LoadMode>
00184     inline void writePacket(Index index, const PacketScalar& x)
00185     {
00186       derived().nestedExpression().const_cast_derived().template writePacket<LoadMode>(index, x);
00187     }
00188 };
00189 
00209 template<typename Derived>
00210 inline Transpose<Derived>
00211 DenseBase<Derived>::transpose()
00212 {
00213   return derived();
00214 }
00215 
00221 template<typename Derived>
00222 inline const typename DenseBase<Derived>::ConstTransposeReturnType
00223 DenseBase<Derived>::transpose() const
00224 {
00225   return ConstTransposeReturnType(derived());
00226 }
00227 
00247 template<typename Derived>
00248 inline const typename MatrixBase<Derived>::AdjointReturnType
00249 MatrixBase<Derived>::adjoint() const
00250 {
00251   return this->transpose(); // in the complex case, the .conjugate() is be implicit here
00252                             // due to implicit conversion to return type
00253 }
00254 
00255 /***************************************************************************
00256 * "in place" transpose implementation
00257 ***************************************************************************/
00258 
00259 namespace internal {
00260 
00261 template<typename MatrixType,
00262   bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic>
00263 struct inplace_transpose_selector;
00264 
00265 template<typename MatrixType>
00266 struct inplace_transpose_selector<MatrixType,true> { // square matrix
00267   static void run(MatrixType& m) {
00268     m.template triangularView<StrictlyUpper>().swap(m.transpose());
00269   }
00270 };
00271 
00272 template<typename MatrixType>
00273 struct inplace_transpose_selector<MatrixType,false> { // non square matrix
00274   static void run(MatrixType& m) {
00275     if (m.rows()==m.cols())
00276       m.template triangularView<StrictlyUpper>().swap(m.transpose());
00277     else
00278       m = m.transpose().eval();
00279   }
00280 };
00281 
00282 } // end namespace internal
00283 
00302 template<typename Derived>
00303 inline void DenseBase<Derived>::transposeInPlace()
00304 {
00305   internal::inplace_transpose_selector<Derived>::run(derived());
00306 }
00307 
00308 /***************************************************************************
00309 * "in place" adjoint implementation
00310 ***************************************************************************/
00311 
00330 template<typename Derived>
00331 inline void MatrixBase<Derived>::adjointInPlace()
00332 {
00333   derived() = adjoint().eval();
00334 }
00335 
00336 #ifndef EIGEN_NO_DEBUG
00337 
00338 // The following is to detect aliasing problems in most common cases.
00339 
00340 namespace internal {
00341 
00342 template<typename BinOp,typename NestedXpr,typename Rhs>
00343 struct blas_traits<SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> >
00344  : blas_traits<NestedXpr>
00345 {
00346   typedef SelfCwiseBinaryOp<BinOp,NestedXpr,Rhs> XprType;
00347   static inline const XprType extract(const XprType& x) { return x; }
00348 };
00349 
00350 template<bool DestIsTransposed, typename OtherDerived>
00351 struct check_transpose_aliasing_compile_time_selector
00352 {
00353   enum { ret = blas_traits<OtherDerived>::IsTransposed != DestIsTransposed
00354   };
00355 };
00356 
00357 template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
00358 struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
00359 {
00360   enum { ret =    blas_traits<DerivedA>::IsTransposed != DestIsTransposed
00361                || blas_traits<DerivedB>::IsTransposed != DestIsTransposed
00362   };
00363 };
00364 
00365 template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
00366 struct check_transpose_aliasing_run_time_selector
00367 {
00368   static bool run(const Scalar* dest, const OtherDerived& src)
00369   {
00370     return (blas_traits<OtherDerived>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src));
00371   }
00372 };
00373 
00374 template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
00375 struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
00376 {
00377   static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
00378   {
00379     return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src.lhs())))
00380         || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(Scalar*)extract_data(src.rhs())));
00381   }
00382 };
00383 
00384 // the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
00385 // is because when the condition controlling the assert is known at compile time, ICC emits a warning.
00386 // This is actually a good warning: in expressions that don't have any transposing, the condition is
00387 // known at compile time to be false, and using that, we can avoid generating the code of the assert again
00388 // and again for all these expressions that don't need it.
00389 
00390 template<typename Derived, typename OtherDerived,
00391          bool MightHaveTransposeAliasing
00392                  = check_transpose_aliasing_compile_time_selector
00393                      <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
00394         >
00395 struct checkTransposeAliasing_impl
00396 {
00397     static void run(const Derived& dst, const OtherDerived& other)
00398     {
00399         eigen_assert((!check_transpose_aliasing_run_time_selector
00400                       <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
00401                       ::run(extract_data(dst), other))
00402           && "aliasing detected during tranposition, use transposeInPlace() "
00403              "or evaluate the rhs into a temporary using .eval()");
00404 
00405     }
00406 };
00407 
00408 template<typename Derived, typename OtherDerived>
00409 struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
00410 {
00411     static void run(const Derived&, const OtherDerived&)
00412     {
00413     }
00414 };
00415 
00416 } // end namespace internal
00417 
00418 template<typename Derived>
00419 template<typename OtherDerived>
00420 void DenseBase<Derived>::checkTransposeAliasing(const OtherDerived& other) const
00421 {
00422     internal::checkTransposeAliasing_impl<Derived, OtherDerived>::run(derived(), other);
00423 }
00424 #endif
00425 
00426 #endif // EIGEN_TRANSPOSE_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:12