#
# Let S be a set of equations of the form {s1=perm1,s2=perm2,...},
#  where the perms are permutations in disjoint cycle notation.
# For any word w=[i1,i2,i3,...], multperm(w,S) computes the product
#  s.i1 * s.i2 * ... in disjoint cycle form.
# The degree can be passed as an optional third argument.
# Alternatively, the second argument can be a permgroup; i.e.,
#  multperm(w,pg), where pg = permgroup(m,S), and S is as above.
# 
`coxeter/multperm`:=proc(w) local S,alive,res,i,j,pi,c,cyc,g;
  if nargs>2 then
    S:=args[2]; alive:={$1..args[3]};
  elif type(args[2],'function') then
    S:=op(2,args[2]); alive:={$1..op(1,args[2])}
  else S:=args[2];
    g:={seq(cat('s',i),i=[op({op(w)})])};
    alive:=map(op,map(op,subs(S,g)))
  fi;
  res:=NULL; pi:=proc(k,x) x end;
  pi:=subsop(4=NULL,eval(pi));    # why is this necessary?
  for i in {op(w)} do
    for c in subs(S,cat('s',i)) do
      pi(i,c[nops(c)]):=c[1];
      for j to nops(c)-1 do pi(i,c[j]):=c[j+1] od
    od
  od;
  while nops(alive)>0 do
    j:=min(op(alive)); cyc:=j; i:=j;
    do
      for g in w do i:=pi(g,i) od;
      if i=j then break else cyc:=cyc,i fi;
    od;
    alive:=alive minus {cyc};
    if nops([cyc])>1 then res:=res,[cyc] fi;
  od; [res]
end:
#
save `coxeter/multperm`, cat(HomeLib,`/coxeter/multperm.m`);
read cat(HomeLib,`/help/coxeter/multperm`);
quit;

