// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2009 Benoit Jacob // Copyright (C) 2009 Gael Guennebaud // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . #ifndef EIGEN_PERMUTATIONMATRIX_H #define EIGEN_PERMUTATIONMATRIX_H /** \class PermutationMatrix * \ingroup Core_Module * * \brief Permutation matrix * * \param SizeAtCompileTime the number of rows/cols, or Dynamic * \param MaxSizeAtCompileTime the maximum number of rows/cols, or Dynamic. This optional parameter defaults to SizeAtCompileTime. Most of the time, you should not have to specify it. * * This class represents a permutation matrix, internally stored as a vector of integers. * The convention followed here is that if \f$ \sigma \f$ is a permutation, the corresponding permutation matrix * \f$ P_\sigma \f$ is such that if \f$ (e_1,\ldots,e_p) \f$ is the canonical basis, we have: * \f[ P_\sigma(e_i) = e_{\sigma(i)}. \f] * This convention ensures that for any two permutations \f$ \sigma, \tau \f$, we have: * \f[ P_{\sigma\circ\tau} = P_\sigma P_\tau. \f] * * Permutation matrices are square and invertible. * * Notice that in addition to the member functions and operators listed here, there also are non-member * operator* to multiply a PermutationMatrix with any kind of matrix expression (MatrixBase) on either side. * * \sa class DiagonalMatrix */ template struct ei_permut_matrix_product_retval; template struct ei_traits > : ei_traits > {}; template class PermutationMatrix : public EigenBase > { public: #ifndef EIGEN_PARSED_BY_DOXYGEN typedef ei_traits Traits; typedef Matrix DenseMatrixType; enum { Flags = Traits::Flags, CoeffReadCost = Traits::CoeffReadCost, RowsAtCompileTime = Traits::RowsAtCompileTime, ColsAtCompileTime = Traits::ColsAtCompileTime, MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime, MaxColsAtCompileTime = Traits::MaxColsAtCompileTime }; typedef typename Traits::Scalar Scalar; typedef typename Traits::Index Index; #endif typedef Matrix IndicesType; inline PermutationMatrix() {} /** Constructs an uninitialized permutation matrix of given size. */ inline PermutationMatrix(int size) : m_indices(size) {} /** Copy constructor. */ template inline PermutationMatrix(const PermutationMatrix& other) : m_indices(other.indices()) {} #ifndef EIGEN_PARSED_BY_DOXYGEN /** Standard copy constructor. Defined only to prevent a default copy constructor * from hiding the other templated constructor */ inline PermutationMatrix(const PermutationMatrix& other) : m_indices(other.indices()) {} #endif /** Generic constructor from expression of the indices. The indices * array has the meaning that the permutations sends each integer i to indices[i]. * * \warning It is your responsibility to check that the indices array that you passes actually * describes a permutation, i.e., each value between 0 and n-1 occurs exactly once, where n is the * array's size. */ template explicit inline PermutationMatrix(const MatrixBase& indices) : m_indices(indices) {} /** Convert the Transpositions \a tr to a permutation matrix */ template explicit PermutationMatrix(const Transpositions& tr) : m_indices(tr.size()) { *this = tr; } /** Copies the other permutation into *this */ template PermutationMatrix& operator=(const PermutationMatrix& other) { m_indices = other.indices(); return *this; } /** Assignment from the Transpositions \a tr */ template PermutationMatrix& operator=(const Transpositions& tr) { setIdentity(tr.size()); for(Index k=size()-1; k>=0; --k) applyTranspositionOnTheRight(k,tr.coeff(k)); return *this; } #ifndef EIGEN_PARSED_BY_DOXYGEN /** This is a special case of the templated operator=. Its purpose is to * prevent a default operator= from hiding the templated operator=. */ PermutationMatrix& operator=(const PermutationMatrix& other) { m_indices = other.m_indices; return *this; } #endif /** \returns the number of rows */ inline Index rows() const { return m_indices.size(); } /** \returns the number of columns */ inline Index cols() const { return m_indices.size(); } /** \returns the size of a side of the respective square matrix, i.e., the number of indices */ inline Index size() const { return m_indices.size(); } #ifndef EIGEN_PARSED_BY_DOXYGEN template void evalTo(MatrixBase& other) const { other.setZero(); for (int i=0; i=0 && j>=0 && i=0 && j>=0 && i inverse() const { return *this; } /** \returns the tranpose permutation matrix. * * \note \note_try_to_help_rvo */ inline Transpose transpose() const { return *this; } /**** multiplication helpers to hopefully get RVO ****/ #ifndef EIGEN_PARSED_BY_DOXYGEN template PermutationMatrix(const Transpose >& other) : m_indices(other.nestedPermutation().size()) { for (int i=0; i inline PermutationMatrix operator*(const PermutationMatrix& other) const { return PermutationMatrix(Product, *this, other); } /** \returns the product of a permutation with another inverse permutation. * * \note \note_try_to_help_rvo */ template inline PermutationMatrix operator*(const Transpose >& other) const { return PermutationMatrix(Product, *this, other.eval()); } /** \returns the product of an inverse permutation with another permutation. * * \note \note_try_to_help_rvo */ template friend inline PermutationMatrix operator*(const Transpose >& other, const PermutationMatrix& perm) { return PermutationMatrix(Product, other.eval(), perm); } protected: IndicesType m_indices; }; /** \returns the matrix with the permutation applied to the columns. */ template inline const ei_permut_matrix_product_retval, Derived, OnTheRight> operator*(const MatrixBase& matrix, const PermutationMatrix &permutation) { return ei_permut_matrix_product_retval , Derived, OnTheRight> (permutation, matrix.derived()); } /** \returns the matrix with the permutation applied to the rows. */ template inline const ei_permut_matrix_product_retval , Derived, OnTheLeft> operator*(const PermutationMatrix &permutation, const MatrixBase& matrix) { return ei_permut_matrix_product_retval , Derived, OnTheLeft> (permutation, matrix.derived()); } template struct ei_traits > { typedef typename MatrixType::PlainObject ReturnType; }; template struct ei_permut_matrix_product_retval : public ReturnByValue > { typedef typename ei_cleantype::type MatrixTypeNestedCleaned; ei_permut_matrix_product_retval(const PermutationType& perm, const MatrixType& matrix) : m_permutation(perm), m_matrix(matrix) {} inline int rows() const { return m_matrix.rows(); } inline int cols() const { return m_matrix.cols(); } template inline void evalTo(Dest& dst) const { const int n = Side==OnTheLeft ? rows() : cols(); if(ei_is_same_type::ret && ei_extract_data(dst) == ei_extract_data(m_matrix)) { // apply the permutation inplace Matrix mask(m_permutation.size()); mask.fill(false); int r = 0; while(r < m_permutation.size()) { // search for the next seed while(r=m_permutation.size()) break; // we got one, let's follow it until we are back to the seed int k0 = r++; int kPrev = k0; mask.coeffRef(k0) = true; for(int k=m_permutation.indices().coeff(k0); k!=k0; k=m_permutation.indices().coeff(k)) { Block(dst, k) .swap(Block (dst,((Side==OnTheLeft) ^ Transposed) ? k0 : kPrev)); mask.coeffRef(k) = true; kPrev = k; } } } else { for(int i = 0; i < n; ++i) { Block (dst, ((Side==OnTheLeft) ^ Transposed) ? m_permutation.indices().coeff(i) : i) = Block (m_matrix, ((Side==OnTheRight) ^ Transposed) ? m_permutation.indices().coeff(i) : i); } } } protected: const PermutationType& m_permutation; const typename MatrixType::Nested m_matrix; }; /* Template partial specialization for transposed/inverse permutations */ template struct ei_traits > > : ei_traits > {}; template class Transpose > : public EigenBase > > { typedef PermutationMatrix PermutationType; typedef typename PermutationType::IndicesType IndicesType; public: #ifndef EIGEN_PARSED_BY_DOXYGEN typedef ei_traits Traits; typedef Matrix DenseMatrixType; enum { Flags = Traits::Flags, CoeffReadCost = Traits::CoeffReadCost, RowsAtCompileTime = Traits::RowsAtCompileTime, ColsAtCompileTime = Traits::ColsAtCompileTime, MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime, MaxColsAtCompileTime = Traits::MaxColsAtCompileTime }; typedef typename Traits::Scalar Scalar; #endif Transpose(const PermutationType& p) : m_permutation(p) {} inline int rows() const { return m_permutation.rows(); } inline int cols() const { return m_permutation.cols(); } #ifndef EIGEN_PARSED_BY_DOXYGEN template void evalTo(MatrixBase& other) const { other.setZero(); for (int i=0; i friend inline const ei_permut_matrix_product_retval operator*(const MatrixBase& matrix, const Transpose& trPerm) { return ei_permut_matrix_product_retval(trPerm.m_permutation, matrix.derived()); } /** \returns the matrix with the inverse permutation applied to the rows. */ template inline const ei_permut_matrix_product_retval operator*(const MatrixBase& matrix) const { return ei_permut_matrix_product_retval(m_permutation, matrix.derived()); } const PermutationType& nestedPermutation() const { return m_permutation; } protected: const PermutationType& m_permutation; }; #endif // EIGEN_PERMUTATIONMATRIX_H