11 #ifndef EIGEN_PERMUTATIONMATRIX_H
12 #define EIGEN_PERMUTATIONMATRIX_H
45 enum PermPermProduct_t {PermPermProduct};
49 template<
typename Derived>
52 typedef internal::traits<Derived> Traits;
56 #ifndef EIGEN_PARSED_BY_DOXYGEN
57 typedef typename Traits::IndicesType IndicesType;
59 Flags = Traits::Flags,
60 RowsAtCompileTime = Traits::RowsAtCompileTime,
61 ColsAtCompileTime = Traits::ColsAtCompileTime,
62 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
63 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
65 typedef typename Traits::StorageIndex StorageIndex;
75 template<
typename OtherDerived>
83 template<
typename OtherDerived>
84 Derived&
operator=(
const TranspositionsBase<OtherDerived>& tr)
92 #ifndef EIGEN_PARSED_BY_DOXYGEN
112 #ifndef EIGEN_PARSED_BY_DOXYGEN
113 template<
typename DenseDerived>
117 for (Index i=0; i<
rows(); ++i)
118 other.coeffRef(
indices().coeff(i),i) =
typename DenseDerived::Scalar(1);
146 StorageIndex n = StorageIndex(
size());
147 for(StorageIndex i = 0; i < n; ++i)
170 eigen_assert(i>=0 && j>=0 && i<
size() && j<
size());
173 if(
indices().coeff(k) == i)
indices().coeffRef(k) = StorageIndex(j);
174 else if(
indices().coeff(k) == j)
indices().coeffRef(k) = StorageIndex(i);
189 eigen_assert(i>=0 && j>=0 && i<
size() && j<
size());
199 {
return TransposeReturnType(
derived()); }
205 {
return TransposeReturnType(
derived()); }
210 #ifndef EIGEN_PARSED_BY_DOXYGEN
212 template<
typename OtherDerived>
217 template<
typename Lhs,
typename Rhs>
218 void assignProduct(
const Lhs& lhs,
const Rhs& rhs)
220 eigen_assert(lhs.cols() == rhs.rows());
221 for (
Index i=0; i<
rows();++i)
indices().coeffRef(i) = lhs.indices().coeff(rhs.indices().coeff(i));
231 template<
typename Other>
233 {
return PlainPermutationType(internal::PermPermProduct,
derived(), other.
derived()); }
239 template<
typename Other>
241 {
return PlainPermutationType(internal::PermPermProduct, *
this, other.eval()); }
247 template<
typename Other>
friend
249 {
return PlainPermutationType(internal::PermPermProduct, other.eval(), perm); }
265 while(r<n && mask[r]) r++;
270 mask.coeffRef(k0) =
true;
273 mask.coeffRef(k) =
true;
299 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename _StorageIndex>
300 struct traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex> >
301 : traits<Matrix<_StorageIndex,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
303 typedef PermutationStorage StorageKind;
304 typedef Matrix<_StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
305 typedef _StorageIndex StorageIndex;
309 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename _StorageIndex>
313 typedef internal::traits<PermutationMatrix> Traits;
318 #ifndef EIGEN_PARSED_BY_DOXYGEN
319 typedef typename Traits::IndicesType IndicesType;
320 typedef typename Traits::StorageIndex StorageIndex;
334 template<
typename OtherDerived>
336 : m_indices(other.
indices()) {}
338 #ifndef EIGEN_PARSED_BY_DOXYGEN
351 template<
typename Other>
356 template<
typename Other>
358 : m_indices(tr.
size())
364 template<
typename Other>
372 template<
typename Other>
378 #ifndef EIGEN_PARSED_BY_DOXYGEN
384 m_indices = other.m_indices;
390 const IndicesType&
indices()
const {
return m_indices; }
397 #ifndef EIGEN_PARSED_BY_DOXYGEN
398 template<
typename Other>
400 : m_indices(other.nestedExpression().
size())
403 StorageIndex end = StorageIndex(m_indices.size());
404 for (StorageIndex i=0; i<end;++i)
405 m_indices.coeffRef(other.nestedExpression().
indices().coeff(i)) = i;
407 template<
typename Lhs,
typename Rhs>
408 PermutationMatrix(internal::PermPermProduct_t,
const Lhs& lhs,
const Rhs& rhs)
411 Base::assignProduct(lhs,rhs);
417 IndicesType m_indices;
422 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename _StorageIndex,
int _PacketAccess>
423 struct traits<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex>,_PacketAccess> >
424 : traits<Matrix<_StorageIndex,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
426 typedef PermutationStorage StorageKind;
427 typedef Map<const Matrix<_StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>, _PacketAccess> IndicesType;
428 typedef _StorageIndex StorageIndex;
432 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename _StorageIndex,
int _PacketAccess>
433 class Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex>,_PacketAccess>
434 :
public PermutationBase<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex>,_PacketAccess> >
436 typedef PermutationBase<Map> Base;
437 typedef internal::traits<Map> Traits;
440 #ifndef EIGEN_PARSED_BY_DOXYGEN
441 typedef typename Traits::IndicesType IndicesType;
442 typedef typename IndicesType::Scalar StorageIndex;
445 inline Map(
const StorageIndex* indicesPtr)
446 : m_indices(indicesPtr)
449 inline Map(
const StorageIndex* indicesPtr, Index size)
450 : m_indices(indicesPtr,size)
454 template<
typename Other>
455 Map& operator=(
const PermutationBase<Other>& other)
456 {
return Base::operator=(other.derived()); }
459 template<
typename Other>
460 Map& operator=(
const TranspositionsBase<Other>& tr)
461 {
return Base::operator=(tr.derived()); }
463 #ifndef EIGEN_PARSED_BY_DOXYGEN
467 Map& operator=(
const Map& other)
469 m_indices = other.m_indices;
475 const IndicesType& indices()
const {
return m_indices; }
477 IndicesType& indices() {
return m_indices; }
481 IndicesType m_indices;
496 template<
typename _IndicesType>
class TranspositionsWrapper;
498 template<
typename _IndicesType>
499 struct traits<PermutationWrapper<_IndicesType> >
501 typedef PermutationStorage StorageKind;
502 typedef typename _IndicesType::Scalar Scalar;
503 typedef typename _IndicesType::Scalar StorageIndex;
504 typedef _IndicesType IndicesType;
506 RowsAtCompileTime = _IndicesType::SizeAtCompileTime,
507 ColsAtCompileTime = _IndicesType::SizeAtCompileTime,
508 MaxRowsAtCompileTime = IndicesType::MaxSizeAtCompileTime,
509 MaxColsAtCompileTime = IndicesType::MaxSizeAtCompileTime,
515 template<
typename _IndicesType>
519 typedef internal::traits<PermutationWrapper> Traits;
522 #ifndef EIGEN_PARSED_BY_DOXYGEN
523 typedef typename Traits::IndicesType IndicesType;
531 const typename internal::remove_all<typename IndicesType::Nested>::type&
536 typename IndicesType::Nested m_indices;
542 template<
typename MatrixDerived,
typename PermutationDerived>
544 const Product<MatrixDerived, PermutationDerived, AliasFreeProduct>
545 operator*(
const MatrixBase<MatrixDerived> &matrix,
546 const PermutationBase<PermutationDerived>& permutation)
548 return Product<MatrixDerived, PermutationDerived, AliasFreeProduct>
549 (matrix.derived(), permutation.derived());
554 template<
typename PermutationDerived,
typename MatrixDerived>
556 const Product<PermutationDerived, MatrixDerived, AliasFreeProduct>
557 operator*(
const PermutationBase<PermutationDerived> &permutation,
558 const MatrixBase<MatrixDerived>& matrix)
560 return Product<PermutationDerived, MatrixDerived, AliasFreeProduct>
561 (permutation.derived(), matrix.derived());
568 template<
typename Derived>
569 struct traits<Transpose<PermutationBase<Derived> > >
577 template<
typename Derived>
578 class Transpose<PermutationBase<Derived> >
579 :
public EigenBase<Transpose<PermutationBase<Derived> > >
581 typedef Derived PermutationType;
582 typedef typename PermutationType::IndicesType IndicesType;
583 typedef typename PermutationType::PlainPermutationType PlainPermutationType;
586 #ifndef EIGEN_PARSED_BY_DOXYGEN
587 typedef internal::traits<PermutationType> Traits;
588 typedef typename Derived::DenseMatrixType DenseMatrixType;
590 Flags = Traits::Flags,
591 RowsAtCompileTime = Traits::RowsAtCompileTime,
592 ColsAtCompileTime = Traits::ColsAtCompileTime,
593 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
594 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
596 typedef typename Traits::Scalar Scalar;
597 typedef typename Traits::StorageIndex StorageIndex;
600 Transpose(
const PermutationType& p) : m_permutation(p) {}
602 inline Index rows()
const {
return m_permutation.rows(); }
603 inline Index cols()
const {
return m_permutation.cols(); }
605 #ifndef EIGEN_PARSED_BY_DOXYGEN
606 template<
typename DenseDerived>
607 void evalTo(MatrixBase<DenseDerived>& other)
const
610 for (Index i=0; i<rows();++i)
611 other.coeffRef(i, m_permutation.indices().coeff(i)) =
typename DenseDerived::Scalar(1);
616 PlainPermutationType eval()
const {
return *
this; }
618 DenseMatrixType toDenseMatrix()
const {
return *
this; }
622 template<
typename OtherDerived>
friend
623 const Product<OtherDerived, Transpose, AliasFreeProduct>
624 operator*(
const MatrixBase<OtherDerived>& matrix,
const Transpose& trPerm)
626 return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trPerm.derived());
631 template<
typename OtherDerived>
632 const Product<Transpose, OtherDerived, AliasFreeProduct>
633 operator*(
const MatrixBase<OtherDerived>& matrix)
const
635 return Product<Transpose, OtherDerived, AliasFreeProduct>(*
this, matrix.derived());
641 const PermutationType& m_permutation;
644 template<
typename Derived>
645 const PermutationWrapper<const Derived> MatrixBase<Derived>::asPermutation()
const
652 template<>
struct AssignmentKind<DenseShape,PermutationShape> {
typedef EigenBase2EigenBase Kind; };
658 #endif // EIGEN_PERMUTATIONMATRIX_H
PermutationMatrix(const MatrixBase< Other > &indices)
Definition: PermutationMatrix.h:352
TransposeReturnType transpose() const
Definition: PermutationMatrix.h:204
const internal::remove_all< typename IndicesType::Nested >::type & indices() const
Definition: PermutationMatrix.h:532
Expression of the transpose of a matrix.
Definition: Transpose.h:53
Derived & applyTranspositionOnTheRight(Index i, Index j)
Definition: PermutationMatrix.h:187
Index size() const
Definition: PermutationMatrix.h:110
const IndicesType & indices() const
Definition: PermutationMatrix.h:132
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:107
friend PlainPermutationType operator*(const Transpose< PermutationBase< Other > > &other, const PermutationBase &perm)
Definition: PermutationMatrix.h:248
Derived & derived()
Definition: EigenBase.h:44
Derived & operator=(const PermutationBase< OtherDerived > &other)
Definition: PermutationMatrix.h:76
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
PermutationMatrix & operator=(const PermutationBase< Other > &other)
Definition: PermutationMatrix.h:365
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition: Map.h:123
Index rows() const
Definition: PermutationMatrix.h:104
Base class for permutations.
Definition: PermutationMatrix.h:50
Definition: EigenBase.h:28
Permutation matrix.
Definition: PermutationMatrix.h:310
PermutationMatrix & operator=(const TranspositionsBase< Other > &tr)
Definition: PermutationMatrix.h:373
IndicesType & indices()
Definition: PermutationMatrix.h:134
PlainPermutationType operator*(const PermutationBase< Other > &other) const
Definition: PermutationMatrix.h:232
void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:326
Derived & operator=(const TranspositionsBase< OtherDerived > &tr)
Definition: PermutationMatrix.h:84
DenseMatrixType toDenseMatrix() const
Definition: PermutationMatrix.h:126
Index determinant() const
Definition: PermutationMatrix.h:255
Class to view a vector of integers as a permutation matrix.
Definition: PermutationMatrix.h:516
void setIdentity()
Definition: PermutationMatrix.h:144
PermutationMatrix(const TranspositionsBase< Other > &tr)
Definition: PermutationMatrix.h:357
Definition: Eigen_Colamd.h:54
Derived & setZero()
Definition: CwiseNullaryOp.h:504
Derived & applyTranspositionOnTheLeft(Index i, Index j)
Definition: PermutationMatrix.h:168
PermutationMatrix(Index size)
Definition: PermutationMatrix.h:328
const IndicesType & indices() const
Definition: PermutationMatrix.h:390
IndicesType & indices()
Definition: PermutationMatrix.h:392
TransposeReturnType inverse() const
Definition: PermutationMatrix.h:198
void setIdentity(Index newSize)
Definition: PermutationMatrix.h:153
PlainPermutationType operator*(const Transpose< PermutationBase< Other > > &other) const
Definition: PermutationMatrix.h:240
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
void resize(Index newSize)
Definition: PermutationMatrix.h:138
const internal::remove_all< typename MatrixType::Nested >::type & nestedExpression() const
Definition: Transpose.h:73
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Index cols() const
Definition: PermutationMatrix.h:107
PermutationMatrix(const PermutationBase< OtherDerived > &other)
Definition: PermutationMatrix.h:335