From 1ee984eda2b75c2edd79629fda54270937269f6c Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Wed, 24 Jan 2024 13:41:39 +0100 Subject: [PATCH] small additions to permutations --- permutation.cc | 43 +++++++++++++++++++++++++++++++++++++++---- permutation.h | 4 +++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/permutation.cc b/permutation.cc index 8df6e95..f8f5322 100644 --- a/permutation.cc +++ b/permutation.cc @@ -135,11 +135,27 @@ if(n!=q.size()) laerror("product of incompatible permutations"); NRPerm r(n); for(T i=1; i<=n; ++i) r[i] = (*this)[q[i]]; return r; - } template -NRPerm NRPerm::conjugate_by(const NRPerm &q) const +NRPerm NRPerm::multiply(const NRPerm &q, bool inverse) const +{ +#ifdef DEBUG + if(!this->is_valid() || !q.is_valid()) laerror("multiplication of invalid permutation"); +#endif + +T n=this->size(); +if(n!=q.size()) laerror("product of incompatible permutations"); +NRPerm r(n); +if(inverse) for(T i=1; i<=n; ++i) r[q[i]] = (*this)[i]; +else for(T i=1; i<=n; ++i) r[i] = (*this)[q[i]]; +return r; +} + + + +template +NRPerm NRPerm::conjugate_by(const NRPerm &q, bool inverse) const { #ifdef DEBUG if(!this->is_valid() || !q.is_valid()) laerror("multiplication of invalid permutation"); @@ -149,11 +165,30 @@ T n=this->size(); if(n!=q.size()) laerror("product of incompatible permutations"); NRPerm qi=q.inverse(); NRPerm r(n); -for(T i=1; i<=n; ++i) r[i] = qi[(*this)[q[i]]]; +if(inverse) for(T i=1; i<=n; ++i) r[i] = q[(*this)[qi[i]]]; +else for(T i=1; i<=n; ++i) r[i] = qi[(*this)[q[i]]]; return r; - } +template +NRPerm NRPerm::commutator(const NRPerm &q, bool inverse) const +{ +#ifdef DEBUG + if(!this->is_valid() || !q.is_valid()) laerror("multiplication of invalid permutation"); +#endif + +T n=this->size(); +if(n!=q.size()) laerror("product of incompatible permutations"); +NRPerm qi=q.inverse(); +NRPerm pi=this->inverse(); +NRPerm r(n); +if(inverse) for(T i=1; i<=n; ++i) r[i] = qi[pi[q[(*this)[i]]]]; +else for(T i=1; i<=n; ++i) r[i] = pi[qi[(*this)[q[i]]]]; +return r; +} + + + template CyclePerm CyclePerm::conjugate_by(const CyclePerm &q) const { diff --git a/permutation.h b/permutation.h index a90fb6b..6f67540 100644 --- a/permutation.h +++ b/permutation.h @@ -67,7 +67,9 @@ public: NRPerm operator|(const NRPerm &rhs) const; //concatenate the permutations rhs,this, renumbering rhs (not commutative) NRPerm operator*(const NRPerm &q) const; //q is rhs and applied first, this applied second NRPerm operator*(const CyclePerm &r) const; - NRPerm conjugate_by(const NRPerm &q) const; //q^-1 p q + NRPerm multiply(const NRPerm &q, bool inverse) const; //multiplication but optionally q inversed + NRPerm conjugate_by(const NRPerm &q, bool reverse=false) const; //q^-1 p q or q p q^-1 + NRPerm commutator(const NRPerm &q, bool inverse=false) const; //p^-1 q^-1 p q or q^-1 p^-1 q p int parity() const; //returns +/- 1 void randomize(void); //uniformly random by Fisher-Yates shuffle bool next(); //generate next permutation in lex order