/*
%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%%
MAGMA code for the building F_4(2,4)

This file contains the following:

(1) Setup of the group E_6(4) and the twisted group 2E_6(2) in the Groups of Lie type package. At the time of writing, there appeared to be some bugs with the built-in twisted groups package, and thus we construct the twisted group directly.

(2) The minimal faithful permutation representation of the twisted group 2E6(4) and its extension 2E6(4).2. At the time of writing, these permutation representations were not available in either MAGMA, GAP, or the online ATLAS, and therefore we constructed them directly via the action on type 1 vertices of the F4(2,4) building. The details of the construction are omitted; here we simply give two generating permutations on 3968055 points, and an explicit isomorphism back to the Group of Lie Type.

(3) An explicit list of conjugacy class representatives for the group 2E6(2).2 (the full automorphism group of the F4(2,4) building). These representatives were computed by Bill Unger (from the MAGMA team) using the above permutation representation. 

(4) Domesticity routines in the F4(2,4) building (code for detecting chambers and simplices mapped onto opposite simplices)

(5) Code accompanying Theorem 4.4 in "Opposition diagrams for automorphisms of small spherical buildings" (J. Parkinson, H. Van Maldeghem)

Note: One may need to set the path for the "load" commands below. 
%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%%
*/


/*
%%%%%%%%%%%
(1) The group E6(4) and the twisted group 2E6(4)
%%%%%%%%%%%
*/

//field setup:
q:=4;
F<e>:=GF(q);
fieldauto:=iso<F -> F | e:->e^2>;
fixed:={x : x in F | fieldauto(x) eq x};

//root datum
RD:=RootDatum("E6" : Isogeny:="SC");
RD1:=RootDatum("F4" : Isogeny:="SC");

//Twisting of the Coxeter groups:
WCox<[u]>:=CoxeterGroup(GrpFPCox,RD);
ww0:=LongestElement(WCox);
WPerm,f:=CoxeterGroup(GrpPermCox,WCox);
invf:=Inverse(f);
WCox1<[t]>:=CoxeterGroup(GrpFPCox,RD1);
WPerm1,f1:=CoxeterGroup(GrpPermCox,WCox1);
invf1:=Inverse(f1);
twisting:=hom<WPerm1->WPerm | f(u[2]), f(u[4]), f(u[3]*u[5]), f(u[1]*u[6])>;
Wtwist:=Image(twisting);
twistinginverse:=Inverse(twisting);

//The E_6(4) group (as a Chevalley group) and its standard matrix representation:
G:=GroupOfLieType(RD,F : Normalising := true);
rho:=StandardRepresentation(G);
H:=Image(rho);//this is E_6(4) in matrix form
inv:=RowReductionHomomorphism(rho);//This gives the inverse of rho.
sigma:=GraphAutomorphism(G,Sym(6)!(1,6)(3,5));//the graph automorphism of E6
	
//Chevalley generators for the untwisted group:
x:=function(i,k);
	return
	elt<G|<i,k>>;
end function;
//representatives of the simple reflections in G
s:=function(i);
		return
		elt<G|i>;
end function;
//the W-valued distance function in the E_6(4) building
delta:=function(g1,g2);
		u,h,w,b:=Bruhat(g1^(-1)*g2);
		return
		w;
end function;

//MAGMA's inbuilt order for the positive and negative roots in E_6:
index:=[2,4,3,1,8,9,7,13,15,12,19,17,18,24,22,23,26,27,29,30,33,34,35,36,2+36,4+36,3+36,1+36,8+36,9+36,7+36,13+36,15+36,12+36,19+36,17+36,18+36,24+36,22+36,23+36,26+36,27+36,29+36,30+36,33+36,34+36,35+36,36+36];

//Those roots fixed by the graph involution sigma:
fixedbysigma:={@ 1, 2, 5, 9, 11, 14, 16, 18, 20, 22, 23, 24, 25, 26, 29, 33, 35, 38, 40, 42, 44, 46, 47, 48 @};

//Chevalley generators for the twisted group F_4(2,4). If i is fixed by sigma then x1(i,a) is defined only for a in F_2 (the fixed subfield). If i is not fixed by sigma then x1(i,a) is defined for all values of a in F. 
x1:=function(i,a);
	if i in fixedbysigma then;
		return
		x(index[i],a);
	end if;
	if i notin fixedbysigma then;
		return
		x(index[i],a)*sigma(x(index[i],a^2));	
	end if;
end function;
//representatives for the simple reflections of the F_4 Coxeter group in G
s1:=function(i);
	if i eq 1 then;
		return
		s(2);
	end if;
	if i eq 2 then;
		return
		s(4);
	end if;
	if i eq 3 then;
		return
		s(3)*s(5);
	end if;
	if i eq 4 then;
		return
		s(1)*s(6);
	end if;
end function;

//Some conversions:
CoxToG:=function(w);
	ww:=Id(G);
	for k in {1..Length(w)} do;
		ww:=ww*s(Eltseq(w)[k]);
	end for;
	return
	delta(Id(G),ww);
end function;
GToCox:=function(w);
	s:=Eltlist(w);
	ww:=Id(WCox);
	for k in {1..#s} do;
		ww:=ww*u[s[k]];
	end for;
	return
	ww;
end function;
w0:=CoxToG(ww0);//the longest element of E_6 as an element of G (this is also the longest element of F_4). 


/*
%%%%%%%%%%%
(2) The minimal faithful permutation representations of 2E_6(4) and 2E6(4).2
%%%%%%%%%%%
*/

permorder:=3968055;
simplegrouporder:=76532479683774853939200;
extendedgrouporder:=2*simplegrouporder;


//Loading the permutation representation. The elements gg1 and gg2 are generators of the simple group, and gg3 is the permutation for the graph automorphism sigma
load "F424lists.txt";
GG:=SymmetricGroup(permorder);
gg1:=GG!listg1;
gg2:=GG!listg2;
gg3:=GG!listsigma;

GPerm1:=sub<GG | {@gg1,gg2@}>;//the inner automorphism group of F_4(2,4) building: The simple group 2E6(4)
GPerm:=sub<GG | {@gg1,gg2,gg3@}>;//the full automorphism group of F_4(2,4) building: 2E6(4).2

GPerm1`Order := simplegrouporder;
GPerm`Order :=extendedgrouporder;

//Converting back to matrix form:
h1:=GL(27,F)!Matrix(F,27,27,[ 1, e, e^2, e, 1, e^2, 1, e, e^2, e^2, 1, e^2, 1, 1, e^2, e, 1, e, 0, 1, 1, e^2, e, 1, 
0, e, e^2, 1, e, e^2, e, 1, e^2, 1, 1, 1, 0, e^2, e^2, e, 1, 0, e^2, e^2, e^2, e, e, 0, 
e, e^2, 0, e, e^2, 1, 0, 0, e, e, e, 0, e, 0, 1, 0, e^2, e, 0, 0, 0, 0, e^2, e, e, 1, e,
e, e, 0, 1, e, 1, 0, 0, e, e, 0, 0, e, e, e^2, e, e^2, 0, e, e, 0, 0, 1, 0, 0, e, 0, 1, 
0, 0, 0, 0, 0, 0, 0, 0, 0, e, 0, 0, 1, 0, 1, e, 0, 1, e^2, 0, e, e, e, 1, e, 1, 1, 0, 0,
0, 0, 0, e, 0, e, 0, 1, e, e, 0, e^2, e, 0, e, e^2, e, e^2, 0, 1, 0, 0, e^2, e, 1, 0, 0,
e, 0, 0, e^2, e, 1, e, 0, 1, e^2, 0, 0, e^2, 1, 1, 0, e^2, e, 0, 0, 0, 0, 0, 1, 0, 0, 0,
1, 0, 0, 1, 1, e, e^2, 0, e^2, 1, 1, 0, e^2, e, e, e, 1, 0, e^2, e, e^2, 1, e^2, 0, 1, 
e, 0, e, 0, 0, 0, e^2, 1, 1, 1, 0, 0, 0, 0, e^2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, e^2, 0, 
1, 0, 1, 0, 0, e, 0, 0, e, 1, 1, e, e, 0, 0, e^2, 1, e, e, 1, e^2, 1, e^2, e, e, 0, e, 
e^2, 0, e, e^2, 1, e, 0, 0, e, 1, 1, e, 0, 0, e, e^2, 1, 0, e, e^2, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, e^2, e^2, 1, e^2, 1, e, e^2, e, 1, 1, 1, 0, 0, 1, 1, e, 1, e, e, 0, 0,
e^2, e^2, 1, e^2, e^2, e^2, 1, e, 1, e^2, e, e^2, 1, e^2, e, e, e, 1, e^2, e, e, e, e, 
e^2, 1, e, e, e, e, e^2, e, e^2, e, 0, 1, 1, 1, 0, e^2, 1, e, 1, e, 1, e^2, 1, 0, 1, e, 
0, 0, e^2, 0, e, 1, e^2, 0, 1, e, 0, e^2, e, 0, e^2, 1, e^2, e^2, e^2, 1, 0, e, 0, e^2, 
0, e, e, 1, 1, 1, e, 1, e, 0, 1, e, e^2, e, e, 1, e^2, e, e, e^2, e, 1, e, e^2, 1, 1, e,
1, e, 1, e^2, e^2, e^2, 1, 0, 1, 1, e, 1, 1, e, 0, e, e, e^2, 0, 0, e^2, e^2, e^2, 1, e,
1, e^2, 0, e, e, 0, 1, 1, 1, 1, e, 0, 1, e, e^2, 1, e, e, e^2, 1, 1, 0, e^2, 1, e, e^2, 
1, e^2, 0, e, 0, e, 0, 0, e^2, 1, e, 0, 0, 1, 0, 0, 1, e^2, 0, 1, e, e^2, 1, 1, e^2, 1, 
1, 0, e, 1, 0, e^2, e, e^2, 1, 0, e, 1, 0, 0, 0, 0, e^2, e^2, 0, 0, e^2, e^2, e, e^2, e,
0, e^2, 0, 1, e, 1, 0, e^2, e^2, e^2, e, e^2, 1, e, 1, 0, 1, 0, 0, e^2, e, e^2, 0, 0, 0,
e^2, e^2, 1, 0, 0, 0, e^2, e^2, 0, 0, e, 0, 0, e^2, 0, e, 0, 0, 0, 0, 0, 1, 0, 0, 1, 
e^2, e^2, 1, e, 0, 1, e, 0, e, 1, 0, e^2, e^2, e^2, e, e^2, e, e, 0, 0, 0, 0, 0, e^2, 
e^2, 0, 1, e^2, e, 1, e, 1, e^2, 1, e, 0, 1, e^2, 0, e, 0, 0, 0, e, e, 0, 0, e, 0, 0, e,
1, e, e, 1, 0, e, e, 0, e^2, e, e, 0, 0, 0, 0, e^2, 0, e, 0, e^2, e, e, 0, e, 0, 0, e^2,
0, 0, e^2, 0, e, e^2, 1, 0, e, e^2, 0, 1, 0, 1, e, 0, 0, 1, 1, 1, 1, e, 0, 1, e, e^2, 
e^2, e^2, e^2, 1, e^2, 0, 1, e, e, e^2, 1, 1, e, 1, 1, e, e, 0, e^2, e^2, e, 0, 1, 1, e,
1, 1, 0, e^2, 0, 1, 0, 0, 1, e^2, e, e^2, 1, 1, e^2, e^2, 1, 1, e, 0, e, e^2, e, 1, 1, 
1, e, 1, e ]);
h2:=GL(27,F)!Matrix(F,27,27,[ 1, e, 0, e, 1, e, 1, 1, e^2, e^2, 1, 0, e^2, e^2, 0, e, 1, e^2, e^2, e, 1, e^2, e, 1, 
e, 1, 0, 1, e, 0, e, 1, e, 1, e, 1, 0, e^2, 1, 1, 0, e^2, 0, e^2, e, 0, 1, 0, e, 1, 1, 
e^2, 0, e^2, 0, 0, e, e, e, 1, e, 1, 1, 0, e^2, e, e, 1, 0, e, e^2, e^2, 0, 0, e, e, e, 
1, 0, e, e^2, 0, 0, e, e, 0, 1, e, e, e^2, e, e^2, e^2, 0, e^2, 0, 0, 1, 1, e^2, e, 0, 
1, e^2, e^2, 1, 0, e^2, 0, 0, 0, 0, e, 0, 0, 0, 0, 1, e, e, 0, 0, 0, 0, e, e, 0, e^2, 1,
1, e, e, e^2, 0, 1, e, 0, e, 0, 1, e, e, e^2, e^2, e, 0, e, e^2, e, e^2, 1, 1, 0, e^2, 
0, e, 1, 0, e^2, e, e, e^2, e^2, e, e, e, 0, 0, e^2, 0, 0, e^2, 1, 1, e, 1, 1, e, 0, 1, 
0, 0, 1, 0, e^2, 0, e^2, 1, 0, 1, 1, 0, e^2, 0, 1, 1, 1, 0, e^2, e, 1, 0, e, e, 1, e, 1,
0, e^2, 0, 1, 0, e, 1, 1, 1, 0, e^2, 0, 1, 1, e^2, 0, e^2, 0, e^2, 0, 0, e, 0, 1, 0, 0, 
e^2, 0, e^2, e^2, 0, 0, 0, 1, 0, 0, e, 0, 0, e, 1, 0, e, 1, 0, 0, e^2, e, 0, e^2, 1, e, 
1, e, 1, 1, 0, e, 0, e^2, e^2, 1, e, e, 0, 0, e, 1, 0, e, e^2, 0, e, e^2, 1, e, e^2, 
e^2, e, 1, 1, e^2, e^2, 0, 0, e^2, 0, 1, 1, e^2, 0, e^2, e, 1, e^2, e, e, 1, e, 1, 1, 
e^2, 1, e, 0, 1, e, e, e^2, 0, 0, 0, 0, 0, e^2, 1, e, e^2, 1, 0, 1, e^2, 1, e^2, e^2, 
e^2, e, e, 0, e^2, 0, 0, 0, e, 1, e, e^2, e, e, 0, 0, 0, 0, e, e, 0, 1, 1, 1, e^2, e^2, 
e, e, 1, e, 0, e, e, 0, e, e, e^2, 1, 0, 0, e, 0, e^2, e^2, e, e^2, 0, e^2, e^2, 0, e^2,
1, e^2, 1, e^2, 1, 0, 0, e, e^2, 1, e, e, 1, 0, e^2, e, 1, 0, e^2, 1, e, e, e, e, e, 
e^2, e, 0, e^2, e^2, 1, e, e^2, 1, e, e^2, e, e^2, 1, 1, 0, e, 1, 0, e, e^2, e^2, 0, e, 
e, 0, e, e, e^2, 1, 0, 1, e^2, e^2, 1, 0, e, 1, 0, e, e, 1, 0, e^2, 1, 1, 0, e^2, e^2, 
e, e, 1, e, 1, e^2, 1, e^2, 0, 0, 1, e, e^2, 0, 0, 1, 1, 1, e, e, 1, 0, 1, e, e, 1, 0, 
e, 1, 1, e^2, 1, 1, e, 0, 1, 0, e^2, 1, 1, 0, e^2, e^2, 1, 1, e, 0, 0, 1, e, 1, 1, e^2, 
e, 1, e, e^2, 0, 0, e^2, e^2, 0, e^2, 0, 0, e^2, 0, 1, e, 1, 0, e^2, e^2, 1, 0, 1, 1, e,
e, e^2, 1, 0, e, e^2, e, 0, 0, 0, 0, e^2, e^2, 1, 0, 0, 1, e, e^2, e^2, e^2, e, 0, 1, 
e^2, 0, e, 1, 1, 0, e^2, 1, 1, 0, 0, 1, e^2, 0, 1, 0, 0, 1, e, 0, e^2, e, 0, 1, e^2, 0, 
0, 1, e, e, 1, e^2, e^2, 1, e, e^2, e^2, 1, 1, e^2, 1, 1, 0, 1, e^2, 1, e, 1, e^2, e, 1,
e, e^2, 1, e, e, e, 1, 1, 0, 1, 1, e, 1, 0, e, 1, 1, e, 1, 0, e^2, e, 0, e, e^2, e, e, 
e^2, 1, 0, e^2, e^2, e, 0, e^2, 1, e, e, e^2, 0, 0, e^2, 0, 0, e^2, 1, 0, e, e^2, e, 
e^2, 1, 1, e^2, 0, e, 1, 1, 1, 1, 1, e, 0, e^2, e^2, e^2, e^2, e, 1, e^2, e^2, 1, 0, e, 
e^2, 1, 1, e^2, e^2, 0, e, e, e^2, e, 1, e, 0, 0, e, 0, e, 0, 0, e^2, 1, 1, 0, e^2, 1, 
e^2, e, e^2, 1, 0, e, 1, 0, 0, e, e^2, e^2, e^2, e, 1, 0, 0, 0, 0, e^2 ]);

HH:=sub<GL(27,F) | {@h1,h2@}>;//This is the simple group 2E6(4) in matrix form
HH`Order := simplegrouporder;

//converting from the permutation group to the matrix group (run takes a few minutes to compute)
convert:=hom<GPerm1->GL(27,F) | gg1->h1,gg2->h2>;


/*
%%%%%%%%%%%
(3) Conjugacy class representatives for 2E6(4).2
%%%%%%%%%%%
*/

//loading the classes in compact form as a variable "cl". The first 105 classes are in the simple group 2E6(4), and the remaining 84 are in the extended group:
load "TE62d2_classes.txt";

//Some conversions and unpacking of the classes data:
RandomSchreier(GPerm);
ChangeBase(~GPerm, cl[1,3]);
X := Representation(GPerm);

//The permutation representative for the i-th class:
permrep:=function(i);
	return
	Permutation(X,cl[i,3]);
end function;

/*
Representatives of the element g for inner automorphisms g and outer automorphisms g*sigma in Chevalley form are obtained by inv(convert(permrep(i))) and inv(convert(permrep(i)*gg3)) respectively. This takes about 15 minutes to run, and so instead we read in the representatives below.
*/

load "F424innerreplist.txt";
load "F424outerreplist.txt";

//function to return the G-part of an inner/outer conjugacy class representative
innerrep:=function(i);//for i in {1..105}
	return
	elt<G|innerreplist[i]>;
end function;
outerrep:=function(i);//for i in {1..84}
	return
	elt<G|outerreplist[i]>;
end function;


/*
%%%%%%%%%%%
(4) Domesticity routines in the F4(2,4) building (use the "sigma" options for outer automorphisms).
%%%%%%%%%%%
*/

//detecting chambers mapped to opposite: Randomly selects nn chambers, and checks if delta(gB,theta gB)=w_0. If such a chamber is detected, then the code terminates and returns 1. Otherwise 0 is returned.
chamberop:=function(nn,g);
	tt:={};
	for n in {1..nn} do;
		gg:=inv(Random(HH));
		if delta(gg,g*gg) eq w0 then;
			tt:=tt join {gg};
			break n;
		end if;
	end for;
	return
	#tt;
end function;
chamberopsigma:=function(nn,g);
	tt:={};
	for n in {1..nn} do;
		gg:=inv(Random(HH));
		if delta(gg,g*sigma(gg)) eq w0 then;
			tt:=tt join {gg};
			break n;
		end if;
	end for;
	return
	#tt;
end function;

//similar to the chamberop command. This code detects simplices of type "type" mapped to opposite. 
simplexop:=function(nn,type,g);
	sequence:=0;
	for n in {1..nn} do;
		gg:=inv(Random(HH));
		w:=invf1(twistinginverse(f(GToCox(delta(Id(G),w0*delta(gg,g*gg))))));
		s:=Eltseq(w);
		ss:={s[j] : j in {1..#s}};
			if ss subset {k : k in {1..Rank(RD1)} | k notin type} then;
				sequence:=1;
				break n;
			end if;
	end for;
	return
	sequence;
end function;
simplexopsigma:=function(nn,type,g);
	sequence:=0;
	for n in {1..nn} do;
		gg:=inv(Random(HH));
		w:=invf1(twistinginverse(f(GToCox(delta(Id(G),w0*delta(gg,g*sigma(gg)))))));
		s:=Eltseq(w);
		ss:={s[j] : j in {1..#s}};
			if ss subset {k : k in {1..Rank(RD1)} | k notin type} then;
				sequence:=1;
				break n;
			end if;
	end for;
	return
	sequence;
end function;



/*
%%%%%%%%%%%
(5) Code to accompany Theorem 4.4 in "Opposition diagrams for automorphisms of small spherical buildings" (J. Parkinson and H. Van Maldeghem)
%%%%%%%%%%%
*/

/*
the automorphisms
theta1 -> innerrep(2)
theta2 -> innerrep(3)
theta4 -> innerrep(8)
theta3 -> outerrep(2)
theta1 and theta3 do not require any computation.
*/

theta1:=x1(24,1);
theta2:=x1(21,1);
theta3:=Id(G);//the G-part of sigma
theta4:=x1(1,1)*x1(2,1);
th4:=x1(14,1)*x1(18,1);//a conjugacy class rep of theta4


//the analysis for theta2 (see Theorem 4.4)
term:=function(z);
	return
	x1(21+24,1)*x1(22+24,z[1])*x1(23+24,z[2])*x1(24+24,z[3]);
end function;
wvalues:={};
search:=CartesianPower(fixed,3);
for z in search do;
	wvalues:=wvalues join {GToCox(delta(Id(G),term(z)))};
end for;
wvaluesF4:={invf1(twistinginverse(f(w))) : w in wvalues};
{Length(w) : w in wvaluesF4};


//The analysis for theta4 (using the representative th4)
noncommuting:={4,7,16,18,9,6,14,2};//4,7,6 are not fixed by sigma (and hence permit F entries) while the others are fixed by sigma. Hence there are 2048=4^3*2^5 elements to check.
uu:=function(z);
return
x1(4,z[1])*x1(7,z[2])*x1(16,z[3])*x1(18,z[4])*x1(9,z[5])*x1(6,z[6])*x1(14,z[7])*x1(2,z[8]);
end function;
//The following code searches the 2048 elements.
search:={[a1,a2,a3,a4,a5,a6,a7,a8] : a1 in F, a2 in F, a6 in F, a3 in fixed, a4 in fixed, a5 in fixed, a7 in fixed, a8 in fixed};
wvalues:={};
for z in search do;
	wvalues:=wvalues join {GToCox(delta(Id(G),w0^(-1)*uu(z)^(-1)*th4*uu(z)*w0))};
end for;
wvaluesF4:={invf1(twistinginverse(f(w))) : w in wvalues};
{Length(w) : w in wvaluesF4};
//we see that maximum length obtained is 23.


/*
Verifying that all other conjugacy classes are non-domestic.
*/


domesticinner:={1,2,3,8};
domesticouter:={2};
nondomesticinner:={1..105} diff domesticinner;
nondomesticouter:={1..84} diff domesticouter;
//the code below finds a non-domestic chamber for all remaining classes:
for i in nondomesticinner do;
	g:=innerrep(i);
	<i,chamberop(10000,g)>;
end for;
for i in nondomesticouter do;
	g:=outerrep(i);
	<i,chamberopsigma(10000,g)>;
end for;



/*
Computing the fixed points
*/
permtheta1:=permrep(2);
permtheta2:=permrep(3);
permtheta3:=permrep(105+2);
permtheta4:=permrep(8);
//fixed points equal permorder-"Degree", where degree is the number of moved points
permorder-Degree(permtheta1);
permorder-Degree(permtheta2);
permorder-Degree(permtheta3);
permorder-Degree(permtheta4);



