11#ifndef EIGEN_PERMUTATIONMATRIX_H
12#define EIGEN_PERMUTATIONMATRIX_H
15#include "./InternalHeaderCheck.h"
21enum PermPermProduct_t { PermPermProduct };
48template <
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>
102 for (
Index i = 0; i <
rows(); ++i) other.coeffRef(
indices().coeff(i), i) =
typename DenseDerived::Scalar(1);
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++;
256template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_>
257struct 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;
281template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_>
282class PermutationMatrix
283 :
public PermutationBase<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
285 typedef internal::traits<PermutationMatrix> Traits;
288 typedef const PermutationMatrix& Nested;
290#ifndef EIGEN_PARSED_BY_DOXYGEN
291 typedef typename Traits::IndicesType IndicesType;
292 typedef typename Traits::StorageIndex StorageIndex;
295 inline PermutationMatrix() {}
300 eigen_internal_assert(
size <= NumTraits<StorageIndex>::highest());
304 template <
typename OtherDerived>
314 template <
typename Other>
318 template <
typename Other>
324 template <
typename Other>
331 template <
typename Other>
332 PermutationMatrix&
operator=(
const TranspositionsBase<Other>& tr) {
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()) {
347 eigen_internal_assert(m_indices.size() <= NumTraits<StorageIndex>::highest());
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;
363template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_,
int PacketAccess_>
364struct 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;
374template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_,
int PacketAccess_>
375class Map<
PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_>
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;
422template <
typename IndicesType_>
423class TranspositionsWrapper;
425template <
typename IndicesType_>
426struct 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,
452template <
typename IndicesType_>
455 typedef internal::traits<PermutationWrapper> Traits;
458#ifndef EIGEN_PARSED_BY_DOXYGEN
459 typedef typename Traits::IndicesType IndicesType;
462 inline PermutationWrapper(
const IndicesType&
indices) : m_indices(
indices) {}
465 const internal::remove_all_t<typename IndicesType::Nested>&
indices()
const {
return m_indices; }
468 typename IndicesType::Nested m_indices;
473template <
typename MatrixDerived,
typename PermutationDerived>
481template <
typename PermutationDerived,
typename MatrixDerived>
487template <
typename PermutationType>
488class 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());
539template <
typename Derived>
547struct AssignmentKind<DenseShape, PermutationShape> {
548 typedef EigenBase2EigenBase Kind;
Derived & setZero()
Definition CwiseNullaryOp.h:552
Expression of the inverse of another expression.
Definition Inverse.h:43
A matrix or vector expression mapping an existing array of data.
Definition Map.h:96
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition Map.h:123
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition PlainObjectBase.h:191
Base class for permutations.
Definition PermutationMatrix.h:49
InverseReturnType transpose() const
Definition PermutationMatrix.h:180
Derived & applyTranspositionOnTheLeft(Index i, Index j)
Definition PermutationMatrix.h:146
void resize(Index newSize)
Definition PermutationMatrix.h:122
Index determinant() const
Definition PermutationMatrix.h:231
Index size() const
Definition PermutationMatrix.h:96
Index cols() const
Definition PermutationMatrix.h:93
DenseMatrixType eval() const
Definition PermutationMatrix.h:113
Derived & applyTranspositionOnTheRight(Index i, Index j)
Definition PermutationMatrix.h:165
friend PlainPermutationType operator*(const InverseImpl< Other, PermutationStorage > &other, const PermutationBase &perm)
Definition PermutationMatrix.h:221
void setIdentity()
Definition PermutationMatrix.h:125
Derived & operator=(const PermutationBase< OtherDerived > &other)
Definition PermutationMatrix.h:76
IndicesType & indices()
Definition PermutationMatrix.h:118
void setIdentity(Index newSize)
Definition PermutationMatrix.h:132
PlainPermutationType operator*(const InverseImpl< Other, PermutationStorage > &other) const
Definition PermutationMatrix.h:212
const IndicesType & indices() const
Definition PermutationMatrix.h:116
Index rows() const
Definition PermutationMatrix.h:90
InverseReturnType inverse() const
Definition PermutationMatrix.h:175
DenseMatrixType toDenseMatrix() const
Definition PermutationMatrix.h:110
Derived & operator=(const TranspositionsBase< OtherDerived > &tr)
Definition PermutationMatrix.h:83
PlainPermutationType operator*(const PermutationBase< Other > &other) const
Definition PermutationMatrix.h:203
Permutation matrix.
Definition PermutationMatrix.h:283
PermutationMatrix(const PermutationBase< OtherDerived > &other)
Definition PermutationMatrix.h:305
PermutationMatrix(const TranspositionsBase< Other > &tr)
Definition PermutationMatrix.h:319
PermutationMatrix & operator=(const PermutationBase< Other > &other)
Definition PermutationMatrix.h:325
PermutationMatrix(const MatrixBase< Other > &indices)
Definition PermutationMatrix.h:315
PermutationMatrix(Index size)
Definition PermutationMatrix.h:299
const IndicesType & indices() const
Definition PermutationMatrix.h:337
IndicesType & indices()
Definition PermutationMatrix.h:339
PermutationMatrix & operator=(const TranspositionsBase< Other > &tr)
Definition PermutationMatrix.h:332
Class to view a vector of integers as a permutation matrix.
Definition PermutationMatrix.h:453
const internal::remove_all_t< typename IndicesType::Nested > & indices() const
Definition PermutationMatrix.h:465
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
const Product< MatrixDerived, PermutationDerived, DefaultProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition PermutationMatrix.h:474
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
Definition EigenBase.h:33
constexpr Derived & derived()
Definition EigenBase.h:49
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:43