#
# eulerian(P,X) returns true/false if the poset P is/not Eulerian:
#  ranked and bounded, and with mobius function (-1)^(rank(y)-rank(x)).
# eulerian(P,n) does the same, assuming X={1,...,n}.
# eulerian(P) does the same, assuming there are no isolated vertices.
# If the poset is ranked and bounded, and the last argument is an
#  unassigned name, then the rank partition is assigned to this name.
#
`posets/eulerian`:=proc(P) local n,F,i,x,y,rk,down,e,f,a;
  n:=nargs; if type(args[n],'name') then n:=n-1 fi;
  if n>2 then ERROR(`invalid arguments`) else
    F:=posets['filter'](args[1..n]) fi;
  if nops(F)=0 or nops(F[1])>1 or nops(F[nops(F)])>1
    then RETURN(false) fi;
  rk:=table([seq(seq(x=i,x=F[i]),i=1..nops(F))]);
  for e in P do if rk[e[2]]-rk[e[1]]<>1 then RETURN(false) fi od;
  if type(args[nargs],'name') then assign(args[nargs],F) fi;
  F:=map(op,F); down:=`posets/coversets`(P,F);
  for x in F do
    rk[x]:=2*modp(rk[x],2)-1;
    f:={a[x],seq(op(indets(down[y])),y=down[x])};
    down[x]:=convert(f,`+`);
    f:=subs({seq(y=rk[op(y)]*down[op(y)],y=f)},down[x]);
    if f<>rk[x]*a[x] then RETURN(false) fi;
  od; true
end:
#
save `posets/eulerian`, cat(HomeLib,`/posets/eulerian.m`);
read cat(HomeLib,`/help/posets/eulerian`);
quit;

