Some more GAP objects
Overview
Teaching: 15 min
Exercises: 5 minQuestions
Further examples of immediate and positional objects and operations with them
Objectives
See examples of types that are built-in in GAP but may be missing in other systems
See examples of list arithmetic
So far we have met three types of GAP types:
-
immediate objects such as integers, rationals, booleans, permutations,
-
positional objects such as lists,
-
component objects with more complex internal representation, such as groups.
In this section, we will demonstrate some other examples of basic objects existing in GAP (the system is extendable, so one can introduce new types of objects, but this is beyond the scope of this lesson!).
Some other immediate objects are floats and finite field elements:
1.15; Float(1232/3456567);
1.15
0.000356423
The elements of a finite field GF(q)
with q
elements
are denoted in terms of Z(q)
which is a generator of its multiplicative group.
AsList(GF(2)); Z(5); Z(5)^4;
[ 0*Z(2), Z(2)^0 ]
Z(5)
Z(5)^0
The next type of composite objects are records. While a list contains subobjects indexed
by their positions in the list, a record contains subobjects, called record
components, which are indexed by their names. Elements of a record are accessed with .
date:= rec(year:= 2015, month:= "Nov", day:= 17);
rec( day := 17, month := "Nov", year := 2015 )
date.year;
2015
date.time:= rec(hour:= 14, minute:= 55, second:= 12);
rec( hour := 14, minute := 55, second := 12 )
date;
rec( day := 17, month := "Nov",
time := rec( hour := 14, minute := 55, second := 12 ), year := 2015 )
RecNames(date);
[ "time", "year", "month", "day" ]
Creating a record
Create a record
weather
having several fields that can be used to describe the current weather.Solution
weather:=rec();
weather.temperature:=4;
weather.humidity:=0.4;
weather.description:="partially cloudy";
weather.precipitation:=0.5;
Next, there are strings and characters. While strings are printed specially by GAP, a string is really just a list of characters, and any function which takes a list will also take a string.
gap> w:="supercalifragilisticexpialidocious"; Length(w);
"supercalifragilisticexpialidocious"
34
Strings are denoted by double quotes, and characters by single ones.
gap> "s" in w; 's' in w; IsSubset(w,"s"); IsSubset(w,['s','f']); ['c','a','t'] = "cat"
false
true
true
true
true
Note that
gap> PositionSublist(w,"sf"); PositionSublist(w,"fr");
fail
10
Be careful that some operations may create a new list, and some may be destructive, for example:
gap> SortedList(w); w;
"aaacccdeefgiiiiiiillloopprrssstuux"
"supercalifragilisticexpialidocious"
but
gap> Sort(w); w;
"aaacccdeefgiiiiiiillloopprrssstuux"
Which letter is occurring in “supercalifragilisticexpialidocious” most often?
gap> c := Collected(w);
[ [ 'a', 3 ], [ 'c', 3 ], [ 'd', 1 ], [ 'e', 2 ], [ 'f', 1 ], [ 'g', 1 ],
[ 'i', 7 ], [ 'l', 3 ], [ 'o', 2 ], [ 'p', 2 ], [ 'r', 2 ], [ 's', 3 ],
[ 't', 1 ], [ 'u', 2 ], [ 'x', 1 ] ]
gap> k := Maximum( List( c, v -> v[2] ) ); Filtered( c, v -> v[2] = 7 );
7
[ [ 'i', 7 ] ]
How to find the most occurring letter(s) using one pass over the list
c
?The command
k := Maximum( List( c, v -> v[2] ) ); Filtered( c, v -> v[2] = 7 );
iterates over the list
c
two times (inList
and inFiltered
), and it also iterates over another list of the same length asc
in the call toMaximum
. If the list is long, this will impose certain performance and memory penalties. Try to find the letter(s) occuring most often using one pass over the listc
without producing an intermediate list. You might find it hard to do this using list comprehension techniques such asFiltered
orList
. Instead, you could use a suitable loop and use?if
to make yourself familiar with the usage of conditional statements in GAP.Solution
maxletters:=[]; maxvalue:=0; for w in c do if w[2]=maxvalue then Add(maxletters, w[1]); else if w[2]>maxvalue then maxletters:=[w[1]]; maxvalue:=w[2]; fi; fi; od;
Key Points
GAP has a plethora of various immediate, positional and component objects.
List arithmetic is very flexible and powerful.
Objects like lists and records are good to keep structured and related data.