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)
90 setIdentity(tr.size());
91 for(Index k=size()-1; k>=0; --k)
92 applyTranspositionOnTheRight(k,tr.coeff(k));
96 #ifndef EIGEN_PARSED_BY_DOXYGEN 108 inline Index
rows()
const {
return Index(indices().size()); }
111 inline Index
cols()
const {
return Index(indices().size()); }
114 inline Index
size()
const {
return Index(indices().
size()); }
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);
136 const IndicesType&
indices()
const {
return derived().indices(); }
138 IndicesType&
indices() {
return derived().indices(); }
144 indices().resize(newSize);
150 for(Index i = 0; i < size(); ++i)
151 indices().coeffRef(i) = i;
173 eigen_assert(i>=0 && j>=0 && i<size() && j<size());
174 for(Index k = 0; k < size(); ++k)
176 if(indices().coeff(k) == i) indices().coeffRef(k) = j;
177 else if(indices().coeff(k) == j) indices().coeffRef(k) = i;
192 eigen_assert(i>=0 && j>=0 && i<size() && j<size());
193 std::swap(indices().coeffRef(i), indices().coeffRef(j));
202 {
return derived(); }
208 {
return derived(); }
213 #ifndef EIGEN_PARSED_BY_DOXYGEN 215 template<
typename OtherDerived>
218 for (
int i=0; i<rows();++i) indices().coeffRef(other.
indices().coeff(i)) = i;
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); }
273 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType>
274 struct traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType> >
275 : traits<Matrix<IndexType,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
277 typedef IndexType Index;
282 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType>
286 typedef internal::traits<PermutationMatrix> Traits;
289 #ifndef EIGEN_PARSED_BY_DOXYGEN 290 typedef typename Traits::IndicesType IndicesType;
302 template<
typename OtherDerived>
304 : m_indices(other.indices()) {}
306 #ifndef EIGEN_PARSED_BY_DOXYGEN 319 template<
typename Other>
324 template<
typename Other>
326 : m_indices(tr.size())
332 template<
typename Other>
340 template<
typename Other>
343 return Base::operator=(tr.derived());
346 #ifndef EIGEN_PARSED_BY_DOXYGEN 352 m_indices = other.m_indices;
358 const IndicesType&
indices()
const {
return m_indices; }
365 #ifndef EIGEN_PARSED_BY_DOXYGEN 366 template<
typename Other>
368 : m_indices(other.nestedPermutation().
size())
370 for (
int i=0; i<m_indices.size();++i) m_indices.coeffRef(other.nestedPermutation().
indices().coeff(i)) = i;
372 template<
typename Lhs,
typename Rhs>
374 : m_indices(lhs.indices().size())
376 Base::assignProduct(lhs,rhs);
382 IndicesType m_indices;
387 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType,
int _PacketAccess>
388 struct traits<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess> >
389 : traits<Matrix<IndexType,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
391 typedef IndexType Index;
396 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType,
int _PacketAccess>
397 class Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess>
398 :
public PermutationBase<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess> >
401 typedef internal::traits<Map> Traits;
404 #ifndef EIGEN_PARSED_BY_DOXYGEN 406 typedef typename IndicesType::Scalar Index;
409 inline Map(
const Index* indicesPtr)
410 : m_indices(indicesPtr)
413 inline Map(
const Index* indicesPtr, Index size)
414 : m_indices(indicesPtr,size)
418 template<
typename Other>
420 {
return Base::operator=(other.
derived()); }
423 template<
typename Other>
424 Map& operator=(
const TranspositionsBase<Other>& tr)
425 {
return Base::operator=(tr.derived()); }
427 #ifndef EIGEN_PARSED_BY_DOXYGEN 431 Map& operator=(
const Map& other)
433 m_indices = other.m_indices;
439 const IndicesType& indices()
const {
return m_indices; }
441 IndicesType& indices() {
return m_indices; }
445 IndicesType m_indices;
460 struct PermutationStorage {};
462 template<
typename _IndicesType>
class TranspositionsWrapper;
464 template<
typename _IndicesType>
465 struct traits<PermutationWrapper<_IndicesType> >
467 typedef PermutationStorage StorageKind;
468 typedef typename _IndicesType::Scalar Scalar;
469 typedef typename _IndicesType::Scalar Index;
470 typedef _IndicesType IndicesType;
472 RowsAtCompileTime = _IndicesType::SizeAtCompileTime,
473 ColsAtCompileTime = _IndicesType::SizeAtCompileTime,
474 MaxRowsAtCompileTime = IndicesType::MaxRowsAtCompileTime,
475 MaxColsAtCompileTime = IndicesType::MaxColsAtCompileTime,
477 CoeffReadCost = _IndicesType::CoeffReadCost
482 template<
typename _IndicesType>
486 typedef internal::traits<PermutationWrapper> Traits;
489 #ifndef EIGEN_PARSED_BY_DOXYGEN 490 typedef typename Traits::IndicesType IndicesType;
494 : m_indices(a_indices)
498 const typename internal::remove_all<typename IndicesType::Nested>::type&
503 typename IndicesType::Nested m_indices;
508 template<
typename Derived,
typename PermutationDerived>
509 inline const internal::permut_matrix_product_retval<PermutationDerived, Derived, OnTheRight>
513 return internal::permut_matrix_product_retval
515 (permutation.
derived(), matrix.derived());
520 template<
typename Derived,
typename PermutationDerived>
521 inline const internal::permut_matrix_product_retval
526 return internal::permut_matrix_product_retval
528 (permutation.
derived(), matrix.derived());
533 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed>
534 struct traits<permut_matrix_product_retval<PermutationType, MatrixType, Side, Transposed> >
536 typedef typename MatrixType::PlainObject ReturnType;
539 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed>
540 struct permut_matrix_product_retval
541 :
public ReturnByValue<permut_matrix_product_retval<PermutationType, MatrixType, Side, Transposed> >
543 typedef typename remove_all<typename MatrixType::Nested>::type MatrixTypeNestedCleaned;
544 typedef typename MatrixType::Index Index;
546 permut_matrix_product_retval(
const PermutationType& perm,
const MatrixType& matrix)
547 : m_permutation(perm), m_matrix(matrix)
550 inline Index rows()
const {
return m_matrix.rows(); }
551 inline Index cols()
const {
return m_matrix.cols(); }
553 template<
typename Dest>
inline void evalTo(Dest& dst)
const 555 const Index n = Side==
OnTheLeft ? rows() : cols();
558 if(is_same<MatrixTypeNestedCleaned,Dest>::value && extract_data(dst) == extract_data(m_matrix))
564 while(r < m_permutation.size())
567 while(r<m_permutation.size() && mask[r]) r++;
568 if(r>=m_permutation.size())
573 mask.coeffRef(k0) =
true;
574 for(Index k=m_permutation.indices().coeff(k0); k!=k0; k=m_permutation.indices().coeff(k))
578 (dst,((Side==
OnTheLeft) ^ Transposed) ? k0 : kPrev));
580 mask.coeffRef(k) =
true;
587 for(
int i = 0; i < n; ++i)
590 (dst, ((Side==
OnTheLeft) ^ Transposed) ? m_permutation.indices().coeff(i) : i)
595 (m_matrix, ((Side==
OnTheRight) ^ Transposed) ? m_permutation.indices().coeff(i) : i);
601 const PermutationType& m_permutation;
602 typename MatrixType::Nested m_matrix;
607 template<
typename Derived>
608 struct traits<Transpose<PermutationBase<Derived> > >
614 template<
typename Derived>
615 class Transpose<PermutationBase<Derived> >
616 :
public EigenBase<Transpose<PermutationBase<Derived> > >
618 typedef Derived PermutationType;
619 typedef typename PermutationType::IndicesType IndicesType;
620 typedef typename PermutationType::PlainPermutationType PlainPermutationType;
623 #ifndef EIGEN_PARSED_BY_DOXYGEN 624 typedef internal::traits<PermutationType> Traits;
625 typedef typename Derived::DenseMatrixType DenseMatrixType;
627 Flags = Traits::Flags,
628 CoeffReadCost = Traits::CoeffReadCost,
629 RowsAtCompileTime = Traits::RowsAtCompileTime,
630 ColsAtCompileTime = Traits::ColsAtCompileTime,
631 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
632 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
634 typedef typename Traits::Scalar Scalar;
637 Transpose(
const PermutationType& p) : m_permutation(p) {}
639 inline int rows()
const {
return m_permutation.rows(); }
640 inline int cols()
const {
return m_permutation.cols(); }
642 #ifndef EIGEN_PARSED_BY_DOXYGEN 643 template<
typename DenseDerived>
647 for (
int i=0; i<rows();++i)
648 other.coeffRef(i, m_permutation.indices().coeff(i)) =
typename DenseDerived::Scalar(1);
653 PlainPermutationType eval()
const {
return *
this; }
655 DenseMatrixType toDenseMatrix()
const {
return *
this; }
659 template<
typename OtherDerived>
friend 660 inline const internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheRight, true>
663 return internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheRight, true>(trPerm.m_permutation, matrix.derived());
668 template<
typename OtherDerived>
669 inline const internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheLeft, true>
672 return internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheLeft, true>(m_permutation, matrix.derived());
675 const PermutationType& nestedPermutation()
const {
return m_permutation; }
678 const PermutationType& m_permutation;
681 template<
typename Derived>
689 #endif // EIGEN_PERMUTATIONMATRIX_H Index cols() const
Definition: PermutationMatrix.h:111
void setIdentity()
Definition: PermutationMatrix.h:148
Derived & applyTranspositionOnTheRight(Index i, Index j)
Definition: PermutationMatrix.h:190
IndicesType & indices()
Definition: PermutationMatrix.h:138
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:104
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
friend PlainPermutationType operator*(const Transpose< PermutationBase< Other > > &other, const PermutationBase &perm)
Definition: PermutationMatrix.h:251
PermutationMatrix(const MatrixBase< Other > &a_indices)
Definition: PermutationMatrix.h:320
Derived & applyTranspositionOnTheLeft(Index i, Index j)
Definition: PermutationMatrix.h:171
PermutationMatrix & operator=(const TranspositionsBase< Other > &tr)
Definition: PermutationMatrix.h:341
PermutationMatrix(const PermutationBase< OtherDerived > &other)
Definition: PermutationMatrix.h:303
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:283
Transpose< PermutationBase > inverse() const
Definition: PermutationMatrix.h:201
Derived & operator=(const TranspositionsBase< OtherDerived > &tr)
Definition: PermutationMatrix.h:88
const IndicesType & indices() const
Definition: PermutationMatrix.h:358
PermutationMatrix & operator=(const PermutationBase< Other > &other)
Definition: PermutationMatrix.h:333
Definition: Constants.h:279
Derived & operator=(const PermutationBase< OtherDerived > &other)
Definition: PermutationMatrix.h:80
Derived & derived()
Definition: EigenBase.h:34
PlainPermutationType operator*(const PermutationBase< Other > &other) const
Definition: PermutationMatrix.h:235
Index size() const
Definition: PermutationMatrix.h:114
PlainPermutationType operator*(const Transpose< PermutationBase< Other > > &other) const
Definition: PermutationMatrix.h:243
PermutationMatrix(int size)
Definition: PermutationMatrix.h:298
Index rows() const
Definition: PermutationMatrix.h:108
Class to view a vector of integers as a permutation matrix.
Definition: PermutationMatrix.h:483
const internal::remove_all< typename IndicesType::Nested >::type & indices() const
Definition: PermutationMatrix.h:499
Definition: Eigen_Colamd.h:54
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:102
void setIdentity(Index newSize)
Definition: PermutationMatrix.h:156
IndicesType & indices()
Definition: PermutationMatrix.h:360
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
const IndicesType & indices() const
Definition: PermutationMatrix.h:136
PermutationMatrix(const TranspositionsBase< Other > &tr)
Definition: PermutationMatrix.h:325
Transpose< PermutationBase > transpose() const
Definition: PermutationMatrix.h:207