#
# char_poly(P,z) returns the characteristic polynomial of a poset P with
#  a minimum element. char_poly(P,X,z) and char_poly(P,n,z) are similar.
# P should also be ranked, although we don't test for this.
# Implementation note: the seq construction uses slightly more space than
#  the obvious "g:=g-up[y]" construction, but on large posets (n>100),
#  is faster by a factor of 2 to 5 depending on Maple version.
#
`posets/char_poly`:=proc(P) local i,F,x,y,f,up;
  if nargs>3 then ERROR(`invalid arguments`) fi;
  F:=posets['filter'](args[1..nargs-1]); f:=nops(F);
  if f=0 or nops(F[1])>1 then
    ERROR(`the poset lacks a minimum element`) fi;
  F:=[seq(F[-i],i=-f..-1)];
  up:=`posets/downsets`(posets['dual'](P),map(op,F));
  for i to f do for x in F[i] do
    up[x]:=args[nargs]^(i-1)-convert([seq(up[y],y=up[x])],`+`)
  od od; up[x]
end:
#
save `posets/char_poly`, cat(HomeLib,`/posets/char_poly.m`);
read cat(HomeLib,`/help/posets/char_poly`);
quit; 

