
# Process the results of the search for NRZ-bar posets:
#   1. collect together the labelings of each poset,
#   2. recompute their W-polynomials via the naive ('linear') algorithm,
#      and check that the results agree with the 'fastW' algorithm,
#   3. reconfirm that they have non-real zeros.

read allrealzeros;
read fastW;
read `../data/nrzbar.out`;     # this will assign n:= number of vertices
read `posets2.2.1v.txt`;

# prettyprint a set of edges -- print either V or V-complement,
# depending on whichever is smaller, and sort the edges.

mypr:=proc(P,V) local sV,i,flag;
  sV:=[op(V)]; flag:=`{[`;
  if 2*nops(V)>nops(P) then sV:=[op(P minus V)]; flag:=`P-{[` fi;
  sV:=sort(sV,(u,v)->evalb(u[1]<v[1] or (u[1]=v[1] and u[2]<v[2])));
  cat(flag,sV[1][1],`,`,sV[1][2],
    seq(cat(`],[`,sV[i][1],`,`,sV[i][2]),i=2..nops(sV)),`]}`);
end;

# compute the enriched W-polynomial of P via the naive algorithm

enrichW:=proc(P,n,t,V) local Q,F,i,rg;
  Q:=(P minus V) union map(x->[x[2],x[1]],V);
  F:=map(op,posets['filter'](Q,n));
  Q:=subs({seq(F[i]=i,i=1..n)},P); rg:=[$2..n-1];
  convert(map(pks,posets['extensions'](Q,n),t,rg),`+`)
end;
pks:=proc(w,t,rg) t^(1+nops(map(foo,rg,w))) end;
foo:=proc(i,w) if max(w[i-1],w[i+1])<w[i] then 1 fi end;

# Collect the results, compute the W-bar-polynomials,
# and group them first by poset, then by W-bar-polynomial.

N:=nops(sa)/2;
sb:=[seq([sa[2*i-1],sa[2*i]],i=1..N)]:
pl:=[]: lab:=table(): Np:=0:

for i to N do
  P:=sb[i][1]; V:=sb[i][2];
  f:=enrichW(P,n,t,V);
  if f<>fastW(P,n,t,V,'enriched') or allrealzeros(f,t) then
    ERROR(`this cannot happen`) fi;
  if not member(P,pl,'j') then
    Np:=Np+1; j:=Np; pl:=[op(pl),P]; lab[j]:=NULL; fi;
  lab[j]:=lab[j],[f,V]
od:

# now sort the results, first by W-bar-polynomial (shifted to have
# nonzero constant term), then by the cardinality of the reversal subset.

mysort:=proc(u,v) local f;
  f:=expand(u[1]*t^ldegree(v[1],t)-v[1]*t^ldegree(u[1],t));
  if f<>0 then evalb(lcoeff(f,t)>0)
    else evalb(nops(u[2])<=nops(v[2])) fi;
end;
for i to Np do lab[i]:=sort([lab[i]],mysort) od:

# now pretty print the results

for i to Np do
  P:=pl[i]: lprint(); print(i,P);
#  posets['plot_poset'](P,labels,title=convert(i,string));
  for a in lab[i] do lprint(sort(a[1]),mypr(P,a[2])) od
od:

quit;

