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f^-1*Af*Cf^-1*Bf*Af^-1*Bf*Af*Bf*Cf*Af^-1*Cf^-1*Af*Bf*Cf^-1*Bf^-1*Af^-1*Bf^-1*Af^-1*Cf*Af*Cf
];


# Finitely presented version of $\bar\Gamma$

GammaBarFP:=FreeGp/Rels;
A:=GammaBarFP.1;
B:=GammaBarFP.2;
C:=GammaBarFP.3;

# Important subgroups of $\bar\Gamma$

# These are Donald's
#index3aFP:=Group(A, C, B*A*B^-1, B*C*B^-1);
#index3bFP:=Group(A, C, B*A*B, B*C*B);

# These are Tim's
index3aFP:=Group(A, C, B*A*B^-1, B*C*B^-1);
index3bFP:=Group(A, C, B*A*B, B*C*B);

index3aFCA:=FactorCosetAction(GammaBarFP,index3aFP);
index3bFCA:=FactorCosetAction(GammaBarFP,index3bFP);

# List of finite order elements

# These are Donald's
#T1:=B;
#T2:=B*C*C*A*C^-1;
#T3:=A^-1*C*A^-1*B*C*C*A;

# These are Tim's
T1:=B;
T2:=B*C*C*A*C^-1;
T3:=A^-1*C*A^-1*B*C*C*A;

FOList:=[T1,T2,T3];

# Check the indexes of the subgroups

Index(GammaBarFP,index3aFP) = 3;
Index(GammaBarFP,index3bFP) = 3;

# Check normality of the subgroups

IsNormal(GammaBarFP,index3aFP);
not IsNormal(GammaBarFP,index3bFP);

# Check that normalizers are as expected

Normalizer(GammaBarFP,index3aFP) = GammaBarFP;
Normalizer(GammaBarFP,index3bFP) = index3bFP;

# Calculate abelianizations of the subgroups

AbelianInvariants(GammaBarFP) = [3,4];

AbelianInvariants(index3aFP) = [4,7];
AbelianInvariants(index3bFP) = [2,3,4];

# Check which elements of finite order belong to which conjugates of
# which finite index subgroups 

List(FOList,k->k^index3aFCA);
List(FOList,k->k^index3bFCA);

# Check that the index 3 groups are torsion free

ForAll(FOList,k->MovedPoints(k^index3aFCA) = [1..3]);
ForAll(FOList,k->MovedPoints(k^index3bFCA) = [1..3]);

# Function to calculate fundamental group

FundGp:=function(G,FOList)
  local e1,e2,e3,e4,FCA,GFO,G0;
  e1:=GeneratorsOfGroup(GammaBarFP);
  e2:=Concatenation(e1,List(e1,Inverse));
  Add(e2,One(G));
  e3:=ListX(e2,e2,\*);
  FCA:=FactorCosetAction(GammaBarFP,G);
  GFO:=Concatenation(
    List(FOList,fo->
      List(Difference([1..Index(GammaBarFP,G)],MovedPoints(fo^FCA)),
        j -> fo^First(e3,e->j^(e^FCA) = 1)
      )
    )
  );
  Print("Size(GFO)=",Size(GFO),"\n");
  e1:=GeneratorsOfGroup(G);
  e2:=Concatenation(e1,List(e1,Inverse));
  Add(e2,One(G));
  e3:=ListX(e2,e2,\*);
  e4:=ListX(e2,e3,\*);
  G0:=Group(ListX(e4,GFO,
    function(elt,fo) return fo^elt; end
  ));
  Print(ForAll(GFO,fo->fo in G0),"\n");
  Print(IsNormal(G,G0),"\n");
  return FactorGroup(G,G0);
end;

StructureDescription(FundGp(GammaBarFP,FOList)) = "C4";

# This calculates the automorphism group of the corresponding surface
AutGp:=G->FactorGroup(Normalizer(GammaBarFP,G),G);

StructureDescription(AutGp(index3aFP)) = "C3";
StructureDescription(AutGp(index3bFP)) = "1";
