11 #ifndef EIGEN_PERMUTATIONMATRIX_H
12 #define EIGEN_PERMUTATIONMATRIX_H
16 template<
int RowCol,
typename IndicesType,
typename MatrixType,
typename StorageKind>
class PermutedImpl;
44 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed=false>
45 struct permut_matrix_product_retval;
46 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed=false>
47 struct permut_sparsematrix_product_retval;
48 enum PermPermProduct_t {PermPermProduct};
52 template<
typename Derived>
55 typedef internal::traits<Derived> Traits;
59 #ifndef EIGEN_PARSED_BY_DOXYGEN
60 typedef typename Traits::IndicesType IndicesType;
62 Flags = Traits::Flags,
63 CoeffReadCost = Traits::CoeffReadCost,
64 RowsAtCompileTime = Traits::RowsAtCompileTime,
65 ColsAtCompileTime = Traits::ColsAtCompileTime,
66 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
67 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
69 typedef typename Traits::Scalar Scalar;
70 typedef typename Traits::Index Index;
79 template<
typename OtherDerived>
87 template<
typename OtherDerived>
88 Derived&
operator=(
const TranspositionsBase<OtherDerived>& tr)
91 for(Index k=
size()-1; k>=0; --k)
96 #ifndef EIGEN_PARSED_BY_DOXYGEN
116 #ifndef EIGEN_PARSED_BY_DOXYGEN
117 template<
typename DenseDerived>
121 for (
int i=0; i<
rows();++i)
122 other.coeffRef(
indices().coeff(i),i) =
typename DenseDerived::Scalar(1);
150 for(Index i = 0; i <
size(); ++i)
173 eigen_assert(i>=0 && j>=0 && i<
size() && j<
size());
174 for(Index k = 0; k <
size(); ++k)
192 eigen_assert(i>=0 && j>=0 && i<
size() && j<
size());
213 #ifndef EIGEN_PARSED_BY_DOXYGEN
215 template<
typename OtherDerived>
220 template<
typename Lhs,
typename Rhs>
221 void assignProduct(
const Lhs& lhs,
const Rhs& rhs)
223 eigen_assert(lhs.cols() == rhs.rows());
224 for (
int i=0; i<
rows();++i)
indices().coeffRef(i) = lhs.indices().coeff(rhs.indices().coeff(i));
234 template<
typename Other>
236 {
return PlainPermutationType(internal::PermPermProduct,
derived(), other.
derived()); }
242 template<
typename Other>
244 {
return PlainPermutationType(internal::PermPermProduct, *
this, other.eval()); }
250 template<
typename Other>
friend
252 {
return PlainPermutationType(internal::PermPermProduct, other.eval(), perm); }
268 while(r<n && mask[r]) r++;
273 mask.coeffRef(k0) =
true;
276 mask.coeffRef(k) =
true;
302 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType>
303 struct traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType> >
304 : traits<Matrix<IndexType,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
306 typedef IndexType Index;
307 typedef Matrix<IndexType, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
311 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType>
315 typedef internal::traits<PermutationMatrix> Traits;
318 #ifndef EIGEN_PARSED_BY_DOXYGEN
319 typedef typename Traits::IndicesType IndicesType;
331 template<
typename OtherDerived>
333 : m_indices(other.
indices()) {}
335 #ifndef EIGEN_PARSED_BY_DOXYGEN
348 template<
typename Other>
353 template<
typename Other>
355 : m_indices(tr.
size())
361 template<
typename Other>
369 template<
typename Other>
375 #ifndef EIGEN_PARSED_BY_DOXYGEN
381 m_indices = other.m_indices;
387 const IndicesType&
indices()
const {
return m_indices; }
394 #ifndef EIGEN_PARSED_BY_DOXYGEN
395 template<
typename Other>
397 : m_indices(other.nestedPermutation().
size())
399 for (
int i=0; i<m_indices.size();++i) m_indices.coeffRef(other.nestedPermutation().
indices().coeff(i)) = i;
401 template<
typename Lhs,
typename Rhs>
402 PermutationMatrix(internal::PermPermProduct_t,
const Lhs& lhs,
const Rhs& rhs)
405 Base::assignProduct(lhs,rhs);
411 IndicesType m_indices;
416 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType,
int _PacketAccess>
417 struct traits<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess> >
418 : traits<Matrix<IndexType,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
420 typedef IndexType Index;
421 typedef Map<const Matrix<IndexType, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>, _PacketAccess> IndicesType;
425 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType,
int _PacketAccess>
426 class Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess>
427 :
public PermutationBase<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess> >
429 typedef PermutationBase<Map> Base;
430 typedef internal::traits<Map> Traits;
433 #ifndef EIGEN_PARSED_BY_DOXYGEN
434 typedef typename Traits::IndicesType IndicesType;
435 typedef typename IndicesType::Scalar Index;
438 inline Map(
const Index* indicesPtr)
439 : m_indices(indicesPtr)
442 inline Map(
const Index* indicesPtr, Index size)
443 : m_indices(indicesPtr,size)
447 template<
typename Other>
448 Map& operator=(
const PermutationBase<Other>& other)
449 {
return Base::operator=(other.derived()); }
452 template<
typename Other>
453 Map& operator=(
const TranspositionsBase<Other>& tr)
454 {
return Base::operator=(tr.derived()); }
456 #ifndef EIGEN_PARSED_BY_DOXYGEN
460 Map& operator=(
const Map& other)
462 m_indices = other.m_indices;
468 const IndicesType& indices()
const {
return m_indices; }
470 IndicesType& indices() {
return m_indices; }
474 IndicesType m_indices;
489 struct PermutationStorage {};
491 template<
typename _IndicesType>
class TranspositionsWrapper;
493 template<
typename _IndicesType>
494 struct traits<PermutationWrapper<_IndicesType> >
496 typedef PermutationStorage StorageKind;
497 typedef typename _IndicesType::Scalar Scalar;
498 typedef typename _IndicesType::Scalar Index;
499 typedef _IndicesType IndicesType;
501 RowsAtCompileTime = _IndicesType::SizeAtCompileTime,
502 ColsAtCompileTime = _IndicesType::SizeAtCompileTime,
503 MaxRowsAtCompileTime = IndicesType::MaxRowsAtCompileTime,
504 MaxColsAtCompileTime = IndicesType::MaxColsAtCompileTime,
506 CoeffReadCost = _IndicesType::CoeffReadCost
511 template<
typename _IndicesType>
515 typedef internal::traits<PermutationWrapper> Traits;
518 #ifndef EIGEN_PARSED_BY_DOXYGEN
519 typedef typename Traits::IndicesType IndicesType;
523 : m_indices(a_indices)
527 const typename internal::remove_all<typename IndicesType::Nested>::type&
532 typename IndicesType::Nested m_indices;
537 template<
typename Derived,
typename PermutationDerived>
538 inline const internal::permut_matrix_product_retval<PermutationDerived, Derived, OnTheRight>
542 return internal::permut_matrix_product_retval
544 (permutation.
derived(), matrix.derived());
549 template<
typename Derived,
typename PermutationDerived>
550 inline const internal::permut_matrix_product_retval
555 return internal::permut_matrix_product_retval
557 (permutation.
derived(), matrix.derived());
562 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed>
563 struct traits<permut_matrix_product_retval<PermutationType, MatrixType, Side, Transposed> >
565 typedef typename MatrixType::PlainObject ReturnType;
568 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed>
569 struct permut_matrix_product_retval
570 :
public ReturnByValue<permut_matrix_product_retval<PermutationType, MatrixType, Side, Transposed> >
572 typedef typename remove_all<typename MatrixType::Nested>::type MatrixTypeNestedCleaned;
573 typedef typename MatrixType::Index Index;
575 permut_matrix_product_retval(
const PermutationType& perm,
const MatrixType& matrix)
576 : m_permutation(perm), m_matrix(matrix)
579 inline Index rows()
const {
return m_matrix.rows(); }
580 inline Index cols()
const {
return m_matrix.cols(); }
582 template<
typename Dest>
inline void evalTo(Dest& dst)
const
584 const Index n = Side==
OnTheLeft ? rows() : cols();
587 if( is_same<MatrixTypeNestedCleaned,Dest>::value
588 && blas_traits<MatrixTypeNestedCleaned>::HasUsableDirectAccess
589 && blas_traits<Dest>::HasUsableDirectAccess
590 && extract_data(dst) == extract_data(m_matrix))
593 Matrix<bool,PermutationType::RowsAtCompileTime,1,0,PermutationType::MaxRowsAtCompileTime> mask(m_permutation.size());
596 while(r < m_permutation.size())
599 while(r<m_permutation.size() && mask[r]) r++;
600 if(r>=m_permutation.size())
605 mask.coeffRef(k0) =
true;
606 for(Index k=m_permutation.indices().coeff(k0); k!=k0; k=m_permutation.indices().coeff(k))
608 Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>(dst, k)
609 .swap(Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>
610 (dst,((Side==
OnTheLeft) ^ Transposed) ? k0 : kPrev));
612 mask.coeffRef(k) =
true;
619 for(
int i = 0; i < n; ++i)
621 Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>
622 (dst, ((Side==
OnTheLeft) ^ Transposed) ? m_permutation.indices().coeff(i) : i)
626 Block<const MatrixTypeNestedCleaned,Side==OnTheLeft ? 1 : MatrixType::RowsAtCompileTime,Side==OnTheRight ? 1 : MatrixType::ColsAtCompileTime>
627 (m_matrix, ((Side==
OnTheRight) ^ Transposed) ? m_permutation.indices().coeff(i) : i);
633 const PermutationType& m_permutation;
634 typename MatrixType::Nested m_matrix;
639 template<
typename Derived>
640 struct traits<Transpose<PermutationBase<Derived> > >
646 template<
typename Derived>
647 class Transpose<PermutationBase<Derived> >
648 :
public EigenBase<Transpose<PermutationBase<Derived> > >
650 typedef Derived PermutationType;
651 typedef typename PermutationType::IndicesType IndicesType;
652 typedef typename PermutationType::PlainPermutationType PlainPermutationType;
655 #ifndef EIGEN_PARSED_BY_DOXYGEN
656 typedef internal::traits<PermutationType> Traits;
657 typedef typename Derived::DenseMatrixType DenseMatrixType;
659 Flags = Traits::Flags,
660 CoeffReadCost = Traits::CoeffReadCost,
661 RowsAtCompileTime = Traits::RowsAtCompileTime,
662 ColsAtCompileTime = Traits::ColsAtCompileTime,
663 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
664 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
666 typedef typename Traits::Scalar Scalar;
669 Transpose(
const PermutationType& p) : m_permutation(p) {}
671 inline int rows()
const {
return m_permutation.rows(); }
672 inline int cols()
const {
return m_permutation.cols(); }
674 #ifndef EIGEN_PARSED_BY_DOXYGEN
675 template<
typename DenseDerived>
676 void evalTo(MatrixBase<DenseDerived>& other)
const
679 for (
int i=0; i<rows();++i)
680 other.coeffRef(i, m_permutation.indices().coeff(i)) =
typename DenseDerived::Scalar(1);
685 PlainPermutationType eval()
const {
return *
this; }
687 DenseMatrixType toDenseMatrix()
const {
return *
this; }
691 template<
typename OtherDerived>
friend
692 inline const internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheRight, true>
693 operator*(
const MatrixBase<OtherDerived>& matrix,
const Transpose& trPerm)
695 return internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheRight, true>(trPerm.m_permutation, matrix.derived());
700 template<
typename OtherDerived>
701 inline const internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheLeft, true>
702 operator*(
const MatrixBase<OtherDerived>& matrix)
const
704 return internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheLeft, true>(m_permutation, matrix.derived());
707 const PermutationType& nestedPermutation()
const {
return m_permutation; }
710 const PermutationType& m_permutation;
713 template<
typename Derived>
714 const PermutationWrapper<const Derived> MatrixBase<Derived>::asPermutation()
const
721 #endif // EIGEN_PERMUTATIONMATRIX_H
void setIdentity()
Definition: PermutationMatrix.h:148
const IndicesType & indices() const
Definition: PermutationMatrix.h:387
Derived & applyTranspositionOnTheRight(Index i, Index j)
Definition: PermutationMatrix.h:190
Transpose< PermutationBase > inverse() const
Definition: PermutationMatrix.h:201
IndicesType & indices()
Definition: PermutationMatrix.h:138
void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:322
Expression of the transpose of a matrix.
Definition: Transpose.h:57
Derived & setZero()
Definition: CwiseNullaryOp.h:499
Index rows() const
Definition: PermutationMatrix.h:108
const internal::permut_matrix_product_retval< PermutationDerived, Derived, OnTheRight > operator*(const MatrixBase< Derived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:539
friend PlainPermutationType operator*(const Transpose< PermutationBase< Other > > &other, const PermutationBase &perm)
Definition: PermutationMatrix.h:251
PermutationMatrix(const MatrixBase< Other > &a_indices)
Definition: PermutationMatrix.h:349
Derived & applyTranspositionOnTheLeft(Index i, Index j)
Definition: PermutationMatrix.h:171
PermutationMatrix & operator=(const TranspositionsBase< Other > &tr)
Definition: PermutationMatrix.h:370
PermutationMatrix(const PermutationBase< OtherDerived > &other)
Definition: PermutationMatrix.h:332
Base class for permutations.
Definition: PermutationMatrix.h:53
Definition: EigenBase.h:26
DenseMatrixType toDenseMatrix() const
Definition: PermutationMatrix.h:130
void resize(Index newSize)
Definition: PermutationMatrix.h:142
Permutation matrix.
Definition: PermutationMatrix.h:312
Definition: Constants.h:279
Derived & operator=(const TranspositionsBase< OtherDerived > &tr)
Definition: PermutationMatrix.h:88
Index size() const
Definition: PermutationMatrix.h:114
PermutationMatrix & operator=(const PermutationBase< Other > &other)
Definition: PermutationMatrix.h:362
Transpose< PermutationBase > transpose() const
Definition: PermutationMatrix.h:207
Derived & operator=(const PermutationBase< OtherDerived > &other)
Definition: PermutationMatrix.h:80
Derived & derived()
Definition: EigenBase.h:34
Index cols() const
Definition: PermutationMatrix.h:111
PermutationMatrix(int size)
Definition: PermutationMatrix.h:327
Class to view a vector of integers as a permutation matrix.
Definition: PermutationMatrix.h:512
Index determinant() const
Definition: PermutationMatrix.h:258
Map(PointerArgType dataPtr, const StrideType &a_stride=StrideType())
Definition: Map.h:139
PlainPermutationType operator*(const PermutationBase< Other > &other) const
Definition: PermutationMatrix.h:235
void setIdentity(Index newSize)
Definition: PermutationMatrix.h:156
IndicesType & indices()
Definition: PermutationMatrix.h:389
const internal::remove_all< typename IndicesType::Nested >::type & indices() const
Definition: PermutationMatrix.h:528
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Definition: Constants.h:277
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
PlainPermutationType operator*(const Transpose< PermutationBase< Other > > &other) const
Definition: PermutationMatrix.h:243
const IndicesType & indices() const
Definition: PermutationMatrix.h:136
PermutationMatrix(const TranspositionsBase< Other > &tr)
Definition: PermutationMatrix.h:354