
# Test ENRICHED W-polynomials of ALL LABELINGS of
# all narrow posets for non-real zeros.
#
# We can skip dual(P), but must look at all labelings of P.
# minimal examples cannot have two consecutive internal [1,0]'s.
#
# Example: testall(8);

read allrealzeros;
read fastW;
read canform;
read narrow;
read labelings;
read ideals;

# Compute the W-polynomial of a labeled poset P whose set of
# inverted covering edges is V, and test it for real zeros.

test1:=proc(ct,P,X,V,t,idls) local g;
  g:=fastW(P,X,t,V,idls,'enriched');
  if not allrealzeros(g,t) then print(`Counterexample:`,P,V) fi;
  ct+1
end;

# If P is in canonical form, and (if a 9th arg is present) not preceded
# by dual(P), compute the W-polynomials of all relabelings of P and test
# them for real zeros.

labtest:=proc(ct,P,down,N,In,Out,t,pg) local X,idls,nl;
  if not canform(P,args[8..nargs]) then RETURN([ct[1]+1,ct[2]]) fi;
  X:={$1..N}; idls:=ideals(down,X);
  nl:=labelings(P,X,0,In,Out,test1,t,idls);
  [ct[1]+1,ct[2]+nl]
end;

# Test all enriched W-polynomials for real zeros (n>=4).

testall:=proc(n) local st0,tot,typ,T,pg,pi,In,Out,st,i,res;
  st0:=time(); tot:=[0,0];
  typ:=`test/types`(n);
  printf(`\n`);
  for T in typ do
    st:=time(); pi:=NULL; In:={}; Out:={};
    for i from 2 to nops(T)-2 do
      if T[i]=[1,0] and T[i+1]=T[i] then pi:=1; break fi
    od;
    if pi=1 then next fi;
    if not `canform/dual`(T,'pg','pi') then next fi;
    if T[1]=[1,0] and T[2]=T[1] then Out:={[1,2]} fi;
    if T[nops(T)]=[1,0] and T[nops(T)-1]=[1,0] then In:={[n-1,n]} fi;
    res:=narrow(T,[0,0],labtest,In,Out,t,pg,pi);
    tot:=zip((x,y)->x+y,tot,res);
    printf(`%-60s\n`,convert(T,'string'));
    printf(`  ct=[%5d,%7d]  time=%8.2f  space=%7d\n`,op(res),stat(st));
  od;
  printf(`nar_lab_e  n=%-2d  tot=[%5d,%8d]  time=%9.2f  space=%7d\n`,
    n,op(tot),stat(st0));
end;

if `+`(0)=0 then # we are using Maple V.4 or later
  stat:=proc(st) time()-st,kernelopts(bytesalloc) end
else
  stat:=proc(st) time()-st,4*status[2] end
fi;

# Generate the type-vectors of all narrow posets on n points.
# With a second argument, generate a list of irreducible types.

`test/types`:=proc(n) local T,k,a;
  option remember;
  if n=0 then [[]] elif [args]=[1] then [[[1,0]]]
    elif [args]=[1,1] then [[1,0]]
    elif nargs>1 then [seq([n-k,k],k=1..iquo(n,2))]
    else [seq(seq(seq([op(T),a],T=procname(n-k)),a=procname(k,1)),k=1..n)]
  fi
end;

