
# Process the results of the search for NRZ 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, and
#   4. check that all labelings producing a given W-polynomial
#      (up to t-shifts) are shift-equivalent.

read allrealzeros;
read fastW;
read shift_equiv;
read `../data/nrz.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;

# Collect the results, compute the W polynomials, and group them
# first by poset, then by W-polynomial (up to shifts). Include
# the complements of all labelings too.

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]:
  if not member(P,pl,'j') then
    Np:=Np+1; j:=Np; pl:=[op(pl),P]; lab[j]:=NULL fi:
  f:=posets['W'](P,n,1,t,V,'linear');
  if f<>fastW(P,n,t,V) or allrealzeros(f,t) then
    ERROR(`this cannot happen`) fi;
  lab[j]:=lab[j],[f,V]:
  f:=posets['W'](P,n,1,t,P minus V,'linear');
  if f<>fastW(P,n,t,P minus V) or allrealzeros(f,t) then 
    ERROR(`this cannot happen`) fi;
  lab[j]:=lab[j],[f,P minus V]:
od:

# now sort the results, first by W-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, grouped by shift-equivalence

for i to Np do
  P:=pl[i]: b:=lab[i][1];
  lprint(); print(i,P);
#  posets['plot_poset'](P,labels,title=convert(i,string));
  lprint(`shift-equivalence class: `);
  for a in lab[i] do
    if not shift_equiv(P,a[2],b[2]) then
      lprint(`\nshift-equivalence class: `) fi;
    b:=a; lprint(sort(a[1]),mypr(P,a[2]));
  od
od:

quit;

