#
# reduce([i_1,...,i_r],R) returns a reduced expression representing the
#  same element of the Coxeter group as [i_1,...,i_r].
# If a third argument is present, the procedure returns a reduced *subword*
#  using the exchange property (a quadratic algorithm).
#
`coxeter/reduce`:=proc(w,R) local S,w0,i,v;
  S:=coxeter['base'](R);
  if nargs<3 then
    v:=coxeter['interior_pt'](S);
    v:=coxeter['reflect'](seq(S[i],i=w),v);
    coxeter['vec2fc'](v,S,'w0'); w0
  elif type(S,'list'('polynom'('rational'))) then
    `coxeter/reduce/subwd`(w,S,0)
  else
    `coxeter/reduce/subwd`(w,S,`coxeter/default`['epsilon'])
  fi
end:
#
`coxeter/reduce/subwd`:=proc(w,S,EPS) local w0,r,i;
  if nops(w)>1 then r:=S[w[1]] else RETURN(w) fi;
  w0:=procname(subsop(1=NULL,w),S,EPS);
  for i to nops(w0) do
    if EPS=0 then
      if S[w0[i]]=r then RETURN(subsop(i=NULL,w0)) fi
    elif convert(map(abs,[coeffs(S[w0[i]]-r)]),`+`)<EPS then
      RETURN(subsop(i=NULL,w0))
    fi;
    r:=coxeter['reflect'](S[w0[i]],r)
  od;
  [w[1],op(w0)];
end:
#
save `coxeter/reduce`, `coxeter/reduce/subwd`,
  cat(HomeLib,`/coxeter/reduce.m`);
read cat(HomeLib,`/help/coxeter/reduce`);
quit;

