"Verbosity 0 just checks that there are elements that power to a given element of order 85 with the correct eigenvalues on L(G) and stabilizing a given set of eigenspaces";
printf("\n");
"Verbosity 1 in addition checks that the element of order 17 has the correct eigenvalues on L(G), that the elements of order 85 that power to it have the correct eigenvalues on L(G), and that those elements form two orbits under the Weyl group, one given by raising the other to the power 69.";
printf("\n");
"Verbosity 2 in addition checks that the 16 elements of order 85 are the only ones with the correct eigenvalues on L(G)";
printf("\n");
"Verbosity 3 in addition constructs PSp(4,4) and checks that the eigenvalues of an element of order 85 on the various simple modules, and on L(G), are what are claimed.";
printf("\n");
"Default is verbosity 3";

Verbosity:=3;

load "Subgroups/functions.txt";

function FindOrb(Orbs,i)
for Orb in Orbs do if(i in Orb) then return Orb; end if; end for;
return {};
end function;

q:=3571;
F:=GF(q);
w:=PrimitiveElement(F);
FF<ww>:=GF(2^8);
G:=GroupOfLieType("E8",q);
V:=VectorSpace(GF(q),8);
rho1:=StandardRepresentation(G);
Over1:=GL(248,q);
g1:=elt<G|V![w,1,1,1,1,1,1,1]>;
g2:=elt<G|V![1,w,1,1,1,1,1,1]>;
g3:=elt<G|V![1,1,w,1,1,1,1,1]>;
g4:=elt<G|V![1,1,1,w,1,1,1,1]>;
g5:=elt<G|V![1,1,1,1,w,1,1,1]>;
g6:=elt<G|V![1,1,1,1,1,w,1,1]>;
g7:=elt<G|V![1,1,1,1,1,1,w,1]>;
g8:=elt<G|V![1,1,1,1,1,1,1,w]>;
W:=Reflections(G);
Mats1:=[rho1(i):i in [g1,g2,g3,g4,g5,g6,g7,g8]];
MatsE:=[rho1(i):i in W];
NGT:=sub<Over1|Mats1 cat MatsE>;
T:=sub<NGT|Mats1>;
E:=sub<NGT|MatsE>;

T17:=sub<T|[T.i^210:i in [1..8]]>;
T1785:=sub<T|[T.i^2:i in [1..8]]>;

R:=ResidueClassRing(1785);
X:=[[R!((Log((T1785.j)[i,i])*1785) div (q-1)):i in [1..248]]:j in [1..8]];

EigsToTest:=[
    { <ww^51, 1>, <ww^204, 1>, <ww^15, 1>, <ww^240, 1>, <ww^195, 1>, <ww^60, 1> },
    { <ww^120, 1>, <ww^153, 1>, <ww^225, 1>, <ww^135, 1>, <ww^30, 1>, <ww^102, 1> },
    { <ww^6, 1>, <ww^96, 1>, <ww^231, 1>, <ww^129, 1>, <ww^159, 1>, <ww^126, 1>, <ww^249,
    1>, <ww^24, 1> },
    { <ww^192, 1>, <ww^3, 1>, <ww^207, 1>, <ww^243, 1>, <ww^48, 1>, <ww^252, 1>, <ww^12,
    1>, <ww^63, 1> }
];

E1:={ <ww^174, 2>, <ww^72, 4>, <ww^69, 2>, <1, 8>, <ww^30, 2>, <ww^147, 3>, <ww^237, 4>,
<ww^57, 3>, <ww^33, 4>, <ww^117, 2>, <ww^222, 4>, <ww^210, 3>, <ww^198, 3>, <ww^153, 2>,
<ww^120, 2>, <ww^81, 2>, <ww^234, 2>, <ww^54, 3>, <ww^246, 4>, <ww^216, 3>, <ww^21, 2>,
<ww^60, 2>, <ww^18, 4>, <ww^126, 3>, <ww^159, 3>, <ww^102, 2>, <ww^111, 4>, <ww^12, 3>,
<ww^225, 2>, <ww^24, 3>, <ww^9, 4>, <ww^114, 3>, <ww^3, 3>, <ww^6, 3>, <ww^87, 2>,
<ww^207, 3>, <ww^66, 4>, <ww^135, 2>, <ww^36, 4>, <ww^78, 3>, <ww^168, 2>, <ww^243, 3>,
<ww^171, 2>, <ww^27, 3>, <ww^96, 3>, <ww^138, 2>, <ww^183, 4>, <ww^51, 2>, <ww^177, 3>,
<ww^45, 3>, <ww^240, 2>, <ww^165, 3>, <ww^141, 3>, <ww^189, 4>, <ww^105, 3>, <ww^15, 2>,
<ww^48, 3>, <ww^201, 3>, <ww^231, 3>, <ww^150, 3>, <ww^39, 3>, <ww^129, 3>, <ww^93, 2>,
<ww^213, 2>, <ww^180, 3>, <ww^108, 3>, <ww^156, 3>, <ww^99, 3>, <ww^75, 3>, <ww^186, 2>,
<ww^123, 4>, <ww^195, 2>, <ww^204, 2>, <ww^228, 3>, <ww^162, 2>, <ww^252, 3>, <ww^90, 3>,
<ww^42, 2>, <ww^132, 4>, <ww^63, 3>, <ww^192, 3>, <ww^219, 4>, <ww^249, 3>, <ww^84, 2>,
<ww^144, 4> };
E2:={ <ww^180, 14>, <ww^210, 14>, <ww^240, 15>, <1, 16>, <ww^60, 15>, <ww^195, 15>, <ww^135,
15>, <ww^15, 15>, <ww^165, 14>, <ww^75, 14>, <ww^225, 15>, <ww^90, 14>, <ww^150, 14>,
<ww^105, 14>, <ww^30, 15>, <ww^120, 15>, <ww^45, 14> };
Eigs1:={<w^(14*Log(i[1])),i[2]>:i in E1};
Eigs2:={<w^(14*Log(i[1])),i[2]>:i in E2};

if(Verbosity ge 3) then
  "Checking that eigenvalues of simple modules on PSp(6,4) are correct";
  G2:=PSp(6,4);
  for i in CompositionFactors(PermutationModule(G2,GF(4))) do if(Dimension(i) eq 6) then S61:=i; break i; end if; end for;
  for i in CompositionFactors(SymmetricSquare(S61)) do
    if(Dimension(i) eq 14) then S141:=i; end if;
    if(Dimension(i) eq 6) then S62:=i; end if;
  end for;
  for i in CompositionFactors(SymmetricSquare(S62)) do
    if(Dimension(i) eq 14) then S142:=i; end if;
  end for;
  S81:=SocleFactors(ExteriorPower(S61,3))[2];
  S82:=SocleFactors(ExteriorPower(S62,3))[2];
  S481:=TensorProduct(S61,S81);
  S482:=TensorProduct(S62,S82);
  S6412:=TensorProduct(S81,S82);
  S1:=TrivialModule(G2,GF(4));
  MM:=DirectSum([S6412,S481,S482,S141,S142,S81,S81,S82,S82,S61,S61,S62,S62,S1,S1,S1,S1]);

  CC:=ConjugacyClasses(G2);
  for i in [113..120] do
    if(EigenvaluesOfElement(ChangeRing(S61,FF),CC[i,3]) eq { <ww^51,1>,<ww^204,1>,<ww^15,1>,<ww^240,1>,<ww^195,1>,<ww^60,1>}) then
      x:=CC[i,3];
    end if;
  end for;
  "Check that the eigenvalues of elements of orders 85 and 17 on L(G) are correct.";
  EigenvaluesOfElement(ChangeRing(MM,FF),x) eq {<ww^(Log(w,i[1]) div 14),i[2]>:i in Eigs1};
  EigenvaluesOfElement(ChangeRing(MM,FF),x^5) eq {<ww^(Log(w,i[1]) div 14),i[2]>:i in Eigs2};
  "Check that the eigenvalues of the simple modules S61,S62,S81,S82 are correct.";
  EigsToTest eq [EigenvaluesOfElement(ChangeRing(i,FF),x):i in [S61,S62,S81,S82]];
end if;

El1:=[735,1155,0,420,525,1155,1680,1365];

if(Verbosity gt 0) then
  "We now test that our given element of order 17 has the correct eigenvalues on L(G).";
  TestElt1:=ProduceGroupElement(T1785,El1);
  Eigenvalues(TestElt1) eq Eigs2;
end if;

Els1:=ConstructPreimages(El1,1785,5);
e11:={*i[1]^^i[2]:i in Eigs1*};
e1:={*R!((Log(i)*1785) div (q-1)):i in e11*};

NextEls1:=[[ 861, 1659, 357, 84, 1176, 231, 1050, 273 ],
    [ 1218, 1659, 1428, 441, 1176, 231, 1050, 273 ],
    [ 861, 588, 357, 1155, 1176, 231, 1764, 273 ],
    [ 1218, 588, 1428, 1512, 1176, 231, 1764, 273 ],
    [ 147, 1302, 357, 798, 819, 1659, 336, 987 ],
    [ 504, 1302, 1428, 1155, 819, 1659, 336, 987 ],
    [ 147, 231, 357, 84, 819, 1659, 1050, 987 ],
    [ 504, 231, 1428, 441, 819, 1659, 1050, 987 ],
    [ 147, 588, 357, 84, 462, 231, 693, 1344 ],
    [ 504, 588, 1428, 441, 462, 231, 693, 1344 ],
    [ 147, 1659, 357, 798, 462, 231, 1764, 1344 ],
    [ 504, 1659, 1428, 1155, 462, 231, 1764, 1344 ],
    [ 861, 231, 357, 1155, 1533, 1659, 336, 1701 ],
    [ 1218, 231, 1428, 1512, 1533, 1659, 336, 1701 ],
    [ 861, 1302, 357, 84, 1533, 1659, 1407, 1701 ],
    [ 1218, 1302, 1428, 441, 1533, 1659, 1407, 1701 ]];

if(Verbosity ge 2) then
  NextElsa:=[];
  for ii in [1..#Els1] do
    if e1 eq {*&+[Els1[ii,i]*X[i,j]:i in [1..8]]:j in [1..248]*} then Append(~NextElsa,Els1[ii]); end if;
    if(ii mod 10000 eq 0) then "At stage",ii,"of",#Els1,"there are",#NextElsa,"elements with the correct eigenvalues"; end if;
  end for;

  NextEls1 eq NextElsa;
end if;


gg1:=ProduceGroupElements(T1785,NextEls1);

if(Verbosity eq 1) then
  "Testing that elements of order 85 do at least have the correct eigenvalues on L(G), but not that these are the only ones.";
  {Eigenvalues(i):i in gg1} eq {Eigs1};
end if;

if(Verbosity gt 1) then
  "Testing conjugacy up to taking powers of elements. This is quick.";
  EltsToMerge:=[
  E.2*E.4*E.3*E.1*E.6*E.5*E.4*E.2*E.3*E.4*E.5*E.6*E.7*E.8*E.7*E.6*E.5*E.4*E.2*E.3*E.1*E.4*E.3*E.5*E.4*E.2*E.6,
  E.4*E.2*E.3*E.4*E.5*E.6*E.7*E.6*E.5*E.4*E.2*E.3*E.4,
  E.4*E.3*E.1*E.5*E.4*E.2*E.3*E.4*E.5*E.6*E.5*E.4*E.2*E.3*E.1*E.4*E.3*E.5*E.4];

  g:=gg1[1]^5;
  Orbs:={{i}:i in [1..#NextEls1]};
  for e in EltsToMerge do
    for i in [1..#NextEls1] do
      j:=Position(gg1,gg1[i]^e);
      Orb1:=FindOrb(Orbs,i);
      if(not(j in Orb1)) then
        Orb2:=FindOrb(Orbs,j);
        Exclude(~Orbs,Orb1);
        Exclude(~Orbs,Orb2);
        Include(~Orbs,Orb1 join Orb2); //printf "Merged %o and %o.\n",Orb1,Orb2;
        delete Orb1; delete Orb2; if(#Orbs eq 2) then "All merged up to powers"; break e; end if;
      end if;
    end for;
    "So far we have merged elements into",#Orbs,"orbits";
  end for;
  "Orbits are",Orbs;
  Orbs:=SetToSequence(Orbs);
  Position(gg1,gg1[Random(Orbs[1])]^69) in Orbs[2];

end if;

El2:=NextEls1[1];
Els2:=ConstructPreimages(El2,1785,3);
Remove(~Els2,5498); //Removes the element of order 85.
E255:=[{*&+[ii[i]*X[i,j]:i in [1..8]]:j in [1..248]*}:ii in Els2];
E255Powers:=[{* 3*i:i in MultisetToSet(j) *}:j in E255];
Eigs255Powers:=[{*ww^(Integers()!i div 7):i in j*}:j in E255Powers];


"For each of S61,S62,S81,S82 we find the number of elements of order 255 stabilizing their constituent eigenspaces";
for i in [1..#EigsToTest] do
  nn:=[j:j in [1..6560]|&*[Multiplicity(Eigs255Powers[j],k[1]):k in EigsToTest[i]] eq 1];
  if(i in {1,3}) then 202 in nn; end if; #nn;
end for;

El3:=Els2[202]; //This stabs both 6.1 and 8.1
g:=ProduceGroupElement(T1785,El3);
n:=#Eigenvalues(g);
Els3:=ConstructPreimages(El3,1785,7);
for ii in Els3 do
 nn:=#{&+[ii[i]*X[i,j]:i in [1..8]]:j in [1..248]};
 if(nn eq n) then "Here is an element:"; ii; break ii; end if; 
end for;

// This is an element that seventh powers to Els2[202] and has the same number of eigenvlaues as Els2[202].
// [ 1061, 674, 612, 599, 736, 1286, 50, 13 ]
