Santiago de Compostela Summer School, July 2007
GAP Lesson 1
Start up GAP. The command
gap> LogTo("GAPlesson1");
keeps a record of your GAP session.
Arithmetic Operations
(You learn: + - * / ^, terminating input with ;, last. Factors, mod,
Int, Gcd, Lcm,Factorial.)
Try the following. Press the return key at the end of each
line.
gap> 5+7;
gap> 12/17;
gap> 2/3 + 3/4;
gap> 2^3*3^3;
gap> 3.14159;
DISCUSS.
gap> Factors(1111111);
gap> Factors(11111111111);
gap> Factors(2^32 +1);
gap> Factors(216);
gap> last;
gap> Collected(last);
There are also variables last2 and last3. Guess what their values are.
EXERCISE 1: Find the first three numbers of the form 111..1 which are prime. Note that such a number must have a prime number of digits. As well as Factors, another useful function is IsPrime.
Try the following.
gap> -5 mod 11;
gap> 6 mod -5;
gap> 6 mod 0;
gap> Int(295/7);
gap> Gcd(216,930);
gap> Lcm(216,930);
gap> Factorial(6);
What
would you expect Int(-1/2) to
be?
Permutations
(You learn: permutations are enclosed in (), operations have the same form as for integers. Conjugation is built in.)
gap> (1,2,3)*(1,2);
gap> (1,2,3)^-1;
gap> (1,2,3)^(2,5);
gap> 1^(1,2,3);
When we come to them, other kinds of elements such as matrices, and elements of finite fields can be manipulated with the same syntax.
Help
(You learn: how to call up and browse sections of the manual and that help lines are not terminated with ";" .)
gap> ?
gap> ?Help
gap> ?Factor
gap> ?A f s
gap> ?>
gap> ??
True and False; Assignment of Variables
(You learn: The difference between = and :=, how to store things in memory.)
gap> 216=2^3*3^3;
gap> 216=2^3;
gap> g=17;
gap> g;
gap> g:=17;
gap> g;
gap> g=17;
gap> g=21;
gap> g^2;
gap> 3<=2;
gap> 3>=2;
EXERCISE 2: What response do you expect from the following?
gap> true and false;
gap> true or false;
For more about this, see the evaluation problem.
Permutation groups; groups from the library; operations on groups
(You learn: how to input a group generated by permutations. The operations Size, Center, DerivedSubgroup, in, SylowSubgroup, Elements, SymmetricGroup, AlternatingGroup, DihedralGroup.)
gap> a:=Group((2,3,5)(6,7,8),(1,2,4,7)(3,6,8,5));
gap> Size(a);
gap> Center(a);
gap> Size(last);
gap> g:=SymmetricGroup(6);
gap> Size(g);
gap> h:=DerivedSubgroup(g);
gap> Size(h);
gap> (1,2) in h;
gap> (1,2,3) in h;
gap> p:=SylowSubgroup(g,2);
gap> Size(p);
gap> Elements(p);
gap> Center(p);
gap> Size(last);
gap> d:=DihedralGroup(8);
gap> Size(d);
EXERCISE 3: Identify the Sylow 2-subgroups of
gap> c:=Group((1,2,3,4,5,6,7),(2,3)(4,7));
and
gap> b:=Group((2,3)(4,6)(5,7),(1,2,4)(3,5,8));
Finitely presented groups; known attributes
(You learn: how to enter a finitely presented group, to time a command, and to find known attributes of an object.)gap> F:=FreeGroup(2);
gap> x:=F.1;
gap> y:=F.2;
gap> n:=25;
gap> G:=F/[x^n*y^(n+1),x^(n-1)*y^n];
gap> Order(G);
gap> TimeToString(time);
gap> Order(G);
gap> TimeToString(time);
DISCUSS
Now try the following (noting the effect of ending a command with
";;").
gap> F:=FreeGroup(2);;
gap> x:=F.1;;
gap> y:=F.2;;
gap> n:=2000;;
gap> D:=F/[x^n,y^2,(x*y)^2];;
gap> KnownAttributesOfObject(D);
gap> Order(DerivedSubgroup(D));
gap> KnownAttributesOfObject(D);
Lists
(You learn: [ ] notation for lists, how to access terms in a list, how lists are stored and duplicated, Sort, Add, Append, Length, Position, Flat, vector operations, List, ranges.)
gap> Primes;
gap> Primes[5];
gap> 15 in Primes;
gap> Position(Primes,31);
gap> Length(Primes);
gap> things:=["apple",true,,17];
gap> things[1];
gap> things[3];
gap> things[4]:=9;
gap> things[6]:=[1,2,3];
gap> things;
gap> newthings[1]:=72;
gap> newthings;
gap> newthings:=[];
gap> newthings;
gap> newthings[1]:=72;
gap> newthings;
gap> newthings:=things;
gap> newthings[1]:=23;
gap> newthings;
gap> things;
gap> things[1]:=14;
gap> things;
gap> newthings;
gap> newthings:=ShallowCopy(things);
gap> newthings[1]:=23;
gap> newthings;
gap> things;
gap> Add(things,16);
gap> things;
gap> Add(things,newthings);
gap> things;
gap> Append(things,newthings);
gap> things;
gap> Flat(things);
gap> ?Lists
gap> ?Flat
gap> r:=[1,2,3];
gap> s:=[3,1,4];
gap> r+s;
gap> 2*r;
gap> Sort(s);
gap> s;
gap> Append(r,s);
gap> r;
gap> List(r,x->x^2);
gap> List([1..10],IsPrime);
gap> [1..10];
gap> List([1..10],x->x);
gap> g:=Group((1,2,3),(1,2));
gap> els:=Elements(g);
gap> List(els,x->Order(x));
gap> els[3]:=14;
gap> els:=ShallowCopy(els);
gap> els[3]:=14;
gap> els;
More things you can do with lists can be found at the problems page. See also the permutation problem.
Programming
(You learn: do loops, how to initialize a list.)
gap> g:=Group((1,2,3),(1,2));
gap> els:=Elements(g);
DISCUSS: What would happen if you were to do the following? Don't try it unless you want to.
gap> for i in els do
> Order(i);
> od;
Instead, do this:
gap> orders:=[];
gap> for i in els do
> Add(orders,Order(i));
> od;
gap> orders;
The following is perhaps a less elegant way to program a list of
orders of the elements of g because the enumeration is done over
[1..Length(els)] instead of els itself. However, sometimes we have to
do this kind of inelegant thing.
gap> orders:=[];
[ ]
gap> for i in [1..Length(els)] do
> orders[i]:=Order(els[i]);
> od;
gap> orders;
[ 1, 2, 2, 3, 3, 2 ]
At this point we note that there are other forms of do loop,
namely:
while ... do ... od;
and
repeat... until...;
Note the following:
gap> for i in [1..10] do
> Print(i);
> od;
gap> for i in [1..10] do
> Print(i,"\n");
> od;
There is a small exercise concerning loops on the problem page. There is also an example
of
writing functions
Reading a file of code into GAP
Create a copy of the file conway.g in your GAP file directory. Its contents are:
Conway:=function(seq)
local i, r, newseq;
newseq:=[];
i:=1;
while i<=Length(seq) do
r:=0;
repeat r:=r+1;
until i+r>Length(seq) or not seq[i]=seq[i+r];
Append(newseq,[r,seq[i]]);
i:=i+r;
od;
return(newseq);
end;
Next, do:
gap> Read("conway.g");
gap> Print(Conway);
gap> Conway([2]);
gap> Conway([1,2]);
gap> Conway(last);
gap> Conway(last);
gap> Conway(last);
Acknowledgments:
This tutorial is based on material by
Peter Webb available from the
official GAP web site.