diff options
author | Gael Guennebaud <g.gael@free.fr> | 2015-02-16 19:08:25 +0100 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2015-02-16 19:08:25 +0100 |
commit | 8768ff3c3134042aa838851191d4587835cbbccd (patch) | |
tree | 3c93b224adeea7d9c671e591dd83df937dcfa977 /Eigen/src/Core/PermutationMatrix.h | |
parent | 64b29e06b9f23a496b28fab4b9e29680bc486cc1 (diff) |
Add PermutationMatrix::determinant method.
Diffstat (limited to 'Eigen/src/Core/PermutationMatrix.h')
-rw-r--r-- | Eigen/src/Core/PermutationMatrix.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Eigen/src/Core/PermutationMatrix.h b/Eigen/src/Core/PermutationMatrix.h index de824c129..065000c12 100644 --- a/Eigen/src/Core/PermutationMatrix.h +++ b/Eigen/src/Core/PermutationMatrix.h @@ -251,6 +251,35 @@ class PermutationBase : public EigenBase<Derived> template<typename Other> friend inline PlainPermutationType operator*(const Transpose<PermutationBase<Other> >& other, const PermutationBase& perm) { return PlainPermutationType(internal::PermPermProduct, other.eval(), perm); } + + /** \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; + } protected: |