// gettestwords.m : This is a Magma program which generates testwords 
// used in the construction of Khukhro's 2-generator 7-group and 
// 3-generator 5-group anti-Hughes groups, and for other counterexamples 
// to the Hughes conjecture.  These are written to two files 
// "twB27c8" and "twB35c5" and are described in HVL09

// In the comments mathematical notation, not Magma notation, is used. 
// Thus, in the comments [a, b] is a commutator, not a sequence.

// First contruct "twB27c8", used for 7-group computations
    
d := 2; p := 7; cl := 8; // ngens; prime; class
G<a,b> := pQuotient( FreeGroup(d), p, cl : Exponent := p );
print "|B(2,7 : 8)| = ", FactoredOrder(G); 
lastg := pRanks(G)[cl];
wt5start := pRanks(G)[4]+1; wt4start := pRanks(G)[3]+1; 
G5 := sub< G | [ G.i : i in [wt5start..lastg] ] >; // G5 is gamma_5 of G

S := [ (a, G.i) : i in [wt4start..lastg] ] ; // S = < [a, gamma_4(G)] >
extrasa := [];
for i := lastg to wt5start by -1 do // reverse order matters
  if G.i notin sub< G | S > then
    Append( ~S, G.i ); Append( ~extrasa, G.i );
  end if;
end for; //extrasa generates a complement for [a, gamma_4(G)] in gamma_5(G)

// Confirm that extrasa also generates a complement for 
// [a * (b,a,b)^i * (b,a,b,b)^j, gamma_4(G)] in gamma_5(G)
// (This relies on the way this complement was built)
for i in [0..p-1] do for j in [0..p-1] do
  S := [ (a * (b,a,b)^i * (b,a,b,b)^j, G.k) : k in [wt4start..lastg] ] ; 
  if sub< G | S cat extrasa > ne G5 then 
    print i, j, "ARGGGHHHH!!!!!!"; 
  end if;
end for; end for; // OK as long as there is no ARGGGHHHH!!!!!!

//Now do it for b
S := [ (b, G.i) : i in [wt4start..lastg] ] ;
extrasb := [];
for i := lastg to wt5start by -1 do
  if G.i notin sub< G | S > then
    Append( ~S, G.i ); Append( ~extrasb, G.i );
  end if;
end for; //extrasb generates a complement for [b, gamma_4(G)] in gamma_5(G)

for m in [0..p-1] do for i in [0..p-1] do for j in [0..p-1] do
  S := [ (a^m * b * (b,a,a)^i * (b,a,a,a)^j, G.k) : k in [wt4start..lastg] ] ;
  if sub< G | S cat extrasb > ne G5 then 
    print m, i, j, "ARGGGHHHH!!!!!!"; 
  end if;
end for; end for; end for;

Reverse(~extrasa); Reverse(~extrasb); // We now need extras in normal order 
wts := [ PCClass(x) : x in extrasa ];
words := [];
for i in [0..p-1] do for j in [0..p-1] do
  g := a * (b,a,b)^i * (b,a,b,b)^j;
  weight := 1 + 3*i + 4*j;
  explen := 1 + i + j;
  //Apply Higman's Lemma
  if weight gt 14 then continue; end if;
  if explen gt 1 and explen lt 7 then
    if weight+7-explen gt 14 then continue; end if;
  end if;
  Append( ~words, g );
  explen := explen+1;
  for k in [1..#extrasa] do
    if weight+wts[k] gt 14 then continue; end if;
    if weight+wts[k]+7-explen gt 14 then continue; end if;
    Append( ~words, g*extrasa[k] );
  end for;
end for; end for;

wts := [ PCClass(x) : x in extrasb ];
for m in [0..p-1] do for i in [0..p-1] do for j in [0..p-1] do
  g := a^m * b * (b,a,a)^i * (b,a,a,a)^j;
  weight := m + 1 + 3*i + 4*j;
  explen := m + 1 + i + j;
  if weight gt 14 then continue; end if;
  if weight+7-explen gt 14 then continue; end if;
  Append( ~words, g );
  explen := explen+1;
  for k in [1..#extrasb] do
    if weight+wts[k] gt 14 then continue; end if;
    if weight+wts[k]+7-explen gt 14 then continue; end if;
    Append( ~words, g*extrasb[k] );
  end for;
end for; end for; end for;

print #words, "testwords for B(2,7 : 8)";

SetOutputFile( "twB27c8" : Overwrite := true );
print "testwords := [";
for i in [1..#words-1] do print words[i], "," ; end for;
print words[#words], "];" ;
UnsetOutputFile();

//Now compute the testwords for Khukhro's 3-generator 5-group counterexample
d := 3; p := 5; cl := 5; // ngens; prime; class
G<a,b,c> := pQuotient( FreeGroup(d), p, cl : Exponent := p );
lastg := pRanks(G)[cl]; 
wt3start := pRanks(G)[2]+1; wt2start := pRanks(G)[1]+1; 
print "|B(3,5 : 5)| = ", FactoredOrder(G);
G3 := sub< G | [ G.i : i in [wt3start..lastg] ] >; // G3 is gamma_3 of G

S := [ (a, G.i) : i in [wt2start..lastg] ] ; // S = < [a, gamma_2(G)] >
extrasa := [];
for i := lastg to wt3start by -1 do
  if G.i notin sub< G | S > then
    Append( ~S, G.i ); Append( ~extrasa, G.i );
  end if;
end for; //extrasa generates a complement for [a, gamma_2(G)] in gamma_3(G)

// Check that extrasa also generates a complement for 
// [a * (c,b)^i, gamma_2(G)] in gamma_3(G)
for i in [0..p-1] do
  S := [ (a * (c,b)^i, G.k) : k in [wt2start..lastg] ] ; 
  if sub< G | S cat extrasa > ne G3 then 
    print i, "ARGGGHHHH!!!!!!"; 
  end if;
end for;

S := [ (b, G.i) : i in [wt2start..lastg] ] ;
extrasb := [];
for i := lastg to wt3start by -1 do
  if G.i notin sub< G | S > then
    Append( ~S, G.i ); Append( ~extrasb, G.i );
  end if;
end for;

for m in [0..p-1] do for i in [0..p-1] do
  S := [ (a^m * b * (c,a)^i, G.k) : k in [wt2start..lastg] ] ;
  if sub< G | S cat extrasb > ne G3 then 
    print m, i, "ARGGGHHHH!!!!!!"; 
  end if;
end for; end for; 

//And now for c
S := [ (c, G.i) : i in [wt2start..lastg] ] ;
extrasc := [];
for i := lastg to wt3start by -1 do
  if G.i notin sub< G | S > then
    Append( ~S, G.i ); Append( ~extrasc, G.i );
  end if;
end for;

for m in [0..p-1] do for n in [0..p-1] do for i in [0..p-1] do
  S := [ (a^m * b^n * c * (b,a)^i, G.k) : k in [wt2start..lastg] ] ;
  if sub< G | S cat extrasc > ne G3 then 
    print m, n, i, "ARGGGHHHH!!!!!!"; 
  end if;
end for; end for; end for;

Reverse(~extrasa); Reverse(~extrasb); Reverse(~extrasc); 
// Compute HVL09 conjugacy class representatives (3)
wts := [ PCClass(x) : x in extrasa ];
words := [];
for i in [0..4] do for j1 in [0..2] do for j2 in [0..2] do
for j3 in [0..2] do for j4 in [0..2] do for j5 in [0..2] do
  weight := 1 + 2*i + 3*(j1+j2+j3+j4+j5);
  if weight gt 9 then continue; end if;
  explen := 1 + i + j1+j2+j3+j4+j5;
  if weight+5-explen gt 9 then continue; end if;
  g := a * (c,b)^i * extrasa[1]^j1 * extrasa[2]^j2 * extrasa[3]^j3
         * extrasa[4]^j4 * extrasa[5]^j5;
  Append( ~words, g );
  explen := explen+1;
  for k in [6..#extrasa] do
    if weight+wts[k] gt 9 then continue; end if;
    if weight+wts[k]+5-explen gt 9 then continue; end if;
    Append( ~words, g*extrasa[k] );
  end for;
end for; end for; end for; end for; end for; end for;

// Compute HVL09 conjugacy class representatives (4)
wts := [ PCClass(x) : x in extrasb ];
for m in [0..4] do for i in [0..4] do for j1 in [0..2] do for j2 in [0..2] do
for j3 in [0..2] do for j4 in [0..2] do for j5 in [0..2] do
  weight := m + 1 + 2*i + 3*(j1+j2+j3+j4+j5);
  if weight gt 9 then continue; end if;
  explen := m + 1 + i + j1+j2+j3+j4+j5;
  if weight+5-explen gt 9 then continue; end if;
  g := a^m * b * (c,a)^i * extrasb[1]^j1 * extrasb[2]^j2 * extrasb[3]^j3
           * extrasb[4]^j4 * extrasb[5]^j5;
  Append( ~words, g );
  explen := explen+1;
  for k in [6..#extrasb] do
    if weight+wts[k] gt 9 then continue; end if;
    if weight+wts[k]+5-explen gt 9 then continue; end if;
    Append( ~words, g*extrasb[k] );
  end for;
end for; end for; end for; end for; end for; end for; end for;

// Compute HVL09 conjugacy class representatives (5)
wts := [ PCClass(x) : x in extrasc ];
for m in [0..4] do for n in [0..4] do
  if m+n eq 0 then continue; end if;
for i in [0..4] do for j1 in [0..2] do for j2 in [0..2] do
for j3 in [0..2] do for j4 in [0..2] do for j5 in [0..2] do
  weight := m + n + 2*i + 3*(j1+j2+j3+j4+j5);
  //Note that c does not contribute to the weight or exponent length. 
  if weight gt 9 then continue; end if;
  explen := m + n + i + j1+j2+j3+j4+j5;
  if weight+5-explen gt 9 then continue; end if;
  g := a^m * b^n * c * (b,a)^i * extrasc[1]^j1 * extrasc[2]^j2 
           * extrasc[3]^j3 * extrasc[4]^j4 * extrasc[5]^j5;
  Append( ~words, g );
  explen := explen+1;
  for k in [6..#extrasc] do
    if weight+wts[k] gt 9 then continue; end if;
    if weight+wts[k]+5-explen gt 9 then continue; end if;
    Append( ~words, g*extrasc[k] );
  end for;
end for; end for; end for; end for; end for; end for; end for; end for;

print #words, "testwords for B(3,5 : 5)";

SetOutputFile( "twB35c5" : Overwrite := true );
print "testwords := [";
for i in [1..#words-1] do print words[i],","; end for;
print words[#words],"];";
UnsetOutputFile();

