Introduction to GAP

Lecture 1

GAP Days Spring 2025,

April 7-11, 2025,
Vrije Universiteit Brussel, Belgium

Ilaria Colazzo

University of Leeds (UK)

What is GAP?

  • GAP stands for Groups, Algorithms, and Programming.
  • It is a system for computational discrete algebra.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Why Use GAP?

  • Open-source and freely available.
  • Symbolic computations, work with abstract algebraic objects
  • Extensive library of algebraic functions.
  • Databases of groups, character tables and much more.
  • Supports both interactive use and scripting.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Installing GAP

  • Installation is well-documented on the official website.
  • Some instructions also in the lecture notes.
  • Available for Windows, macOS, and Linux.
  • From now on, I'll assume you all have GAP on your computer up and running. 😄
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

What are we going to do?

  • Basic arithmetic (GAP as a fancy         
    calculator)
  • Basic programming
    • data types (int, lists, vectors, matrices)
    • loops (for, while)
    • conditional statement (if, if/else)
  • Basic group theory
    • Permutation groups
    • Matrix groups
                  
    • Cyclic groups
    • Dihedral groups ...
  • More group theory
    • Group actions
    • (Semi)Direct product of groups
    • Some application to "conjectures"
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Let's start

After running GAP we will see some information related to the distribution on our computer.

┌───────┐   GAP 4.13.1 of 2024-06-11
│  GAP  │   https://www.gap-system.org
└───────┘   Architecture: aarch64-apple-darwin23-default64-kv9
 Configuration:  gmp 6.3.0, GASMAN
 Loading the library and packages ...
 Packages:   AClib 1.3.2, Alnuth 3.2.1, AtlasRep 2.1.8, AutPGrp 1.11,
             CRISP 1.4.6, Cryst 4.1.27, CrystCat 1.1.10, CTblLib 1.3.9,
             FactInt 1.6.3, FGA 1.5.0, Forms 1.2.11, GAPDoc 1.6.7,
             genss 1.6.8, IO 4.8.2, IRREDSOL 1.4.4, LAGUNA 3.9.6, orb 4.9.0,
             Polenta 1.3.10, Polycyclic 2.16, PrimGrp 3.4.4, RadiRoot 2.9,
             recog 1.4.2, ResClasses 4.7.3, SmallGrp 1.5.3, Sophus 1.27,
             SpinSym 1.5.2, StandardFF 1.0, TomLib 1.2.11, TransGrp 3.6.5,
             utils 0.85
 Try '??help' for help. See also '?copyright', '?cite' and '?authors'
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

First session

  • We will see that GAP is ready:
gap>
  • To end a GAP session one uses quit:
gap> quit;
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Every command should end with the symbol ; (semicolon).

  • The symbol ;; (double semicolon) can also be used.

    • It means that no screen output will be produced.
    • Useful when we assign an object to a variable (see here).
gap> 2+5;;
gap> 2+5;
7
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Use ? to access documentation or tutorials related to specific functions or commands.
  • Here we have some examples:
gap> ? tutorial
gap> ? sets
gap> ? help
gap> ? permutations
gap> ? eigenvalues
gap> ? cyclicgroup
gap> ? freegroups
gap> ? sylowsubgroups
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • The online documentation is very useful!
    • The Tutorial (PDF) gives a first introduction to the system.
    • The Reference Manual (PDF) has complete descriptions of all library functions and examples of their use.
       

                          

GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • The function LogTo allows us to record everything on the terminal into a text file.
  • Everything we see on the terminal will also appear in the file.
gap> # Save the output to the file mylog
gap> LogTo("output.log");
gap> "From this point on everything will be recorded also in the file";
From this point on everything will be recorded also in the file
gap> LogTo();
gap> # Stop saving the output
gap> LogTo();
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • The content of the file output.log.
  • ⚠️ Careful! Typing again LogTo("output.log"); will erase the content of the file.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • GAP can handle standard arithmetic operations like addition, multiplication, exponentiation, and modular arithmetic.

  • The results of computations are displayed immediately in the GAP console.

gap> 2 + 3;
5
gap> 5 * 7;
35
gap> 9^2;
81
gap> 15 mod 4;
3
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Mathematical expressions follow standard operator precedence:
    exponentiation → multiplication and division → addition and subtraction.
  • Brackets can be used to override this precedence.
gap> 5 + 7 * 2;
19
gap> 5 + (7 * 2);
19
gap> (5 + 7) * 2;
24
  • Use ;; instead of ; to execute the computation without displaying the result.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Variables store objects.
  • Use := for assignment.
  • Variables can be used in expressions, calculations, and functions.
  • Variable assignments hold throughout the session unless explicitly overwritten.
gap> x := 10;
10
gap> y := x * 2;
20
gap> z := x + y;
30
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Variables are case-sensitive: v and V are different!

  • Unlike some programming languages, you don't need to declare the type of a variable.

gap> x := [1, 2, 3];
[ 1, 2, 3 ]
gap> x * 2;
[ 2, 4, 6 ]
gap> x := "Hello";
"Hello"
gap> x * 2; # This will cause an error since x is a string now!
Error, no method found!
  • ⚠️ Careful! Changing the type of a variable may lead to unexpected behaviour and can also lead to errors.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Keywords

  • Keywords are reserved words.
  • Cannot be used as names for variables.
  • Stored in GAPInfo.Keywords.
  • Let's use GAP to visualise the list of keywords.
gap> List(GAPInfo.Keywords);
[ "Assert", "Info", "IsBound", "QUIT", "TryNextMethod", "Unbind", "and",
  "atomic", "break", "continue", "do", "elif", "else", "end", "false", "fi",
  "for", "function", "if", "in", "local", "mod", "not", "od", "or", "quit",
  "readonly", "readwrite", "rec", "repeat", "return", "then", "true",
  "until", "while" ]
  • There are also other reserved words such as X, Z E,...
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Difference between = and :=

  • The expression := is used for assignment.
  • The expression = is used for comparison.
  • Using = for assignment will result in an error.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Comparison operators:

Operator Logical meaning
= Equal
<> Not equal
< (Strictly) less than
> (Strictly) greater than
<= Less or equal than
>= Greater or equal than
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
gap> a = 3;  # This will cause an error since a doesn't have a value!
Error, Variable: 'a' must have a value
gap> a := 5;
5
gap> b := 10;
10
gap> a = b;
false
gap> a <> b;
true
gap> a < b;
true
  • Comparisons return true or false values.
  • This result can be used in conditional statements and logical expressions, as we will see later.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Lists are ordered collections of elements.
  • Lists contain numbers, other lists, or even algebraic structures.
  • Lists don't need to contain the same type of data even though it is a good practice to avoid storing different data types in a list.
  • To create a list, use square brackets [ ]
L := [3, 1, 4, 1, 5];
  • To access an element of a list, we just use the name of the list and in square brackets the index.
  • ⚠️ Careful! Lists are indexed starting from 1!
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Elements in a list can be:
    • accessed using their index.
      L[3];accesses the third element of L.
    • modified by assigning a new value
      L[3] := 10; changes the value at position 3.
  • A list can be dynamically expanded or reduced:
    • Add(L, x); adds a new element.
    • Remove(L, index); removes an element at a specific index.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
gap> L;
[ 3, 1, 4, 1, 5 ]
gap> L[3] := 10;
gap> L;
[ 3, 1, 10, 1, 5 ]
gap> Add(L, 9);
gap> L;
[ 3, 1, 10, 1, 5, 9 ]
gap> Remove(L, 1);
3
gap> L;
[ 1, 10, 1, 5, 9 ]
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Sets are special lists without repetitions.
  • The command AddSet(S, x) adds the element x to S if it is not already there.
  • The command RemoveSet(S, x) deletes the element x from a set.
gap> S := Set([3, 1, 4, 1, 5]);
[ 1, 3, 4, 5 ]
gap> AddSet(S, 1); # 1 is already in S 
gap> S; # We expect that nothing has changed
[ 1, 3, 4, 5 ]
gap> AddSet(S, 2); # 2 was not in S
gap> S; # We expect that now S has 5 elements 
[ 1, 2, 3, 4, 5 ]
  • If L is a list Set(L) converts L into a set.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Vectors are lists of elements in some algebraic structure, e.g. a field, a ring.

  • Matrices are lists of row vectors.

  • If M is a matrix, the entries of M are accessed using M[row][column].

  • [Edit, thanks to M. Horn] Since GAP 4.9 you can also access a matrix entry via M[row,column] instead of M[row][column]. it may be more efficient for some kinds of matrices, but never worse that.

gap> vec := [-1, 2, 1];
[ -1, 2, 1 ]
gap> M := [[1,2,3],[4,5,6],[7,8,9]];
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
gap> M[3][2]; # Returns the element of M in position (3, 2)
8
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • We can also perform basic linear algebra operations like multiplying vectors and matrices or computing the inner product.
gap> vec*M; 
[ 14, 16, 18 ]
gap> M*vec;
[ 6, 12, 18 ]
gap> vec*vec;
6
  • Note that GAP automatically treats vec as a row vector in the multiplication vec*M but as a column vector in the multiplication M*vec.
  • Also both for vec*M and M*vec we obtain a row vector.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • To create a copy of a vector it is not sufficient to use the assignment operator :=.
gap> vec2 := vec;
[ -1, 2, 1 ]
gap> vec2[1] := 5;
5
gap> vec;
[ 5, 2, 1 ]
gap> vec2;
[ 5, 2, 1 ]
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • To make a copy that can be modified without changing the original, we use the function ShallowCopy(vec).
gap> vec2 := ShallowCopy(vec);
[ 5, 2, 1 ]
gap> vec2[1] := -1;
-1
gap> vec;
[ 5, 2, 1 ]
gap> vec2;
[ -1, 2, 1 ]
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • What about for matrices?
  • The trick of making a copy using ShallowCopy doesn't work somehow we need to go one layer deeper.
gap> P := ShallowCopy(M);
gap> P[1][1] := 10;
1
gap> Display(P);
[ [  10,  2,  3 ],
  [  4,  5,  6 ],
  [  7,  8,  9 ] ]
gap> Display(M);
[ [  10,  2,  3 ],
  [  4,  5,  6 ],
  [  7,  8,  9 ] ]
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • To make a good duplicate of a matrix, we can use the function MutableCopyMat(M).
gap> L := MutableCopyMat(M);;
gap> L[1][1] := 100;
100
gap> Display(M);
[ [  1,  2,  3 ],
  [  4,  5,  6 ],
  [  7,  8,  9 ] ]
gap> Display(L);
[ [  100,    2,    3 ],
  [    4,    5,    6 ],
  [    7,    8,    9 ] ]    
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

GAP as a fancy calculator

  • There are many functions already implemented in GAP that allow us to use it as a fancy calculator such as:
    • Gcd,
    • PrimeDivisors,
    • DivisorsInt,
    • Maximum,
    • Minimum,
    • IsPrime.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Greatest Common Divisor

Given two integers n and m we can use the function Gcd(n,m) to compute their greatest common divisor.

gap> Gcd(2025,5);
5
  • The same function can be used to compute the greatest common divisor between two polynomials over a field.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

(Prime) divisors

  • The function PrimeDivisors returns the list of prime divisors of a given integer.
  • The function DivisorsInt returns the list of all positive divisors.
gap> PrimeDivisors(60);
[ 2, 3, 5 ]
gap> DivisorsInt(60);
[ 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60 ]
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Maximum and Minimum

  • We can obtain the maximum (resp. the minimum) using the functions Maximum and MaximumList (resp. Minimum and MinimumList).
gap> Maximum(6, 7);
7
gap> MaximumList([ 6, 7, 35 ]);
35
gap> Minimum(6, 7);
6
gap> MinimumList([ 6, 7, 35 ]);
6
gap> Minimum(6, 7, 8);
6
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Is it a prime number?

  • The function IsPrime allows one to check if a given integer is a prime number.
gap> IsPrime(25);
false
gap> IsPrime(5);
true
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Checking Types

  • We can check the type of a given variable using functions like IsInt, IsList, IsMatrix, IsSet, etc.
gap> IsInt(4);
true
gap> IsInt([ 4 ]);
false
gap> IsList([ 1, 2, 2, 4 ]);
true
gap> IsSet([ 1, 2, 2, 4 ]);
false
gap> IsList([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]);
true
gap> IsMatrix([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]);
true
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • We want to print the square of the first five integers.
gap> 1^2;
1
gap> 2^2;
4
gap> 3^2;
9
gap> 4^2;
16
gap> 5^2;
25    
  • This process is quite tedious and error-prone.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

A for loop iterates over a specified range of values.

gap> for i in [1..5] do 
> j := i^2;
> Print(j, "\n");
> od;
1
4
9
16
25
  • A range is a way to create sequences of numbers.
    • [1..5][1, 2, 3, 4, 5].
  • We use the Print function to display the result.
  • We conclude the loop with the keyword od.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • A while loop runs until a condition becomes false.
  • Let's display the first 5 numbers using a while statement.
gap> i := 1;
1
gap> while i <= 5 do
> Print(i, " ");
> i := i + 1;
> od;
1 2 3 4 5
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Conditional statements if, else, elif allow us to make decisions in our code.

  • For example, we can check if a number is even or odd by using the modulo operator.

gap> n := 10;;
gap> if n mod 2 = 0 then
> Print("Even");
Even
> else
> Print("Odd");
> fi;
  • fi tells GAP we finished with the body of the if-statement.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • We can also extend this logic using elif conditions to check for multiple cases, such as whether a number is positive, negative, or zero.
gap> n := -5;;
gap> if n > 0 then
> Print("Positive");
> elif n < 0 then
> Print("Negative");
Negative
> else
> Print("Zero");
> fi;

Question: What happens if n is not a number?

GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • We would like to reuse the block of codes we just wrote. Do we need to retype everything?
  • This is not ideal: hard to check, and we could insert errors in the process.
  • The solution: Function.
gap> square := function(n)
> return n^2;
> end;
function( n ) ... end
gap> square(2);
4
gap> square(25);
625
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • We can use loops and conditionals to obtain more complex functions.
  • Let us write a function that computes the factorial of a given positive integer.
gap> factorial := function(n)
> local result, i;
> result := 1;
> for i in [1..n] do
> result := result * i;
> od;
> return result;
> end;
function( n ) ... end
gap> factorial(5);
120
  • We use local to define local variables.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Functions can be used for recursion, where a function calls itself.
gap> factorial2 := function(n) 
> if n = 0 then return 1; 
> else return n * factorial2(n-1); 
> fi; 
> end;
function( n ) ... end
gap> factorial2(3);
6
gap> factorial2(5);
120
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • ⚠️ Caution! A missing or incorrect base case can lead to infinite recursion!
gap> BadRecursion := function(n)
>     return n + BadRecursion(n - 1);  # No base case!
> end;
function( n ) ... end
gap> BadRecursion(5);
Error, recursion depth trap (5000) in
  BadRecursion( n - 1 ) at *stdin*:2 called from
BadRecursion( n - 1 ) at *stdin*:2 called from
BadRecursion( n - 1 ) at *stdin*:2 called from
BadRecursion( n - 1 ) at *stdin*:2 called from
BadRecursion( n - 1 ) at *stdin*:2 called from
BadRecursion( n - 1 ) at *stdin*:2 called from
...  at *stdin*:4
you may 'return;'
brk>
  • After brk> we use quit; to go back to the GAP console.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Implicit functions (anonymous functions) are used to define a function with giving it a name.
    • in GAP use -> notation.
  • Useful for working with lists. We can use an implicit function for sorting, filtering, and mapping lists.

Let us see an example of an implicit function that returns the square of a given number.

gap> f := x -> x^2;
function( x ) ... end
gap> Print(f(5));
25
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Filtering a list

  • We combine the function Filtered and an implicit function.
  • Filtered is a built-in GAP function that returns the elements of a list that satisfy a given condition.
gap> L := [1, 2, 3, 4, 5, 6];
[ 1, 2, 3, 4, 5, 6 ]
gap> evens := Filtered(L, x -> x mod 2 = 0);
[ 2, 4, 6 ]
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Apply a function to all elements of a list

  • We combine the function List and an implicit function.
gap> L := [1, 2, 3, 4, 5, 6];
[ 1, 2, 3, 4, 5, 6 ]
gap> squares := List(L, x-> x^2);
[ 1, 4, 9, 16, 25, 36 ]
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Sort a list

  • We combine the function SortBy and an implicit function.
gap> L := [1, 2, 3, 4, 5, 6];
[ 1, 2, 3, 4, 5, 6 ]
gap> SortBy(L, x -> x mod 3);
gap> L;
[ 3, 6, 1, 4, 2, 5 ]
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • If we want to sort a list, say in decreasing order, we might be tempted to try
gap> L := [1, 2, 3, 4, 5, 6];
gap> Sort(L, x, y -> x > y);
Error, Variable: 'x' must have a value
  • We obtain an Error message.
  • This, because GAP only allows single-variable implicit functions.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • We can still use an explicit function with a similar syntax.
gap> L := [1, 2, 3, 4, 5, 6];
gap>  Sort(L, function(x, y) return x > y; end);
gap> L;
[ 6, 5, 4, 3, 2, 1 ]
  • We used Sort and not SortBy.
  • Sort compares elements directly using a function,
  • SortBy sorts based on a function applied to each element.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • A permutation in letters is a bijective map .

  • For example, the permutation is the bijective map such that , , , .

  • We write a permutation as a product of disjoint cycles.

  • For example: or .

  • To write a permutation in GAP we use brackets and we separate the element with a comma ,.

  • Then in GAP is (1,2)(3,4,5).

GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |
  • Caution! Composition of permutations in GAP is performed from left to right.
  • For example as the following code shows:
gap> (1 ,2 ,3) * (2 ,3 ,4);
(1,3)(2,4)
  • Write a permutation as product of transpositions . The sign of is .

  • Recall that every cycle in is a product of transpositions:

    • the identity is .
    • a -cycle with can be written as .
  • It's then clear that any permutation can be written as a product of transpositions.

GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

📌 Key Takeaways 📌

  • End with ; (or ;;), use quit; to exit, ? for help.
  • Variables: Use := for assignment (different from =).
  • Lists/Vectors: [ ], index from , Add, Remove.
  • Sets: Set(list), no duplicates, AddSet.
  • Matrices: Lists of lists, access with [i][j].
  • Functions: Define with function(...) ... end;, can be recursive.
  • Control Structures: if/elif/else, for, while loops.
  • Copying for vectors/matrices can be tricky, use ShallowCopy()/MutableCopyMat().
  • Logging: LogTo("file") to record session.
GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |

Thanks!

GAP Days Spring 2025 | Ilaria Colazzo | Introduction to GAP |