# List of finite order elements together with certain other data

FOQuints:=List(FOList,Quint);
Sort(FOQuints,QListOrd);

# Routine to find a canonical representative of the conjugacy class
# of a finite order element. _g_ should be an element of GammaBar
# or of FreeGp.

Canonical:=function(g)
  local sDist2List,gM,sDist2Min,MtxList;
  gM:=ToMtx(g);
  # Find out how far g moves each point on SmallPtList
  sDist2List:=List(SmallPtList,
    Trip->RatAppr(sDist2(Trip[1],OnPt(gM,Trip[1]))));
  sDist2Min:=Minimum(sDist2List);
  Print(
    "Canonical: g=",g,"\t# of relevant points=",
    SumX([1..Length(SmallPtList)],j->sDist2List[j] = sDist2Min,j->1),
    "\n");
  MtxList:=ListX([1..Length(SmallPtList)],
    #Select points which are moved the minimum distance
    j->sDist2List[j] = sDist2Min,
    [1..Length(KK)], # to consider KK-conjugates of the answers
    function(j,k)    #Calculate a conjugate which moves the origin
                     #the minimum possible distance
      return NormM(KKMtxes[k]*SmallPtList[j][3]*gM*SmallPtList[j][2]*KKInvList[k]);
    end
  );
  return Minimum(MtxList);
end;

# Attempt to find a list of conjugacy classes of finite order elements

CanList66:=[];

CanListKK:=List(
  [Identity(GammaBar),U^4,U^-4,U^8,U^-8,U^12,
     U,U^-1,U^2,U^-2,U^7,U^-7,
     U^3,U^-3,U^6,
     V,U*V,U^2*V,U^3*V,U^4*V,U^5*V],
  g->[Canonical(g),66,UnderlyingElement(g)]);
Sort(CanListKK);
CanKKF:=CanListKK{Filtered([1..Length(CanListKK)],
  j->j=1 or CanListKK[j][1] <> CanListKK[j-1][1])};

Append(CanList66,CanListKK);

CanListA:=List([A,A^-1,A*V,A^-1*V],
  g->[Canonical(g),66,UnderlyingElement(g)]);
Sort(CanListA);
CanAF:=CanListA{Filtered([1..Length(CanListA)],
  j->j=1 or CanListA[j][1] <> CanListA[j-1][1])};

Append(CanList66,CanListA);

CanListAUm7:=List([A*U^-7,(A*U^-7)^-1],
  g->[Canonical(g),66,UnderlyingElement(g)]);
Sort(CanListAUm7);
CanAUm7F:=CanListAUm7{Filtered([1..Length(CanListAUm7)],
  j->j=1 or CanListAUm7[j][1] <> CanListAUm7[j-1][1])};

Append(CanList66,CanListAUm7);

CanListAU4:=List(
  [A*U^4,(A*U^4)^2,V*(A*U^4)^-1,
     V*(A*U^4),(A*U^4)^-2,(A*U^4)^-1],
  g->[Canonical(g),66,UnderlyingElement(g)]);
Sort(CanListAU4);
CanAU4F:=CanListAU4{Filtered([1..Length(CanListAU4)],
  j->j=1 or CanListAU4[j][1] <> CanListAU4[j-1][1])};

Append(CanList66,CanListAU4);

CanListX1:=List(
  [A*U^7*A^-1*U^9,(A*U^7*A^-1*U^9)^-1],
  g->[Canonical(g),66,UnderlyingElement(g)]);
Sort(CanListX1);
CanX1F:=CanListX1{Filtered([1..Length(CanListX1)],
  j->j=1 or CanListX1[j][1] <> CanListX1[j-1][1])};

Append(CanList66,CanListX1);

CanListX2:=List(
  [A*U^-7*A^-1*U^2,(A*U^-7*A^-1*U^2)^-1],
  g->[Canonical(g),66,UnderlyingElement(g)]);
Sort(CanListX2);
CanX2F:=CanListX2{Filtered([1..Length(CanListX2)],
  j->j=1 or CanListX2[j][1] <> CanListX2[j-1][1])};

Append(CanList66,CanListX2);

CanListY:=List(
  [A*U^11*A^-1*U^4,(A*U^11*A^-1*U^4)^2,(A*U^11*A^-1*U^4)^-1],
  g->[Canonical(g),66,UnderlyingElement(g)]);
Sort(CanListY);
CanYF:=CanListY{Filtered([1..Length(CanListY)],
  j->j=1 or CanListY[j][1] <> CanListY[j-1][1])};

Append(CanList66,CanListY);

Sort(CanList66);
CanList66F:=CanList66{Filtered([1..Length(CanList66)],
  j->j=1 or CanList66[j][1] <> CanList66[j-1][1])};
Length(CanList66F);

CanList77:=List(FOList,g->[Canonical(g),77,UnderlyingElement(g)]);

CanList:=Concatenation(CanList66,CanList77);
Sort(CanList);
CanListF:=CanList{Filtered([1..Length(CanList)],
  j->j=1 or CanList[j][1] <> CanList[j-1][1])};
Length(CanListF);

# Results in this file:
Read("CanListF.gap");

# Orders of Elements in CanList66
#
# [ <identity ...>, 1 ]
#
# [ V, 2 ]
#
# [ A, 3 ]
# [ A^-1, 3 ]
# [ A*V, 6 ]
# [ A^-1*V, 6 ]
#
# [ A*U^-7, 3 ]
# [ U^7*A^-1, 3 ]
#
# [ A*U^-7*A^-1*U^2, 3 ]
# [ U^-2*A*U^7*A^-1, 3 ]
#
# [ A*U^7*A^-1*U^9, 3 ]
# [ U^-9*A*U^-7*A^-1, 3 ]
#
# [ A*U^4, 8 ]
# [ A*U^4*A*U^4, 4 ]
# [ U^-4*A^-1*U^-4*A^-1, 4 ]
# [ U^-4*A^-1, 8 ]
# [ V*A*U^4, 8 ]
# [ V*U^-4*A^-1, 8 ]
#
# [ A*U^11*A^-1*U^4, 4 ]
# [ A*U^11*A^-1*U^4*A*U^11*A^-1*U^4, 2 ]
# [ U^-4*A*U^-11*A^-1, 4 ]
#
# [ U, 24 ]
# [ U^2, 12 ]
# [ U^3, 8 ]
# [ U^4, 6 ]
# [ U^6, 4 ]
# [ U^7, 24 ]
# [ U^8, 3 ]
# [ U^12, 2 ]
# [ U^-8, 3 ]
# [ U^-7, 24 ]
# [ U^-4, 6 ]
# [ U^-3, 8 ]
# [ U^-2, 12 ]
# [ U^-1, 24 ]
#
# [ U*V, 12 ]
# [ U^2*V, 6 ]
# [ U^3*V, 4 ]
# [ U^4*V, 6 ]
# [ U^5*V, 12 ]



for pr in
  SortedList(List(CanListF,trip->
    [RatAppr(AbsSq(OnZero(trip[1])))*3*83739041,
      trip[3]]
  ))
do
  Print(pr[1],"\t",pr[2],"\n");
od;

######
# Code to check whether certain pairs of elements of finite order may
# belong to a common subgroup of finite order, after being
# appropriately conjugated.

SML2:=Filtered(SmallMtxList,m->AbsSq(OnZero(m))/R02k<>-3/2+rkk);;
SML3:=List(SML2,m->[m,m^-1]);;

FOEltList:=[A*U^-7,A*U^7*A^-1*U^9,A*U^-7*A^-1*U^2];
FOEltList2:=List(FOEltList,g->[ToMtx(g),UnderlyingElement(g)]);

FOTripList:=ListX(FOEltList2,SML3,
  function(gpr,mpr)
    return [NormM(mpr[1]*gpr[1]*mpr[2]),gpr[2],mpr[1]];
  end
);;


FOPList:=ListX(FOEltList2,FOTripList,
  function(gpr,gtrip)
    return NormM((((Comm(gpr[1],gtrip[1])^2)^2)^2)^3) = OneM;
  end,
  function(gpr,gtrip)
    Print("g1=",gpr[2],"\tg2=",gtrip[2],"\tm=",WordFromMtx(gtrip[3]),"\n");
    return [gpr[2],gtrip[2],gtrip[3]];tmp/yeung3
  end
);

Read("FOPList.gap");

List(FOPList{[7..24]},function(trip)
  local m1,m2;
  m1:=ToMtx(trip[1]);
  m2:=NormM(trip[3]*ToMtx(trip[2])*trip[3]^-1);
  return NormM(Comm(Comm(m1,m2),m2)^24) = OneM;
  end
);


## Elements which commute with V

mV:=ToMtx(V);
CommVList:=Set(ListX(KKMtxes,KKMtxes,KKCosets,
  function(k1,k2,m)
    local m2;
    m2:=k1*m*k2;
    return m2*mV = mV*m2;
  end,
  function(k1,k2,m)
    local w;
    w:=WordFromMtx(k1*m*k2);
    return w;
  end
));



#####
#
# The 6 index~16 subgroups of C18a

Ix16a:=Subgroup(GammaBar, [ V, U^(-1), A^(-1)*U^(-1)*A^(-1),
  A^(-1)*U^2*A^(-1)*U^(-1)*A^(-1)*U^2*A^(-1) ] );
Ix16b:=Subgroup(GammaBar, [ V, U^-1, A*U^-2*A, A*U*V*U^-1*A^-1,
  A^-1*U^-1*A^-1*U^-1*A^-1*U^-1*A^-1, A^-1*U^-1*A*U^-1*A*U^-1*A^-1,
  A*U^2*A*U*A*U^-1*A^-1, A*U^2*A^-1*U^-1*A^-1*U*A ] );
Ix16c:=Subgroup(GammaBar, [ V, U^-1, A*U*V*U^-1*A^-1, A*U^-3*A^-1,
  A^-1*U*A^-1*U*A^-1, A^-1*U^-3*A ] );
Ix16d:=Subgroup(GammaBar, [ V, U^-1, A*U*V*U^-1*A^-1, A*U^-1*V*U*A^-1,
  A^-1*U*A^-1*U*A^-1, A*U^2*A*U*A^-1,
  A^-1*U^-1*A^-1*U^-1*A^-1*U^-1*A^-1, A^-1*U^3*A^-1*U^-2*A ] );
Ix16e:=Subgroup(GammaBar, [ V, U^-1, A*U*V*U^-1*A^-1, A*U^-1*V*U*A^-1,
  A^-1*U*A^-1*U*A^-1, A*U*A^-1*U^-2*A, A^-1*U^-1*V*U*A,
  A*U^2*A^-1*U^-2*A^-1 ] );
Ix16f:=Subgroup(GammaBar, [ U^-1, V*A^-1*U^-1*A^-1, A*U^-2*A ] );

Index(GammaBar,Ix16a);
Index(GammaBar,Ix16b);
Index(GammaBar,Ix16c);
Index(GammaBar,Ix16d);
Index(GammaBar,Ix16e);
Index(GammaBar,Ix16f);

AbelianInvariants(Ix16a);
AbelianInvariants(Ix16b);
AbelianInvariants(Ix16c);
AbelianInvariants(Ix16d);
AbelianInvariants(Ix16e);
AbelianInvariants(Ix16f);

FCAa:=FactorCosetAction(GammaBar,Ix16a); # This is a _Mapping_
FCAb:=FactorCosetAction(GammaBar,Ix16b); # This is a _Mapping_
FCAc:=FactorCosetAction(GammaBar,Ix16c); # This is a _Mapping_
FCAd:=FactorCosetAction(GammaBar,Ix16d); # This is a _Mapping_
FCAe:=FactorCosetAction(GammaBar,Ix16e); # This is a _Mapping_
FCAf:=FactorCosetAction(GammaBar,Ix16f); # This is a _Mapping_

V^FCAa;
U^FCAa;
A^FCAa;
(A*U^4)^FCAa;
(A*U^-7)^FCAa;
(A*U^7*A^-1*U^9)^FCAa;
(A*U^-7*A^-1*U^2)^FCAa;
(A*U^11*A^-1*U^4)^FCAa;

V^FCAb;
U^FCAb;
A^FCAb;
(A*U^4)^FCAb;
(A*U^-7)^FCAb;
(A*U^7*A^-1*U^9)^FCAb;
(A*U^-7*A^-1*U^2)^FCAb;
(A*U^11*A^-1*U^4)^FCAb;

V^FCAc;
U^FCAc;
A^FCAc;
(A*U^4)^FCAc;
(A*U^-7)^FCAc;
(A*U^7*A^-1*U^9)^FCAc;
(A*U^-7*A^-1*U^2)^FCAc;
(A*U^11*A^-1*U^4)^FCAc;

V^FCAd;
U^FCAd;
A^FCAd;
(A*U^4)^FCAd;
(A*U^-7)^FCAd;
(A*U^7*A^-1*U^9)^FCAd;
(A*U^-7*A^-1*U^2)^FCAd;
(A*U^11*A^-1*U^4)^FCAd;

V^FCAe;
U^FCAe;
A^FCAe;
(A*U^4)^FCAe;
(A*U^-7)^FCAe;
(A*U^7*A^-1*U^9)^FCAe;
(A*U^-7*A^-1*U^2)^FCAe;
(A*U^11*A^-1*U^4)^FCAe;

V^FCAf;
U^FCAf;
A^FCAf;
(A*U^4)^FCAf;
(A*U^-7)^FCAf;
(A*U^7*A^-1*U^9)^FCAf;
(A*U^-7*A^-1*U^2)^FCAf;
(A*U^11*A^-1*U^4)^FCAf;


## Subgroup _a_ of index 16
## 
## Is generated by its finite order elements!
##

Ix16aSubgp0:=Subgroup(Ix16a,FOIx16a);
CosetTable(GammaBar,Ix16aSubgp0);

KKCosetList:=[A];

FOList1:=
  List(Cartesian(KK,KKCosetList,KK,FOList),
    q->q[1]*q[2]*q[3]*q[4]*q[3]^(-1)*q[2]^(-1)*q[1]^(-1));;
FOIx16a1:=Filtered(FOList1,e->e in Ix16a);;
List(FOIx16a1,e -> e^FCAa);
Ix16aSubgp1:=Subgroup(Ix16a,FOIx16a1);

FOIx16a2:=FOIx16a1{[2,8,20]};;
Ix16aSubgp2:=Subgroup(Ix16a,FOIx16a2);
CosetTable(GammaBar,Ix16aSubgp2);

List(GeneratorsOfGroup(Ix16a),g->g in Ix16aSubgp2);
List(GeneratorsOfGroup(Ix16aSubgp2),
  g->NormM((ToMtx(g))^24) = OneM);             
GeneratorsOfGroup(Ix16aSubgp2);

# Best version so far

FOIx16a3:=[ A^2*U^7, A*U^7*A*U^13, U*A*U^4*A*U^3 ];
List(FOIx16a3,g->
  Filtered([2,3,4,6,8,12,24],j->NormM((ToMtx(g))^j) = OneM)
);

Ix16aSubgp3:=Subgroup(GammaBar,FOIx16a3);
CosetTable(GammaBar,Ix16aSubgp3);
FCA16a3:=FactorCosetAction(GammaBar,Ix16aSubgp3); # This is a _Mapping_


#FOIx16a3:=
#[ U^-11*A*U^-11*V*A*U^17*A^2*U^11*A^-1*U^11,
#U^-11*A*U^-10*A^2*U^3*V*U^10*A^-1*U^11,
#U^-11*A*U^-10*A*U^4*A*U^14*A^-1*U^11 ]

# Generates all triples of elements in [1..20]

#I3list:=Concatenation(List([1..20],
#  n1->Concatenation(List([n1+1..20],
#    n2->List([n2+1..20],
#      n3->[n1,n2,n3]
#    )
#  ))
#));

#Looks for subgroups of index 16 generated by finite order elements

#for trip in I3list do
#  FOIx16a3:=FOIx16a1{trip};;
#  Ix16aSubgp3:=Subgroup(Ix16a,FOIx16a3);
#  Print("trip=",trip,"\n");
#  Print(GeneratorsOfGroup(Ix16aSubgp3),"\n");
#  PushOptions(rec(silent:=true));
#    Print(TryCosetTableInWholeGroup(Ix16aSubgp3),"\n");
#  PopOptions();
#od;

#####

## Subgroup _f_ of index 16
## 
## Is generated by its finite order elements!
##

FOIx16f2:=[ U^-1*A*U^7*A^2*U, A*U^15*V, A ];
List(FOIx16f2,g->
  Filtered([2,3,4,6,8,12,24],j->NormM((ToMtx(g))^j) = OneM)
);
List(FOIx16f2,g->g^(U^8*A) in Ix16f); 
Ix16fSubgp2:=Subgroup(GammaBar,FOIx16f2);
Index(GammaBar,Ix16fSubgp2);
 
FCAf2:=FactorCosetAction(GammaBar,Ix16fSubgp2); # This is a _Mapping_

V^FCAf2;
U^FCAf2;
A^FCAf2;
(A*U^4)^FCAf2;
(A*U^-7)^FCAf2;
(A*U^7*A^-1*U^9)^FCAf2;
(A*U^-7*A^-1*U^2)^FCAf2;
(A*U^11*A^-1*U^4)^FCAf2;
	
