From 883b8565c0dd3b3ce84bd61fd9f8112bebf68123 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Thu, 13 Nov 2025 17:05:57 +0100 Subject: [PATCH] added some operations to permutation.h --- permutation.cc | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ permutation.h | 25 +++++++++------ 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/permutation.cc b/permutation.cc index 2663b7b..bc56247 100644 --- a/permutation.cc +++ b/permutation.cc @@ -96,6 +96,39 @@ for(T i=1; i<=this->size(); ++i) q[i]=(*this)[this->size()-i+1]; return q; } + +template +NRPerm NRPerm::cutleft(int n) const +{ +if(n<0||n>size()) laerror("illegal n in ::cutleft"); +if(n==0) return *this; +for(int i=1; i<=n; ++i) if((*this)[i]!=i) laerror("left subset is not identity permutation"); +if(n==size()) return NRPerm(); +NRPerm res(size()-n); +for(int i=n+1; i<=size(); ++i) res[i-n] = (*this)[i]-n; +#ifdef DEBUG +if(!res.is_valid()) laerror("inetrnal error in cutleft"); +#endif +return res; +} + +template +NRPerm NRPerm::cutright(int n) const +{ +if(n<0||n>size()) laerror("illegal n in ::cutright"); +if(n==0) return *this; +for(int i=size(); i>size()-n; --i) if((*this)[i]!=i) laerror("right subset is not identity permutation"); +if(n==size()) return NRPerm(); +NRPerm res(size()-n); +for(int i=1; i<=size()-n; ++i) res[i] = (*this)[i]; +#ifdef DEBUG +if(!res.is_valid()) laerror("inetrnal error in cutleft"); +#endif +return res; +} + + + template NRPerm NRPerm::operator&(const NRPerm &q) const { @@ -730,6 +763,56 @@ for(k=n-1; k>=0; --k) *this = NRPerm(3,inv); } +template +PermutationAlgebra PermutationAlgebra::cutleft(int n) const +{ +PermutationAlgebra res(this->size()); +for(int i=0; isize(); ++i) {res[i].perm = (*this)[i].perm.cutleft(n) ; res[i].weight=(*this)[i].weight;} +return res; +} + +template +PermutationAlgebra PermutationAlgebra::cutright(int n) const +{ +PermutationAlgebra res(this->size()); +for(int i=0; isize(); ++i) {res[i].perm = (*this)[i].perm.cutright(n) ; res[i].weight=(*this)[i].weight;} +return res; +} + + +template +PermutationAlgebra PermutationAlgebra::operator&(const NRPerm &rhs) const +{ +PermutationAlgebra res(this->size()); +for(int i=0; isize(); ++i) {res[i].perm = (*this)[i].perm & rhs; res[i].weight=(*this)[i].weight;} +return res; +} + +template +PermutationAlgebra PermutationAlgebra::operator|(const NRPerm &rhs) const +{ +PermutationAlgebra res(this->size()); +for(int i=0; isize(); ++i) {res[i].perm = (*this)[i].perm | rhs; res[i].weight=(*this)[i].weight;} +return res; +} + +template +PermutationAlgebra PermutationAlgebra::operator*(const NRPerm &rhs) const +{ +PermutationAlgebra res(this->size()); +for(int i=0; isize(); ++i) {res[i].perm = (*this)[i].perm * rhs; res[i].weight=(*this)[i].weight;} +return res; +} + +template template +PermutationAlgebra NRPerm::operator*(const PermutationAlgebra &pa) const +{ +PermutationAlgebra res(pa.size()); +for(int i=0; i diff --git a/permutation.h b/permutation.h index 4400bd3..a926f60 100644 --- a/permutation.h +++ b/permutation.h @@ -65,8 +65,11 @@ public: NRPerm reverse() const; //backward order NRPerm operator&(const NRPerm &rhs) const; //concatenate the permutations this,rhs, renumbering rhs (not commutative) NRPerm operator|(const NRPerm &rhs) const; //concatenate the permutations rhs,this, renumbering rhs (not commutative) + NRPerm cutleft(int n) const; //remove left identity subset and renumber + NRPerm cutright(int n) const; //remove right identity subset NRPerm operator*(const NRPerm &q) const; //q is rhs and applied first, this applied second NRPerm operator*(const CyclePerm &r) const; + template PermutationAlgebra operator*(const PermutationAlgebra &pa) const; 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 @@ -117,13 +120,12 @@ public: bool is_identity() const {return weight==1 && is_scaledidentity();} bool is_plaindata() const {return false;}; WeightPermutation() : weight(0) {}; - WeightPermutation(const R w, const NRPerm &p) : weight(w), perm(p) {}; - WeightPermutation(const NRPerm &p) : perm(p) {weight= p.parity();}; + explicit WeightPermutation(const NRPerm &p, const R w=1) : weight(w), perm(p) {}; void copyonwrite() {perm.copyonwrite();}; - WeightPermutation operator&(const WeightPermutation &rhs) const {return WeightPermutation(weight*rhs.weight,perm&rhs.perm);}; - WeightPermutation operator|(const WeightPermutation &rhs) const {return WeightPermutation(weight*rhs.weight,perm|rhs.perm);}; - WeightPermutation operator*(const WeightPermutation &rhs) const {return WeightPermutation(weight*rhs.weight,perm*rhs.perm);}; - WeightPermutation operator*(const R &x) const {return WeightPermutation(weight*x,perm); } + WeightPermutation operator&(const WeightPermutation &rhs) const {return WeightPermutation(perm&rhs.perm,weight*rhs.weight);}; + WeightPermutation operator|(const WeightPermutation &rhs) const {return WeightPermutation(perm|rhs.perm,weight*rhs.weight);}; + WeightPermutation operator*(const WeightPermutation &rhs) const {return WeightPermutation(perm*rhs.perm,weight*rhs.weight);}; + WeightPermutation operator*(const R &x) const {return WeightPermutation(perm,weight*x); } bool operator==(const WeightPermutation &rhs) const {return this->perm == rhs.perm;}; //NOTE for sorting, compares only the permutation not the weight! bool operator!=(const WeightPermutation &rhs) const {return !(*this==rhs);} //NOTE: compares only the permutation @@ -186,9 +188,14 @@ public: void copyonwrite() {NRVec >::copyonwrite();}; int sort(int direction = 0, int from = 0, int to = -1, int *permut = NULL, bool stable=false) {return NRVec >::sort(direction,from, to,permut,stable);}; - PermutationAlgebra operator&(const PermutationAlgebra &rhs) const; - PermutationAlgebra operator|(const PermutationAlgebra &rhs) const; - PermutationAlgebra operator*(const PermutationAlgebra &rhs) const; + PermutationAlgebra operator&(const NRPerm &rhs) const; //applied to all terms + PermutationAlgebra operator|(const NRPerm &rhs) const; //applied to all terms + PermutationAlgebra operator*(const NRPerm &rhs) const; //applied to all terms + PermutationAlgebra cutleft(int n) const; //applied to all terms + PermutationAlgebra cutright(int n) const; //applied to all terms + PermutationAlgebra operator&(const PermutationAlgebra &rhs) const; //each term with each + PermutationAlgebra operator|(const PermutationAlgebra &rhs) const; //each term with each + PermutationAlgebra operator*(const PermutationAlgebra &rhs) const; //each term with each PermutationAlgebra operator+(const PermutationAlgebra &rhs) const; PermutationAlgebra &operator*=(const R &x) {this->copyonwrite(); for(int i=1; i<=size(); ++i) (*this)[i].weight *= x; return *this;}; PermutationAlgebra operator*(const R &x) const {PermutationAlgebra r(*this); return r*=x;};