(*This Mathematica notebook forms part of the proof of Proposition 4.6 in the paper "On the arithmetic of simple singularities of type E" by Beth Romano and Jack A. Thorne*)
(*The following program begins with the root datum of the group E7. It implements the procedure outlined in Proposition 4.6 to generate a list of cusp data, and then verifies for each cusp datum (M_0, M_1) the existence of a function f satisfying the given conditions*)

(*We first list the positive roots of E7 in terms of a basis of simple roots; the first 7 roots are the simple roots of E7 in the Bourbaki numbering.*)
PRH = {{1,0,0,0,0,0,0},{0,1,0,0,0,0,0},{0,0,1,0,0,0,0},{0,0,0,1,0,0,0},{0,0,0,0,1,0,0},{0,0,0,0,0,1,0},{0,0,0,0,0,0,1},{1,0,1,0,0,0,0},{0,1,0,1,0,0,0},{0,0,1,1,0,0,0},{0,0,0,1,1,0,0},{0,0,0,0,1,1,0},{0,0,0,0,0,1,1},{1,0,1,1,0,0,0},{0,1,1,1,0,0,0},{0,1,0,1,1,0,0},{0,0,1,1,1,0,0},{0,0,0,1,1,1,0},{0,0,0,0,1,1,1},{1,1,1,1,0,0,0},{1,0,1,1,1,0,0},{0,1,1,1,1,0,0},{0,1,0,1,1,1,0},{0,0,1,1,1,1,0},{0,0,0,1,1,1,1},{1,1,1,1,1,0,0},{1,0,1,1,1,1,0},{0,1,1,2,1,0,0},{0,1,1,1,1,1,0},{0,1,0,1,1,1,1},{0,0,1,1,1,1,1},{1,1,1,2,1,0,0},{1,1,1,1,1,1,0},{1,0,1,1,1,1,1},{0,1,1,2,1,1,0},{0,1,1,1,1,1,1},{1,1,2,2,1,0,0},{1,1,1,2,1,1,0},{1,1,1,1,1,1,1},{0,1,1,2,2,1,0},{0,1,1,2,1,1,1},{1,1,2,2,1,1,0},{1,1,1,2,2,1,0},{1,1,1,2,1,1,1},{0,1,1,2,2,1,1},{1,1,2,2,2,1,0},{1,1,2,2,1,1,1},{1,1,1,2,2,1,1},{0,1,1,2,2,2,1},{1,1,2,3,2,1,0},{1,1,2,2,2,1,1},{1,1,1,2,2,2,1},{1,2,2,3,2,1,0},{1,1,2,3,2,1,1},{1,1,2,2,2,2,1},{1,2,2,3,2,1,1},{1,1,2,3,2,2,1},{1,2,2,3,2,2,1},{1,1,2,3,3,2,1},{1,2,2,3,3,2,1},{1,2,2,4,3,2,1},{1,2,3,4,3,2,1},{2,2,3,4,3,2,1}};

(*The Cartan matrix of E7*)
CartanH={{2,0,-1,0,0,0,0},{0,2,0,-1,0,0,0},{-1,0,2,-1,0,0,0},{0,-1,-1,2,-1,0,0},{0,0,0,-1,2,-1,0},{0,0,0,0,-1,2,-1},{0,0,0,0,0,-1,2}};
MatrixForm[CartanH]

(*All roots of E7*)
RH = Join[PRH,-PRH];
(* Odd and even parts of grading*)
ROdd = {}; REven={};
For[i=1,i≤Length[RH],i++,
If[OddQ[{1,1,1,1,1,1,1}.RH[[i]]],AppendTo[ROdd,RH[[i]]],AppendTo[REven,RH[[i]]]
];
];

(*A basis of simple roots for G, as linear combinations of the simple roots of H = E7*)
SG = {{0,0,1,1,0,0,0},{0,0,0,0,1,1,0},{0,1,0,1,0,0,0},{1,0,1,0,0,0,0},{0,0,0,1,1,0,0},{0,0,0,0,0,1,1},{0,1,1,1,1,0,0}};
(*The Cartan matrix of G*)
CartanG = SG.CartanH.Transpose[SG];
MatrixForm[CartanG]

(*The weights of V (i.e. elements of ROdd) in the basis .5*SG*)
vwts=2*ROdd.Inverse[SG];
(*Positive roots of G in the basis SG*)
PRG={{1,0,0,0,0,0,0 }, 
{0,1,0,0,0,0,0 }, 
{0,0,1,0,0,0,0 }, 
{0,0,0,1,0,0,0 }, 
{0,0,0,0,1,0,0 },
{0,0,0,0,0,1,0 },
{ 0,0,0,0,0,0,1 },
{1,1,0,0,0,0,0 },
{0,1,1,0,0,0,0 },
{0,0,1,1,0,0,0 },
{0,0,0,1,1,0,0 },
{0,0,0,0,1,1,0 }, 
{ 0,0,0,0,0,1,1 }, 
{1,1,1,0,0,0,0 }, 
{0,1,1,1,0,0,0 },
{0,0,1,1,1,0,0 },
{0,0,0,1,1,1,0 },
{0,0,0,0,1,1,1 },
{1,1,1,1,0,0,0 },
{0,1,1,1,1,0,0 },
{0,0,1,1,1,1,0 },
{ 0,0,0,1,1,1,1 },
{1,1,1,1,1,0,0 },
{0,1,1,1,1,1,0 },
{ 0,0,1,1,1,1,1 },
{1,1,1,1,1,1,0 },
{ 0,1,1,1,1,1,1 },
{ 1,1,1,1,1,1,1}};

(*The partial order on weights, with respect to the root basis SG*)
GeQ[x_,y_]:=(
For[q=1,q≤7,q++,
If[vwts[[x]][[q]]<vwts[[y]][[q]],
Return [False]
]
];
Return[True]
); 
LLeQ[x_,y_]:=(
For[q=1,q≤7,q++,
If[vwts[[x]][[q]]>vwts[[y]][[q]],
Return [False]
]
]; 
Return[True]
); 
(*For a subset M_0 of {1..70} (identified with the rows of vwts), the function min returns the set of upper bounds of {1..70} - M_0 with respect to the above partial order*)
min[x_]:=( 
l = Complement[Range[70],x];
z={}; 
For[k=1,k<Length[l]+1,k++,
For[p=1,p<Length[l]+1,p++,
If[GeQ[l[[k]],l[[p]]] && (k ≠p), 
AppendTo[z, l[[p]]]
]
]
]; 
For[k=1,k<Length[z]+1,k++, 
l=DeleteCases[l, z[[k]]]
]; 
Return[l];
);
(*We verify that the highest weight of V equals the highest weight of H*)
Print[ROdd[[min[{}][[1]]]]=={2,2,3,4,3,2,1}]

(*The following function returns True if a set M_0 satisfies 2. of Proposition 2.15*)
UnstableQ[x_]:=(
y=Complement[Range[70],x];
If[Reduce[
Exists[{b1,b2,b3,b4,b5,b6,b7},{b1,b2,b3,b4,b5,b6,b7}≠{0,0,0,0,0,0,0}&&AllTrue[Range[Length[y]],{b1,b2,b3,b4,b5,b6,b7}.vwts[[y[[#]]]]≤0&]
]
]
,Return[True]];
Return[False];
);
(*The following function returns True if a set M_0 satisfies 1. of Proposition 2.15*)
KostantQ[x_]:= (
If[ContainsAll[x, Range[8,35]],Return[True]];
If[ContainsAll[x,{4,6,8,9,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35}],Return[True]];
Return[False];
);
(*The following function of (M0, M1) returns True if a set M_0 satisfies 3. of Proposition 2.15 for some alpha in M1*)
sl2UnstableQ[x_]:=(
M0=x[[1]];
M1=x[[2]];
M2=Complement[Range[70], M0];
rootlist={};
For[i=1,i≤7,i++,
counter=0;
For[j=1,j≤Length[M0],j++,If[MemberQ[vwts[[M2]],vwts[[M0[[j]]]]+Normal[SparseArray[{i->2},7]]]∨MemberQ[vwts[[M2]],vwts[[M0[[j]]]]-Normal[SparseArray[{i->2},7]]],counter=1]];
If[counter==0,AppendTo[rootlist,i]];];
goodrootlist={};
For[i=1,i≤Length[rootlist],i++,counter=0;
For[j=1,j≤Length[M1],j++,If[MemberQ[vwts[[M2]],vwts[[M1[[j]]]]-Normal[SparseArray[{rootlist[[i]]->2},7]]]&&UnstableQ[Join[M0,{M1[[j]]}]],counter=1];];
If[counter==1,AppendTo[goodrootlist,rootlist[[i]]]];];
If[Length[goodrootlist]>0,Return[True],Return[False]];
);

(* 2 * the character \delta_G which appears in the description of the Haar measure on G in terms of the Iwasawa decomposition*)
delta=2*ConstantArray[1,28].PRG;

(*We now generate a list of cusp data, using UnstableQ to remove those (M_0, M_1) for which a cocharacter witnesses the non-stability of the elements of V(M_0) (observe the existence of a cocharacter is hereditary under inclusions M_0 \subset M_0')*)
allcusps={{{35},min[{35}],delta-vwts[[35]]}};(*The final list is stored in allcusps. allcusps is initialised with the cusp datum corresponding to the highest weight of V = highest root of H*)
case1={{{35},min[{35}]}};
marker=1; (*Set to 0 once we create no new cusp data in a given step*)
timesaround=1; (*The size of the sets M_0 created during the current step*)
counter=0; (*The number of cusp data so far added to the list*)
While[marker==1,
(marker=0;
case={};
For[i=1,i<Length[case1]+1,i++, 
For[j=1,j<Length[case1[[i]][[2]]]+1,j++,
temp=Sort[Append[case1[[i]][[1]],case1[[i]][[2]][[j]]]]; 
If[¬UnstableQ[temp],(AppendTo[case,{temp,min[temp]}];marker=1;counter=counter+1; )
];
];
];
 case1=DeleteDuplicates[case];
For[i=1,i<Length[case1]+1,i++,temp=delta-Sum[vwts[[case1[[i]][[1]][[k]]]],{k,1,Length[case1[[i]][[1]]]}];
AppendTo[allcusps,Join[case1[[i]],{temp}]]
];
);
timesaround = timesaround+1;
];

(*9 minutes*)

Length[allcusps]

(*We get 1429 cusp data. This is the number which appears in the introduction to our paper*)

(*The next function tests a cusp datum (M_0, M_1)) for the existence of a function f(a) (a in M_1) that witnesses a power-saving on the number of points in S(M_0, M_1) of bounded height*)
H[c_]:=(
m=Length[c[[2]]];
var=Take[{a1,a2,a3,a4,a5,a6,a7,a8},m];
l=ConstantArray[1,m];
Return [Reduce[Exists[{a1,a2,a3,a4,a5,a6,a7,a8},If[Sum[If[(var.vwts[[c[[2]]]]+c[[3]])[[i]]>0,1,0],{i,1,7}]==7,True,False]&&(var.l<Length[c[[1]]])]]]
);
(*We test all the elements of allcusps to see whether either they satisfy the condition for existence of a function f; or if they satisfy the remaining conditions 1. or 3. of Proposition 2.15*)
For[w=1,w≤Length[allcusps],w++,
If[¬(H[allcusps[[w]]]∨ sl2UnstableQ[allcusps[[w]]]∨KostantQ[allcusps[[w]][[1]]]),Print[w, " ", allcusps[[w]]]];
];

(*We find that all cusp data have now been eliminated!*)