#
# omega(P,X,z) returns the order polynomial of a poset P with vertex set X.
# omega(P,n,z) does the same, using X={1,...,n}.
# omega(P,z) does the same, assuming that P has no isolated vertices.
# omega(P,X,z,S), where S is a subset of the cover relations of P, returns
#  the order polynomial relative to a labeling in which descents occur at S.
# omega(P,n,z,S), omega(P,z,S) function similarly.
# Optionally, append the string 'ideals' or 'linear' to force the use of
#  an algorithm that is either quadratic in the number of order ideals,
#  or linear in the number of linear extensions.
#
`posets/omega`:=proc() posets['W'](args,'omega') end:
#
# These will be called by 'W', after argument-parsing and testing.
#
`posets/omega/linear`:=proc(Q,X,x0,m0,z) local n,f,g,t,fac,i;
  n:=nops(X); if n=0 then RETURN(1) fi;
  g:=`posets/W/linear`(Q,X,0,1,1,t);
  f:=0; fac:=[seq(z+n-i,i=1..n)];
  for i to degree(g,t) do
     f:=f+coeff(g,t,i)*convert(fac,`*`);
     fac:=subsop(i=z-i,fac);
  od;
  expand(f)/n!
end:
#
`posets/omega/ideals`:=proc(P,X,V) local down,R,f,g,n,t,z;
  if 2*nops(V)>nops(P) then
    down:=`posets/labtab`(P,X,V,'R'); g:=1; z:=args[4]
  else
    down:=`posets/labtab`(P,X,P minus V,'R');
    g:=(-1)^nops(X); z:=-args[4]
  fi;
  g:=g*`posets/flagtool`(down,R,(r,w)->w,1,t);
  n:=degree(g,t); f:=coeff(g,t,n);
  while n>0 do n:=n-1; f:=coeff(g,t,n)+expand((z-n)*f)/(n+1) od; f
end:
#
save `posets/omega`, `posets/omega/linear`, `posets/omega/ideals`,
  cat(HomeLib,`/posets/omega.m`);
read cat(HomeLib,`/help/posets/omega`);
quit;

