#
# char_poly(w,R,q) returns det(1-q*w), relative to the reflection action.
# Modified to run faster for numeric q.
#
`coxeter/char_poly`:=proc(w) local S,z,vars,A,v,n,i,Z;
  S:=coxeter['base'](args[2]); A:=NULL;
  vars:=[op(indets(S))]; n:=nops(vars);
  for v in vars do;
    z:=coxeter['reflect'](seq(S[i],i=w),v);
    A:=A,[seq(coeff(z,vars[i]),i=1..n)];
  od;
  if n=0 then RETURN(1) fi;
  A:=array(1..n,1..n,[A]);
  if nargs>2 then z:=args[3] else z:=q fi;
  if n=nops(S) then
    A:=`coxeter/char_poly/mat`(A,n,z);
    expand(linalg['det'](A))
  elif type(z,'numeric') and z-1<>0 then 
    A:=`coxeter/char_poly/mat`(A,n,z);
    linalg['det'](A)/(1-z)^(n-nops(S))
  else
    A:=linalg['det'](`coxeter/char_poly/mat`(A,n,Z));
    subs(Z=z,expand(quo(A,(1-Z)^(n-nops(S)),Z)))
  fi;
end:
#
# this is faster than evalm()
#
if `+`(0)=0 then # we are using Maple V.4 or later
  `coxeter/char_poly/mat`:=proc(B,n,z)
    linalg['matadd'](linalg['band']([1],n),B,1,-z) end
else
  `coxeter/char_poly/mat`:=proc(B,n,z)
    linalg['add'](linalg['band']([1],n),B,1,-z) end
fi:
#
save `coxeter/char_poly`, `coxeter/char_poly/mat`,
  cat(HomeLib,`/coxeter/char_poly.m`);
read cat(HomeLib,`/help/coxeter/char_poly`);
quit;

