// FILENAME: neargonal.m
// THIS ACCOMPANIES THE ARTICLE
// W. Castryck, F. Cools, "Linear pencils encoded in the Newton polygon", preprint (2014)

load "basic_commands.m";

//*********************************************************************************************************************
// THE AIM OF THIS FILE IS TO PROVE THE BOUNDS (10)
// 
// REMARK: SUPPOSE THAT Delta^(1) HAS GENUS 18 OR MORE. BY PICK'S THEOREM WE FIND
// 2Vol(Delta^max)  =  2*g(Delta^max) + b(Delta^max) - 2
//                  >= 2*28 + 3 - 2 > 55,
// WHERE g DENOTES THE GENUS AND b DENOTES THE NUMBER OF BOUNDARY POINTS. HERE WE USED THAT THE TOTAL NUMBER OF
// LATTICE POINTS IN AN INTERIOR POLYGON OF GENUS 18 OR MORE IS AT LEAST 28, AS CAN BE SEEN FROM TABLE 1 IN [6].
// WE USED THAT THE BOUNDARY OF A 2-DIMENSIONAL
// LATTICE POLYGON CONTAINS AT LEAST 3 LATTICE POINTS, AND THAT THE BOUNDARY OF AN INTERIOR 2-DIMENSIONAL LATTICE
// POLYGON OF GENUS > 1 CONTAINS AT LEAST 4 LATTICE POINTS (THIS IS NOT HARD TO SEE, AND ENTAILS A SMALL GAIN).
//
// (IF ONE WANTS A  PROOF THAT DOES NOT DEPEND ON THE DATA IN [6] ONE CAN REPLACE
maxgen := 17;
// BY maxgen := 24; INDEED, IF Delta^(1) HAS GENUS 25 OR MORE THEN CLEARLY THE BOUND g(Delta^max) >= 28 HOLDS.
// THIS IS COMPUTATIONALLY MUCH MORE INVOLVED; IT MAY TAKE ABOUT A WEEK.)
// 
// THUS IT SUFFICES TO LIST ALL Delta^(1)'S WHOSE GENUS IS AT MOST 17.

print "Now listing all interior polygons whose genus is between 1 and 17; this should take between half an hour and an hour.";
L := EnumeratePolytopesOfGenus(maxgen : interior := true);
print "Concatenating the lists for genus 3, ..., 17.";
L := &cat[ L[i] : i in [1..maxgen]];

// THE INTERIOR POLYGON CAN ALSO HAVE GENUS 0. SUPPOSE THAT Delta^(1) HAS 28 BOUNDARY POINTS OR MORE. BY PICK'S THEOREM
// WE SIMILARLY FIND
// 2Vol(Delta^max)  >=  2*28 + 3 - 2 > 55.
// THUS IT SUFFICES TO LIST ALL Delta^(1)'S OF GENUS 0 WITH AT MOST 27 LATTICE POINTS ON THE BOUNDARY.

print "Now adding all interior polygons of genus 0 with at most 27 lattice points on the boundary.";
genus0 := [];
for R in [3..27] do
  genus0 cat:= EnumerateGenusZeroPolytopes(R : interior := true);
end for;
L := genus0 cat L;

// THESE ARE THE Delta^(1)'S THAT WERE EXPLICITLY EXCLUDED IN THE ENONCE.

print "The excluded polygons are:";
Upsilontimes2 := LatticePolytope([<-2,-2>,<2,0>,<0,2>]); print "2*Upsilon =", Upsilontimes2;
Upsilontimes3 := LatticePolytope([<-3,-3>,<3,0>,<0,3>]); print "3*Upsilon =", Upsilontimes3;
Gamma7 := LatticePolytope([<0,0>,<1,0>,<3,1>,<2,2>,<1,2>]); print "Gamma^7 =", Gamma7;
Gamma8 := LatticePolytope([<0,0>,<1,0>,<4,1>,<2,2>,<1,2>]); print "Gamma^8 =", Gamma8;

// WE NOW VERIFY ALL THE BOUNDS. THIS WE DO BY GOING THROUGH ALL Delta^(1)'S IN L THAT ARE NOT AMONG THE EXCLUDED
// POLYGONS, EACH TIME VERIFYING THE BOUNDS (6).

print "Now verifying all the bounds (this may take a couple of minutes)";

boundsverified := true;
for P in L do
  if (not AreEquivalent(P,Upsilontimes2)) and
     (not AreEquivalent(P,Upsilontimes3)) and
     (not AreEquivalent(P,Gamma7)) and
     (not AreEquivalent(P,Gamma8)) and
     (not AreEquivalent(P,LatticePolytope([<0,1>,<0,3>,<2,2>,<5,0>,<4,0>]))) and // THIS IS THE INTERIOR OF DELTA_1
     (not AreEquivalent(P,LatticePolytope([<0,2>,<2,4>,<4,0>,<3,0>,<1,1>]))) and // THIS IS THE INTERIOR OF DELTA_2
     (not AreEquivalent(P,LatticePolytope([<0,0>,<1,0>,<4,1>,<4,2>,<2,4>]))) and // THIS IS THE INTERIOR OF DELTA_3
     (LatticeSizeRecursive(P) ge LatticeWidth(P) + 2) then     

    if LatticeWidth(P) eq 1 then 
        _,Deltamax := IsInteriorPolytope(P);    
        boundsverified and:= (2*PolytopeVolume(Deltamax) ge 24);
    elif LatticeWidth(P) eq 2 then
        _,Deltamax := IsInteriorPolytope(P);
        boundsverified and:= (2*PolytopeVolume(Deltamax) ge 24);   
    elif LatticeWidth(P) eq 3 then
        _,Deltamax := IsInteriorPolytope(P);
        boundsverified and:= (2*PolytopeVolume(Deltamax) ge 30);
    elif LatticeWidth(P) eq 4 then
        _,Deltamax := IsInteriorPolytope(P);
        boundsverified and:= (2*PolytopeVolume(Deltamax) ge 34); 
    elif LatticeWidth(P) eq 5 then
        _,Deltamax := IsInteriorPolytope(P);
        boundsverified and:= (2*PolytopeVolume(Deltamax) ge 46);
    elif LatticeWidth(P) eq 6 then
        _,Deltamax := IsInteriorPolytope(P);
        boundsverified and:= (2*PolytopeVolume(Deltamax) ge 55);
    end if;
  end if;
end for;

print "The bounds are", boundsverified;
