#
# Stabilizer chain for a permutation rep pg of a reflection group W.
# Works under the assumption that the representation is isomorphic to
#  the action of W on some proper parabolic subgroup.
# For a chain W > W1 > W2 > ..., the output is [[b1,O1,X1],[b2,O2,X2],...],
#  where W2 is the stabilizer of b2 in W1, O2 is the list of points in
#  the W1-orbit of b2, X2 is a list of coset reps for W2/W1, ....
# In most cases, if the generators [1,...,n] of W are listed in the standard
#  order, one can take W.i to be generated by [1,...,n-i], so the algorithm
#  tries to do this first before trying any other possibilities.
#
`coxeter/stab_chain`:=proc(pg) local n,i,pi,alive,X,x,b,new,j,gens,reps,ct,k;
  n:=nops(op(2,pg)); alive:=[$1..n]; gens:=NULL;
  pi:=subs(op(2,pg),[seq(cat('s',i),i=1..n)]);
  pi:=[seq(map(proc(y) if nops(y)=2 then op(y) else
    ERROR(`improper permutation representation`) fi end,pi[i]),i=1..n)];
  while n>0 do;
    X:={seq(op(pi[alive[i]]),i=1..n-1)};
    b:={op(pi[alive[n]])} minus X;
    if nops(b)>0 then
      b:=min(op(b)); new:=subsop(n=NULL,alive);
    else
      new:=-1; ct:=table([seq(x=NULL,x=X)]);
      for i in alive do
        for x in X minus {op(pi[i])} do ct[x]:=ct[x],i od;
      od;
      for x in X do k:=nops({ct[x]});
        if k>new then new:=k; b:=x fi
      od;
      new:=[ct[b]]
    fi;
    X:=[b]; reps:=[[]];
    for j while j<=nops(X) do
      for i in alive do
        if member(X[j],pi[i],'k') then 
          x:=pi[i][k-1+2*modp(k,2)];     # we know pi[i]^2=1
          if not member(x,X) then X:=[op(X),x];
            reps:=[op(reps),[op(reps[j]),i]]
          fi
        fi
      od
    od;
    gens:=gens,[b,X,reps]; alive:=new; n:=nops(alive);
    userinfo(2,'stab_chain',`Orbit size:`,nops(X),`Stabilizer:`,alive);
  od; [gens]
end:
#
save `coxeter/stab_chain`, cat(HomeLib,`/coxeter/stab_chain.m`);
read cat(HomeLib,`/help/coxeter/stab_chain`);
quit;
 
