##############################################################################
##
#W  gpd.g                        Gpd Package                     Chris Wensley
#W                                                                & Emma Moore
##
#H  @(#)$Id: gpd.g,v 1.01 2006/05/12 gap Exp $
##
#Y  Copyright (C) 1999,2001 University of St. Andrews, North Haugh,
#Y                          St. Andrews, Fife KY16 9SS, Scotland
##
Revision.("gpd/examples/gpd.g") :=
   "@(#)$Id: gpd.g,v 1.01 2006/05/12 gap Exp $";

SetInfoLevel( InfoGpd, 2 );

Print( "\n===============================================================\n");
Print( "<<<< testing examples in the Gpd manual (version 12/05/06) >>>>\n" );
Print( "<<<<      functions for groupoids and their morphisms      >>>>\n" );
Print( "===============================================================\n\n");

d8 := Group( (1,2,3,4), (1,3) );;
SetName( d8, "d8" );
Gd8 := ConnectedGroupoid( [-9,-8,-7], d8 );
Print( Gd8, "\n" );

c6 := Group( (5,6,7)(8,9) );;
SetName( c6, "c6" );
Gc6 := GroupAsGroupoid( -6, c6 );
Print( Gc6, "\n" );
Gd8c6 := GroupoidByUnion( [ Gd8, Gc6 ] );
Display( Gd8c6 );
SetName( Gd8, "Gd8" );
SetName( Gc6, "Gc6" );
SetName( Gd8c6, "Gd8+Gc6" );

Print( ObjectsOfGroupoid( Gd8c6 ), "\n" );

f2 := FreeGroup( 2 );
SetName( f2, "f2" );
Gf2 := Groupoid( -22, f2 );
q8 := SmallGroup( 8,4 );
Gq8 := Groupoid( [ -28, -27 ], q8 );
SetName( Gq8, "Gq8" );
Gq8f2 := Groupoid( [ Gf2, Gq8 ] );
Print( [ IsFpGroupoid(Gf2), IsPcGroupoid(Gq8), IsPcGroupoid(Gq8f2) ], "\n" );

e1 := GroupoidElement( Gd8, (1,2)(3,4), -9, -8 );
e2 := GroupoidElement( Gd8, (1,3), -8, -7 );
prod := e1*e2;
Print( "e1 = ", e1, "\ne2 = ", e2, "\n" );
Print( [ GroupElement( e2 ), Tail( e2 ), Head( e2 ) ], "\n" );
Print( "prod = ", prod, "\n" );
e3 := GroupoidElement( Gd8, (1,3)(2,4), -7, -9 );
cycle := prod*e3;
Print("e3 = ",e3, "\ncycle = ",cycle, "  has order = ", Order(cycle), "\n\n" );
for a in Gd8 do 
    if ( ( GroupElement(a) = (1,3) ) and ( Tail(a) = -8) )  then 
        Print( a, "\n" ); 
    fi; 
od;

Print( [ Size( Gd8 ), Size( Gc6 ), Size( Gd8c6 ), Size( Gq8f2 ) ], "\n\n" );

G4 := Groupoid( [ Gd8c6, Gf2, Gq8 ] );
Display( G4 );
Print( "Size of G4 is ", Size(G4), "\n\n" );
G5 := Groupoid( [ Gq8, Gf2, Gd8c6 ] );
Print( "G4 = G5 ?  ", G4=G5, "\n\n" );

### Section 2.2 : Subgroupoids ###

c4d := Subgroup( d8, [ (1,2,3,4) ] );
SetName( c4d, "c4d" );
k4d := Subgroup( d8, [ (1,2)(3,4), (1,3)(2,4) ] );
SetName( k4d, "k4d" );
Ud8 := Subgroupoid( Gd8, [ [[-9], k4d], [[-8,-7], c4d] ] );
SetName( Ud8, "Ud8" );
Uk4d := Subgroupoid( Gd8, [ [[-9], k4d] ] );
SetName( Uk4d, "Uk4d" );
Uc4d := Subgroupoid( Gd8, [ [[-8,-7], c4d] ] );
SetName( Uc4d, "Uc4d" );
Display( Ud8 );

e := GroupoidElement( Gd8, (1,3), -7, -8 );
re := RightCoset( Gd8, Ud8, e );
Print( "elements of right coset of Ud8 by ", e, "\n" );
for x in re do Print( x, "\n" ); od;

rcrd8 := RightCosetRepresentatives( Gd8, Ud8 );
Print( "right coset reps for Ud8 in Gd8:\n", rcrd8, "\n\n");
rcd8 := RightCosetsNC( Gd8, Ud8 );;
Print( "right cosets for Ud8 in Gd8:\n" );
Display( rcd8 );
## check that this is a partition
elGd8 := ElementsOfGroupoid( Gd8 );
Pr := ListWithIdenticalEntries( 72, 0 );
Qr := ListWithIdenticalEntries(  9, 0 );
count := 0;  q := 0; num := 0;
for r in rcd8 do 
    num := 0;
    q := q+1;
    for x in r do
        pos := Position( elGd8, x );
        count := count + 1;
        num := num+1;
        Pr[pos] := count;
    od;
    Qr[q] := num;
od;
Print( Pr, "\n", Qr, "\n" );

lcrd8 := LeftCosetRepresentatives( Gd8, Ud8 );
Print( "left coset reps for Ud8 in Gd8:\n", lcrd8, "\n\n");
lcd8 := LeftCosetsNC( Gd8, Ud8 );;
Print( "left cosets for Ud8 in Gd8:\n" );
Display( lcd8 );
## check that this is a partition
Pl := ListWithIdenticalEntries( 72, 0 );
Ql := ListWithIdenticalEntries(  9, 0 );
count := 0;  q := 0; num := 0;
for l in lcd8 do 
    num := 0;
    q := q+1;
    for x in l do
        pos := Position( elGd8, x );
        count := count + 1;
        num := num+1;
        Pl[pos] := count;
    od;
    Ql[q] := num;
od;
Print( Pl, "\n", Ql, "\n" );

SGd8 := FullSubgroupoid( Gd8, [-9,-7] );
Print( "SGd8 = ", SGd8, "\n" );
SUd8 := FullSubgroupoid( Ud8, [-9,-7] );
Print( "SUd8 = ", SUd8, "\n" );

MUd8 := MaximalDiscreteSubgroupoid( Ud8 );
Print( "MUd8 = ", MUd8, "\n" );
c3c := Subgroup( c6, [ (5,6,7) ] );;
SetName( c3c, "c3c" );
comp1 := [ [-9], k4d ];;
comp2 := [ [-8,-7], c4d ];;
comp3 := [ [-6], c3c ];;
Uc3c := Subgroupoid( Gc6, [comp3] );
SetName( Uc3c, "Uc3c" );
Ud8c6 := SubgroupoidByComponents( Gd8c6, [comp1,comp2,comp3] );
Print( "Ud8c6 = ", Ud8c6, "\n" );
FUd8 := FullIdentitySubgroupoid( Ud8 );
Print( "FUd8 = ", FUd8, "\n" );
DUd8 := DiscreteIdentitySubgroupoid( Ud8 );
Print( "DUd8= = ", DUd8, "\n\n" );

c4q := Subgroup( q8, [ q8.1 ] );
SetName( c4q, "c4q" );
Uc4q := Subgroupoid( Gq8, [ [[-28,-27], c4q] ] );
SetName( Uc4q, "Uc4q" );


### Section 2.3 : Operations on Groupoid Elements ###

star9 := ObjectStar( Gd8, -9 );
Print( "elements in ", star9, " with group element of order 4:\n" );
for e in star9 do
    if ( Order( e![1] ) = 4 ) then Print( e, "\n" ); fi;
od;
costar9 := ObjectCostar( Gd8, -9 );
Print( "elements in ", costar9, " with group element of order 4:\n" );
for e in costar9 do
    if ( Order( e![1] ) = 4 ) then Print( e, "\n" ); fi;
od;
hset78 := Homset( Ud8, -7, -8 );
Print( hset78, " has elements:\n" );
for e in hset78 do  Print( e, "\n" );  od;

Print("\n");
i7 := IdentityElement( Gd8, -7 );;
i8 := IdentityElement( Gd8, -8 );
Print( "i7 = ", i7, ", i8 = ", i8, "\n" );
L := [ ];
for e in hset78 do  Add( L, i7*e*i8 = e );  od;
Print( "checking that i7, i8 are left/right identites for the homset:\n" );
Print( L, "\n" );

x := GroupoidElement( Gd8, (2,4), -9, -9 );
y := GroupoidElement( Gd8, (1,2,3,4), -8, -9 );
z := x^y;
Print( "Conjugation: elements x, y, and z = x^y in Gd8:\n" );
Print( x, ",  ", y, ",  ", z, "\n" );

#####################################################################

Print("\n\n*** switching to q8 and a4 ***\n\n");

a4 := Group( (6,7,8), (7,8,9) );
SetName( a4, "a4" );
Ga4 := Groupoid( [-21,-20], a4 );
SetName( Ga4, "Ga4" );
Gq8a4 := Groupoid( [ Gq8, Ga4 ] );
SetName( Gq8a4, "Gq8+Ga4" );
c3a := Subgroup( a4, [ (6,7,8) ] );
SetName( c3a, "c3a" );
k4a := Subgroup( a4, [ (6,7)(8,9), (6,8)(7,9) ] );
SetName( k4a, "k4a" );
C1 := [ [-28,-27], c4q ];
C2 := [ [-21], k4a ];
C3 := [ [-20], c3a ];
Ua4 := Subgroupoid( Ga4, [C2,C3] );
SetName( Ua4, "Ua4" );
Uk4a := Subgroupoid( Ga4, [C2] );
SetName( Uk4a, "Uk4a" );
Uc3a := Subgroupoid( Ga4, [C3] );
SetName( Uc3a, "Uc3a" );
Uq8a4 := SubgroupoidByComponents( Gq8a4, [C1,C2,C3] );
Display( Gq8a4 );
Display( Uq8a4 );

lcrs3ba4 := LeftCosetRepresentatives( Gq8a4, Uq8a4 );
Print( "left coset reps for Uq8a4 in Gq8a4:\n", lcrq8a4, "\n\n");
lcq8a4 := LeftCosetsNC( Gq8a4, Uq8a4 );;
Print( "left cosets:\n" );
Display( lcq8a4 );


#####################################################################

Print("\n\n*** switching to Emma's example 1.2.13 ***\n\n");

c2b := Group( (11,12) );
SetName( c2b, "c2b" );
Gc2b := Groupoid( [-19,-18,-17], c2b );
SetName( Gc2b, "Gc2b" );
B78 := [ [-18,-17], IdentitySubgroup(c2b) ];
B9 := [ [-19], c2b ];	
Hc2b := SubgroupoidByComponents( Gc2b, [ B78, B9 ] );
Display( Gc2b );
Display( Hc2b );

lcrc2b := LeftCosetRepresentatives( Gc2b, Hc2b );
Print( "left coset reps for Hc2b in Gc2b:\n", lcrc2b, "\n" );
lcc2b := LeftCosetsNC( Gc2b, Hc2b );;
Print( "left cosets:\n" ); 
Display( lcc2b ); 
for i in [1.. Length( lcc2b )] do 
    lc := lcc2b[i];
    cset := [ ];
    for e in lc do
        Add( cset, e );
    od;
    Print( cset, "\n" );
od;

#####################################################################

Print("\n\n*** and now to Emma's example 1.2.14 ***\n\n");

s3b := Group( (11,12), (12,13) );
SetName( s3b, "s3b" );
Gs3b := Groupoid( [-26,-25], s3b );
SetName( Gs3b, "Gs3b" );
c3b := Subgroup( s3b, [ (11,12,13) ] );
SetName( c3b, "c3b" );	
Hs3b := SubgroupoidByComponents( Gs3b, [ [ [-26], c3b ], [ [-25], c2b] ] );
Display( Gs3b ); 
Display( Hs3b );

lcrs3b := LeftCosetRepresentatives( Gs3b, Hs3b );
Print( "left coset reps for Hs3b in Gs3b:\n", lcrs3b, "\n" );
lcs3b := LeftCosetsNC( Gs3b, Hs3b );;
Print( "left cosets:\n" ); 
Display( lcs3b ); 
for i in [1.. Length( lcs3b )] do 
    lc := lcs3b[i];
    cset := [ ];
    for e in lc do
        Add( cset, e );
    od;
    Print( cset, "\n" );
od;
Print("\n\n");
rcrs3b := RightCosetRepresentatives( Gs3b, Hs3b );
Print( "right coset reps for Hs3b in Gs3b:\n", rcrs3b, "\n" );
rcs3b := RightCosetsNC( Gs3b, Hs3b );;
Print( "right cosets:\n" ); 
Display( rcs3b ); 
for i in [1.. Length( rcs3b )] do 
    rc := rcs3b[i];
    cset := [ ];
    for e in rc do
        Add( cset, e );
    od;
    Print( cset, "\n" );
od;

#####################################################################

Print("\n\n*** extending Emma's example 1.2.14 to 4 objects ***\n\n");

Ks3b := Groupoid( [-26,-25,-24,-23], s3b );
SetName( Ks3b, "Ks3b" );
Js3b := SubgroupoidByComponents( Ks3b, [ [[-26,-23],c3b], [[-25,-24],c2b] ] );
Display( Ks3b );
Display( Js3b );

lcr2s3b := LeftCosetRepresentatives( Ks3b, Js3b );
Print(Length(lcr2s3b)," left coset reps for Js3b in Ks3b:\n", lcr2s3b, "\n" );
lc2s3b := LeftCosetsNC( Ks3b, Js3b );;
Print( "left cosets:\n" ); 
Display( lc2s3b ); 
for i in [1.. Length( lc2s3b )] do 
    lc := lc2s3b[i];
    cset := [ ];
    for e in lc do
        Add( cset, e );
    od;
    Print( cset, "\n" );
od;
Print("\n\n");
rcr2s3b := RightCosetRepresentatives( Ks3b, Js3b );
Print(Length(rcr2s3b)," right coset reps for Js3b in Ks3b:\n", rcr2s3b, "\n" );
rc2s3b := RightCosetsNC( Ks3b, Js3b );;
Print( "right cosets:\n" ); 
Display( rc2s3b ); 
for i in [1.. Length( rc2s3b )] do 
    rc := rc2s3b[i];
    cset := [ ];
    for e in rc do
        Add( cset, e );
    od;
    Print( cset, "\n" );
od;

#####################################################################

Print("\n\n*** switching to a five object example ***\n\n");

s3c := Group( (1,2), (2,3) );
SetName( s3c, "s3c" );
Gs3c := Groupoid( [-5,-4,-3,-2,-1], s3c );
SetName( Gs3c, "Gs3c" );
c2c := Subgroup( s3c, [ (1,2) ] );
SetName( c2c, "c2c" );
c3cc := Subgroup( s3c, [ (1,2,3) ] );
SetName( c3cc, "c3cc" );
B15 := [ [-5,-1], c2c ];
B24 := [ [-4,-2], c2c^(1,2,3) ];
B3 := [ [-3], c3cc] ];
Us3c := SubgroupoidByComponents( Gs3c, 
    [ [ [-5,-3,-1], c2c ], [ [-4,-2], c3cc ] ] );
Vs3c := SubgroupoidByComponents( Gs3c, 
    [ [ [-5,-1], c2c ], [ [-4,-2], c2c^(1,2,3) ], [ [-3], c3cc ] ] );
Display( Gs3c );
Display( Us3c );
Display( Vs3c );
SetName( Gs3c, "Gs3c" );
SetName( Us3c, "Us3c" );
SetName( Vs3c, "Vs3c" );
Print("\n\n");

lcrs3c := LeftCosetRepresentatives( Gs3c, Us3c );
Print( Length(lcrs3c), " left coset reps for Us3c in Gs3c:\n", lcrs3c, "\n" );
lcs3c := LeftCosetsNC( Gs3c, Us3c );;
Print( "left cosets:\n" ); 
Display( lcs3c ); 
for i in [1.. Length( lcs3c )] do 
    lc := lcs3c[i];
    cset := [ ];
    for e in lc do
        Add( cset, e );
    od;
    Print( cset, "\n" );
od;
Print("\n\n");
rcrs3c := RightCosetRepresentatives( Gs3c, Us3c );
Print( Length(rcrs3c), " right coset reps for Us3c in Gs3c:\n", rcrs3c, "\n" );
rcs3c := RightCosetsNC( Gs3c, Us3c );;
Print( "right cosets:\n" ); 
Display( rcs3c ); 
for i in [1.. Length( rcs3c )] do 
    rc := rcs3c[i];
    cset := [ ];
    for e in rc do
        Add( cset, e );
    od;
    Print( cset, "\n" );
od;

dcrs3c := DoubleCosetRepresentatives( Gs3c, Us3c, Vs3c );
Print( Length(dcrs3c), " double coset reps for Us3c.Gs3c.Vs3c:\n" );
Print( dcrs3c, "\n" );
dcs3c := DoubleCosetsNC( Gs3c, Us3c, Vs3c );;
Print( "double cosets:\n" ); 
Display( dcs3c ); 
for i in [1.. Length( dcs3c )] do 
    dc := dcs3c[i];
    cset := [ ];
    for e in dc do
        Add( cset, e );
    od;
    Print( cset, "\n" );
od;

d12 := Group( (15,16,17,18,19,20), (15,20)(16,19)(17,18) );
SetName( d12, "d12" );
Gd12 := ConnectedGroupoid( [-37,-36,-35,-34], d12 );
SetName( Gd12, "Gd12" );
s3d := Subgroup( d12, [ (15,17,19)(16,18,20), (15,20)(16,19)(17,18) ] );
SetName( s3d, "s3d" );
Gs3d := SubgroupoidByComponents( Gd12, [ [[-36,-35,-34], s3d] ] );
SetName( Gs3d, "Gs3d" );

Print("\n=============================================================\n\n");
