26 #ifndef EIGEN_PERMUTATIONMATRIX_H
27 #define EIGEN_PERMUTATIONMATRIX_H
31 template<
int RowCol,
typename IndicesType,
typename MatrixType,
typename StorageKind>
class PermutedImpl;
59 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed=false>
60 struct permut_matrix_product_retval;
61 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed=false>
62 struct permut_sparsematrix_product_retval;
67 template<
typename Derived>
70 typedef internal::traits<Derived> Traits;
74 #ifndef EIGEN_PARSED_BY_DOXYGEN
75 typedef typename Traits::IndicesType IndicesType;
77 Flags = Traits::Flags,
78 CoeffReadCost = Traits::CoeffReadCost,
79 RowsAtCompileTime = Traits::RowsAtCompileTime,
80 ColsAtCompileTime = Traits::ColsAtCompileTime,
81 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
82 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
84 typedef typename Traits::Scalar Scalar;
85 typedef typename Traits::Index
Index;
94 template<
typename OtherDerived>
102 template<
typename OtherDerived>
111 #ifndef EIGEN_PARSED_BY_DOXYGEN
131 #ifndef EIGEN_PARSED_BY_DOXYGEN
132 template<
typename DenseDerived>
136 for (
int i=0; i<
rows();++i)
137 other.coeffRef(
indices().coeff(i),i) =
typename DenseDerived::Scalar(1);
228 #ifndef EIGEN_PARSED_BY_DOXYGEN
230 template<
typename OtherDerived>
235 template<
typename Lhs,
typename Rhs>
236 void assignProduct(
const Lhs& lhs,
const Rhs& rhs)
239 for (
int i=0; i<
rows();++i)
indices().coeffRef(i) = lhs.indices().coeff(rhs.indices().coeff(i));
249 template<
typename Other>
257 template<
typename Other>
265 template<
typename Other>
friend
288 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType>
289 struct traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType> >
290 :
traits<Matrix<IndexType,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
292 typedef IndexType Index;
293 typedef Matrix<IndexType, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
297 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType>
301 typedef internal::traits<PermutationMatrix> Traits;
304 #ifndef EIGEN_PARSED_BY_DOXYGEN
305 typedef typename Traits::IndicesType IndicesType;
317 template<
typename OtherDerived>
321 #ifndef EIGEN_PARSED_BY_DOXYGEN
334 template<
typename Other>
339 template<
typename Other>
347 template<
typename Other>
355 template<
typename Other>
361 #ifndef EIGEN_PARSED_BY_DOXYGEN
380 #ifndef EIGEN_PARSED_BY_DOXYGEN
381 template<
typename Other>
387 template<
typename Lhs,
typename Rhs>
391 Base::assignProduct(lhs,rhs);
402 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType,
int _PacketAccess>
403 struct traits<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess> >
404 :
traits<Matrix<IndexType,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
406 typedef IndexType Index;
411 template<
int SizeAtCompileTime,
int MaxSizeAtCompileTime,
typename IndexType,
int _PacketAccess>
413 :
public PermutationBase<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,_PacketAccess> >
416 typedef internal::traits<Map> Traits;
419 #ifndef EIGEN_PARSED_BY_DOXYGEN
420 typedef typename Traits::IndicesType IndicesType;
421 typedef typename IndicesType::Scalar
Index;
429 : m_indices(indices,size)
433 template<
typename Other>
435 {
return Base::operator=(other.
derived()); }
438 template<
typename Other>
440 {
return Base::operator=(tr.
derived()); }
442 #ifndef EIGEN_PARSED_BY_DOXYGEN
446 Map& operator=(
const Map& other)
448 m_indices = other.m_indices;
454 const IndicesType&
indices()
const {
return m_indices; }
479 template<
typename _IndicesType>
483 typedef typename _IndicesType::Scalar Scalar;
484 typedef typename _IndicesType::Scalar Index;
485 typedef _IndicesType IndicesType;
487 RowsAtCompileTime = _IndicesType::SizeAtCompileTime,
488 ColsAtCompileTime = _IndicesType::SizeAtCompileTime,
489 MaxRowsAtCompileTime = IndicesType::MaxRowsAtCompileTime,
490 MaxColsAtCompileTime = IndicesType::MaxColsAtCompileTime,
492 CoeffReadCost = _IndicesType::CoeffReadCost
497 template<
typename _IndicesType>
501 typedef internal::traits<PermutationWrapper> Traits;
504 #ifndef EIGEN_PARSED_BY_DOXYGEN
505 typedef typename Traits::IndicesType IndicesType;
513 const typename internal::remove_all<typename IndicesType::Nested>::type&
523 template<
typename Derived,
typename PermutationDerived>
524 inline const internal::permut_matrix_product_retval<PermutationDerived, Derived, OnTheRight>
528 return internal::permut_matrix_product_retval
530 (permutation.
derived(), matrix.derived());
535 template<
typename Derived,
typename PermutationDerived>
536 inline const internal::permut_matrix_product_retval
541 return internal::permut_matrix_product_retval
543 (permutation.
derived(), matrix.derived());
548 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed>
549 struct traits<permut_matrix_product_retval<PermutationType, MatrixType, Side, Transposed> >
551 typedef typename MatrixType::PlainObject ReturnType;
554 template<
typename PermutationType,
typename MatrixType,
int S
ide,
bool Transposed>
555 struct permut_matrix_product_retval
556 :
public ReturnByValue<permut_matrix_product_retval<PermutationType, MatrixType, Side, Transposed> >
558 typedef typename remove_all<typename MatrixType::Nested>::type MatrixTypeNestedCleaned;
560 permut_matrix_product_retval(
const PermutationType& perm,
const MatrixType& matrix)
561 : m_permutation(perm), m_matrix(matrix)
564 inline int rows()
const {
return m_matrix.rows(); }
565 inline int cols()
const {
return m_matrix.cols(); }
567 template<
typename Dest>
inline void evalTo(Dest& dst)
const
574 Matrix<bool,PermutationType::RowsAtCompileTime,1,0,PermutationType::MaxRowsAtCompileTime> mask(m_permutation.size());
577 while(r < m_permutation.size())
580 while(r<m_permutation.size() && mask[r]) r++;
581 if(r>=m_permutation.size())
586 mask.coeffRef(k0) =
true;
587 for(
int k=m_permutation.indices().coeff(k0); k!=k0; k=m_permutation.indices().coeff(k))
589 Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>(dst, k)
590 .swap(Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>
591 (dst,((Side==
OnTheLeft) ^ Transposed) ? k0 : kPrev));
593 mask.coeffRef(k) =
true;
600 for(
int i = 0; i < n; ++i)
602 Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>
603 (dst, ((Side==
OnTheLeft) ^ Transposed) ? m_permutation.indices().coeff(i) : i)
607 Block<const MatrixTypeNestedCleaned,Side==OnTheLeft ? 1 : MatrixType::RowsAtCompileTime,Side==OnTheRight ? 1 : MatrixType::ColsAtCompileTime>
608 (m_matrix, ((Side==
OnTheRight) ^ Transposed) ? m_permutation.indices().coeff(i) : i);
614 const PermutationType& m_permutation;
615 typename MatrixType::Nested m_matrix;
620 template<
typename Derived>
621 struct traits<Transpose<PermutationBase<Derived> > >
627 template<
typename Derived>
629 :
public EigenBase<Transpose<PermutationBase<Derived> > >
631 typedef Derived PermutationType;
632 typedef typename PermutationType::IndicesType IndicesType;
633 typedef typename PermutationType::PlainPermutationType PlainPermutationType;
636 #ifndef EIGEN_PARSED_BY_DOXYGEN
637 typedef internal::traits<PermutationType> Traits;
638 typedef typename Derived::DenseMatrixType DenseMatrixType;
640 Flags = Traits::Flags,
641 CoeffReadCost = Traits::CoeffReadCost,
642 RowsAtCompileTime = Traits::RowsAtCompileTime,
643 ColsAtCompileTime = Traits::ColsAtCompileTime,
644 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
645 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
647 typedef typename Traits::Scalar Scalar;
652 inline int rows()
const {
return m_permutation.rows(); }
653 inline int cols()
const {
return m_permutation.cols(); }
655 #ifndef EIGEN_PARSED_BY_DOXYGEN
656 template<
typename DenseDerived>
660 for (
int i=0; i<
rows();++i)
661 other.coeffRef(i, m_permutation.indices().coeff(i)) =
typename DenseDerived::Scalar(1);
666 PlainPermutationType
eval()
const {
return *
this; }
672 template<
typename OtherDerived>
friend
673 inline const internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheRight, true>
676 return internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheRight, true>(trPerm.m_permutation, matrix.derived());
681 template<
typename OtherDerived>
682 inline const internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheLeft, true>
685 return internal::permut_matrix_product_retval<PermutationType, OtherDerived, OnTheLeft, true>(m_permutation, matrix.derived());
694 template<
typename Derived>
702 #endif // EIGEN_PERMUTATIONMATRIX_H