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:=2^12;
F<w2>:=GF(q);
G:=GroupOfLieType("E8",q);
V:=VectorSpace(GF(q),8);
rho1:=StandardRepresentation(G);
Over1:=GL(248,q);
g1:=elt<G|V![w2,1,1,1,1,1,1,1]>;
g2:=elt<G|V![1,w2,1,1,1,1,1,1]>;
g3:=elt<G|V![1,1,w2,1,1,1,1,1]>;
g4:=elt<G|V![1,1,1,w2,1,1,1,1]>;
g5:=elt<G|V![1,1,1,1,w2,1,1,1]>;
g6:=elt<G|V![1,1,1,1,1,w2,1,1]>;
g7:=elt<G|V![1,1,1,1,1,1,w2,1]>;
g8:=elt<G|V![1,1,1,1,1,1,1,w2]>;
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>;
T13:=sub<T|[T.i^(315):i in [1..8]]>;
T91:=sub<T|[T.i^(45):i in [1..8]]>;
T273:=sub<T|[T.i^(15):i in [1..8]]>;

R91:=ResidueClassRing(91);
X91:=[[R91!(Log((T91.j)[i,i]) div 45):i in [1..248]]:j in [1..8]];



Eigs1:={ <w2^2205, 12>, <w2^1260, 12>, <w2^630, 1>, <w2^315, 32>, <w2^3465, 1>,
<w2^2520, 32>, <w2^3780, 32>, <1, 68>, <w2^1890, 12>, <w2^945, 1>, <w2^3150, 1>,
<w2^1575, 32>, <w2^2835, 12> };

Eigs2:={ <w2^3780, 8>, <w2^2295, 8>, <w2^3285, 8>, <w2^1350, 1>, <w2^1935, 1>,
<w2^1800, 8>, <w2^1890, 1>, <w2^3555, 8>, <w2^1980, 8>, <w2^1755, 1>, <w2^900,
1>, <w2^3240, 1>, <w2^2385, 8>, <w2^1575, 8>, <w2^3105, 1>, <w2^3825, 1>,
<w2^2160, 1>, <w2^405, 1>, <w2^315, 8>, <w2^3195, 1>, <w2^1710, 8>, <w2^3690,
1>, <w2^1485, 1>, <w2^2700, 8>, <w2^1260, 1>, <w2^540, 8>, <w2^2610, 1>,
<w2^2925, 8>, <w2^225, 8>, <w2^585, 9>, <w2^1170, 8>, <w2^1125, 8>, <w2^855, 1>,
<w2^2340, 1>, <1, 32>, <w2^3510, 9>, <w2^810, 8>, <w2^990, 1>, <w2^3870, 8>,
<w2^270, 1>, <w2^2205, 1>, <w2^2520, 8>, <w2^2745, 1>, <w2^2115, 8>, <w2^2970,
8>, <w2^2835, 1>, <w2^1395, 8> };

El1:=[0,0,35,0,49,7,0,7];
Els1:=ConstructPreimages(El1,91,7);
NextEls1:=[];
e22:={*i[1]^^i[2]:i in Eigs2*};
e2:={*R91!(Log(i) div 45):i in e22*};
for ii in [1..#Els1] do
  if e2 eq {*&+[Els1[ii,i]*X91[i,j]:i in [1..8]]:j in [1..248]*} then Append(~NextEls1,Els1[ii]); end if;
  if(ii mod 10000 eq 0) then ii,#Els1,#NextEls1; end if;
end for;

gg1:=ProduceGroupElements(T91,NextEls1);
EltsToMerge:=[E.2*E.3*E.4*E.2*E.3*E.1*E.4*E.5*E.4*E.3*E.1*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.5*E.4*E.7*E.6*E.5*E.4*E.2*E.3*E.4*E.5*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.3*E.4*E.2*E.3*E.1*E.4*E.5*E.4*E.2*E.3*E.4*E.5*E.6*E.5*E.4*E.2*E.3*E.4*E.5*E.7*E.6*E.5*E.4*E.2*E.3*E.1*E.4*E.5*E.6*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.5*E.4*E.3*E.1*E.7,E.3*E.1*E.4*E.3*E.5*E.4*E.2*E.6*E.5*E.4*E.2*E.3*E.1*E.4*E.3*E.5*E.4*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.7*E.8*E.7*E.6*E.5*E.4*E.2*E.3*E.1*E.4*E.3*E.5,E.1*E.2*E.3*E.4*E.2*E.5*E.4*E.3*E.1*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.7*E.6*E.8*E.7*E.6*E.5*E.4*E.2*E.3*E.1*E.4*E.3*E.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(j ne 0 and 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 1) then "All merged"; break e; end if;
    end if;
  end for;
  "So far we have merged elements into",#Orbs,"orbits";
end for;


// Here is an element of order 1365 that powers to a particular element of gg1 (they are all conjugate) and has the
// same number of eigenspaces. This completes the proof.

El2:=[ 3861, 0, 288, 0, 21, 3786, 234, 159 ];
gg2:=ProduceGroupElement(T,El2);
gg1[53] eq gg2^15;
#Eigenvalues(gg1[52]) eq #Eigenvalues(gg2);

// For completeness, here is NextEls1.

NextElsa:=[
    [ 13, 0, 5, 0, 7, 79, 78, 53 ],
    [ 78, 0, 18, 0, 7, 79, 78, 53 ],
    [ 13, 65, 31, 0, 7, 79, 78, 53 ],
    [ 78, 65, 44, 0, 7, 79, 78, 53 ],
    [ 13, 26, 31, 65, 7, 79, 78, 53 ],
    [ 78, 26, 44, 65, 7, 79, 78, 53 ],
    [ 13, 0, 5, 26, 72, 79, 78, 53 ],
    [ 78, 0, 18, 26, 72, 79, 78, 53 ],
    [ 0, 26, 5, 0, 7, 53, 0, 66 ],
    [ 0, 78, 44, 0, 7, 53, 0, 66 ],
    [ 0, 65, 5, 26, 7, 53, 0, 66 ],
    [ 0, 52, 18, 26, 7, 53, 0, 66 ],
    [ 0, 39, 18, 78, 7, 53, 0, 66 ],
    [ 0, 13, 44, 78, 7, 53, 0, 66 ],
    [ 0, 13, 44, 52, 33, 53, 0, 66 ],
    [ 0, 0, 31, 65, 33, 53, 0, 66 ],
    [ 0, 78, 44, 65, 33, 53, 0, 66 ],
    [ 0, 26, 5, 13, 85, 53, 0, 66 ],
    [ 0, 0, 31, 13, 85, 53, 0, 66 ],
    [ 0, 65, 5, 39, 85, 53, 0, 66 ],
    [ 13, 26, 5, 0, 7, 40, 13, 66 ],
    [ 78, 26, 18, 0, 7, 40, 13, 66 ],
    [ 13, 0, 31, 0, 7, 40, 13, 66 ],
    [ 78, 0, 44, 0, 7, 40, 13, 66 ],
    [ 13, 65, 5, 26, 7, 40, 13, 66 ],
    [ 78, 65, 18, 26, 7, 40, 13, 66 ],
    [ 13, 0, 31, 65, 33, 40, 13, 66 ],
    [ 78, 0, 44, 65, 33, 40, 13, 66 ],
    [ 26, 0, 5, 0, 7, 79, 65, 66 ],
    [ 26, 78, 18, 0, 7, 79, 65, 66 ],
    [ 65, 0, 31, 0, 7, 79, 65, 66 ],
    [ 65, 78, 44, 0, 7, 79, 65, 66 ],
    [ 26, 13, 18, 78, 7, 79, 65, 66 ],
    [ 65, 13, 44, 78, 7, 79, 65, 66 ],
    [ 26, 0, 5, 13, 85, 79, 65, 66 ],
    [ 65, 0, 31, 13, 85, 79, 65, 66 ],
    [ 39, 0, 5, 0, 7, 66, 78, 66 ],
    [ 52, 0, 44, 0, 7, 66, 78, 66 ],
    [ 0, 26, 18, 0, 7, 40, 0, 79 ],
    [ 0, 13, 31, 0, 7, 40, 0, 79 ],
    [ 0, 13, 5, 13, 7, 40, 0, 79 ],
    [ 0, 78, 31, 13, 7, 40, 0, 79 ],
    [ 0, 78, 5, 26, 7, 40, 0, 79 ],
    [ 0, 65, 18, 26, 7, 40, 0, 79 ],
    [ 0, 65, 18, 13, 20, 40, 0, 79 ],
    [ 0, 26, 18, 78, 20, 40, 0, 79 ],
    [ 0, 0, 44, 78, 20, 40, 0, 79 ],
    [ 0, 13, 31, 65, 33, 40, 0, 79 ],
    [ 0, 0, 44, 65, 33, 40, 0, 79 ],
    [ 0, 78, 31, 78, 33, 40, 0, 79 ],
    [ 13, 0, 18, 0, 7, 79, 52, 79 ],
    [ 78, 0, 31, 0, 7, 79, 52, 79 ],
    [ 26, 13, 5, 0, 7, 66, 65, 79 ],
    [ 26, 0, 18, 0, 7, 66, 65, 79 ],
    [ 65, 13, 31, 0, 7, 66, 65, 79 ],
    [ 65, 0, 44, 0, 7, 66, 65, 79 ],
    [ 26, 78, 5, 13, 7, 66, 65, 79 ],
    [ 65, 78, 31, 13, 7, 66, 65, 79 ],
    [ 26, 0, 18, 78, 20, 66, 65, 79 ],
    [ 65, 0, 44, 78, 20, 66, 65, 79 ],
    [ 13, 26, 5, 0, 7, 53, 78, 79 ],
    [ 78, 26, 18, 0, 7, 53, 78, 79 ],
    [ 13, 0, 31, 0, 7, 53, 78, 79 ],
    [ 78, 0, 44, 0, 7, 53, 78, 79 ],
    [ 13, 65, 5, 26, 7, 53, 78, 79 ],
    [ 78, 65, 18, 26, 7, 53, 78, 79 ],
    [ 13, 0, 31, 65, 33, 53, 78, 79 ],
    [ 78, 0, 44, 65, 33, 53, 78, 79 ],
    [ 0, 78, 18, 0, 7, 79, 0, 40 ],
    [ 0, 65, 31, 0, 7, 79, 0, 40 ],
    [ 0, 26, 31, 65, 7, 79, 0, 40 ],
    [ 0, 13, 44, 65, 7, 79, 0, 40 ],
    [ 0, 13, 18, 78, 7, 79, 0, 40 ],
    [ 0, 78, 44, 78, 7, 79, 0, 40 ],
    [ 0, 13, 18, 13, 72, 79, 0, 40 ],
    [ 0, 0, 5, 26, 72, 79, 0, 40 ],
    [ 0, 78, 18, 26, 72, 79, 0, 40 ],
    [ 0, 0, 5, 13, 85, 79, 0, 40 ],
    [ 0, 65, 31, 13, 85, 79, 0, 40 ],
    [ 0, 26, 31, 78, 85, 79, 0, 40 ],
    [ 13, 0, 5, 0, 7, 66, 13, 40 ],
    [ 78, 0, 18, 0, 7, 66, 13, 40 ],
    [ 13, 65, 31, 0, 7, 66, 13, 40 ],
    [ 78, 65, 44, 0, 7, 66, 13, 40 ],
    [ 13, 26, 31, 65, 7, 66, 13, 40 ],
    [ 78, 26, 44, 65, 7, 66, 13, 40 ],
    [ 13, 0, 5, 26, 72, 66, 13, 40 ],
    [ 78, 0, 18, 26, 72, 66, 13, 40 ],
    [ 26, 0, 5, 0, 7, 53, 26, 40 ],
    [ 26, 78, 18, 0, 7, 53, 26, 40 ],
    [ 65, 0, 31, 0, 7, 53, 26, 40 ],
    [ 65, 78, 44, 0, 7, 53, 26, 40 ],
    [ 26, 13, 18, 78, 7, 53, 26, 40 ],
    [ 65, 13, 44, 78, 7, 53, 26, 40 ],
    [ 26, 0, 5, 13, 85, 53, 26, 40 ],
    [ 65, 0, 31, 13, 85, 53, 26, 40 ],
    [ 13, 0, 18, 0, 7, 40, 39, 40 ],
    [ 78, 0, 31, 0, 7, 40, 39, 40 ],
    [ 0, 13, 5, 0, 7, 66, 0, 53 ],
    [ 0, 65, 44, 0, 7, 66, 0, 53 ],
    [ 0, 78, 5, 13, 7, 66, 0, 53 ],
    [ 0, 52, 31, 13, 7, 66, 0, 53 ],
    [ 0, 39, 31, 65, 7, 66, 0, 53 ],
    [ 0, 26, 44, 65, 7, 66, 0, 53 ],
    [ 0, 26, 44, 52, 20, 66, 0, 53 ],
    [ 0, 0, 18, 78, 20, 66, 0, 53 ],
    [ 0, 65, 44, 78, 20, 66, 0, 53 ],
    [ 0, 13, 5, 26, 72, 66, 0, 53 ],
    [ 0, 0, 18, 26, 72, 66, 0, 53 ],
    [ 0, 78, 5, 39, 72, 66, 0, 53 ],
    [ 39, 0, 5, 0, 7, 53, 13, 53 ],
    [ 52, 0, 44, 0, 7, 53, 13, 53 ],
    [ 26, 13, 5, 0, 7, 40, 26, 53 ],
    [ 26, 0, 18, 0, 7, 40, 26, 53 ],
    [ 65, 13, 31, 0, 7, 40, 26, 53 ],
    [ 65, 0, 44, 0, 7, 40, 26, 53 ],
    [ 26, 78, 5, 13, 7, 40, 26, 53 ],
    [ 65, 78, 31, 13, 7, 40, 26, 53 ],
    [ 26, 0, 18, 78, 20, 40, 26, 53 ],
    [ 65, 0, 44, 78, 20, 40, 26, 53 ]
];

Sort(NextEls1) eq Sort(NextElsa);