#
# flag_f(P,X,q) computes the flag f-polynomial of a ranked, bounded poset
#  P with vertex set X. The output is multilinear in the variables
#  q[1],q[2],... and the coefficient of q[i]*q[j]*...*q[r] is the number
#  of chains 0 < x < y < ... < 1  in which rank(x)=i, rank(y)=j, ....
# By convention, every chain terminates with the maximum 1, so (if 0 < 1)
#  every term of the output includes the factor q[r], where r=rank(1).
# flag_f(P,n,q) does the same, assuming X={1,2,...,n}.
# flag_f(P,q) does the same, assuming there are no isolated vertices.
#
`posets/flag_f`:=proc() `posets/flag_f/main`(1,args) end:
#
# Do the same for the flag h-polynomial. If F is the flag f-poly, then
#  (1-q[1])*(1-q[2])*...*F( q[1]/(1-q[1]) , q[2]/(1-q[2]), ...)
# is the flag h-polynomial, but this is not how we do the computation.
#
`posets/flag_h`:=proc() `posets/flag_f/main`((r,z)->1-z[r],args) end:
#
`posets/flag_f/main`:=proc(g,P) local R,down;
  if not type(args[nargs],'name') or nargs>4 then
    ERROR(`invalid arguments`)
  elif not posets['ranked'](args[2..nargs-1],'R') then
    ERROR(`the poset is not ranked`)
  elif nops(R)=0 or nops(R[1])>1 then
    ERROR(`the poset lacks a minimum element`)
  elif nops(R[nops(R)])>1 then
    ERROR(`the poset lacks a maximum element`) fi;
  down:=`posets/downsets`(P,map(op,R));
  `posets/flagtool`(down,R,(r,z)->z[r],g,args[nargs])
end:
#
save `posets/flag_f`, `posets/flag_h`, `posets/flag_f/main`,
  cat(HomeLib,`/posets/flag_f.m`);
read cat(HomeLib,`/help/posets/flag_f`);
read cat(HomeLib,`/help/posets/flag_h`);
quit;

