load "Subgroups/functions.txt";

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;

G2:=PSU(3,8);
CC:=ConjugacyClasses(G2);

X:=IrreducibleModules(G2,GF(64));
S1:=X[1];
S81:=X[2];
S82:=GModule(G2,FrobeniusImage(S81,1));
S83:=GModule(G2,FrobeniusImage(S81,2));
for j in [5..10] do if([#CompositionFactors(TensorProduct(X[j],i))-1:i in [S81,S82,S83]] eq [9,5,0]) then S912:=X[j]; S912d:=Dual(S912); break; end if; end for;
S923:=GModule(G2,FrobeniusImage(S912,1)); S923d:=Dual(S923);
S931d:=GModule(G2,FrobeniusImage(S912,2)); S931:=Dual(S931d);

if(#SocleFactors(TensorProduct(S912,X[11])) eq 3) then S27d:=X[11]; S27:=X[12]; else S27d:=X[12]; S27:=X[11]; end if;
S6412:=TensorProduct(S81,S82);
S6413:=TensorProduct(S81,S83);
S6423:=TensorProduct(S82,S83);
Irr:=[S1,S81,S82,S83,S912,S912d,S931,S931d,S923,S923d,S27,S27d,S6412,S6413,S6423] cat[X[i]:i in [16..21]];
labs:=["1","8_1","8_2","8_3","9_{1,2}","9_{1,2}^*","\\bar 9_{1,3}","\\bar 9_{1,3}^*","9_{2,3}","9_{2,3}^*","27","27^*","64_{1,2}","64_{1,3}","64_{2,3}"];

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


load "Subgroups/E8/ComputeFactorsE8.txt";
load "Traces/E8/Tr19E8Eigs";
Tr19E8:={EigsToTracePowers(i,19):i in Tr19E8Eigs};
delete Tr19E8Eigs;
Tr cat:=<{},{},{},Tr19E8>;
P:=[19,9];

Ans3,Mods,I:=ComputeConspicuousFactorsE8(Irr,CC,Tr,P,[]);

Ans4:=OrbitReps([IdentifyFactors(i,Irr):i in Mods],frob);

"The conspicuous sets of composition factors are";
[{*labs[i]:i in j*}:j in Ans4];


cohom:=[i:i in [1..14]|Dimension(Ext(Irr[i],S1)) eq 1];
"The pressures are";
[&+[Multiplicity(i,j):j in cohom]-Multiplicity(i,1):i in Ans4];

Ans5:=[i:i in Ans4a|not(1 in i) or Multiplicity(i,1) lt &+[Multiplicity(i,j):j in Cohoms]];


"Now we need to check radicals from Case 3.";

A1:=S931d;
for i in [S923,S81,S1,S931,S931d,S923d,S81,S1,S931d,S81,S931] do
  E,rho:=Ext(A1,i); A1:=MaximalExtension(A1,i,E,rho); DescribeLayers(Dual(A1),Irr,labs);
end for;
for i in [1,2,4,7,8,9,10] do Dimension(Ext(A1,Irr[i])) eq 0; end for;


A2:=S923d;
for i in [S931,S83,S1,S923,S923d,S81,S931d,S1] do
  E,rho:=Ext(A2,i); A2:=MaximalExtension(A2,i,E,rho); DescribeLayers(Dual(A2),Irr,labs);
end for;
for i in [1,2,4,7,8,9,10] do Dimension(Ext(A2,Irr[i])) eq 0; end for;


"Now we compute the irreducible modules for L, the centralizer of an element of order 3";
L:=Centralizer(G2,CC[3,3]);
Ld:=DerivedSubgroup(L);

IrrL:=[Restriction(S1,L)];
for i in [2,3,4] do Append(~IrrL,SocleFactors(Restriction(Irr[i],L))[2]); end for;
Append(~IrrL,TensorProduct(IrrL[2],IrrL[3]));
Append(~IrrL,TensorProduct(IrrL[2],IrrL[4]));
Append(~IrrL,TensorProduct(IrrL[3],IrrL[4]));
Append(~IrrL,TensorProduct(IrrL[5],IrrL[4]));
X1:=OrderByDimension(CompositionFactors(Restriction(S912,L)));
Append(~IrrL,X1[1]);
X1:=OrderByDimension(CompositionFactors(Restriction(S923,L)));
if(IdentifyModule(X1[2],IrrL) eq 0) then Append(~IrrL,X1[2]); else Append(~IrrL,X1[3]); end if;
Append(~IrrL,Dual(GModule(L,FrobeniusImage(IrrL[10],1))));
Append(~IrrL,GModule(L,FrobeniusImage(IrrL[10],2)));
Append(~IrrL,Dual(TensorProduct(IrrL[10],IrrL[11])));
Append(~IrrL,Dual(TensorProduct(IrrL[10],IrrL[12])));
Append(~IrrL,Dual(TensorProduct(IrrL[11],IrrL[12])));
Append(~IrrL,Dual(TensorProduct(IrrL[13],IrrL[12])));
for i in [9..16] do Append(~IrrL,Dual(IrrL[i])); end for;
labsL:=["1","2_1","2_2","2_3","4_{1,2}","4_{1,3}","4_{2,3}","8",
"1w","2_1w","2_2w","2_3w","4_{1,2}w","4_{1,3}w","4_{2,3}w","8w",
"1m","2_1m","2_2m","2_3m","4_{1,2}m","4_{1,3}m","4_{2,3}m","8m"];

"The labels for irreducible modules are 1,2_1,etc., for the 1-eigenspace, 1w, etc. for the w-eigenspace, and 1m, etc., for the w^-1-eigenspace.";

IrrLd:=[Restriction(IrrL[i],Ld):i in [1..8]];

for i in [1,2,3,4,5,9,7,11,14] do
  labs[i] cat " restricts to L with summands";
  [DescribeLayers(j,IrrL,labsL):j in IndecomposableSummands(Restriction(Irr[i],L))];
end for;

"We verify the statements about P_L made in the paper";
PL:=PermutationModule(G2,L,GF(64));
/* As PL is defineable over GF(2), we need only check one module from each orbit under the Frobenius.
   Of course 1 is a simple quotient. We see that 8_1 and 64_{1,3} are simple quotients as L fixes
   a point on each of them. We also need to check that one of the 72s has no L-fixed points, and 512
   has one. */

Dimension(Fix(Restriction(X[22],L))) eq 1 and Dimension(Fix(Restriction(X[21],L))) eq 0;
JPL:=Dual(PL/FPL);
// We now check that removing a 1 from this does not affect the dimensions of hom-spaces.
// The first one of these takes a few minutes, the later ones are faster.
Dimension(AHom(JPL,S81)) eq 1;
Dimension(AHom(JPL,S6413)) eq 1;
Dimension(AHom(JPL,S912)) eq 0;
Dimension(AHom(JPL,S27)) eq 0;
Dimension(AHom(JPL,X[21])) eq 0;
// We of course do not need to check 512.

homs1:=AHom(PL,S81);
homs2:=AHom(PL,S82);
homs3:=AHom(PL,S83);
J2PL:=&meet[Kernel(homs1.1),Kernel(homs2.1),Kernel(homs3.1)];
Dimension(AHom(J2PL,S1)) eq 1;
Dimension(AHom(J2PL,S81)) eq 0;
Dimension(AHom(J2PL,S6413)) eq 1;
Dimension(AHom(J2PL,S912)) eq 0;
Dimension(AHom(J2PL,S27)) eq 0;
Dimension(AHom(J2PL,X[21])) eq 0;

"The calculations for Case 4";

"Check that I am right about the composition factors for M(A_8).";

MM:=DirectSum([Irr[i]:i in Ans4[4]]);
SequenceToMultiset(IdentifyFactors(Restriction(MM,L),IrrL)) eq
{* 1^^20, 2^^6, 3^^4, 4^^14, 6, 7^^2, 9^^10, 10^^3, 11^^8, 12^^6, 13^^3, 15^^7,
17^^10, 18^^3, 19^^8, 20^^6, 21^^3, 23^^7 *};

// Thus the 1-eigenspace is 1^^21, 2^^6, 3^^4, 4^^14, 6, 7^^2, if one adds a single trivial.

// Since M(A_8) has dimension 9, we need a trivial factor. Thus M(A_8)+M(A_8) is a subset of the factors above.
// So the pool from which to choose is 1^10,2^3,3^2,4^7,7 if there is a single trivial.

// If there are three trivials then each other factor appears six times in the tensor product with itself.
// So the pool becomes 2,4^2

Poss9s:=[[7,2,2,1],[7,2,3,1],[7,2,4,1],[7,3,3,1],[7,3,4,1],[7,4,4,1],[2,4,4,1,1,1]];
for i in Poss9s do SequenceToMultiset(IdentifyFactors(TensorPower(DirectSum([IrrL[j]:j in i]),2),IrrL)) eq {*1^^21, 2^^6, 3^^4, 4^^14, 6, 7^^2*}; end for;
"The case that is true is:";
[labsL[i]:i in Poss9s[4]];

Mod1:=DirectSum([IrrL[i]:i in [3,7]] cat [BuildLargestModule(IrrL[1],[IrrL[3]])]);
Mod2:=DirectSum([IrrL[i]:i in [1,3,3,7]]);

"Note there is no 4_{2,3} (or any 4) in this collection of summands:";
[DescribeLayers(i,IrrL,labsL):i in IndecomposableSummands(TensorProduct(Mod1,Dual(Mod1)))];


"Now Case 6. First check that the composition factors are as claimed.";

MM:=DirectSum([Irr[i]:i in Ans4[6]]);
SequenceToMultiset(IdentifyFactors(Restriction(MM,L),IrrL)) eq
{* 1^^14, 2^^5, 3^^5, 4^^5, 5^^4, 6^^4, 7^^4, 9^^8, 10^^5, 11^^5, 12^^5, 13^^2,
14^^2, 15^^2, 16^^2, 17^^8, 18^^5, 19^^5, 20^^5, 21^^2, 22^^2, 23^^2, 24^^2 *};

// Thus the exterior square of M(D_7) should have factors {*1^^13, 2^^5, 3^^5, 4^^5, 5^^4, 6^^4, 7^^4 *}

// The factors are invariant under the Frobenius, so we can just take one representative
// from each orbit. As A x B is a factor of the exterior square for A,B factors of the original
// module, cannot have 4,5, or 3,6, or 2,7, or 8,1. Cannot have more than 4 trivial factors

// Start with those with at least one 4 or 8 in them.
Poss14s:=[
[8,5,2],[8,5,3],[8,2,2,2],[8,2,2,3],[8,2,2,4],[8,2,3,4],
[5,5,5,2],[5,5,5,3],[5,5,5,1,1],[5,5,6,2],[5,5,7,3],[5,5,6,1,1],[5,5,7,1,1],[5,6,7,1,1],
[5,5,2,2,2],[5,5,2,2,3],[5,5,2,3,3],[5,5,3,3,3],[5,5,2,2,1,1],[5,5,2,3,1,1],[5,5,3,3,1,1],
[5,5,2,1,1,1,1],[5,5,3,1,1,1,1],[5,6,2,2,2],[5,6,2,2,1,1],[5,6,2,1,1,1,1],
[5,2,2,2,2,2],[5,2,2,2,2,3],[5,2,2,2,3,3],[5,2,2,3,3,3],[5,2,3,3,3,3],[5,3,3,3,3,3],
[5,2,2,2,2,1,1],[5,2,2,2,3,1,1],[5,2,2,3,3,1,1],[5,2,3,3,3,1,1],[5,3,3,3,3,1,1],
[5,2,2,2,1,1,1,1],[5,2,2,3,1,1,1,1],[5,2,3,3,1,1,1,1],[5,3,3,3,1,1,1,1]];


// For modules consisting entirely of 2s and 1s, to obtain 4_{i,j} we need both 2_i and 2_j.
// Since each occurs with multiplicity 4, we need with 2_i four times and 2_j once, or each two times.
// But 2_j and 2_k once means 4_{j,k} occurs once, so need each two times.

Append(~Poss14s,[2,2,3,3,4,4,1,1]);

for i in Poss14s do if(SequenceToMultiset(IdentifyFactors(ExteriorSquare(DirectSum([IrrL[j]:j in i])),IrrL)) eq {*1^^13, 2^^5, 3^^5, 4^^5, 5^^4, 6^^4, 7^^4 *}) then {*labsL[j]:j in i*}; end if; end for;

// First case is semisimple.
"The exterior square in the first case.";
MM1:=DirectSum([IrrL[i]:i in [2,3,4,8]]);
[DescribeLayers(i,IrrL,labsL):i in IndecomposableSummands(ExteriorSquare(MM1))];

"The exterior square in the second case.";
MM2:=DirectSum([IrrL[i]:i in [1,1,5,6,7]]);
[DescribeLayers(i,IrrL,labsL):i in IndecomposableSummands(ExteriorSquare(MM2))];


"Now Case 7. First, the 1-eigenspace.";

MM:=DirectSum([Irr[i]:i in Ans4[7]]);
SequenceToMultiset(IdentifyFactors(Restriction(MM,L),IrrL)) eq
{* 1^^14, 2^^6, 3^^4, 4^^8, 5^^2, 6^^3, 7^^4, 9^^9, 10^^4, 11^^6, 12^^6, 13^^3,
14, 15^^4, 16, 17^^9, 18^^4, 19^^6, 20^^6, 21^^3, 22, 23^^4, 24 *};

aa:={*1^^14, 2^^6, 3^^4, 4^^8, 5^^2, 6^^3, 7^^4*};

{*labsL[j]:j in aa*};

aa1:=aa diff {*1^^2,2^^2,3*};
aa2:=aa diff {*1^^2,3^^2,4*};
aa3:=aa diff {*1^^2,4^^2,2*};
NN1:=DirectSum([IrrL[i]:i in aa1]);
NN2:=DirectSum([IrrL[i]:i in aa2]);
NN3:=DirectSum([IrrL[i]:i in aa3]);


load "Traces/E6/TracesE6.txt";

"Only i=3 works for conspicuousness:";
[BrauerCharacter(NN1)[10]] in {i[2]:i in Tr7E6};
[BrauerCharacter(NN2)[10]] in {i[2]:i in Tr7E6};
[BrauerCharacter(NN3)[10]] in {i[2]:i in Tr7E6};
BrNN3:=BrauerCharacter(NN3);


"Now we compute the composition factors on M(E_6).";
BrNN3[5] eq 6 and BrNN3[10] eq 1;

[i[1]:i in Tr3E6|i[2] eq [BrNN3[5]]] eq [[0]],[i[1]:i in Tr7E6|i[2] eq [BrNN3[10]]] eq [[-1]];

[i[1]:i in Tr9E6|i[2] eq [BrNN3[13],BrNN3[5]]];

load "Subgroups/ComputeFactors.txt";

Tr3:={[0]};
Tr7:={[-1]};
Tr9:={i[1]:i in Tr9E6|i[2] in {[BrNN3[13],BrNN3[5]],[BrNN3[14],BrNN3[5]],[BrNN3[15],BrNN3[5]]}};
TrL:=<{[27]},{},Tr3,{},{},{},Tr7,{},Tr9>;
CCL:=ConjugacyClasses(Ld);

Ans3L,Mods3L:=ComputeConspicuousFactors(27,IrrLd,CCL,TrL,[7,9],[]);
Ans3L eq [
    [ 1, 1, 1, 2, 2, 2, 3, 4, 4, 6, 6, 7 ],
    [ 1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6 ],
    [ 1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 7, 7 ]
];

// These three all satisfy the requirements for the 1-eigenspace, but what about the w-eigenspace?

TT:=DirectSum([IrrLd[1],IrrLd[4]]);

// From the action above, we see that there are exactly nine trivials in the w-eigenspace.
"This is the correct action on M(E_6):";
for i in [1..3] do if(Multiplicity(SequenceToMultiset(IdentifyFactors(TensorProduct(Mods3L[i],TT),IrrLd)),1) eq 9) then {*labsL[j]:j in Ans3L[i]*}; end if; end for;

"Check the claim that the layers in the A_5 parabolic have no 1-cohomology. This is clear for L(0).";

B1:=DirectSum([IrrL[4],IrrL[7]]);
B2:=BuildLargestModule(IrrL[7],[IrrL[4]]);
Dimension(Ext(IrrL[1],ExteriorPower(B1,3))) eq 0;
Dimension(Ext(IrrL[1],ExteriorPower(B2,3))) eq 0;

"Summands of action on L(E_6) if M(A_5) is not semisimple:";

for i in IndecomposableSummands(TensorProduct(B2,Dual(B2))) do DescribeLayers(i,IrrL,labsL); end for;
for i in IndecomposableSummands(ExteriorPower(B2,3)) do DescribeLayers(i,IrrL,labsL); end for;

"Summands of action on L(E_6) if M(A_5) is semisimple (note that the desired summand does occur):";

for i in IndecomposableSummands(TensorProduct(B1,Dual(B1))) do DescribeLayers(i,IrrL,labsL); end for;
for i in IndecomposableSummands(ExteriorPower(B1,3)) do DescribeLayers(i,IrrL,labsL); end for;
