q:=64^2;
m:=315;
nn:=13;

F<w>:=GF(q);

load "Subgroups/E7/ComputeFactorsE7.txt";


function LargeModulesSelfDual(Irr,n)

Irrsmall:=[]; 
for i in Irr do if(Dimension(i) le n) then Append(~Irrsmall,i); end if; end for;

for i in [1..#Irrsmall] do
  if(not(IsSelfDual(Irrsmall[i]))) then
    Irrsmall[i]:=DirectSum(Irrsmall[i],Dual(Irrsmall[i]));
  end if;
end for;
Irrsmall:=StripDuplicates(Irrsmall);
IrrDual:=[];
for i in Irrsmall do if(Dimension(i) le n) then Append(~IrrDual,i); end if; end for;
return OrderByDimension(IrrDual);

end function;

function OrbitsUnderGroup(G,X)

if(#X eq 0) then return []; end if;

Orbs:=[X[1]];
for i in X do for j in Orbs do if(IsConjugate(G,i,j)) then continue i; end if; end for; Append(~Orbs,i); end for;
return Orbs;

end function;

G:=PSL(3,8);
Irr:=IrreducibleModules(G,GF(8));
Irr:=LargeModulesSelfDual(Irr,56);

function OrbitReps(X,frob)

Orbs:=[];
SpanOfOrbs:=[];
for i in X do
  if(not(Sort(i) in SpanOfOrbs)) then
    Append(~Orbs,i);
    SpanOfOrbs cat:=[Sort(i^(frob^j)):j in [1..Order(frob)-1]];
  end if;
end for;
return Orbs;
end function;

frob:=Sym(23)![IdentifyModule(FrobeniusImage(i,1),Irr):i in Irr];

CC:=ConjugacyClasses(G);

load "Traces/E7/Tr21E7";
Trac cat:=<{},{},{},{},{},Tr21E7>;
Tr:=<{i[1]:i in j}:j in Trac>;


P:=[21,9,7]; Ans3:=ComputeConspicuousFactors(56,Irr,CC,Tr,P,[]);

Mods:=[ChangeRing(DirectSum([Irr[i]:i in j]),F):j in Ans3];
Eigs0:=[EigenvaluesOfElement(i,CC[31,3]^9):i in Mods];
Eigs1:=[EigenvaluesOfElement(i,CC[31,3]^3):i in Mods];
Eigs2:=[EigenvaluesOfElement(i,CC[31,3]):i in Mods];

GG:=GroupOfLieType("E7",q);
V:=VectorSpace(GF(q),7);
rho1:=StandardRepresentation(GG);
Over1:=GL(56,q);
rho2:=AdjointRepresentation(GG);
Over2:=GL(133,q);


g1:=elt<GG|V![w^nn,1,1,1,1,1,1]>;
g2:=elt<GG|V![1,w^nn,1,1,1,1,1]>;
g3:=elt<GG|V![1,1,w^nn,1,1,1,1]>;
g4:=elt<GG|V![1,1,1,w^nn,1,1,1]>;
g5:=elt<GG|V![1,1,1,1,w^nn,1,1]>;
g6:=elt<GG|V![1,1,1,1,1,w^nn,1]>;
g7:=elt<GG|V![1,1,1,1,1,1,w^nn]>;
W:=Reflections(GG);
Mats1:=[rho1(i):i in [g1,g2,g3,g4,g5,g6,g7]];
Mats2:=[rho2(i):i in [g1,g2,g3,g4,g5,g6,g7]];
MatsE:=[rho1(i):i in W];

NGT:=sub<Over1|Mats1 cat MatsE>;
T:=sub<NGT|Mats1>;
V1:=GModule(T,Mats1);
V2:=GModule(T,Mats2);
T63:=sub<T|[T.i^5:i in [1..7]]>;
T21:=sub<T|[T.i^15:i in [1..7]]>;
T7:=sub<T|[T.i^45:i in [1..7]]>;
NGT63:=sub<NGT|MatsE,T63>;
NGT21:=sub<NGT|MatsE,T21>;
NGT7:=sub<NGT|MatsE,T7>;

// We only choose representatives from the orbits under the field automorphism, you can do all of them if you want...
Reps:=[Position(Ans3,i):i in OrbitReps(Ans3,frob)];

for iter in Reps do
  tt0:=Eigs0[iter];
  tt1:=Eigs1[iter];
  tt2:=Eigs2[iter];
  repeat g:=Random(T7); until Eigenvalues(g) eq tt0;
  El1:=ExpressElementInGenerators(T7,g);
  Els1:=ConstructPreimages([3*i:i in El1],21,3);
  NextEls1:=[];
  for i in Els1 do
    gg:=ProduceGroupElement(T21,i);
    if(Eigenvalues(gg) eq tt1) then Append(~NextEls1,i); delete gg; end if;
  end for;
  gg:=ProduceGroupElements(T21,NextEls1);
  Eigsgg:=[EigenvaluesOfElement(V2,i):i in gg];
  Egg:=SequenceToSet(Eigsgg);
  Orbsgg:=Sort([Position(Eigsgg,i):i in Egg]);
  for ii in Orbsgg do
    El2:=[3*i:i in NextEls1[ii]];
    Els2:=ConstructPreimages(El2,63,3);
    NextEls2:=[];
    for i in Els2 do
      gg:=ProduceGroupElement(T63,i);
      if(Eigenvalues(gg) eq tt2) then Append(~NextEls2,i); delete gg; end if;
    end for;
    if(NextEls2 eq []) then "Orbit",Position(Orbsgg,ii),"in case",iter,"has no preimages of order 63"; continue ii; end if;
    // This has size 1 for 1,4,7,8,13,14,15,25,26,27,30, size 2 for 35
    // Size 3 for n1, size 4 for 31,n2, size 6 for 36
    for iii in [1..#NextEls2] do
      El3:=[5*i:i in NextEls2[iii]];
      Els3:=ConstructPreimages(El3,315,5);
      for i in Els3 do
        gg:=ProduceGroupElement(T,i);
        if(#Eigenvalues(gg) eq #tt2 and Order(gg) eq 315) then "Case",iter,"is a blueprint for preimage",iii,"of",#NextEls2; delete gg; break i; end if;
      end for;
    end for;
  end for;
// All cases are blueprints
end for;
