11 #ifndef EIGEN_PERMUTATIONMATRIX_H 12 #define EIGEN_PERMUTATIONMATRIX_H 15 #include "./InternalHeaderCheck.h" 21 enum PermPermProduct_t { PermPermProduct };
48 template <
typename Derived>
50 typedef internal::traits<Derived> Traits;
54 #ifndef EIGEN_PARSED_BY_DOXYGEN 55 typedef typename Traits::IndicesType IndicesType;
57 Flags = Traits::Flags,
58 RowsAtCompileTime = Traits::RowsAtCompileTime,
59 ColsAtCompileTime = Traits::ColsAtCompileTime,
60 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
61 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
63 typedef typename Traits::StorageIndex StorageIndex;
68 typedef PlainPermutationType PlainObject;
75 template <
typename OtherDerived>
82 template <
typename OtherDerived>
83 Derived&
operator=(
const TranspositionsBase<OtherDerived>& tr) {
98 #ifndef EIGEN_PARSED_BY_DOXYGEN 99 template <
typename DenseDerived>
126 StorageIndex n = StorageIndex(
size());
127 for (StorageIndex i = 0; i < n; ++i)
indices().coeffRef(i) = i;
147 eigen_assert(i >= 0 && j >= 0 && i <
size() && j <
size());
150 indices().coeffRef(k) = StorageIndex(j);
151 else if (
indices().coeff(k) == j)
152 indices().coeffRef(k) = StorageIndex(i);
166 eigen_assert(i >= 0 && j >= 0 && i <
size() && j <
size());
175 inline InverseReturnType
inverse()
const {
return InverseReturnType(
derived()); }
184 #ifndef EIGEN_PARSED_BY_DOXYGEN 186 template <
typename OtherDerived>
190 template <
typename Lhs,
typename Rhs>
191 void assignProduct(
const Lhs& lhs,
const Rhs& rhs) {
192 eigen_assert(lhs.cols() == rhs.rows());
193 for (
Index i = 0; i <
rows(); ++i)
indices().coeffRef(i) = lhs.indices().coeff(rhs.indices().coeff(i));
202 template <
typename Other>
204 return PlainPermutationType(internal::PermPermProduct,
derived(), other.
derived());
211 template <
typename Other>
212 inline PlainPermutationType
operator*(
const InverseImpl<Other, PermutationStorage>& other)
const {
213 return PlainPermutationType(internal::PermPermProduct, *
this, other.eval());
220 template <
typename Other>
221 friend inline PlainPermutationType
operator*(
const InverseImpl<Other, PermutationStorage>& other,
223 return PlainPermutationType(internal::PermPermProduct, other.eval(), perm);
239 while (r < n && mask[r]) r++;
256 template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_>
257 struct traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> >
259 Matrix<StorageIndex_, SizeAtCompileTime, SizeAtCompileTime, 0, MaxSizeAtCompileTime, MaxSizeAtCompileTime> > {
260 typedef PermutationStorage StorageKind;
261 typedef Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
262 typedef StorageIndex_ StorageIndex;
281 template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_>
283 :
public PermutationBase<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
285 typedef internal::traits<PermutationMatrix> Traits;
290 #ifndef EIGEN_PARSED_BY_DOXYGEN 291 typedef typename Traits::IndicesType IndicesType;
292 typedef typename Traits::StorageIndex StorageIndex;
304 template <
typename OtherDerived>
314 template <
typename Other>
318 template <
typename Other>
324 template <
typename Other>
331 template <
typename Other>
337 const IndicesType&
indices()
const {
return m_indices; }
343 #ifndef EIGEN_PARSED_BY_DOXYGEN 344 template <
typename Other>
346 : m_indices(other.
derived().nestedExpression().
size()) {
348 StorageIndex
end = StorageIndex(m_indices.size());
349 for (StorageIndex i = 0; i <
end; ++i)
350 m_indices.coeffRef(other.derived().nestedExpression().indices().coeff(i)) = i;
352 template <
typename Lhs,
typename Rhs>
353 PermutationMatrix(internal::PermPermProduct_t,
const Lhs& lhs,
const Rhs& rhs) : m_indices(lhs.
indices().
size()) {
354 Base::assignProduct(lhs, rhs);
359 IndicesType m_indices;
363 template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_,
int PacketAccess_>
364 struct traits<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_> >
366 Matrix<StorageIndex_, SizeAtCompileTime, SizeAtCompileTime, 0, MaxSizeAtCompileTime, MaxSizeAtCompileTime> > {
367 typedef PermutationStorage StorageKind;
368 typedef Map<const Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>, PacketAccess_> IndicesType;
369 typedef StorageIndex_ StorageIndex;
374 template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_,
int PacketAccess_>
375 class Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_>
376 :
public PermutationBase<
377 Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_> > {
378 typedef PermutationBase<Map> Base;
379 typedef internal::traits<Map> Traits;
382 #ifndef EIGEN_PARSED_BY_DOXYGEN 383 typedef typename Traits::IndicesType IndicesType;
384 typedef typename IndicesType::Scalar StorageIndex;
387 inline Map(
const StorageIndex* indicesPtr) : m_indices(indicesPtr) {}
389 inline Map(
const StorageIndex* indicesPtr,
Index size) : m_indices(indicesPtr, size) {}
392 template <
typename Other>
393 Map& operator=(
const PermutationBase<Other>& other) {
394 return Base::operator=(other.derived());
398 template <
typename Other>
399 Map& operator=(
const TranspositionsBase<Other>& tr) {
400 return Base::operator=(tr.derived());
403 #ifndef EIGEN_PARSED_BY_DOXYGEN 407 Map& operator=(
const Map& other) {
408 m_indices = other.m_indices;
414 const IndicesType& indices()
const {
return m_indices; }
416 IndicesType& indices() {
return m_indices; }
419 IndicesType m_indices;
422 template <
typename IndicesType_>
423 class TranspositionsWrapper;
425 template <
typename IndicesType_>
426 struct traits<PermutationWrapper<IndicesType_> > {
427 typedef PermutationStorage StorageKind;
429 typedef typename IndicesType_::Scalar StorageIndex;
430 typedef IndicesType_ IndicesType;
432 RowsAtCompileTime = IndicesType_::SizeAtCompileTime,
433 ColsAtCompileTime = IndicesType_::SizeAtCompileTime,
434 MaxRowsAtCompileTime = IndicesType::MaxSizeAtCompileTime,
435 MaxColsAtCompileTime = IndicesType::MaxSizeAtCompileTime,
452 template <
typename IndicesType_>
455 typedef internal::traits<PermutationWrapper> Traits;
458 #ifndef EIGEN_PARSED_BY_DOXYGEN 459 typedef typename Traits::IndicesType IndicesType;
465 const internal::remove_all_t<typename IndicesType::Nested>&
indices()
const {
return m_indices; }
468 typename IndicesType::Nested m_indices;
473 template <
typename MatrixDerived,
typename PermutationDerived>
481 template <
typename PermutationDerived,
typename MatrixDerived>
487 template <
typename PermutationType>
488 class InverseImpl<PermutationType, PermutationStorage> :
public EigenBase<Inverse<PermutationType> > {
489 typedef typename PermutationType::PlainPermutationType PlainPermutationType;
490 typedef internal::traits<PermutationType> PermTraits;
496 typedef Inverse<PermutationType> InverseType;
497 using EigenBase<Inverse<PermutationType> >::derived;
499 #ifndef EIGEN_PARSED_BY_DOXYGEN 500 typedef typename PermutationType::DenseMatrixType DenseMatrixType;
502 RowsAtCompileTime = PermTraits::RowsAtCompileTime,
503 ColsAtCompileTime = PermTraits::ColsAtCompileTime,
504 MaxRowsAtCompileTime = PermTraits::MaxRowsAtCompileTime,
505 MaxColsAtCompileTime = PermTraits::MaxColsAtCompileTime
509 #ifndef EIGEN_PARSED_BY_DOXYGEN 510 template <
typename DenseDerived>
511 void evalTo(MatrixBase<DenseDerived>& other)
const {
513 for (
Index i = 0; i < derived().rows(); ++i)
514 other.coeffRef(i, derived().nestedExpression().indices().coeff(i)) =
typename DenseDerived::Scalar(1);
519 PlainPermutationType eval()
const {
return derived(); }
521 DenseMatrixType toDenseMatrix()
const {
return derived(); }
525 template <
typename OtherDerived>
526 friend const Product<OtherDerived, InverseType, DefaultProduct>
operator*(
const MatrixBase<OtherDerived>& matrix,
527 const InverseType& trPerm) {
528 return Product<OtherDerived, InverseType, DefaultProduct>(matrix.derived(), trPerm.derived());
533 template <
typename OtherDerived>
534 const Product<InverseType, OtherDerived, DefaultProduct>
operator*(
const MatrixBase<OtherDerived>& matrix)
const {
535 return Product<InverseType, OtherDerived, DefaultProduct>(derived(), matrix.derived());
539 template <
typename Derived>
540 const PermutationWrapper<const Derived> MatrixBase<Derived>::asPermutation()
const {
547 struct AssignmentKind<DenseShape, PermutationShape> {
548 typedef EigenBase2EigenBase Kind;
555 #endif // EIGEN_PERMUTATIONMATRIX_H static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
const Product< MatrixDerived, PermutationDerived, DefaultProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:474
constexpr Derived & derived()
Definition: EigenBase.h:49
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:198
PlainPermutationType operator*(const PermutationBase< Other > &other) const
Definition: PermutationMatrix.h:203
friend PlainPermutationType operator*(const InverseImpl< Other, PermutationStorage > &other, const PermutationBase &perm)
Definition: PermutationMatrix.h:221
InverseReturnType inverse() const
Definition: PermutationMatrix.h:175
PermutationMatrix & operator=(const TranspositionsBase< Other > &tr)
Definition: PermutationMatrix.h:332
PlainPermutationType operator*(const InverseImpl< Other, PermutationStorage > &other) const
Definition: PermutationMatrix.h:212
const IndicesType & indices() const
Definition: PermutationMatrix.h:337
DenseMatrixType toDenseMatrix() const
Definition: PermutationMatrix.h:110
Derived & applyTranspositionOnTheRight(Index i, Index j)
Definition: PermutationMatrix.h:165
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
PermutationMatrix(Index size)
Definition: PermutationMatrix.h:299
IndicesType & indices()
Definition: PermutationMatrix.h:339
Derived & operator=(const PermutationBase< OtherDerived > &other)
Definition: PermutationMatrix.h:76
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition: Map.h:123
Base class for permutations.
Definition: PermutationMatrix.h:49
Definition: EigenBase.h:33
const internal::remove_all_t< typename IndicesType::Nested > & indices() const
Definition: PermutationMatrix.h:465
Expression of the inverse of another expression.
Definition: Inverse.h:43
Permutation matrix.
Definition: PermutationMatrix.h:282
Index size() const
Definition: PermutationMatrix.h:96
IndicesType & indices()
Definition: PermutationMatrix.h:118
Index cols() const
Definition: PermutationMatrix.h:93
PermutationMatrix(const TranspositionsBase< Other > &tr)
Definition: PermutationMatrix.h:319
Derived & operator=(const TranspositionsBase< OtherDerived > &tr)
Definition: PermutationMatrix.h:83
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
constexpr Scalar & coeffRef(Index row, Index col)
Definition: DenseCoeffsBase.h:306
Class to view a vector of integers as a permutation matrix.
Definition: PermutationMatrix.h:453
void setIdentity()
Definition: PermutationMatrix.h:125
const IndicesType & indices() const
Definition: PermutationMatrix.h:116
constexpr Derived & derived()
Definition: EigenBase.h:49
PermutationMatrix(const PermutationBase< OtherDerived > &other)
Definition: PermutationMatrix.h:305
Derived & setZero()
Definition: CwiseNullaryOp.h:552
Derived & applyTranspositionOnTheLeft(Index i, Index j)
Definition: PermutationMatrix.h:146
DenseMatrixType eval() const
Definition: PermutationMatrix.h:113
InverseReturnType transpose() const
Definition: PermutationMatrix.h:180
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:191
void setIdentity(Index newSize)
Definition: PermutationMatrix.h:132
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:186
void resize(Index newSize)
Definition: PermutationMatrix.h:122
Index determinant() const
Definition: PermutationMatrix.h:231
PermutationMatrix(const MatrixBase< Other > &indices)
Definition: PermutationMatrix.h:315
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
PermutationMatrix & operator=(const PermutationBase< Other > &other)
Definition: PermutationMatrix.h:325
Index rows() const
Definition: PermutationMatrix.h:90