#
# express permutation tau as a word in the generators of pg. If a
# stabilizer chain has been precomputed, it can be supplied as a third
# argument. FAIL is returned if tau does not belong to the group.
#
`coxeter/perm2word`:=proc(tau,pg) local res,pi,n,pt,i,w,g,sc,c;
  res:=NULL; pi:=tau; n:=1+nops(op(2,pg));
  if nargs>2 then sc:=args[3] else sc:=coxeter['stab_chain'](pg) fi;
  for g in sc while nops(pi)>0 do pt:=g[1];
    for c in pi do
      if member(pt,c,'i') then
        if nops(c)=i then pt:=c[1] else pt:=c[i+1] fi; break
      fi
    od;
    if not member(pt,g[2],'i') then RETURN(FAIL) fi;
    w:=g[3][i]; res:=op(w),res;
    pi:=coxeter['multperm']([n,seq(w[-i],i=-nops(w)..-1)],
      {op(op(2,pg)),cat('s',n)=pi});
  od;
  if pi=[] then [res] else FAIL fi;
end:
#
save `coxeter/perm2word`, cat(HomeLib,`/coxeter/perm2word.m`);
read cat(HomeLib,`/help/coxeter/perm2word`);
quit;

