Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:18

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2010-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_TRANSPOSITIONS_H
0011 #define EIGEN_TRANSPOSITIONS_H
0012 
0013 namespace Eigen {
0014 
0015 template<typename Derived>
0016 class TranspositionsBase
0017 {
0018     typedef internal::traits<Derived> Traits;
0019 
0020   public:
0021 
0022     typedef typename Traits::IndicesType IndicesType;
0023     typedef typename IndicesType::Scalar StorageIndex;
0024     typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
0025 
0026     EIGEN_DEVICE_FUNC
0027     Derived& derived() { return *static_cast<Derived*>(this); }
0028     EIGEN_DEVICE_FUNC
0029     const Derived& derived() const { return *static_cast<const Derived*>(this); }
0030 
0031     /** Copies the \a other transpositions into \c *this */
0032     template<typename OtherDerived>
0033     Derived& operator=(const TranspositionsBase<OtherDerived>& other)
0034     {
0035       indices() = other.indices();
0036       return derived();
0037     }
0038 
0039     /** \returns the number of transpositions */
0040     EIGEN_DEVICE_FUNC
0041     Index size() const { return indices().size(); }
0042     /** \returns the number of rows of the equivalent permutation matrix */
0043     EIGEN_DEVICE_FUNC
0044     Index rows() const { return indices().size(); }
0045     /** \returns the number of columns of the equivalent permutation matrix */
0046     EIGEN_DEVICE_FUNC
0047     Index cols() const { return indices().size(); }
0048 
0049     /** Direct access to the underlying index vector */
0050     EIGEN_DEVICE_FUNC
0051     inline const StorageIndex& coeff(Index i) const { return indices().coeff(i); }
0052     /** Direct access to the underlying index vector */
0053     inline StorageIndex& coeffRef(Index i) { return indices().coeffRef(i); }
0054     /** Direct access to the underlying index vector */
0055     inline const StorageIndex& operator()(Index i) const { return indices()(i); }
0056     /** Direct access to the underlying index vector */
0057     inline StorageIndex& operator()(Index i) { return indices()(i); }
0058     /** Direct access to the underlying index vector */
0059     inline const StorageIndex& operator[](Index i) const { return indices()(i); }
0060     /** Direct access to the underlying index vector */
0061     inline StorageIndex& operator[](Index i) { return indices()(i); }
0062 
0063     /** const version of indices(). */
0064     EIGEN_DEVICE_FUNC
0065     const IndicesType& indices() const { return derived().indices(); }
0066     /** \returns a reference to the stored array representing the transpositions. */
0067     EIGEN_DEVICE_FUNC
0068     IndicesType& indices() { return derived().indices(); }
0069 
0070     /** Resizes to given size. */
0071     inline void resize(Index newSize)
0072     {
0073       indices().resize(newSize);
0074     }
0075 
0076     /** Sets \c *this to represents an identity transformation */
0077     void setIdentity()
0078     {
0079       for(StorageIndex i = 0; i < indices().size(); ++i)
0080         coeffRef(i) = i;
0081     }
0082 
0083     // FIXME: do we want such methods ?
0084     // might be useful when the target matrix expression is complex, e.g.:
0085     // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
0086     /*
0087     template<typename MatrixType>
0088     void applyForwardToRows(MatrixType& mat) const
0089     {
0090       for(Index k=0 ; k<size() ; ++k)
0091         if(m_indices(k)!=k)
0092           mat.row(k).swap(mat.row(m_indices(k)));
0093     }
0094 
0095     template<typename MatrixType>
0096     void applyBackwardToRows(MatrixType& mat) const
0097     {
0098       for(Index k=size()-1 ; k>=0 ; --k)
0099         if(m_indices(k)!=k)
0100           mat.row(k).swap(mat.row(m_indices(k)));
0101     }
0102     */
0103 
0104     /** \returns the inverse transformation */
0105     inline Transpose<TranspositionsBase> inverse() const
0106     { return Transpose<TranspositionsBase>(derived()); }
0107 
0108     /** \returns the tranpose transformation */
0109     inline Transpose<TranspositionsBase> transpose() const
0110     { return Transpose<TranspositionsBase>(derived()); }
0111 
0112   protected:
0113 };
0114 
0115 namespace internal {
0116 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
0117 struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
0118  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
0119 {
0120   typedef Matrix<_StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
0121   typedef TranspositionsStorage StorageKind;
0122 };
0123 }
0124 
0125 /** \class Transpositions
0126   * \ingroup Core_Module
0127   *
0128   * \brief Represents a sequence of transpositions (row/column interchange)
0129   *
0130   * \tparam SizeAtCompileTime the number of transpositions, or Dynamic
0131   * \tparam MaxSizeAtCompileTime the maximum number of transpositions, or Dynamic. This optional parameter defaults to SizeAtCompileTime. Most of the time, you should not have to specify it.
0132   *
0133   * This class represents a permutation transformation as a sequence of \em n transpositions
0134   * \f$[T_{n-1} \ldots T_{i} \ldots T_{0}]\f$. It is internally stored as a vector of integers \c indices.
0135   * Each transposition \f$ T_{i} \f$ applied on the left of a matrix (\f$ T_{i} M\f$) interchanges
0136   * the rows \c i and \c indices[i] of the matrix \c M.
0137   * A transposition applied on the right (e.g., \f$ M T_{i}\f$) yields a column interchange.
0138   *
0139   * Compared to the class PermutationMatrix, such a sequence of transpositions is what is
0140   * computed during a decomposition with pivoting, and it is faster when applying the permutation in-place.
0141   *
0142   * To apply a sequence of transpositions to a matrix, simply use the operator * as in the following example:
0143   * \code
0144   * Transpositions tr;
0145   * MatrixXf mat;
0146   * mat = tr * mat;
0147   * \endcode
0148   * In this example, we detect that the matrix appears on both side, and so the transpositions
0149   * are applied in-place without any temporary or extra copy.
0150   *
0151   * \sa class PermutationMatrix
0152   */
0153 
0154 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
0155 class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
0156 {
0157     typedef internal::traits<Transpositions> Traits;
0158   public:
0159 
0160     typedef TranspositionsBase<Transpositions> Base;
0161     typedef typename Traits::IndicesType IndicesType;
0162     typedef typename IndicesType::Scalar StorageIndex;
0163 
0164     inline Transpositions() {}
0165 
0166     /** Copy constructor. */
0167     template<typename OtherDerived>
0168     inline Transpositions(const TranspositionsBase<OtherDerived>& other)
0169       : m_indices(other.indices()) {}
0170 
0171     /** Generic constructor from expression of the transposition indices. */
0172     template<typename Other>
0173     explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices)
0174     {}
0175 
0176     /** Copies the \a other transpositions into \c *this */
0177     template<typename OtherDerived>
0178     Transpositions& operator=(const TranspositionsBase<OtherDerived>& other)
0179     {
0180       return Base::operator=(other);
0181     }
0182 
0183     /** Constructs an uninitialized permutation matrix of given size.
0184       */
0185     inline Transpositions(Index size) : m_indices(size)
0186     {}
0187 
0188     /** const version of indices(). */
0189     EIGEN_DEVICE_FUNC
0190     const IndicesType& indices() const { return m_indices; }
0191     /** \returns a reference to the stored array representing the transpositions. */
0192     EIGEN_DEVICE_FUNC
0193     IndicesType& indices() { return m_indices; }
0194 
0195   protected:
0196 
0197     IndicesType m_indices;
0198 };
0199 
0200 
0201 namespace internal {
0202 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
0203 struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,_PacketAccess> >
0204  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
0205 {
0206   typedef Map<const Matrix<_StorageIndex,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1>, _PacketAccess> IndicesType;
0207   typedef _StorageIndex StorageIndex;
0208   typedef TranspositionsStorage StorageKind;
0209 };
0210 }
0211 
0212 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int PacketAccess>
0213 class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess>
0214  : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess> >
0215 {
0216     typedef internal::traits<Map> Traits;
0217   public:
0218 
0219     typedef TranspositionsBase<Map> Base;
0220     typedef typename Traits::IndicesType IndicesType;
0221     typedef typename IndicesType::Scalar StorageIndex;
0222 
0223     explicit inline Map(const StorageIndex* indicesPtr)
0224       : m_indices(indicesPtr)
0225     {}
0226 
0227     inline Map(const StorageIndex* indicesPtr, Index size)
0228       : m_indices(indicesPtr,size)
0229     {}
0230 
0231     /** Copies the \a other transpositions into \c *this */
0232     template<typename OtherDerived>
0233     Map& operator=(const TranspositionsBase<OtherDerived>& other)
0234     {
0235       return Base::operator=(other);
0236     }
0237 
0238     #ifndef EIGEN_PARSED_BY_DOXYGEN
0239     /** This is a special case of the templated operator=. Its purpose is to
0240       * prevent a default operator= from hiding the templated operator=.
0241       */
0242     Map& operator=(const Map& other)
0243     {
0244       m_indices = other.m_indices;
0245       return *this;
0246     }
0247     #endif
0248 
0249     /** const version of indices(). */
0250     EIGEN_DEVICE_FUNC
0251     const IndicesType& indices() const { return m_indices; }
0252 
0253     /** \returns a reference to the stored array representing the transpositions. */
0254     EIGEN_DEVICE_FUNC
0255     IndicesType& indices() { return m_indices; }
0256 
0257   protected:
0258 
0259     IndicesType m_indices;
0260 };
0261 
0262 namespace internal {
0263 template<typename _IndicesType>
0264 struct traits<TranspositionsWrapper<_IndicesType> >
0265  : traits<PermutationWrapper<_IndicesType> >
0266 {
0267   typedef TranspositionsStorage StorageKind;
0268 };
0269 }
0270 
0271 template<typename _IndicesType>
0272 class TranspositionsWrapper
0273  : public TranspositionsBase<TranspositionsWrapper<_IndicesType> >
0274 {
0275     typedef internal::traits<TranspositionsWrapper> Traits;
0276   public:
0277 
0278     typedef TranspositionsBase<TranspositionsWrapper> Base;
0279     typedef typename Traits::IndicesType IndicesType;
0280     typedef typename IndicesType::Scalar StorageIndex;
0281 
0282     explicit inline TranspositionsWrapper(IndicesType& indices)
0283       : m_indices(indices)
0284     {}
0285 
0286     /** Copies the \a other transpositions into \c *this */
0287     template<typename OtherDerived>
0288     TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other)
0289     {
0290       return Base::operator=(other);
0291     }
0292 
0293     /** const version of indices(). */
0294     EIGEN_DEVICE_FUNC
0295     const IndicesType& indices() const { return m_indices; }
0296 
0297     /** \returns a reference to the stored array representing the transpositions. */
0298     EIGEN_DEVICE_FUNC
0299     IndicesType& indices() { return m_indices; }
0300 
0301   protected:
0302 
0303     typename IndicesType::Nested m_indices;
0304 };
0305 
0306 
0307 
0308 /** \returns the \a matrix with the \a transpositions applied to the columns.
0309   */
0310 template<typename MatrixDerived, typename TranspositionsDerived>
0311 EIGEN_DEVICE_FUNC
0312 const Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
0313 operator*(const MatrixBase<MatrixDerived> &matrix,
0314           const TranspositionsBase<TranspositionsDerived>& transpositions)
0315 {
0316   return Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
0317             (matrix.derived(), transpositions.derived());
0318 }
0319 
0320 /** \returns the \a matrix with the \a transpositions applied to the rows.
0321   */
0322 template<typename TranspositionsDerived, typename MatrixDerived>
0323 EIGEN_DEVICE_FUNC
0324 const Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
0325 operator*(const TranspositionsBase<TranspositionsDerived> &transpositions,
0326           const MatrixBase<MatrixDerived>& matrix)
0327 {
0328   return Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
0329             (transpositions.derived(), matrix.derived());
0330 }
0331 
0332 // Template partial specialization for transposed/inverse transpositions
0333 
0334 namespace internal {
0335 
0336 template<typename Derived>
0337 struct traits<Transpose<TranspositionsBase<Derived> > >
0338  : traits<Derived>
0339 {};
0340 
0341 } // end namespace internal
0342 
0343 template<typename TranspositionsDerived>
0344 class Transpose<TranspositionsBase<TranspositionsDerived> >
0345 {
0346     typedef TranspositionsDerived TranspositionType;
0347     typedef typename TranspositionType::IndicesType IndicesType;
0348   public:
0349 
0350     explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
0351 
0352     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0353     Index size() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
0354     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0355     Index rows() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
0356     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0357     Index cols() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
0358 
0359     /** \returns the \a matrix with the inverse transpositions applied to the columns.
0360       */
0361     template<typename OtherDerived> friend
0362     const Product<OtherDerived, Transpose, AliasFreeProduct>
0363     operator*(const MatrixBase<OtherDerived>& matrix, const Transpose& trt)
0364     {
0365       return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
0366     }
0367 
0368     /** \returns the \a matrix with the inverse transpositions applied to the rows.
0369       */
0370     template<typename OtherDerived>
0371     const Product<Transpose, OtherDerived, AliasFreeProduct>
0372     operator*(const MatrixBase<OtherDerived>& matrix) const
0373     {
0374       return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
0375     }
0376 
0377     EIGEN_DEVICE_FUNC
0378     const TranspositionType& nestedExpression() const { return m_transpositions; }
0379 
0380   protected:
0381     const TranspositionType& m_transpositions;
0382 };
0383 
0384 } // end namespace Eigen
0385 
0386 #endif // EIGEN_TRANSPOSITIONS_H