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>
123 StorageIndex n = StorageIndex(
size());
124 for (StorageIndex i = 0; i < n; ++i)
indices().coeffRef(i) = i;
144 eigen_assert(i >= 0 && j >= 0 && i <
size() && j <
size());
147 indices().coeffRef(k) = StorageIndex(j);
148 else if (
indices().coeff(k) == j)
149 indices().coeffRef(k) = StorageIndex(i);
163 eigen_assert(i >= 0 && j >= 0 && i <
size() && j <
size());
172 inline InverseReturnType
inverse()
const {
return InverseReturnType(
derived()); }
181#ifndef EIGEN_PARSED_BY_DOXYGEN
183 template <
typename OtherDerived>
187 template <
typename Lhs,
typename Rhs>
188 void assignProduct(
const Lhs& lhs,
const Rhs& rhs) {
189 eigen_assert(lhs.cols() == rhs.rows());
190 for (Index i = 0; i <
rows(); ++i)
indices().coeffRef(i) = lhs.indices().coeff(rhs.indices().coeff(i));
199 template <
typename Other>
201 return PlainPermutationType(internal::PermPermProduct,
derived(), other.
derived());
208 template <
typename Other>
209 inline PlainPermutationType
operator*(
const InverseImpl<Other, PermutationStorage>& other)
const {
210 return PlainPermutationType(internal::PermPermProduct, *
this, other.eval());
217 template <
typename Other>
218 friend inline PlainPermutationType
operator*(
const InverseImpl<Other, PermutationStorage>& other,
220 return PlainPermutationType(internal::PermPermProduct, other.eval(), perm);
236 while (r < n && mask[r]) r++;
253template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_>
254struct traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> >
256 Matrix<StorageIndex_, SizeAtCompileTime, SizeAtCompileTime, 0, MaxSizeAtCompileTime, MaxSizeAtCompileTime> > {
257 typedef PermutationStorage StorageKind;
258 typedef Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
259 typedef StorageIndex_ StorageIndex;
278template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_>
280 :
public PermutationBase<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
282 typedef internal::traits<PermutationMatrix> Traits;
287#ifndef EIGEN_PARSED_BY_DOXYGEN
288 typedef typename Traits::IndicesType IndicesType;
289 typedef typename Traits::StorageIndex StorageIndex;
301 template <
typename OtherDerived>
311 template <
typename Other>
315 template <
typename Other>
321 template <
typename Other>
328 template <
typename Other>
334 const IndicesType&
indices()
const {
return m_indices; }
340#ifndef EIGEN_PARSED_BY_DOXYGEN
341 template <
typename Other>
343 : m_indices(other.
derived().nestedExpression().
size()) {
345 StorageIndex end = StorageIndex(m_indices.size());
346 for (StorageIndex i = 0; i < end; ++i)
347 m_indices.coeffRef(other.derived().nestedExpression().indices().coeff(i)) = i;
349 template <
typename Lhs,
typename Rhs>
350 PermutationMatrix(internal::PermPermProduct_t,
const Lhs& lhs,
const Rhs& rhs) : m_indices(lhs.
indices().
size()) {
351 Base::assignProduct(lhs, rhs);
356 IndicesType m_indices;
360template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_,
int PacketAccess_>
361struct traits<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_> >
363 Matrix<StorageIndex_, SizeAtCompileTime, SizeAtCompileTime, 0, MaxSizeAtCompileTime, MaxSizeAtCompileTime> > {
364 typedef PermutationStorage StorageKind;
365 typedef Map<const Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>, PacketAccess_> IndicesType;
366 typedef StorageIndex_ StorageIndex;
371template <
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename StorageIndex_,
int PacketAccess_>
372class Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_>
373 :
public PermutationBase<
374 Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_> > {
375 typedef PermutationBase<Map> Base;
376 typedef internal::traits<Map> Traits;
379#ifndef EIGEN_PARSED_BY_DOXYGEN
380 typedef typename Traits::IndicesType IndicesType;
381 typedef typename IndicesType::Scalar StorageIndex;
384 inline Map(
const StorageIndex* indicesPtr) : m_indices(indicesPtr) {}
386 inline Map(
const StorageIndex* indicesPtr, Index size) : m_indices(indicesPtr, size) {}
389 template <
typename Other>
390 Map& operator=(
const PermutationBase<Other>& other) {
391 return Base::operator=(other.derived());
395 template <
typename Other>
396 Map& operator=(
const TranspositionsBase<Other>& tr) {
397 return Base::operator=(tr.derived());
400#ifndef EIGEN_PARSED_BY_DOXYGEN
404 Map& operator=(
const Map& other) {
405 m_indices = other.m_indices;
411 const IndicesType& indices()
const {
return m_indices; }
413 IndicesType& indices() {
return m_indices; }
416 IndicesType m_indices;
419template <
typename IndicesType_>
420class TranspositionsWrapper;
422template <
typename IndicesType_>
423struct traits<PermutationWrapper<IndicesType_> > {
424 typedef PermutationStorage StorageKind;
426 typedef typename IndicesType_::Scalar StorageIndex;
427 typedef IndicesType_ IndicesType;
429 RowsAtCompileTime = IndicesType_::SizeAtCompileTime,
430 ColsAtCompileTime = IndicesType_::SizeAtCompileTime,
431 MaxRowsAtCompileTime = IndicesType::MaxSizeAtCompileTime,
432 MaxColsAtCompileTime = IndicesType::MaxSizeAtCompileTime,
449template <
typename IndicesType_>
452 typedef internal::traits<PermutationWrapper> Traits;
455#ifndef EIGEN_PARSED_BY_DOXYGEN
456 typedef typename Traits::IndicesType IndicesType;
462 const internal::remove_all_t<typename IndicesType::Nested>&
indices()
const {
return m_indices; }
465 typename IndicesType::Nested m_indices;
470template <
typename MatrixDerived,
typename PermutationDerived>
478template <
typename PermutationDerived,
typename MatrixDerived>
484template <
typename PermutationType>
485class InverseImpl<PermutationType, PermutationStorage> :
public EigenBase<Inverse<PermutationType> > {
486 typedef typename PermutationType::PlainPermutationType PlainPermutationType;
487 typedef internal::traits<PermutationType> PermTraits;
493 typedef Inverse<PermutationType> InverseType;
494 using EigenBase<Inverse<PermutationType> >::derived;
496#ifndef EIGEN_PARSED_BY_DOXYGEN
497 typedef typename PermutationType::DenseMatrixType DenseMatrixType;
499 RowsAtCompileTime = PermTraits::RowsAtCompileTime,
500 ColsAtCompileTime = PermTraits::ColsAtCompileTime,
501 MaxRowsAtCompileTime = PermTraits::MaxRowsAtCompileTime,
502 MaxColsAtCompileTime = PermTraits::MaxColsAtCompileTime
506#ifndef EIGEN_PARSED_BY_DOXYGEN
507 template <
typename DenseDerived>
508 void evalTo(MatrixBase<DenseDerived>& other)
const {
510 for (Index i = 0; i < derived().rows(); ++i)
511 other.coeffRef(i, derived().nestedExpression().indices().coeff(i)) =
typename DenseDerived::Scalar(1);
516 PlainPermutationType eval()
const {
return derived(); }
518 DenseMatrixType toDenseMatrix()
const {
return derived(); }
522 template <
typename OtherDerived>
523 friend const Product<OtherDerived, InverseType, AliasFreeProduct>
operator*(
const MatrixBase<OtherDerived>& matrix,
524 const InverseType& trPerm) {
525 return Product<OtherDerived, InverseType, AliasFreeProduct>(matrix.derived(), trPerm.derived());
530 template <
typename OtherDerived>
531 const Product<InverseType, OtherDerived, AliasFreeProduct>
operator*(
const MatrixBase<OtherDerived>& matrix)
const {
532 return Product<InverseType, OtherDerived, AliasFreeProduct>(derived(), matrix.derived());
536template <
typename Derived>
537const PermutationWrapper<const Derived> MatrixBase<Derived>::asPermutation()
const {
544struct AssignmentKind<DenseShape, PermutationShape> {
545 typedef EigenBase2EigenBase Kind;
void fill(const Scalar &value)
Definition CwiseNullaryOp.h:335
Derived & setZero()
Definition CwiseNullaryOp.h:549
Derived & derived()
Definition EigenBase.h:49
Scalar & coeffRef(Index row, Index col)
Definition DenseCoeffsBase.h:301
Expression of the inverse of another expression.
Definition Inverse.h:43
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:217
Base class for permutations.
Definition PermutationMatrix.h:49
InverseReturnType transpose() const
Definition PermutationMatrix.h:177
Derived & applyTranspositionOnTheLeft(Index i, Index j)
Definition PermutationMatrix.h:143
void resize(Index newSize)
Definition PermutationMatrix.h:119
Index determinant() const
Definition PermutationMatrix.h:228
Index size() const
Definition PermutationMatrix.h:96
Index cols() const
Definition PermutationMatrix.h:93
Derived & applyTranspositionOnTheRight(Index i, Index j)
Definition PermutationMatrix.h:162
friend PlainPermutationType operator*(const InverseImpl< Other, PermutationStorage > &other, const PermutationBase &perm)
Definition PermutationMatrix.h:218
void setIdentity()
Definition PermutationMatrix.h:122
Derived & operator=(const PermutationBase< OtherDerived > &other)
Definition PermutationMatrix.h:76
IndicesType & indices()
Definition PermutationMatrix.h:115
void setIdentity(Index newSize)
Definition PermutationMatrix.h:129
PlainPermutationType operator*(const InverseImpl< Other, PermutationStorage > &other) const
Definition PermutationMatrix.h:209
const IndicesType & indices() const
Definition PermutationMatrix.h:113
Index rows() const
Definition PermutationMatrix.h:90
InverseReturnType inverse() const
Definition PermutationMatrix.h:172
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:200
Permutation matrix.
Definition PermutationMatrix.h:280
PermutationMatrix(const PermutationBase< OtherDerived > &other)
Definition PermutationMatrix.h:302
PermutationMatrix(const TranspositionsBase< Other > &tr)
Definition PermutationMatrix.h:316
PermutationMatrix & operator=(const PermutationBase< Other > &other)
Definition PermutationMatrix.h:322
PermutationMatrix(const MatrixBase< Other > &indices)
Definition PermutationMatrix.h:312
PermutationMatrix(Index size)
Definition PermutationMatrix.h:296
const IndicesType & indices() const
Definition PermutationMatrix.h:334
IndicesType & indices()
Definition PermutationMatrix.h:336
PermutationMatrix & operator=(const TranspositionsBase< Other > &tr)
Definition PermutationMatrix.h:329
Class to view a vector of integers as a permutation matrix.
Definition PermutationMatrix.h:450
const internal::remove_all_t< typename IndicesType::Nested > & indices() const
Definition PermutationMatrix.h:462
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:202
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition PermutationMatrix.h:471
Definition EigenBase.h:33
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:43
Derived & derived()
Definition EigenBase.h:49
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523