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>
46 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed=false>
48 enum PermPermProduct_t {PermPermProduct};
52 template<
typename Derived>
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
70 typedef typename Traits::Index Index;
79 template<
typename OtherDerived>
87 template<
typename OtherDerived>
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)
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); }
268 while(r<n && mask[r]) r++;
273 mask.coeffRef(k0) =
true;
274 for(Index k=indices().coeff(k0); k!=k0; k=indices().coeff(k))
276 mask.coeffRef(k) =
true;
302 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType>
304 :
traits<Matrix<IndexType,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
306 typedef IndexType Index;
311 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType>
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>
372 return Base::operator=(tr.derived());
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>
403 : m_indices(lhs.indices().size())
405 Base::assignProduct(lhs,rhs);
411 IndicesType m_indices;
416 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType,
int _PacketAccess>
418 :
traits<Matrix<IndexType,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
420 typedef IndexType Index;
425 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType,
int _PacketAccess>
427 :
public PermutationBase<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess> >
433 #ifndef EIGEN_PARSED_BY_DOXYGEN 434 typedef typename Traits::IndicesType IndicesType;
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>
449 {
return Base::operator=(other.
derived()); }
452 template<
typename Other>
454 {
return Base::operator=(tr.derived()); }
456 #ifndef EIGEN_PARSED_BY_DOXYGEN 462 m_indices = other.m_indices;
468 const IndicesType&
indices()
const {
return m_indices; }
474 IndicesType m_indices;
493 template<
typename _IndicesType>
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>
518 #ifndef EIGEN_PARSED_BY_DOXYGEN 519 typedef typename Traits::IndicesType IndicesType;
523 : m_indices(a_indices)
532 typename IndicesType::Nested m_indices;
537 template<
typename Derived,
typename PermutationDerived>
544 (permutation.
derived(), matrix.derived());
549 template<
typename Derived,
typename PermutationDerived>
557 (permutation.
derived(), matrix.derived());
562 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed>
565 typedef typename MatrixType::PlainObject ReturnType;
568 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed>
570 :
public ReturnByValue<permut_matrix_product_retval<PermutationType, MatrixType, Side, Transposed> >
573 typedef typename MatrixType::Index Index;
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();
590 && extract_data(dst) == extract_data(m_matrix))
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))
610 (dst,((Side==
OnTheLeft) ^ Transposed) ? k0 : kPrev));
612 mask.coeffRef(k) =
true;
619 for(
int i = 0; i < n; ++i)
622 (dst, ((Side==
OnTheLeft) ^ Transposed) ? m_permutation.indices().coeff(i) : i)
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>
646 template<
typename 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 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
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>
679 for (
int i=0; i<rows();++i)
685 PlainPermutationType
eval()
const {
return *
this; }
687 DenseMatrixType toDenseMatrix()
const {
return *
this; }
691 template<
typename OtherDerived>
friend 700 template<
typename OtherDerived>
707 const PermutationType& nestedPermutation()
const {
return m_permutation; }
710 const PermutationType& m_permutation;
713 template<
typename Derived>
721 #endif // EIGEN_PERMUTATIONMATRIX_H PermutationMatrix(const MatrixBase< Other > &a_indices)
Generic constructor from expression of the indices.
Definition: PermutationMatrix.h:349
Definition: PermutationMatrix.h:491
Definition: BlasUtil.h:151
Apply transformation on the right.
Definition: Constants.h:279
Index size() const
Definition: Transpositions.h:81
Map & operator=(const TranspositionsBase< Other > &tr)
Assignment from the Transpositions tr.
Definition: PermutationMatrix.h:453
PlainPermutationType operator*(const PermutationBase< Other > &other) const
Definition: PermutationMatrix.h:235
PlainPermutationType eval() const
Definition: PermutationMatrix.h:685
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:104
Expression of the transpose of a matrix.
Definition: Transpose.h:57
PlainPermutationType operator*(const Transpose< PermutationBase< Other > > &other) const
Definition: PermutationMatrix.h:243
Definition: PermutationMatrix.h:489
DenseMatrixType toDenseMatrix() const
Definition: PermutationMatrix.h:130
Derived & applyTranspositionOnTheRight(Index i, Index j)
Multiplies *this by the transposition on the right.
Definition: PermutationMatrix.h:190
Derived & setZero()
Sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:499
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
PermutationMatrix(const TranspositionsBase< Other > &tr)
Convert the Transpositions tr to a permutation matrix.
Definition: PermutationMatrix.h:354
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
Definition: PermutationMatrix.h:16
Derived & derived()
Definition: EigenBase.h:34
Derived & operator=(const PermutationBase< OtherDerived > &other)
Copies the other permutation into *this.
Definition: PermutationMatrix.h:80
Definition: PermutationMatrix.h:45
PermutationMatrix & operator=(const PermutationMatrix &other)
This is a special case of the templated operator=.
Definition: PermutationMatrix.h:379
Base class for permutations.
Definition: PermutationMatrix.h:53
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:26
Definition: ReturnByValue.h:50
Permutation matrix.
Definition: PermutationMatrix.h:312
Index size() const
Definition: PermutationMatrix.h:114
detail::size< coerce_list< Ts... >> size
Get the size of a list (number of elements.)
Definition: Size.h:56
IndicesType & indices()
Definition: PermutationMatrix.h:138
PermutationMatrix(const PermutationBase< OtherDerived > &other)
Copy constructor.
Definition: PermutationMatrix.h:332
PermutationMatrix & operator=(const PermutationBase< Other > &other)
Copies the other permutation into *this.
Definition: PermutationMatrix.h:362
Index cols() const
Definition: PermutationMatrix.h:111
Derived & operator=(const TranspositionsBase< OtherDerived > &tr)
Assignment from the Transpositions tr.
Definition: PermutationMatrix.h:88
Map & operator=(const PermutationBase< Other > &other)
Copies the other permutation into *this.
Definition: PermutationMatrix.h:448
PermutationMatrix(int size)
Constructs an uninitialized permutation matrix of given size.
Definition: PermutationMatrix.h:327
Class to view a vector of integers as a permutation matrix.
Definition: PermutationMatrix.h:512
Derived & operator=(const PermutationBase &other)
This is a special case of the templated operator=.
Definition: PermutationMatrix.h:100
void setIdentity()
Sets *this to be the identity permutation matrix.
Definition: PermutationMatrix.h:148
const internal::permut_matrix_product_retval< PermutationType, OtherDerived, OnTheLeft, true > operator*(const MatrixBase< OtherDerived > &matrix) const
Definition: PermutationMatrix.h:702
const IndicesType & indices() const
const version of indices().
Definition: PermutationMatrix.h:136
Definition: BandTriangularSolver.h:13
Transpose< PermutationBase > transpose() const
Definition: PermutationMatrix.h:207
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
Transpose< PermutationBase > inverse() const
Definition: PermutationMatrix.h:201
Derived & applyTranspositionOnTheLeft(Index i, Index j)
Multiplies *this by the transposition on the left.
Definition: PermutationMatrix.h:171
PermutationMatrix(const PermutationMatrix &other)
Standard copy constructor.
Definition: PermutationMatrix.h:338
const IndicesType & indices() const
const version of indices().
Definition: PermutationMatrix.h:387
const IndicesType & indices() const
const version of indices().
Definition: PermutationMatrix.h:468
const Index & coeff(Index i) const
Direct access to the underlying index vector.
Definition: Transpositions.h:84
Definition: PermutationMatrix.h:47
Map & operator=(const Map &other)
This is a special case of the templated operator=.
Definition: PermutationMatrix.h:460
const internal::remove_all< typename IndicesType::Nested >::type & indices() const
const version of indices().
Definition: PermutationMatrix.h:528
IndicesType & indices()
Definition: PermutationMatrix.h:389
Definition: Transpositions.h:49
void setIdentity(Index newSize)
Sets *this to be the identity permutation matrix of given size.
Definition: PermutationMatrix.h:156
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
void resize(Index newSize)
Resizes to given size.
Definition: PermutationMatrix.h:142
Index determinant() const
Definition: PermutationMatrix.h:258
friend const internal::permut_matrix_product_retval< PermutationType, OtherDerived, OnTheRight, true > operator*(const MatrixBase< OtherDerived > &matrix, const Transpose &trPerm)
Definition: PermutationMatrix.h:693
IndicesType & indices()
Definition: PermutationMatrix.h:470
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Apply transformation on the left.
Definition: Constants.h:277
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48
PermutationMatrix & operator=(const TranspositionsBase< Other > &tr)
Assignment from the Transpositions tr.
Definition: PermutationMatrix.h:370
Index rows() const
Definition: PermutationMatrix.h:108