PermutationMatrix.h 21.1 KB
Newer Older
LM's avatar
LM committed
1 2 3 4
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
5
// Copyright (C) 2009-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
LM's avatar
LM committed
6
//
Don Gagne's avatar
Don Gagne committed
7 8 9
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
LM's avatar
LM committed
10 11 12 13

#ifndef EIGEN_PERMUTATIONMATRIX_H
#define EIGEN_PERMUTATIONMATRIX_H

Don Gagne's avatar
Don Gagne committed
14 15
namespace Eigen { 

16 17 18 19 20
namespace internal {

enum PermPermProduct_t {PermPermProduct};

} // end namespace internal
LM's avatar
LM committed
21 22 23 24 25 26

/** \class PermutationBase
  * \ingroup Core_Module
  *
  * \brief Base class for permutations
  *
27
  * \tparam Derived the derived class
LM's avatar
LM committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  *
  * This class is the base class for all expressions representing a permutation matrix,
  * internally stored as a vector of integers.
  * The convention followed here is that if \f$ \sigma \f$ is a permutation, the corresponding permutation matrix
  * \f$ P_\sigma \f$ is such that if \f$ (e_1,\ldots,e_p) \f$ is the canonical basis, we have:
  *  \f[ P_\sigma(e_i) = e_{\sigma(i)}. \f]
  * This convention ensures that for any two permutations \f$ \sigma, \tau \f$, we have:
  *  \f[ P_{\sigma\circ\tau} = P_\sigma P_\tau. \f]
  *
  * Permutation matrices are square and invertible.
  *
  * Notice that in addition to the member functions and operators listed here, there also are non-member
  * operator* to multiply any kind of permutation object with any kind of matrix expression (MatrixBase)
  * on either side.
  *
  * \sa class PermutationMatrix, class PermutationWrapper
  */
template<typename Derived>
class PermutationBase : public EigenBase<Derived>
{
    typedef internal::traits<Derived> Traits;
    typedef EigenBase<Derived> Base;
  public:

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    typedef typename Traits::IndicesType IndicesType;
    enum {
      Flags = Traits::Flags,
      RowsAtCompileTime = Traits::RowsAtCompileTime,
      ColsAtCompileTime = Traits::ColsAtCompileTime,
      MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
      MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
    };
61 62
    typedef typename Traits::StorageIndex StorageIndex;
    typedef Matrix<StorageIndex,RowsAtCompileTime,ColsAtCompileTime,0,MaxRowsAtCompileTime,MaxColsAtCompileTime>
LM's avatar
LM committed
63
            DenseMatrixType;
64
    typedef PermutationMatrix<IndicesType::SizeAtCompileTime,IndicesType::MaxSizeAtCompileTime,StorageIndex>
LM's avatar
LM committed
65
            PlainPermutationType;
66
    typedef PlainPermutationType PlainObject;
LM's avatar
LM committed
67
    using Base::derived;
68 69
    typedef Inverse<Derived> InverseReturnType;
    typedef void Scalar;
LM's avatar
LM committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    #endif

    /** Copies the other permutation into *this */
    template<typename OtherDerived>
    Derived& operator=(const PermutationBase<OtherDerived>& other)
    {
      indices() = other.indices();
      return derived();
    }

    /** Assignment from the Transpositions \a tr */
    template<typename OtherDerived>
    Derived& operator=(const TranspositionsBase<OtherDerived>& tr)
    {
      setIdentity(tr.size());
      for(Index k=size()-1; k>=0; --k)
        applyTranspositionOnTheRight(k,tr.coeff(k));
      return derived();
    }

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    /** This is a special case of the templated operator=. Its purpose is to
      * prevent a default operator= from hiding the templated operator=.
      */
    Derived& operator=(const PermutationBase& other)
    {
      indices() = other.indices();
      return derived();
    }
    #endif

    /** \returns the number of rows */
Don Gagne's avatar
Don Gagne committed
102
    inline Index rows() const { return Index(indices().size()); }
LM's avatar
LM committed
103 104

    /** \returns the number of columns */
Don Gagne's avatar
Don Gagne committed
105
    inline Index cols() const { return Index(indices().size()); }
LM's avatar
LM committed
106 107

    /** \returns the size of a side of the respective square matrix, i.e., the number of indices */
Don Gagne's avatar
Don Gagne committed
108
    inline Index size() const { return Index(indices().size()); }
LM's avatar
LM committed
109 110 111 112 113 114

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    template<typename DenseDerived>
    void evalTo(MatrixBase<DenseDerived>& other) const
    {
      other.setZero();
115
      for (Index i=0; i<rows(); ++i)
LM's avatar
LM committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        other.coeffRef(indices().coeff(i),i) = typename DenseDerived::Scalar(1);
    }
    #endif

    /** \returns a Matrix object initialized from this permutation matrix. Notice that it
      * is inefficient to return this Matrix object by value. For efficiency, favor using
      * the Matrix constructor taking EigenBase objects.
      */
    DenseMatrixType toDenseMatrix() const
    {
      return derived();
    }

    /** const version of indices(). */
    const IndicesType& indices() const { return derived().indices(); }
    /** \returns a reference to the stored array representing the permutation. */
    IndicesType& indices() { return derived().indices(); }

    /** Resizes to given size.
      */
Don Gagne's avatar
Don Gagne committed
136
    inline void resize(Index newSize)
LM's avatar
LM committed
137
    {
Don Gagne's avatar
Don Gagne committed
138
      indices().resize(newSize);
LM's avatar
LM committed
139 140 141 142 143
    }

    /** Sets *this to be the identity permutation matrix */
    void setIdentity()
    {
144 145
      StorageIndex n = StorageIndex(size());
      for(StorageIndex i = 0; i < n; ++i)
LM's avatar
LM committed
146 147 148 149 150
        indices().coeffRef(i) = i;
    }

    /** Sets *this to be the identity permutation matrix of given size.
      */
Don Gagne's avatar
Don Gagne committed
151
    void setIdentity(Index newSize)
LM's avatar
LM committed
152
    {
Don Gagne's avatar
Don Gagne committed
153
      resize(newSize);
LM's avatar
LM committed
154 155 156 157 158 159 160
      setIdentity();
    }

    /** Multiplies *this by the transposition \f$(ij)\f$ on the left.
      *
      * \returns a reference to *this.
      *
161
      * \warning This is much slower than applyTranspositionOnTheRight(Index,Index):
LM's avatar
LM committed
162 163
      * this has linear complexity and requires a lot of branching.
      *
164
      * \sa applyTranspositionOnTheRight(Index,Index)
LM's avatar
LM committed
165 166 167 168 169 170
      */
    Derived& applyTranspositionOnTheLeft(Index i, Index j)
    {
      eigen_assert(i>=0 && j>=0 && i<size() && j<size());
      for(Index k = 0; k < size(); ++k)
      {
171 172
        if(indices().coeff(k) == i) indices().coeffRef(k) = StorageIndex(j);
        else if(indices().coeff(k) == j) indices().coeffRef(k) = StorageIndex(i);
LM's avatar
LM committed
173 174 175 176 177 178 179 180 181 182
      }
      return derived();
    }

    /** Multiplies *this by the transposition \f$(ij)\f$ on the right.
      *
      * \returns a reference to *this.
      *
      * This is a fast operation, it only consists in swapping two indices.
      *
183
      * \sa applyTranspositionOnTheLeft(Index,Index)
LM's avatar
LM committed
184 185 186 187 188 189 190 191 192 193
      */
    Derived& applyTranspositionOnTheRight(Index i, Index j)
    {
      eigen_assert(i>=0 && j>=0 && i<size() && j<size());
      std::swap(indices().coeffRef(i), indices().coeffRef(j));
      return derived();
    }

    /** \returns the inverse permutation matrix.
      *
194
      * \note \blank \note_try_to_help_rvo
LM's avatar
LM committed
195
      */
196 197
    inline InverseReturnType inverse() const
    { return InverseReturnType(derived()); }
LM's avatar
LM committed
198 199
    /** \returns the tranpose permutation matrix.
      *
200
      * \note \blank \note_try_to_help_rvo
LM's avatar
LM committed
201
      */
202 203
    inline InverseReturnType transpose() const
    { return InverseReturnType(derived()); }
LM's avatar
LM committed
204 205 206 207 208 209 210 211 212

    /**** multiplication helpers to hopefully get RVO ****/

  
#ifndef EIGEN_PARSED_BY_DOXYGEN
  protected:
    template<typename OtherDerived>
    void assignTranspose(const PermutationBase<OtherDerived>& other)
    {
213
      for (Index i=0; i<rows();++i) indices().coeffRef(other.indices().coeff(i)) = i;
LM's avatar
LM committed
214 215 216 217 218
    }
    template<typename Lhs,typename Rhs>
    void assignProduct(const Lhs& lhs, const Rhs& rhs)
    {
      eigen_assert(lhs.cols() == rhs.rows());
219
      for (Index i=0; i<rows();++i) indices().coeffRef(i) = lhs.indices().coeff(rhs.indices().coeff(i));
LM's avatar
LM committed
220 221 222 223 224 225 226
    }
#endif

  public:

    /** \returns the product permutation matrix.
      *
227
      * \note \blank \note_try_to_help_rvo
LM's avatar
LM committed
228 229 230 231 232 233 234
      */
    template<typename Other>
    inline PlainPermutationType operator*(const PermutationBase<Other>& other) const
    { return PlainPermutationType(internal::PermPermProduct, derived(), other.derived()); }

    /** \returns the product of a permutation with another inverse permutation.
      *
235
      * \note \blank \note_try_to_help_rvo
LM's avatar
LM committed
236 237
      */
    template<typename Other>
238
    inline PlainPermutationType operator*(const InverseImpl<Other,PermutationStorage>& other) const
LM's avatar
LM committed
239 240 241 242
    { return PlainPermutationType(internal::PermPermProduct, *this, other.eval()); }

    /** \returns the product of an inverse permutation with another permutation.
      *
243
      * \note \blank \note_try_to_help_rvo
LM's avatar
LM committed
244 245
      */
    template<typename Other> friend
246
    inline PlainPermutationType operator*(const InverseImpl<Other, PermutationStorage>& other, const PermutationBase& perm)
LM's avatar
LM committed
247
    { return PlainPermutationType(internal::PermPermProduct, other.eval(), perm); }
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    
    /** \returns the determinant of the permutation matrix, which is either 1 or -1 depending on the parity of the permutation.
      *
      * This function is O(\c n) procedure allocating a buffer of \c n booleans.
      */
    Index determinant() const
    {
      Index res = 1;
      Index n = size();
      Matrix<bool,RowsAtCompileTime,1,0,MaxRowsAtCompileTime> mask(n);
      mask.fill(false);
      Index r = 0;
      while(r < n)
      {
        // search for the next seed
        while(r<n && mask[r]) r++;
        if(r>=n)
          break;
        // we got one, let's follow it until we are back to the seed
        Index k0 = r++;
        mask.coeffRef(k0) = true;
        for(Index k=indices().coeff(k0); k!=k0; k=indices().coeff(k))
        {
          mask.coeffRef(k) = true;
          res = -res;
        }
      }
      return res;
    }
LM's avatar
LM committed
277 278 279 280 281

  protected:

};

282 283 284 285 286 287 288 289 290 291 292 293
namespace internal {
template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
struct traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex> >
 : traits<Matrix<_StorageIndex,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
{
  typedef PermutationStorage StorageKind;
  typedef Matrix<_StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
  typedef _StorageIndex StorageIndex;
  typedef void Scalar;
};
}

LM's avatar
LM committed
294 295 296 297 298
/** \class PermutationMatrix
  * \ingroup Core_Module
  *
  * \brief Permutation matrix
  *
299 300 301
  * \tparam SizeAtCompileTime the number of rows/cols, or Dynamic
  * \tparam MaxSizeAtCompileTime the maximum number of rows/cols, or Dynamic. This optional parameter defaults to SizeAtCompileTime. Most of the time, you should not have to specify it.
  * \tparam _StorageIndex the integer type of the indices
LM's avatar
LM committed
302 303 304 305 306
  *
  * This class represents a permutation matrix, internally stored as a vector of integers.
  *
  * \sa class PermutationBase, class PermutationWrapper, class DiagonalMatrix
  */
307 308
template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
class PermutationMatrix : public PermutationBase<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex> >
LM's avatar
LM committed
309 310 311 312 313
{
    typedef PermutationBase<PermutationMatrix> Base;
    typedef internal::traits<PermutationMatrix> Traits;
  public:

314 315
    typedef const PermutationMatrix& Nested;

LM's avatar
LM committed
316 317
    #ifndef EIGEN_PARSED_BY_DOXYGEN
    typedef typename Traits::IndicesType IndicesType;
318
    typedef typename Traits::StorageIndex StorageIndex;
LM's avatar
LM committed
319 320 321 322 323 324 325
    #endif

    inline PermutationMatrix()
    {}

    /** Constructs an uninitialized permutation matrix of given size.
      */
326 327 328 329
    explicit inline PermutationMatrix(Index size) : m_indices(size)
    {
      eigen_internal_assert(size <= NumTraits<StorageIndex>::highest());
    }
LM's avatar
LM committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

    /** Copy constructor. */
    template<typename OtherDerived>
    inline PermutationMatrix(const PermutationBase<OtherDerived>& other)
      : m_indices(other.indices()) {}

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    /** Standard copy constructor. Defined only to prevent a default copy constructor
      * from hiding the other templated constructor */
    inline PermutationMatrix(const PermutationMatrix& other) : m_indices(other.indices()) {}
    #endif

    /** Generic constructor from expression of the indices. The indices
      * array has the meaning that the permutations sends each integer i to indices[i].
      *
      * \warning It is your responsibility to check that the indices array that you passes actually
      * describes a permutation, i.e., each value between 0 and n-1 occurs exactly once, where n is the
      * array's size.
      */
    template<typename Other>
350
    explicit inline PermutationMatrix(const MatrixBase<Other>& indices) : m_indices(indices)
LM's avatar
LM committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    {}

    /** Convert the Transpositions \a tr to a permutation matrix */
    template<typename Other>
    explicit PermutationMatrix(const TranspositionsBase<Other>& tr)
      : m_indices(tr.size())
    {
      *this = tr;
    }

    /** Copies the other permutation into *this */
    template<typename Other>
    PermutationMatrix& operator=(const PermutationBase<Other>& other)
    {
      m_indices = other.indices();
      return *this;
    }

    /** Assignment from the Transpositions \a tr */
    template<typename Other>
    PermutationMatrix& operator=(const TranspositionsBase<Other>& tr)
    {
      return Base::operator=(tr.derived());
    }

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    /** This is a special case of the templated operator=. Its purpose is to
      * prevent a default operator= from hiding the templated operator=.
      */
    PermutationMatrix& operator=(const PermutationMatrix& other)
    {
      m_indices = other.m_indices;
      return *this;
    }
    #endif

    /** const version of indices(). */
    const IndicesType& indices() const { return m_indices; }
    /** \returns a reference to the stored array representing the permutation. */
    IndicesType& indices() { return m_indices; }


    /**** multiplication helpers to hopefully get RVO ****/

#ifndef EIGEN_PARSED_BY_DOXYGEN
    template<typename Other>
397 398
    PermutationMatrix(const InverseImpl<Other,PermutationStorage>& other)
      : m_indices(other.derived().nestedExpression().size())
LM's avatar
LM committed
399
    {
400 401 402 403
      eigen_internal_assert(m_indices.size() <= NumTraits<StorageIndex>::highest());
      StorageIndex end = StorageIndex(m_indices.size());
      for (StorageIndex i=0; i<end;++i)
        m_indices.coeffRef(other.derived().nestedExpression().indices().coeff(i)) = i;
LM's avatar
LM committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    }
    template<typename Lhs,typename Rhs>
    PermutationMatrix(internal::PermPermProduct_t, const Lhs& lhs, const Rhs& rhs)
      : m_indices(lhs.indices().size())
    {
      Base::assignProduct(lhs,rhs);
    }
#endif

  protected:

    IndicesType m_indices;
};


namespace internal {
420 421 422
template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
struct traits<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex>,_PacketAccess> >
 : traits<Matrix<_StorageIndex,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
LM's avatar
LM committed
423
{
424 425 426 427
  typedef PermutationStorage StorageKind;
  typedef Map<const Matrix<_StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>, _PacketAccess> IndicesType;
  typedef _StorageIndex StorageIndex;
  typedef void Scalar;
LM's avatar
LM committed
428 429 430
};
}

431 432 433
template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
class Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex>,_PacketAccess>
  : public PermutationBase<Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, _StorageIndex>,_PacketAccess> >
LM's avatar
LM committed
434 435 436 437 438 439 440
{
    typedef PermutationBase<Map> Base;
    typedef internal::traits<Map> Traits;
  public:

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    typedef typename Traits::IndicesType IndicesType;
441
    typedef typename IndicesType::Scalar StorageIndex;
LM's avatar
LM committed
442 443
    #endif

444
    inline Map(const StorageIndex* indicesPtr)
Don Gagne's avatar
Don Gagne committed
445
      : m_indices(indicesPtr)
LM's avatar
LM committed
446 447
    {}

448
    inline Map(const StorageIndex* indicesPtr, Index size)
Don Gagne's avatar
Don Gagne committed
449
      : m_indices(indicesPtr,size)
LM's avatar
LM committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    {}

    /** Copies the other permutation into *this */
    template<typename Other>
    Map& operator=(const PermutationBase<Other>& other)
    { return Base::operator=(other.derived()); }

    /** Assignment from the Transpositions \a tr */
    template<typename Other>
    Map& operator=(const TranspositionsBase<Other>& tr)
    { return Base::operator=(tr.derived()); }

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    /** This is a special case of the templated operator=. Its purpose is to
      * prevent a default operator= from hiding the templated operator=.
      */
    Map& operator=(const Map& other)
    {
      m_indices = other.m_indices;
      return *this;
    }
    #endif

    /** const version of indices(). */
    const IndicesType& indices() const { return m_indices; }
    /** \returns a reference to the stored array representing the permutation. */
    IndicesType& indices() { return m_indices; }

  protected:

    IndicesType m_indices;
};

template<typename _IndicesType> class TranspositionsWrapper;
namespace internal {
template<typename _IndicesType>
struct traits<PermutationWrapper<_IndicesType> >
{
  typedef PermutationStorage StorageKind;
489 490
  typedef void Scalar;
  typedef typename _IndicesType::Scalar StorageIndex;
LM's avatar
LM committed
491 492 493 494
  typedef _IndicesType IndicesType;
  enum {
    RowsAtCompileTime = _IndicesType::SizeAtCompileTime,
    ColsAtCompileTime = _IndicesType::SizeAtCompileTime,
495 496 497
    MaxRowsAtCompileTime = IndicesType::MaxSizeAtCompileTime,
    MaxColsAtCompileTime = IndicesType::MaxSizeAtCompileTime,
    Flags = 0
LM's avatar
LM committed
498 499 500 501
  };
};
}

502 503 504 505 506 507 508 509 510 511 512
/** \class PermutationWrapper
  * \ingroup Core_Module
  *
  * \brief Class to view a vector of integers as a permutation matrix
  *
  * \tparam _IndicesType the type of the vector of integer (can be any compatible expression)
  *
  * This class allows to view any vector expression of integers as a permutation matrix.
  *
  * \sa class PermutationBase, class PermutationMatrix
  */
LM's avatar
LM committed
513 514 515 516 517 518 519 520 521 522 523
template<typename _IndicesType>
class PermutationWrapper : public PermutationBase<PermutationWrapper<_IndicesType> >
{
    typedef PermutationBase<PermutationWrapper> Base;
    typedef internal::traits<PermutationWrapper> Traits;
  public:

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    typedef typename Traits::IndicesType IndicesType;
    #endif

524 525
    inline PermutationWrapper(const IndicesType& indices)
      : m_indices(indices)
LM's avatar
LM committed
526 527 528 529 530 531 532 533
    {}

    /** const version of indices(). */
    const typename internal::remove_all<typename IndicesType::Nested>::type&
    indices() const { return m_indices; }

  protected:

Don Gagne's avatar
Don Gagne committed
534
    typename IndicesType::Nested m_indices;
LM's avatar
LM committed
535 536
};

537

LM's avatar
LM committed
538 539
/** \returns the matrix with the permutation applied to the columns.
  */
540 541 542 543 544
template<typename MatrixDerived, typename PermutationDerived>
EIGEN_DEVICE_FUNC
const Product<MatrixDerived, PermutationDerived, AliasFreeProduct>
operator*(const MatrixBase<MatrixDerived> &matrix,
          const PermutationBase<PermutationDerived>& permutation)
LM's avatar
LM committed
545
{
546 547
  return Product<MatrixDerived, PermutationDerived, AliasFreeProduct>
            (matrix.derived(), permutation.derived());
LM's avatar
LM committed
548 549 550 551
}

/** \returns the matrix with the permutation applied to the rows.
  */
552 553 554
template<typename PermutationDerived, typename MatrixDerived>
EIGEN_DEVICE_FUNC
const Product<PermutationDerived, MatrixDerived, AliasFreeProduct>
LM's avatar
LM committed
555
operator*(const PermutationBase<PermutationDerived> &permutation,
556
          const MatrixBase<MatrixDerived>& matrix)
LM's avatar
LM committed
557
{
558 559
  return Product<PermutationDerived, MatrixDerived, AliasFreeProduct>
            (permutation.derived(), matrix.derived());
LM's avatar
LM committed
560 561 562
}


563 564 565
template<typename PermutationType>
class InverseImpl<PermutationType, PermutationStorage>
  : public EigenBase<Inverse<PermutationType> >
LM's avatar
LM committed
566 567
{
    typedef typename PermutationType::PlainPermutationType PlainPermutationType;
568 569 570
    typedef internal::traits<PermutationType> PermTraits;
  protected:
    InverseImpl() {}
LM's avatar
LM committed
571
  public:
572 573
    typedef Inverse<PermutationType> InverseType;
    using EigenBase<Inverse<PermutationType> >::derived;
LM's avatar
LM committed
574 575

    #ifndef EIGEN_PARSED_BY_DOXYGEN
576
    typedef typename PermutationType::DenseMatrixType DenseMatrixType;
LM's avatar
LM committed
577
    enum {
578 579 580 581
      RowsAtCompileTime = PermTraits::RowsAtCompileTime,
      ColsAtCompileTime = PermTraits::ColsAtCompileTime,
      MaxRowsAtCompileTime = PermTraits::MaxRowsAtCompileTime,
      MaxColsAtCompileTime = PermTraits::MaxColsAtCompileTime
LM's avatar
LM committed
582 583 584 585 586 587 588 589
    };
    #endif

    #ifndef EIGEN_PARSED_BY_DOXYGEN
    template<typename DenseDerived>
    void evalTo(MatrixBase<DenseDerived>& other) const
    {
      other.setZero();
590 591
      for (Index i=0; i<derived().rows();++i)
        other.coeffRef(i, derived().nestedExpression().indices().coeff(i)) = typename DenseDerived::Scalar(1);
LM's avatar
LM committed
592 593 594 595
    }
    #endif

    /** \return the equivalent permutation matrix */
596
    PlainPermutationType eval() const { return derived(); }
LM's avatar
LM committed
597

598
    DenseMatrixType toDenseMatrix() const { return derived(); }
LM's avatar
LM committed
599 600 601 602

    /** \returns the matrix with the inverse permutation applied to the columns.
      */
    template<typename OtherDerived> friend
603 604
    const Product<OtherDerived, InverseType, AliasFreeProduct>
    operator*(const MatrixBase<OtherDerived>& matrix, const InverseType& trPerm)
LM's avatar
LM committed
605
    {
606
      return Product<OtherDerived, InverseType, AliasFreeProduct>(matrix.derived(), trPerm.derived());
LM's avatar
LM committed
607 608 609 610 611
    }

    /** \returns the matrix with the inverse permutation applied to the rows.
      */
    template<typename OtherDerived>
612
    const Product<InverseType, OtherDerived, AliasFreeProduct>
LM's avatar
LM committed
613 614
    operator*(const MatrixBase<OtherDerived>& matrix) const
    {
615
      return Product<InverseType, OtherDerived, AliasFreeProduct>(derived(), matrix.derived());
LM's avatar
LM committed
616 617 618 619 620 621 622 623 624
    }
};

template<typename Derived>
const PermutationWrapper<const Derived> MatrixBase<Derived>::asPermutation() const
{
  return derived();
}

625 626 627 628 629 630
namespace internal {

template<> struct AssignmentKind<DenseShape,PermutationShape> { typedef EigenBase2EigenBase Kind; };

} // end namespace internal

Don Gagne's avatar
Don Gagne committed
631 632
} // end namespace Eigen

LM's avatar
LM committed
633
#endif // EIGEN_PERMUTATIONMATRIX_H