
# Test ORDINARY W-polynomials of ALL LABELINGS of
# all narrow posets for non-real zeros.
#
# Here, minimal examples cannot have any irreducible components
# that are singletons (except possibly minimum/maximum elements),
# and there is no need to check both P and P-dual.
# Also, there is no need to check the complement of a previous labeling.
#
# 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);
  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 W-polynomials for real zeros (n>=4).
# Note that [1,2] is always a relation of P, and if P has a type of
# the form [[1,0],[m,n],...] (n>0), then [1,m+2] is also a relation.

testall:=proc(n) local st0,tot,typ,T,pg,pi,In,st,res;
  st0:=time(); tot:=[0,0];
  typ:=map(op,[`test/types`(n), map(x->[[1,0],op(x)],`test/types`(n-1)),
    map(x->[[1,0],op(x),[1,0]],`test/types`(n-2))]);
  printf(`\n`);
  for T in typ do
    st:=time(); pi:=NULL;
    if not `canform/dual`(T,'pg','pi') then next fi;
    if T[1]=[1,0] then In:={[1,T[2][1]+2]} else In:={} fi;
    res:=narrow(T,[0,0],labtest,In,{[1,2]},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_o  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
# that do not include any singletons.
# 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 n=1 then []
    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=2..n)]
  fi
end;

