class Permutation_Error(Exception): pass class Permutation(): def __init__(self,n,p=[]): if p==[]: self.p=[i for i in range(n)] else: self.p=p self.n=n def __str__(self): return "["+", ".join(map(str,self.p))+"]" def __mul__(self,other): T=[] n=max(self.n,other.n) for i in range(n): T.append(self.Replace(other.Replace(i))) return Permutation(n,T) def __truediv__(self,other): pass def __sgn__(self): if self.Minimum_Transposition()%2: return -1 else: return 1 def Inverse(self): Q=list(range(self.n)) for k in range(self.n): Q[self.p[k]]=k return Permutation(self.n,Q) def Transposition(self,u,v): a=self.p.index(u) b=self.p.index(v) self.p[a]=v self.p[b]=u def Minimum_Transposition(self): X=self.Cycle_Division() T=0 for d in X: T+=len(d)-1 return T def Cycle_Multiplication(self,*C): X=[self.p.index(k) for k in C] N=len(C) for i in range(N): self.p[X[i]]=C[(i+1)%N] def Cycle_Division(self): T=[False]*self.n k=0 v=0 A=[] for k in range(self.n): if (not T[k]) and self.p[k]!=k: v=k B=[k] while self.p[v]!=k: v=self.p[v] T[v]=True B.append(v) A.append(B) return A def Replace(self,x): if x