Eigen::PermutationBase< Derived > Class Template Reference

Base class for permutations. More...

+ Inheritance diagram for Eigen::PermutationBase< Derived >:

Public Member Functions

Derived & applyTranspositionOnTheLeft (Index i, Index j)
 
Derived & applyTranspositionOnTheRight (Index i, Index j)
 
Index cols () const
 
Index determinant () const
 
IndicesType & indices ()
 
const IndicesType & indices () const
 
InverseReturnType inverse () const
 
template<typename Other >
PlainPermutationType operator* (const InverseImpl< Other, PermutationStorage > &other) const
 
template<typename Other >
PlainPermutationType operator* (const PermutationBase< Other > &other) const
 
template<typename OtherDerived >
Derived & operator= (const PermutationBase< OtherDerived > &other)
 
template<typename OtherDerived >
Derived & operator= (const TranspositionsBase< OtherDerived > &tr)
 
void resize (Index newSize)
 
Index rows () const
 
void setIdentity ()
 
void setIdentity (Index newSize)
 
Index size () const
 
DenseMatrixType toDenseMatrix () const
 
InverseReturnType transpose () const
 
- Public Member Functions inherited from Eigen::EigenBase< Derived >
template<typename Dest >
void addTo (Dest &dst) const
 
template<typename Dest >
void applyThisOnTheLeft (Dest &dst) const
 
template<typename Dest >
void applyThisOnTheRight (Dest &dst) const
 
EIGEN_CONSTEXPR Index cols () const EIGEN_NOEXCEPT
 
Derived & const_cast_derived () const
 
const Derived & const_derived () const
 
Derived & derived ()
 
const Derived & derived () const
 
template<typename Dest >
void evalTo (Dest &dst) const
 
EIGEN_CONSTEXPR Index rows () const EIGEN_NOEXCEPT
 
EIGEN_CONSTEXPR Index size () const EIGEN_NOEXCEPT
 
template<typename Dest >
void subTo (Dest &dst) const
 

Private Types

typedef EigenBase< Derived > Base
 
typedef internal::traits< Derived > Traits
 

Additional Inherited Members

- Public Types inherited from Eigen::EigenBase< Derived >
typedef Eigen::Index Index
 The interface type of indices. More...
 
typedef internal::traits< Derived >::StorageKind StorageKind
 

Detailed Description

template<typename Derived>
class Eigen::PermutationBase< Derived >

Base class for permutations.

Template Parameters
Derivedthe derived class

This class is the base class for all expressions representing a permutation matrix, internally stored as a vector of integers. The convention followed here is that if \( \sigma \) is a permutation, the corresponding permutation matrix \( P_\sigma \) is such that if \( (e_1,\ldots,e_p) \) is the canonical basis, we have:

\[ P_\sigma(e_i) = e_{\sigma(i)}. \]

This convention ensures that for any two permutations \( \sigma, \tau \), we have:

\[ P_{\sigma\circ\tau} = P_\sigma P_\tau. \]

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 any kind of permutation object with any kind of matrix expression (MatrixBase) on either side.

See also
class PermutationMatrix, class PermutationWrapper

Definition at line 48 of file PermutationMatrix.h.

Member Typedef Documentation

◆ Base

template<typename Derived >
typedef EigenBase<Derived> Eigen::PermutationBase< Derived >::Base
private

Definition at line 51 of file PermutationMatrix.h.

◆ Traits

template<typename Derived >
typedef internal::traits<Derived> Eigen::PermutationBase< Derived >::Traits
private

Definition at line 50 of file PermutationMatrix.h.

Member Function Documentation

◆ applyTranspositionOnTheLeft()

template<typename Derived >
Derived& Eigen::PermutationBase< Derived >::applyTranspositionOnTheLeft ( Index  i,
Index  j 
)
inline

Multiplies *this by the transposition \((ij)\) on the left.

Returns
a reference to *this.
Warning
This is much slower than applyTranspositionOnTheRight(Index,Index): this has linear complexity and requires a lot of branching.
See also
applyTranspositionOnTheRight(Index,Index)

Definition at line 157 of file PermutationMatrix.h.

158  {
159  eigen_assert(i>=0 && j>=0 && i<size() && j<size());
160  for(Index k = 0; k < size(); ++k)
161  {
162  if(indices().coeff(k) == i) indices().coeffRef(k) = StorageIndex(j);
163  else if(indices().coeff(k) == j) indices().coeffRef(k) = StorageIndex(i);
164  }
165  return derived();
166  }
#define eigen_assert(x)
Definition: Macros.h:902
const IndicesType & indices() const
Derived & derived()
Definition: EigenBase.h:48
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
std::ptrdiff_t j

◆ applyTranspositionOnTheRight()

template<typename Derived >
Derived& Eigen::PermutationBase< Derived >::applyTranspositionOnTheRight ( Index  i,
Index  j 
)
inline

Multiplies *this by the transposition \((ij)\) on the right.

Returns
a reference to *this.

This is a fast operation, it only consists in swapping two indices.

See also
applyTranspositionOnTheLeft(Index,Index)

Definition at line 176 of file PermutationMatrix.h.

177  {
178  eigen_assert(i>=0 && j>=0 && i<size() && j<size());
179  std::swap(indices().coeffRef(i), indices().coeffRef(j));
180  return derived();
181  }
void swap(scoped_array< T > &a, scoped_array< T > &b)
Definition: Memory.h:788

◆ cols()

template<typename Derived >
Index Eigen::PermutationBase< Derived >::cols ( void  ) const
inline
Returns
the number of columns

Definition at line 96 of file PermutationMatrix.h.

96 { return Index(indices().size()); }

◆ determinant()

template<typename Derived >
Index Eigen::PermutationBase< Derived >::determinant ( ) const
inline
Returns
the determinant of the permutation matrix, which is either 1 or -1 depending on the parity of the permutation.

This function is O(n) procedure allocating a buffer of n booleans.

Definition at line 244 of file PermutationMatrix.h.

245  {
246  Index res = 1;
247  Index n = size();
248  Matrix<bool,RowsAtCompileTime,1,0,MaxRowsAtCompileTime> mask(n);
249  mask.fill(false);
250  Index r = 0;
251  while(r < n)
252  {
253  // search for the next seed
254  while(r<n && mask[r]) r++;
255  if(r>=n)
256  break;
257  // we got one, let's follow it until we are back to the seed
258  Index k0 = r++;
259  mask.coeffRef(k0) = true;
260  for(Index k=indices().coeff(k0); k!=k0; k=indices().coeff(k))
261  {
262  mask.coeffRef(k) = true;
263  res = -res;
264  }
265  }
266  return res;
267  }
int n
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res

◆ indices() [1/2]

template<typename Derived >
IndicesType& Eigen::PermutationBase< Derived >::indices ( )
inline
Returns
a reference to the stored array representing the permutation.

Definition at line 123 of file PermutationMatrix.h.

123 { return derived().indices(); }

◆ indices() [2/2]

template<typename Derived >
const IndicesType& Eigen::PermutationBase< Derived >::indices ( ) const
inline

const version of indices().

Definition at line 121 of file PermutationMatrix.h.

121 { return derived().indices(); }

◆ inverse()

template<typename Derived >
InverseReturnType Eigen::PermutationBase< Derived >::inverse ( ) const
inline
Returns
the inverse permutation matrix.
Note
This function returns the result by value. In order to make that efficient, it is implemented as just a return statement using a special constructor, hopefully allowing the compiler to perform a RVO (return value optimization).

Definition at line 187 of file PermutationMatrix.h.

188  { return InverseReturnType(derived()); }
CwiseUnaryOp< internal::scalar_inverse_op< Scalar >, const Derived > InverseReturnType

◆ operator*() [1/2]

template<typename Derived >
template<typename Other >
PlainPermutationType Eigen::PermutationBase< Derived >::operator* ( const InverseImpl< Other, PermutationStorage > &  other) const
inline
Returns
the product of a permutation with another inverse permutation.
Note
This function returns the result by value. In order to make that efficient, it is implemented as just a return statement using a special constructor, hopefully allowing the compiler to perform a RVO (return value optimization).

Definition at line 229 of file PermutationMatrix.h.

230  { return PlainPermutationType(internal::PermPermProduct, *this, other.eval()); }

◆ operator*() [2/2]

template<typename Derived >
template<typename Other >
PlainPermutationType Eigen::PermutationBase< Derived >::operator* ( const PermutationBase< Other > &  other) const
inline
Returns
the product permutation matrix.
Note
This function returns the result by value. In order to make that efficient, it is implemented as just a return statement using a special constructor, hopefully allowing the compiler to perform a RVO (return value optimization).

Definition at line 221 of file PermutationMatrix.h.

222  { return PlainPermutationType(internal::PermPermProduct, derived(), other.derived()); }

◆ operator=() [1/2]

template<typename Derived >
template<typename OtherDerived >
Derived& Eigen::PermutationBase< Derived >::operator= ( const PermutationBase< OtherDerived > &  other)
inline

Copies the other permutation into *this

Definition at line 76 of file PermutationMatrix.h.

77  {
78  indices() = other.indices();
79  return derived();
80  }

◆ operator=() [2/2]

template<typename Derived >
template<typename OtherDerived >
Derived& Eigen::PermutationBase< Derived >::operator= ( const TranspositionsBase< OtherDerived > &  tr)
inline

Assignment from the Transpositions tr

Definition at line 84 of file PermutationMatrix.h.

85  {
86  setIdentity(tr.size());
87  for(Index k=size()-1; k>=0; --k)
88  applyTranspositionOnTheRight(k,tr.coeff(k));
89  return derived();
90  }
Derived & applyTranspositionOnTheRight(Index i, Index j)

◆ resize()

template<typename Derived >
void Eigen::PermutationBase< Derived >::resize ( Index  newSize)
inline

Resizes to given size.

Definition at line 127 of file PermutationMatrix.h.

128  {
129  indices().resize(newSize);
130  }

◆ rows()

template<typename Derived >
Index Eigen::PermutationBase< Derived >::rows ( void  ) const
inline
Returns
the number of rows

Definition at line 93 of file PermutationMatrix.h.

93 { return Index(indices().size()); }

◆ setIdentity() [1/2]

template<typename Derived >
void Eigen::PermutationBase< Derived >::setIdentity ( )
inline

Sets *this to be the identity permutation matrix

Definition at line 133 of file PermutationMatrix.h.

134  {
135  StorageIndex n = StorageIndex(size());
136  for(StorageIndex i = 0; i < n; ++i)
137  indices().coeffRef(i) = i;
138  }

◆ setIdentity() [2/2]

template<typename Derived >
void Eigen::PermutationBase< Derived >::setIdentity ( Index  newSize)
inline

Sets *this to be the identity permutation matrix of given size.

Definition at line 142 of file PermutationMatrix.h.

143  {
144  resize(newSize);
145  setIdentity();
146  }
void resize(Index newSize)

◆ size()

template<typename Derived >
Index Eigen::PermutationBase< Derived >::size ( ) const
inline
Returns
the size of a side of the respective square matrix, i.e., the number of indices

Definition at line 99 of file PermutationMatrix.h.

99 { return Index(indices().size()); }

◆ toDenseMatrix()

template<typename Derived >
DenseMatrixType Eigen::PermutationBase< Derived >::toDenseMatrix ( ) const
inline
Returns
a Matrix object initialized from this permutation matrix. Notice that it is inefficient to return this Matrix object by value. For efficiency, favor using the Matrix constructor taking EigenBase objects.

Definition at line 115 of file PermutationMatrix.h.

116  {
117  return derived();
118  }

◆ transpose()

template<typename Derived >
InverseReturnType Eigen::PermutationBase< Derived >::transpose ( ) const
inline
Returns
the tranpose permutation matrix.
Note
This function returns the result by value. In order to make that efficient, it is implemented as just a return statement using a special constructor, hopefully allowing the compiler to perform a RVO (return value optimization).

Definition at line 193 of file PermutationMatrix.h.

194  { return InverseReturnType(derived()); }

The documentation for this class was generated from the following file: