/* In this case we let H denote a copy of PSL(2,19), L denote the subgroup 19.9, and let G denote E6(3^18).
   We give matrices defining L, an element of order 19 and an element of order 9. We construct L to lie in
   normalizer of a torus NGT, as constructed using the GroupOfLieType command. We give H as L and a matrix
   h1, chosen at random. A posteriori, one could produce H as a subgroup of E6 and show that no other
   overgroup of L preserves the E6 trilinear form, but we prefer a less synthetic proof.
*/

q:=3^18;
F<zz>:=GF(q);
z:=zz^20390552;

l1:=GL(27,F)![[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]];

l2:=GL(27,F)!DiagonalMatrix([z^i:i in [12,3,14,15,4,7,9,15,17,16,18,8,10,10,18,2,1,3,12,11,13,2,5,13,14,6,8]]);

L:=sub<GL(27,F)|l1,l2>;

h1vals:=[[209518475,35036641,0,55986262,0,0,0,0,0,0,0,0,8789873,0,39114830,327828539,0,0,0,0,0,0,0,53911865,308920777,0,53208489],
[324559225,311915491,0,245571499,0,0,0,0,0,0,0,0,209518475,0,92083649,48262241,0,0,0,0,0,0,0,233015446,68343577,0,105235697],
[0,0,257516491,0,228863609,324032255,25368027,288621494,165141948,214316675,363289040,102769958,0,338337166,0,0,361624121,307542393,147145198,158161210,256655420,387315484,102315707,0,0,127731817,0],
[272506686,42471181,0,82823051,0,0,0,0,0,0,0,0,352188483,0,132846531,163312134,0,0,0,0,0,0,0,111942905,97786297,0,232645169],
[0,0,209002397,0,115303168,364693260,95775554,25325074,98405507,184715082,70611340,85842030,0,248692140,0,0,51371526,171042448,260393628,68029865,236179802,252483836,354159655,0,0,376682301,0],
[0,0,36444604,0,202946343,9111120,212978102,339496618,4484260,322238222,93774888,225142406,0,258472322,0,0,35542089,24080429,268328233,74923246,59636168,328384242,378371261,0,0,340678119,0],
[0,0,232622564,0,86532233,257475606,373184064,326412826,222385442,98405507,211351891,213180218,0,257185328,0,0,3949121,321899249,343036693,262887536,310372124,269324337,64824323,0,0,84691026,0],
[0,0,170137445,0,227614695,384681393,285386485,224543547,306522164,351897313,307542393,359010438,0,372796340,0,0,127672586,382102400,183332086,71258835,298170374,55641245,357939666,0,0,224627179,0],
[0,0,381411679,0,292468317,88072991,273824810,38317306,262887536,68029865,196471811,311277041,0,248190178,0,0,110808587,2237055,269336408,112753786,129398444,53443141,87139010,0,0,55858184,0],
[0,0,274885609,0,9410114,204724790,9111120,143165055,96841794,350580232,303866238,97287748,0,278357584,0,0,166993287,41002473,167434614,312107867,355440535,301569727,305977445,0,0,340430673,0],
[0,0,170572500,0,32830062,24611775,287266463,102250842,199324782,170923505,366684859,253496060,0,245315477,0,0,253600709,127920422,365125964,119258744,355208470,344362100,342664801,0,0,345399349,0],
[0,0,325549907,0,31101003,293634072,335776572,221415965,328921877,57627031,256655420,24949283,0,234137765,0,0,31080002,298170374,85909732,192041000,128549264,186769772,28734286,0,0,324903543,0],
[48262241,840721,0,248075265,0,0,0,0,0,0,0,0,327828539,0,90439534,368397129,0,0,0,0,0,0,0,359379569,116455870,0,357986971],
[0,0,270736853,0,143548619,360127397,289597533,344362100,279581155,246506538,209779902,376442822,0,200799827,0,0,58105261,97512326,269362433,175871883,34171377,145416602,75733261,0,0,330334487,0],
[126024505,82823051,0,340801757,0,0,0,0,0,0,0,0,68098401,0,122518835,53095558,0,0,0,0,0,0,0,172280297,300215835,0,178086390],
[105235697,278067702,0,156704081,0,0,0,0,0,0,0,0,53208489,0,7566489,357986971,0,0,0,0,0,0,0,95296003,70332443,0,277177345],
[0,0,359093957,0,12515294,9410114,202946343,294053047,307691925,96196972,33349919,229212436,0,35394936,0,0,41464896,167241344,334634533,224848297,288397784,153888373,139900362,0,0,97027387,0],
[0,0,285876286,0,116318779,349687120,267261675,365125964,67564023,161309288,112108749,368975541,0,380545979,0,0,50295526,218005747,249084266,157469421,248363646,269362433,319717132,0,0,330678389,0],
[0,0,378915164,0,349617059,369725015,152813731,245315477,209687960,218780307,386475452,18952500,0,156130122,0,0,239553019,122809745,380545979,310106577,162045318,200799827,73135712,0,0,321443012,0],
[0,0,177867272,0,17815943,41853589,214303819,136405738,84691026,376682301,378646751,310402581,0,311841589,0,0,96735170,133579832,108932009,55858184,199103306,7950513,82000080,0,0,20523898,0],
[0,0,369875640,0,341777870,62970110,253994904,116022601,73261576,134070086,257516491,255803156,0,333091078,0,0,379259328,170137445,310872572,275768962,325549907,188536350,199829211,0,0,16803927,0],
[0,0,255803156,0,199358028,315001276,374542561,131730038,246298558,71065761,102769958,339557696,0,55938121,0,0,368392367,359010438,340794654,177775766,24949283,83789483,364271521,0,0,28036025,0],
[0,0,62598204,0,43544972,195998419,128134693,160595909,3949121,51371526,347365818,259851150,0,193779065,0,0,64366538,139002959,71714003,110808587,335600277,340013787,259292672,0,0,96735170,0],
[68937326,97786297,0,300215835,0,0,0,0,0,0,0,0,352033470,0,238513435,225465121,0,0,0,0,0,0,0,335984323,196691429,0,315329769],
[273261854,132846531,0,122518835,0,0,0,0,0,0,0,0,335651577,0,98087669,295574945,0,0,0,0,0,0,0,297412379,238513435,0,307547742],
[0,0,310764807,0,286888726,166965793,319878801,68981041,64824323,354159655,27096134,162110593,0,9839521,0,0,259292672,119301928,316966076,87139010,35931878,371561166,177223234,0,0,82000080,0],
[233015446,160036574,0,276426118,0,0,0,0,0,0,0,0,53911865,0,46939681,359379569,0,0,0,0,0,0,0,209089609,288558846,0,95296003]];

h2vals:=[[209518475,35036641,0,55986262,0,0,0,0,0,0,0,0,8789873,0,39114830,327828539,0,0,0,0,0,0,0,53911865,308920777,0,53208489],
[324559225,311915491,0,245571499,0,0,0,0,0,0,0,0,209518475,0,92083649,48262241,0,0,0,0,0,0,0,233015446,68343577,0,105235697],
[0,0,167574552,0,47704829,262030330,324795811,121564420,28813772,237627220,103682498,255490969,0,280956947,0,0,368997723,290205510,291082161,75075788,17123842,125560767,204625976,0,0,120389965,0],
[272506686,42471181,0,82823051,0,0,0,0,0,0,0,0,352188483,0,132846531,163312134,0,0,0,0,0,0,0,111942905,97786297,0,232645169],
[0,0,376494065,0,385128985,91023506,147786203,378225774,54015106,52043374,333783711,211687840,0,178778860,0,0,382545772,312517675,260675519,240170522,96655056,348585487,340052151,0,0,387105476,0],
[0,0,243573735,0,122991847,286210153,371466224,93423360,162575770,6317500,239186420,99279550,0,185486983,0,0,119670146,174690184,220755520,343548044,122705767,17253934,342779012,0,0,166923735,0],
[0,0,365137029,0,82787882,320536916,266596753,25844913,359832310,54015106,182885910,2255683,0,82353615,0,0,357670450,254853610,223304373,366796961,67303354,202497164,247542986,0,0,33246323,0],
[0,0,67648781,0,247691911,53784114,198601226,3037040,155769942,40504196,290205510,106626267,0,154114578,0,0,49179086,329273026,130634916,185572874,11847363,113559373,334954333,0,0,271561206,0],
[0,0,105890103,0,361904516,348525943,368429235,245012772,366796961,240170522,43588065,80308479,0,139181738,0,0,98715466,366031031,347954249,80969878,4988560,180767868,167814363,0,0,214978993,0],
[0,0,106456919,0,348067803,257386568,286210153,207567827,11390459,325213601,160358592,346027678,0,43943840,0,0,276827407,327888958,333008058,62845450,247009830,248547512,373067692,0,0,69926634,0],
[0,0,177181258,0,39167490,4911234,27882093,144249214,301817847,304998077,246000240,360272807,0,287637989,0,0,211634565,121075020,85825202,25290273,194472969,143091359,386997270,0,0,33831383,0],
[0,0,13821632,0,314051251,328447031,259180884,55664429,340353873,243995320,17123842,344711216,0,93609973,0,0,289232783,11847363,148234799,87424004,337912938,385006330,216830981,0,0,161286203,0],
[48262241,840721,0,248075265,0,0,0,0,0,0,0,0,327828539,0,90439534,368397129,0,0,0,0,0,0,0,359379569,116455870,0,357986971],
[0,0,264218973,0,106192475,288699555,162511887,143091359,316876579,95801487,246451043,314834204,0,276899720,0,0,375697590,200574769,28230342,129711324,290525382,135981462,347068546,0,0,302070344,0],
[126024505,82823051,0,340801757,0,0,0,0,0,0,0,0,68098401,0,122518835,53095558,0,0,0,0,0,0,0,172280297,300215835,0,178086390],
[105235697,278067702,0,156704081,0,0,0,0,0,0,0,0,53208489,0,7566489,357986971,0,0,0,0,0,0,0,95296003,70332443,0,277177345],
[0,0,240151612,0,334785944,348067803,122991847,288128562,157776740,11914714,220582040,244675016,0,25603525,0,0,201808745,371970614,134566572,224432258,133575356,287254031,216499211,0,0,178188562,0],
[0,0,287124403,0,147810755,237644881,122505767,85825202,119665806,257518974,53447829,221418541,0,345909504,0,0,217707117,124394688,332408806,299878701,259596699,28230342,191467420,0,0,202715557,0],
[0,0,97489439,0,176845982,358381444,279102042,287637989,353874295,349450978,355205927,191873690,0,166724758,0,0,14141446,349555262,345909504,222555105,295216521,276899720,323017505,0,0,350997691,0],
[0,0,151891666,0,37369583,258245324,292536978,152463592,33246323,387105476,322968300,343349134,0,232024026,0,0,102514131,299672690,169557063,214978993,99687023,339155061,251368449,0,0,48829318,0],
[0,0,262452090,0,210540700,206478305,181207545,261417030,48154490,305754759,167574552,224769738,0,204089595,0,0,216151084,67648781,102563975,145006743,13821632,161482625,156933972,0,0,112179113,0],
[0,0,224769738,0,314982070,230582590,216554171,184795427,172072465,323968362,255490969,251513818,0,287326662,0,0,166620887,106626267,279888433,5948492,344711216,27333360,69278386,0,0,54314946,0],
[0,0,297427547,0,190250858,290784434,382460324,78830711,357670450,382545772,183984160,357880606,0,31042485,0,0,139126886,342752395,3309789,98715466,250468518,319911203,74847849,0,0,102514131,0],
[68937326,97786297,0,300215835,0,0,0,0,0,0,0,0,352033470,0,238513435,225465121,0,0,0,0,0,0,0,335984323,196691429,0,315329769],
[273261854,132846531,0,122518835,0,0,0,0,0,0,0,0,335651577,0,98087669,295574945,0,0,0,0,0,0,0,297412379,238513435,0,307547742],
[0,0,218157368,0,379988980,7769626,302190338,65479590,247542986,340052151,143750958,348991679,0,113627879,0,0,74847849,157494798,53551317,167814363,381481750,226843293,34083614,0,0,251368449,0],
[233015446,160036574,0,276426118,0,0,0,0,0,0,0,0,53911865,0,46939681,359379569,0,0,0,0,0,0,0,209089609,288558846,0,95296003]];

h1:=GL(27,F)![[zz^h1vals[i,j]-1:j in[1..27]]:i in[1..27]];
h2:=GL(27,F)![[zz^h2vals[i,j]-1:j in[1..27]]:i in[1..27]];

// Here is the hom space for L, even as a subgroup of NGT. This makes computation incredibly easy

R<a,m1,m2,m3,m4>:=PolynomialRing(F,5);
mats:=MatrixRing(R,27);
scal:=mats!DiagonalMatrix([m1,m1,m3,m1,a,a,a,m3,a,a,m3,m3,m1,m3,m1,m1,a,m3,m3,a,m3,m3,a,m1,m1,a,m1]);
a1:=[1,25,13,15,16,4,27,2,24];
a2:=[19,3,14,11,22,8,12,18,21];
for i in [1..9] do scal[a1[i],a2[i]]:=m2; scal[a2[i],a1[i]]:=m4; end for;
scal[2,18]:=-m2;
scal[13,14]:=-m2;
scal[25,3]:=-m2;
scal[18,2]:=-m4;
scal[14,13]:=-m4;
scal[3,25]:=-m4;

scal*mats!l1 eq mats!l1*scal and scal*mats!l2 eq mats!l2*scal;

seqs:=[[i,j,k]:i,j,k in [1..NumberOfRows(l1)]|i le j and j le k];

ents:=[300,322,340,354,359,609,637,661,681,697,901,917,947,1004,1020,1164,1182,1246,1283,1311,1399,1419,1521,1558,1577,1710,1744,1787,
1821,1922,1958,1989,2029,2184,2227,2260,2357,2388,2427,2586,2609,2710,2749,2859,2991];

vals:=[1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,1,2,1,2,2,1,2,1,2,1,2,1,1,2,2,1];

f27:=[0:i in [1..#seqs]];
for i in [1..#ents] do f27[ents[i]]:=vals[i]; end for;

function Matricise(v)
return Matrix(1,NumberOfColumns(v),[R!v[i]:i in [1..NumberOfColumns(v)]]);
end function;

V:=GModule(L);
g1:=mats!h1;
g2:=mats!h2;

intseqs1:=[seqs[i]:i in ents];
intseqs:=&join{{[i[1],i[2],i[3]],[i[1],i[3],i[2]],[i[2],i[1],i[3]],[i[2],i[3],i[1]],[i[3],i[1],i[2]],[i[3],i[2],i[1]]}:i in intseqs1};

function ProdRel(seq,g)

u:=V.seq[1]; v:=V.seq[2]; w:=V.seq[3];
u1:=Matricise(u)*scal; v1:=Matricise(v)*scal; w1:=Matricise(w)*scal;
u2:=Matricise(u)*g*scal; v2:=Matricise(v)*g*scal; w2:=Matricise(w)*g*scal;
e1:=&+[u1[1,i[1]]*v1[1,i[2]]*w1[1,i[3]]*f27[Position(seqs,Sort([i[1],i[2],i[3]]))]:i in intseqs];
e2:=&+[u2[1,i[1]]*v2[1,i[2]]*w2[1,i[3]]*f27[Position(seqs,Sort([i[1],i[2],i[3]]))]:i in intseqs];

return e1-e2;
end function;

function CheckRel(seq,gg)
u:=V.seq[1]; v:=V.seq[2]; w:=V.seq[3];
u2:=u^gg; v2:=v^gg; w2:=w^gg;
e1:=&+[u[i[1]]*v[i[2]]*w[i[3]]*f27[Position(seqs,Sort([i[1],i[2],i[3]]))]:i in intseqs];
e2:=&+[u2[i[1]]*v2[i[2]]*w2[i[3]]*f27[Position(seqs,Sort([i[1],i[2],i[3]]))]:i in intseqs];
return e1-e2;
end function;

coeffs0:=[a,m1,m2,m3,m4];

function ChangeCoefficient(coeffs,coeff,target)
coeffs2:=coeffs;
nn:=Position(coeffs0,coeff);
coeffs2[nn]:=target;
return [Evaluate(i,coeffs2):i in coeffs];
end function;

function ChangeCoefficients(coeffs,coeff,target)
coeffs2:=coeffs;
for i in [1..#coeff] do
  nn:=Position(coeffs0,coeff[i]);
  coeffs2[nn]:=target[i];
end for;
return [Evaluate(i,coeffs2):i in coeffs];
end function;

// We now check that there is a unique H over L for each module action.

function CheckDetermination()

ProdRel([3,5,17],g1) eq zz^323104754*(a+zz^258025978*m3+zz^258025978*m4)^3;
ProdRel([3,5,17],g2) eq zz^74478661*(a+zz^119231583*m3+zz^119231583*m4)^3;
c11:=ChangeCoefficient(coeffs0,m4,zz^323104754*a-m3);
c21:=ChangeCoefficient(coeffs0,m4,zz^74478661*a-m3);

Evaluate(ProdRel([3,6,9],g1),c11) eq a^2*(zz^226249632*a+m3);
Evaluate(ProdRel([3,6,9],g2),c21) eq a^2*(zz^171333783*a+m3);
c12:=ChangeCoefficient(c11,m3,-a*zz^226249632);
c22:=ChangeCoefficient(c21,m3,-a*zz^171333783);

Evaluate(ProdRel([3,6,13],g1),c12) eq zz^137040967*a^2*(m1+zz^242137805*m2);
Evaluate(ProdRel([3,6,13],g2),c22) eq zz^372690484*a^2*(m1+zz^338992927*m2);
c13:=ChangeCoefficient(c12,m2,-m1/zz^242137805);
c23:=ChangeCoefficient(c22,m2,-m1/zz^338992927);

// Now remove the centralizer of H in GL(27,k):

c14:=ChangeCoefficients(c13,[a,m1],[1,1]);
c24:=ChangeCoefficients(c23,[a,m1],[1,1]);

h11:=h1^(GL(27,F)!Evaluate(scal,c14));
h22:=h2^(GL(27,F)!Evaluate(scal,c24));

H1:=sub<GL(27,F)|l1,l2,h11>;
H2:=sub<GL(27,F)|l1,l2,h22>;

// As the two 18-dimensional factors here are not Aut(H)-conjugate, one obtains two classes of H
// containing the given L. (Note that the two dual 9s are Aut(H)-conjugate.)

// Now check that H1 and H2 lie in E6.

{CheckRel(i,h11):i in seqs} eq {0};
{CheckRel(i,h22):i in seqs} eq {0};

// This proves that the copy of PSL(2,19) we constructed is contained in E6.

return "";
end function;

// We now give code that can prove that the trilinear form we gave is the correct one. Choose two random elements that
// generate the subgroup 2^6.W(E6), and then build the trilinear form relations. We also check that l1 and l2 lie in
// NGT.

function CheckE6Form()

GG:=GroupOfLieType("E6",q);
W:=VectorSpace(F,6);
rho:=StandardRepresentation(GG);
Over:=GL(27,q);
g1:=elt<GG|W![z,1,1,1,1,1]>;
g2:=elt<GG|W![1,z,1,1,1,1]>;
g3:=elt<GG|W![1,1,z,1,1,1]>;
g4:=elt<GG|W![1,1,1,z,1,1]>;
g5:=elt<GG|W![1,1,1,1,z,1]>;
g6:=elt<GG|W![1,1,1,1,1,z]>;
Refs:=Reflections(GG);
Mats:=[rho(i):i in [g1,g2,g3,g4,g5,g6]];
MatsE:=[rho(i):i in Refs];
NGT:=sub<Over|Mats cat MatsE>;
T:=sub<NGT|Mats>;
E:=sub<NGT|MatsE>;

l1 in NGT and l2 in NGT;

repeat y1:=Random(E); y2:=Random(E); until E eq sub<E|y1,y2>;

mat:=[];
for h in [y1,y2] do
  for nn in [1..#seqs] do aa:=seqs[nn,1]; bb:=seqs[nn,2]; cc:=seqs[nn,3];
    val:=[F!0:i in [1..#seqs]];
    for i in [1..NumberOfRows(l1)] do if(h[aa,i] ne 0) then for j in [1..NumberOfRows(l1)] do if(h[bb,j] ne 0) then for k in [1..NumberOfRows(l1)] do if(h[cc,k] ne 0) then
      val[Position(seqs,Sort([i,j,k]))]+:=h[aa,i]*h[bb,j]*h[cc,k];
    end if; end for; end if; end for; end if; end for;
    val[nn]-:=1; Append(~mat,val); delete val;
  end for;
end for;

ftest:=Nullspace(Transpose(Matrix(F,mat))).1;
return &and[ftest[i] eq f27[i]:i in [1..#seqs]];
end function;

"The function CheckE6Form() checks that the trilinear form f27 from E6 is correct";
"The function CheckDetermination() checks that the determination of the possible conjugates into E6 is correct.";
