// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2009 Benoit Jacob // // 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 /** \nonstableyet * \class PermutationMatrix * * \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. * * This class represents a permutation matrix, internally stored as a vector of integers. * The convention followed here is the same as on Wikipedia, * namely: the matrix of permutation \a p is the matrix such that on each row \a i, the only nonzero coefficient is * in column p(i). * * \sa class DiagonalMatrix */ template class PermutationMatrix; template struct ei_permut_matrix_product_retval; template struct ei_traits > : ei_traits > {}; template class PermutationMatrix : public AnyMatrixBase > { public: 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 Matrix IndicesType; inline PermutationMatrix() { } template inline PermutationMatrix(const PermutationMatrix& other) : m_indices(other.indices()) {} /** copy constructor. prevent a default copy constructor from hiding the other templated constructor */ inline PermutationMatrix(const PermutationMatrix& other) : m_indices(other.indices()) {} /** generic constructor from expression of the indices */ template explicit inline PermutationMatrix(const MatrixBase& other) : m_indices(other) {} template PermutationMatrix& operator=(const PermutationMatrix& other) { m_indices = other.indices(); return *this; } /** 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; } inline PermutationMatrix(int rows, int cols) : m_indices(rows) { ei_assert(rows == cols); } /** \returns the number of columns */ inline int rows() const { return m_indices.size(); } /** \returns the number of rows */ inline int cols() const { return m_indices.size(); } template void evalTo(MatrixBase& other) const { other.setZero(); for (int i=0; i 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::PlainMatrixType ReturnMatrixType; }; 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(); for(int i = 0; i < n; ++i) { Block< Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime >(dst, Side==OnTheRight ? m_permutation.indices().coeff(i) : i) = Block< MatrixTypeNestedCleaned, Side==OnTheLeft ? 1 : MatrixType::RowsAtCompileTime, Side==OnTheRight ? 1 : MatrixType::ColsAtCompileTime >(m_matrix, Side==OnTheLeft ? m_permutation.indices().coeff(i) : i); } } protected: const PermutationType& m_permutation; const typename MatrixType::Nested m_matrix; }; #endif // EIGEN_PERMUTATIONMATRIX_H