CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / doc / tut / chap3.txt
Views: 418346
1
2
3 Lists and Records
3
4
Modern mathematics, especially algebra, is based on set theory. When sets
5
are represented in a computer, they inadvertently turn into lists. That's
6
why we start our survey of the various objects GAP can handle with a
7
description of lists and their manipulation. GAP regards sets as a special
8
kind of lists, namely as lists without holes or duplicates whose entries are
9
ordered with respect to the precedence relation <.
10
11
After the introduction of the basic manipulations with lists in 3.1, some
12
difficulties concerning identity and mutability of lists are discussed
13
in 3.2 and 3.3. Sets, ranges, row vectors, and matrices are introduced as
14
special kinds of lists in 3.4, 3.5, 3.8. Handy list operations are shown
15
in 3.7. Finally we explain how to use records in 3.9.
16
17
18
3.1 Plain Lists
19
20
A list is a collection of objects separated by commas and enclosed in
21
brackets. Let us for example construct the list primes of the first ten
22
prime numbers.
23
24
 Example 
25
gap> primes:= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
26
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]
27

28
29
The next two primes are 31 and 37. They may be appended to the existing list
30
by the function Append which takes the existing list as its first and
31
another list as a second argument. The second argument is appended to the
32
list primes and no value is returned. Note that by appending another list
33
the object primes is changed.
34
35
 Example 
36
gap> Append(primes, [31, 37]);
37
gap> primes;
38
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 ]
39

40
41
You can as well add single new elements to existing lists by the function
42
Add which takes the existing list as its first argument and a new element as
43
its second argument. The new element is added to the list primes and again
44
no value is returned but the list primes is changed.
45
46
 Example 
47
gap> Add(primes, 41);
48
gap> primes;
49
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 ]
50

51
52
Single elements of a list are referred to by their position in the list. To
53
get the value of the seventh prime, that is the seventh entry in our list
54
primes, you simply type
55
56
 Example 
57
gap> primes[7];
58
17
59

60
61
This value can be handled like any other value, for example multiplied by 2
62
or assigned to a variable. On the other hand this mechanism allows one to
63
assign a value to a position in a list. So the next prime 43 may be inserted
64
in the list directly after the last occupied position of primes. This last
65
occupied position is returned by the function Length.
66
67
 Example 
68
gap> Length(primes);
69
13
70
gap> primes[14]:= 43;
71
43
72
gap> primes;
73
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 ]
74

75
76
Note that this operation again has changed the object primes. The next
77
position after the end of a list is not the only position capable of taking
78
a new value. If you know that 71 is the 20th prime, you can enter it right
79
now in the 20th position of primes. This will result in a list with holes
80
which is however still a list and now has length 20.
81
82
 Example 
83
gap> primes[20]:= 71;
84
71
85
gap> primes;
86
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ]
87
gap> Length(primes);
88
20
89

90
91
The list itself however must exist before a value can be assigned to a
92
position of the list. This list may be the empty list [ ].
93
94
 Example 
95
gap> lll[1]:= 2;
96
Error, Variable: 'lll' must have a value
97

98

99
gap> lll:= []; lll[1]:= 2;
100
[ ]
101
2
102

103
104
Of course existing entries of a list can be changed by this mechanism, too.
105
We will not do it here because primes then may no longer be a list of
106
primes. Try for yourself to change the 17 in the list into a 9.
107
108
To get the position of 17 in the list primes use the function Position
109
(Reference: Position) which takes the list as its first argument and the
110
element as its second argument and returns the position of the first
111
occurrence of the element 17 in the list primes. If the element is not
112
contained in the list then Position (Reference: Position) will return the
113
special object fail.
114
115
 Example 
116
gap> Position(primes, 17);
117
7
118
gap> Position(primes, 20);
119
fail
120

121
122
In all of the above changes to the list primes, the list has been
123
automatically resized. There is no need for you to tell GAP how big you want
124
a list to be. This is all done dynamically.
125
126
It is not necessary for the objects collected in a list to be of the same
127
type.
128
129
 Example 
130
gap> lll:= [true, "This is a String",,, 3];
131
[ true, "This is a String",,, 3 ]
132

133
134
In the same way a list may be part of another list.
135
136
 Example 
137
gap> lll[3]:= [4,5,6];; lll;
138
[ true, "This is a String", [ 4, 5, 6 ],, 3 ]
139

140
141
A list may even be part of itself.
142
143
 Example 
144
gap> lll[4]:= lll;
145
[ true, "This is a String", [ 4, 5, 6 ], ~, 3 ]
146

147
148
Now the tilde in the fourth position of lll denotes the object that is
149
currently printed. Note that the result of the last operation is the actual
150
value of the object lll on the right hand side of the assignment. In fact it
151
is identical to the value of the whole list lll on the left hand side of the
152
assignment.
153
154
A string is a special type of list, namely a dense list of characters, where
155
dense means that the list has no holes. Here, characters are special GAP
156
objects representing an element of the character set of the operating
157
system. The input of printable characters is by enclosing them in single
158
quotes '. A string literal can either be entered as the list of characters
159
or by writing the characters between doublequotes ". Strings are handled
160
specially by Print (Reference: Print). You can learn much more about strings
161
in the reference manual.
162
163
 Example 
164
gap> s1 := ['H','a','l','l','o',' ','w','o','r','l','d','.'];
165
"Hallo world."
166
gap> s1 = "Hallo world.";
167
true
168
gap> s1[7];
169
'w'
170

171
172
Sublists of lists can easily be extracted and assigned using the operator
173
list{ positions }.
174
175
 Example 
176
gap> sl := lll{ [ 1, 2, 3 ] };
177
[ true, "This is a String", [ 4, 5, 6 ] ]
178
gap> sl{ [ 2, 3 ] } := [ "New String", false ];
179
[ "New String", false ]
180
gap> sl;
181
[ true, "New String", false ]
182

183
184
This way you get a new list whose i-th entry is that element of the original
185
list whose position is the i-th entry of the argument in the curly braces.
186
187
188
3.2 Identical Lists
189
190
This second section about lists is dedicated to the subtle difference
191
between equality and identity of lists. It is really important to understand
192
this difference in order to understand how complex data structures are
193
realized in GAP. This section applies to all GAP objects that have
194
subobjects, e.g., to lists and to records. After reading the section 3.9
195
about records you should return to this section and translate it into the
196
record context.
197
198
Two lists are equal if all their entries are equal. This means that the
199
equality operator = returns true for the comparison of two lists if and only
200
if these two lists are of the same length and for each position the values
201
in the respective lists are equal.
202
203
 Example 
204
gap> numbers := primes;; numbers = primes;
205
true
206

207
208
We assigned the list primes to the variable numbers and, of course they are
209
equal as they have both the same length and the same entries. Now we will
210
change the third number to 4 and compare the result again with primes.
211
212
 Example 
213
gap> numbers[3]:= 4;; numbers = primes;
214
true
215

216
217
You see that numbers and primes are still equal, check this by printing the
218
value of primes. The list primes is no longer a list of primes! What has
219
happened? The truth is that the lists primes and numbers are not only equal
220
but they are also identical. primes and numbers are two variables pointing
221
to the same list. If you change the value of the subobject numbers[3] of
222
numbers this will also change primes. Variables do not point to a certain
223
block of storage memory but they do point to an object that occupies storage
224
memory. So the assignment numbers := primes did not create a new list in a
225
different place of memory but only created the new name numbers for the same
226
old list of primes.
227
228
From this we see that the same object can have several names.
229
230
If you want to change a list with the contents of primes independently from
231
primes you will have to make a copy of primes by the function ShallowCopy
232
which takes an object as its argument and returns a copy of the argument.
233
(We will first restore the old value of primes.)
234
235
 Example 
236
gap> primes[3]:= 5;; primes;
237
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ]
238
gap> numbers:= ShallowCopy(primes);; numbers = primes;
239
true
240
gap> numbers[3]:= 4;; numbers = primes;
241
false
242

243
244
Now numbers is no longer equal to primes and primes still is a list of
245
primes. Check this by printing the values of numbers and primes.
246
247
Lists and records can be changed this way because GAP objects of these types
248
have subobjects. To clarify this statement consider the following
249
assignments.
250
251
 Example 
252
gap> i:= 1;; j:= i;; i:= i+1;; 
253

254
255
By adding 1 to i the value of i has changed. What happens to j? After the
256
second statement j points to the same object as i, namely to the integer 1.
257
The addition does not change the object 1 but creates a new object according
258
to the instruction i+1. It is actually the assignment that changes the value
259
of i. Therefore j still points to the object 1. Integers (like permutations
260
and booleans) have no subobjects. Objects of these types cannot be changed
261
but can only be replaced by other objects. And a replacement does not change
262
the values of other variables. In the above example an assignment of a new
263
value to the variable numbers would also not change the value of primes.
264
265
Finally try the following examples and explain the results.
266
267
 Example 
268
gap> l:= [];; l:= [l];
269
[ [ ] ]
270
gap> l[1]:= l;
271
[ ~ ]
272

273
274
Now return to Section 3.1 and find out whether the functions Add (Reference:
275
Add) and Append (Reference: Append) change their arguments.
276
277
278
3.3 Immutability
279
280
GAP has a mechanism that protects lists against changes like the ones that
281
have bothered us in Section 3.2. The function Immutable (Reference:
282
Immutable) takes as argument a list and returns an immutable copy of it,
283
i.e., a list which looks exactly like the old one, but has two extra
284
properties: (1) The new list is immutable, i.e., the list itself and its
285
subobjects cannot be changed. (2) In constructing the copy, every part of
286
the list that can be changed has been copied, so that changes to the old
287
list will not affect the new one. In other words, the new list has no
288
mutable subobjects in common with the old list.
289
290
 Example 
291
gap> list := [ 1, 2, "three", [ 4 ] ];; copy := Immutable( list );;
292
gap> list[3][5] := 'w';; list; copy;
293
[ 1, 2, "threw", [ 4 ] ]
294
[ 1, 2, "three", [ 4 ] ]
295
gap> copy[3][5] := 'w';
296
Lists Assignment: <list> must be a mutable list
297
not in any function
298
Entering break read-eval-print loop ...
299
you can 'quit;' to quit to outer loop, or
300
you can 'return;' and ignore the assignment to continue
301
brk> quit;
302

303
304
As a consequence of these rules, in the immutable copy of a list which
305
contains an already immutable list as subobject, this immutable subobject
306
need not be copied, because it is unchangeable. Immutable lists are useful
307
in many complex GAP objects, for example as generator lists of groups. By
308
making them immutable, GAP ensures that no generators can be added to the
309
list, removed or exchanged. Such changes would of course lead to serious
310
inconsistencies with other knowledge that may already have been calculated
311
for the group.
312
313
A converse function to Immutable (Reference: Immutable) is ShallowCopy
314
(Reference: ShallowCopy), which produces a new mutable list whose i-th entry
315
is the i-th entry of the old list. The single entries are not copied, they
316
are just placed in the new list. If the old list is immutable, and hence the
317
list entries are immutable themselves, the result of ShallowCopy (Reference:
318
ShallowCopy) is mutable only on the top level.
319
320
It should be noted that also other objects than lists can appear in mutable
321
or immutable form. Records (see Section 3.9) provide another example.
322
323
324
3.4 Sets
325
326
GAP knows several special kinds of lists. A set in GAP is a list that
327
contains no holes (such a list is called dense) and whose elements are
328
strictly sorted w.r.t. <; in particular, a set cannot contain duplicates.
329
(More precisely, the elements of a set in GAP are required to lie in the
330
same family, but roughly this means that they can be compared using the <
331
operator.)
332
333
This provides a natural model for mathematical sets whose elements are given
334
by an explicit enumeration.
335
336
GAP also calls a set a strictly sorted list, and the function IsSSortedList
337
(Reference: IsSSortedList) tests whether a given list is a set. It returns a
338
boolean value. For almost any list whose elements are contained in the same
339
family, there exists a corresponding set. This set is constructed by the
340
function Set (Reference: Set) which takes the list as its argument and
341
returns a set obtained from this list by ignoring holes and duplicates and
342
by sorting the elements.
343
344
The elements of the sets used in the examples of this section are strings.
345
346
 Example 
347
gap> fruits:= ["apple", "strawberry", "cherry", "plum"];
348
[ "apple", "strawberry", "cherry", "plum" ]
349
gap> IsSSortedList(fruits);
350
false
351
gap> fruits:= Set(fruits);
352
[ "apple", "cherry", "plum", "strawberry" ]
353

354
355
Note that the original list fruits is not changed by the function Set
356
(Reference: Set). We have to make a new assignment to the variable fruits in
357
order to make it a set.
358
359
The operator in is used to test whether an object is an element of a set. It
360
returns a boolean value true or false.
361
362
 Example 
363
gap> "apple" in fruits;
364
true
365
gap> "banana" in fruits;
366
false
367

368
369
The operator in can also be applied to ordinary lists. It is however much
370
faster to perform a membership test for sets since sets are always sorted
371
and a binary search can be used instead of a linear search. New elements may
372
be added to a set by the function AddSet (Reference: AddSet) which takes the
373
set fruits as its first argument and an element as its second argument and
374
adds the element to the set if it wasn't already there. Note that the object
375
fruits is changed.
376
377
 Example 
378
gap> AddSet(fruits, "banana");
379
gap> fruits; # The banana is inserted in the right place.
380
[ "apple", "banana", "cherry", "plum", "strawberry" ]
381
gap> AddSet(fruits, "apple");
382
gap> fruits; # fruits has not changed.
383
[ "apple", "banana", "cherry", "plum", "strawberry" ]
384

385
386
Note that inserting new elements into a set with AddSet (Reference: AddSet)
387
is usually more expensive than simply adding new elements at the end of a
388
list.
389
390
Sets can be intersected by the function Intersection (Reference:
391
Intersection) and united by the function Union (Reference: Union) which both
392
take two sets as their arguments and return the intersection resp. union of
393
the two sets as a new object.
394
395
 Example 
396
gap> breakfast:= ["tea", "apple", "egg"];
397
[ "tea", "apple", "egg" ]
398
gap> Intersection(breakfast, fruits);
399
[ "apple" ]
400

401
402
The arguments of the functions Intersection (Reference: Intersection) and
403
Union (Reference: Union) could be ordinary lists, while their result is
404
always a set. Note that in the preceding example at least one argument of
405
Intersection (Reference: Intersection) was not a set. The functions
406
IntersectSet (Reference: IntersectSet) and UniteSet (Reference: UniteSet)
407
also form the intersection resp. union of two sets. They will however not
408
return the result but change their first argument to be the result. Try them
409
carefully.
410
411
412
3.5 Ranges
413
414
A range is a finite arithmetic progression of integers. This is another
415
special kind of list. A range is described by the first two values and the
416
last value of the arithmetic progression which are given in the form
417
[first,second..last]. In the usual case of an ascending list of consecutive
418
integers the second entry may be omitted.
419
420
 Example 
421
gap> [1..999999]; # a range of almost a million numbers
422
[ 1 .. 999999 ]
423
gap> [1, 2..999999]; # this is equivalent
424
[ 1 .. 999999 ]
425
gap> [1, 3..999999]; # here the step is 2
426
[ 1, 3 .. 999999 ]
427
gap> Length( last );
428
500000
429
gap> [ 999999, 999997 .. 1 ];
430
[ 999999, 999997 .. 1 ]
431

432
433
This compact printed representation of a fairly long list corresponds to a
434
compact internal representation. The function IsRange (Reference: IsRange)
435
tests whether an object is a range, the function ConvertToRangeRep
436
(Reference: ConvertToRangeRep) changes the representation of a list that is
437
in fact a range to this compact internal representation.
438
439
 Example 
440
gap> a:= [-2,-1,0,1,2,3,4,5];
441
[ -2, -1, 0, 1, 2, 3, 4, 5 ]
442
gap> IsRange( a );
443
true
444
gap> ConvertToRangeRep( a );; a;
445
[ -2 .. 5 ]
446
gap> a[1]:= 0;; IsRange( a );
447
false
448

449
450
Note that this change of representation does not change the value of the
451
list a. The list a still behaves in any context in the same way as it would
452
have in the long representation.
453
454
455
3.6 For and While Loops
456
457
Given a list pp of permutations we can form their product by means of a for
458
loop instead of writing down the product explicitly.
459
460
 Example 
461
gap> pp:= [ (1,3,2,6,8)(4,5,9), (1,6)(2,7,8), (1,5,7)(2,3,8,6),
462
>  (1,8,9)(2,3,5,6,4), (1,9,8,6,3,4,7,2)];;
463
gap> prod:= (); 
464
()
465
gap> for p in pp do
466
>  prod:= prod*p; 
467
>  od;
468
gap> prod; 
469
(1,8,4,2,3,6,5,9)
470

471
472
First a new variable prod is initialized to the identity permutation ().
473
Then the loop variable p takes as its value one permutation after the other
474
from the list pp and is multiplied with the present value of prod resulting
475
in a new value which is then assigned to prod.
476
477
The for loop has the following syntax
478
479
for var in list do statements od;
480
481
The effect of the for loop is to execute the statements for every element of
482
the list. A for loop is a statement and therefore terminated by a semicolon.
483
The list of statements is enclosed by the keywords do and od (reverse do). A
484
for loop returns no value. Therefore we had to ask explicitly for the value
485
of prod in the preceding example.
486
487
The for loop can loop over any kind of list, even a list with holes. In many
488
programming languages the for loop has the form
489
490
for var from first to last do statements od;
491
492
In GAP this is merely a special case of the general for loop as defined
493
above where the list in the loop body is a range (see 3.5):
494
495
for var in [first..last] do statements od;
496
497
You can for instance loop over a range to compute the factorial 15! of the
498
number 15 in the following way.
499
500
 Example 
501
gap> ff:= 1;
502
1
503
gap> for i in [1..15] do
504
>  ff:= ff * i;
505
>  od;
506
gap> ff;
507
1307674368000
508

509
510
The while loop has the following syntax
511
512
while condition do statements od;
513
514
The while loop loops over the statements as long as the condition evaluates
515
to true. Like the for loop the while loop is terminated by the keyword od
516
followed by a semicolon.
517
518
We can use our list primes to perform a very simple factorization. We begin
519
by initializing a list factors to the empty list. In this list we want to
520
collect the prime factors of the number 1333. Remember that a list has to
521
exist before any values can be assigned to positions of the list. Then we
522
will loop over the list primes and test for each prime whether it divides
523
the number. If it does we will divide the number by that prime, add it to
524
the list factors and continue.
525
526
 Example 
527
gap> n:= 1333;;
528
gap> factors:= [];;
529
gap> for p in primes do
530
>  while n mod p = 0 do
531
>  n:= n/p;
532
>  Add(factors, p);
533
>  od;
534
>  od;
535
gap> factors;
536
[ 31, 43 ]
537
gap> n;
538
1
539

540
541
As n now has the value 1 all prime factors of 1333 have been found and
542
factors contains a complete factorization of 1333. This can of course be
543
verified by multiplying 31 and 43.
544
545
This loop may be applied to arbitrary numbers in order to find prime
546
factors. But as primes is not a complete list of all primes this loop may
547
fail to find all prime factors of a number greater than 2000, say. You can
548
try to improve it in such a way that new primes are added to the list primes
549
if needed.
550
551
You have already seen that list objects may be changed. This of course also
552
holds for the list in a loop body. In most cases you have to be careful not
553
to change this list, but there are situations where this is quite useful.
554
The following example shows a quick way to determine the primes smaller than
555
1000 by a sieve method. Here we will make use of the function Unbind to
556
delete entries from a list, and the if statement covered in 4.2.
557
558
 Example 
559
gap> primes:= [];;
560
gap> numbers:= [2..1000];;
561
gap> for p in numbers do
562
>  Add(primes, p);
563
>  for n in numbers do
564
>  if n mod p = 0 then
565
>  Unbind(numbers[n-1]);
566
>  fi;
567
>  od;
568
>  od;
569

570
571
The inner loop removes all entries from numbers that are divisible by the
572
last detected prime p. This is done by the function Unbind which deletes the
573
binding of the list position numbers[n-1] to the value n so that afterwards
574
numbers[n-1] no longer has an assigned value. The next element encountered
575
in numbers by the outer loop necessarily is the next prime.
576
577
In a similar way it is possible to enlarge the list which is looped over.
578
This yields a nice and short orbit algorithm for the action of a group, for
579
example.
580
581
More about for and while loops can be found in the sections 'Reference:
582
While' and 'Reference: For'.
583
584
585
3.7 List Operations
586
587
There is a more comfortable way than that given in the previous section to
588
compute the product of a list of numbers or permutations.
589
590
 Example 
591
gap> Product([1..15]);
592
1307674368000
593
gap> Product(pp);
594
(1,8,4,2,3,6,5,9)
595

596
597
The function Product (Reference: Product) takes a list as its argument and
598
computes the product of the elements of the list. This is possible whenever
599
a multiplication of the elements of the list is defined. So Product
600
(Reference: Product) executes a loop over all elements of the list.
601
602
There are other often used loops available as functions. Guess what the
603
function Sum (Reference: Sum) does. The function List (Reference: Lists) may
604
take a list and a function as its arguments. It will then apply the function
605
to each element of the list and return the corresponding list of results. A
606
list of cubes is produced as follows with the function cubed from Section 4.
607
608
 Example 
609
gap> cubed:= x -> x^3;;
610
gap> List([2..10], cubed);
611
[ 8, 27, 64, 125, 216, 343, 512, 729, 1000 ]
612

613
614
To add all these cubes we might apply the function Sum (Reference: Sum) to
615
the last list. But we may as well give the function cubed to Sum (Reference:
616
Sum) as an additional argument.
617
618
 Example 
619
gap> Sum(last) = Sum([2..10], cubed);
620
true
621

622
623
The primes less than 30 can be retrieved out of the list primes from
624
Section 3.1 by the function Filtered (Reference: Filtered). This function
625
takes the list primes and a property as its arguments and will return the
626
list of those elements of primes which have this property. Such a property
627
will be represented by a function that returns a boolean value. In this
628
example the property of being less than 30 can be represented by the
629
function x -> x < 30 since x < 30 will evaluate to true for values x less
630
than 30 and to false otherwise.
631
632
 Example 
633
gap> Filtered(primes, x -> x < 30);
634
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]
635

636
637
We have already mentioned the operator { } that forms sublists. It takes a
638
list of positions as its argument and will return the list of elements from
639
the original list corresponding to these positions.
640
641
 Example 
642
gap> primes{ [1 .. 10] };
643
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]
644

645
646
Finally we mention the function ForAll (Reference: ForAll) that checks
647
whether a property holds for all elements of a list. It takes as its
648
arguments a list and a function that returns a boolean value. ForAll
649
(Reference: ForAll) checks whether the function returns true for all
650
elements of the list.
651
652
 Example 
653
gap> list:= [ 1, 2, 3, 4 ];;
654
gap> ForAll( list, x -> x > 0 );
655
true
656
gap> ForAll( list, x -> x in primes );
657
false
658

659
660
You will find more predefined for loops in chapter 'Reference: Lists'.
661
662
663
3.8 Vectors and Matrices
664
665
This section describes how GAP uses lists to represent row vectors and
666
matrices. A row vector is a dense list of elements from a common field. A
667
matrix is a dense list of row vectors over a common field and of equal
668
length.
669
670
 Example 
671
gap> v:= [3, 6, 2, 5/2];; IsRowVector(v);
672
true
673

674
675
Row vectors may be added and multiplied by scalars from their field.
676
Multiplication of row vectors of equal length results in their scalar
677
product.
678
679
 Example 
680
gap> 2 * v; v * 1/3;
681
[ 6, 12, 4, 5 ]
682
[ 1, 2, 2/3, 5/6 ]
683
gap> v * v; # the scalar product of `v' with itself
684
221/4
685

686
687
Note that the expression v * 1/3 is actually evaluated by first multiplying
688
v by 1 (which yields again v) and by then dividing by 3. This is also an
689
allowed scalar operation. The expression v/3 would result in the same value.
690
691
Such arithmetical operations (if the results are again vectors) result in
692
mutable vectors except if the operation is binary and both operands are
693
immutable; thus the vectors shown in the examples above are all mutable.
694
695
So if you want to produce a mutable list with 100 entries equal to 25, you
696
can simply say 25 + 0 * [ 1 .. 100 ]. Note that ranges are also vectors
697
(over the rationals), and that [ 1 .. 100 ] is mutable.
698
699
A matrix is a dense list of row vectors of equal length.
700
701
 Example 
702
gap> m:= [[1,-1, 1],
703
>  [2, 0,-1],
704
>  [1, 1, 1]];
705
[ [ 1, -1, 1 ], [ 2, 0, -1 ], [ 1, 1, 1 ] ]
706
gap> m[2][1];
707
2
708

709
710
Syntactically a matrix is a list of lists. So the number 2 in the second row
711
and the first column of the matrix m is referred to as the first element of
712
the second element of the list m via m[2][1].
713
714
A matrix may be multiplied by scalars, row vectors and other matrices. (If
715
the row vectors and matrices involved in such a multiplication do not have
716
suitable dimensions then the missing entries are treated as zeros, so the
717
results may look unexpectedly in such cases.)
718
719
 Example 
720
gap> [1, 0, 0] * m;
721
[ 1, -1, 1 ]
722
gap> [1, 0, 0, 2] * m;
723
[ 1, -1, 1 ]
724
gap> m * [1, 0, 0];
725
[ 1, 2, 1 ]
726
gap> m * [1, 0, 0, 2];
727
[ 1, 2, 1 ]
728

729
730
Note that multiplication of a row vector with a matrix will result in a
731
linear combination of the rows of the matrix, while multiplication of a
732
matrix with a row vector results in a linear combination of the columns of
733
the matrix. In the latter case the row vector is considered as a column
734
vector.
735
736
A vector or matrix of integers can also be multiplied with a finite field
737
scalar and vice versa. Such products result in a matrix over the finite
738
field with the integers mapped into the finite field in the obvious way.
739
Finite field matrices are nicer to read when they are Displayed rather than
740
Printed. (Here we write Z(q) to denote a primitive root of the finite field
741
with q elements.)
742
743
 Example 
744
gap> Display( m * One( GF(5) ) );
745
 1 4 1
746
 2 . 4
747
 1 1 1
748
gap> Display( m^2 * Z(2) + m * Z(4) );
749
z = Z(4)
750
 z^1 z^1 z^2
751
 1 1 z^2
752
 z^1 z^1 z^2
753

754
755
Submatrices can easily be extracted using the expression mat{rows}{columns}.
756
They can also be assigned to, provided the big matrix is mutable (which it
757
is not if it is the result of an arithmetical operation, see above).
758
759
 Example 
760
gap> sm := m{ [ 1, 2 ] }{ [ 2, 3 ] };
761
[ [ -1, 1 ], [ 0, -1 ] ]
762
gap> sm{ [ 1, 2 ] }{ [2] } := [[-2],[0]];; sm;
763
[ [ -1, -2 ], [ 0, 0 ] ]
764

765
766
The first curly brackets contain the selection of rows, the second that of
767
columns.
768
769
Matrices appear not only in linear algebra, but also as group elements,
770
provided they are invertible. Here we have the opportunity to meet a
771
group-theoretical function, namely Order (Reference: Order), which computes
772
the order of a group element.
773
774
 Example 
775
gap> Order( m * One( GF(5) ) );
776
8
777
gap> Order( m );
778
infinity
779

780
781
For matrices whose entries are more complex objects, for example rational
782
functions, GAP's Order (Reference: Order) methods might not be able to prove
783
that the matrix has infinite order, and one gets the following warning.
784
785
 Example 
786
#I Order: warning, order of <mat> might be infinite
787

788
789
In such a case, if the order of the matrix really is infinite, you will have
790
to interrupt GAP by pressing ctl-C (followed by ctl-D or quit; to leave the
791
break loop).
792
793
To prove that the order of m is infinite, we also could look at the minimal
794
polynomial of m over the rationals.
795
796
 Example 
797
gap> f:= MinimalPolynomial( Rationals, m );; Factors( f );
798
[ x_1-2, x_1^2+3 ]
799

800
801
Factors (Reference: Factors) returns a list of irreducible factors of the
802
polynomial f. The first irreducible factor X-2 reveals that 2 is an
803
eigenvalue of m, hence its order cannot be finite.
804
805
806
3.9 Plain Records
807
808
A record provides another way to build new data structures. Like a list a
809
record contains subobjects. In a record the elements, the so-called record
810
components, are not indexed by numbers but by names.
811
812
In this section you will see how to define and how to use records. Records
813
are changed by assignments to record components or by unbinding record
814
components.
815
816
Initially a record is defined as a comma separated list of assignments to
817
its record components.
818
819
 Example 
820
gap> date:= rec(year:= 1997,
821
>  month:= "Jul",
822
>  day:= 14);
823
rec( day := 14, month := "Jul", year := 1997 )
824

825
826
The value of a record component is accessible by the record name and the
827
record component name separated by one dot as the record component selector.
828
829
 Example 
830
gap> date.year;
831
1997
832

833
834
Assignments to new record components are possible in the same way. The
835
record is automatically resized to hold the new component.
836
837
 Example 
838
gap> date.time:= rec(hour:= 19, minute:= 23, second:= 12);
839
rec( hour := 19, minute := 23, second := 12 )
840
gap> date;
841
rec( day := 14, month := "Jul", 
842
 time := rec( hour := 19, minute := 23, second := 12 ), year := 1997 )
843

844
845
Records are objects that may be changed. An assignment to a record component
846
changes the original object. The remarks made in Sections 3.2 and 3.3 about
847
identity and mutability of lists are also true for records.
848
849
Sometimes it is interesting to know which components of a certain record are
850
bound. This information is available from the function RecNames (Reference:
851
RecNames), which takes a record as its argument and returns a list of names
852
of all bound components of this record as a list of strings.
853
854
 Example 
855
gap> RecNames(date);
856
[ "time", "year", "month", "day" ]
857

858
859
Now return to Sections 3.2 and 3.3 and find out what these sections mean for
860
records.
861
862
863
3.10 Further Information about Lists
864
865
(The following cross-references point to the GAP Reference Manual.)
866
867
You will find more about lists, sets, and ranges in Chapter 'Reference:
868
Lists', in particular more about identical lists in Section 'Reference:
869
Identical Lists'. A more detailed description of strings is contained in
870
Chapter 'Reference: Strings and Characters'. Fields are described in
871
Chapter 'Reference: Fields and Division Rings', some known fields in GAP are
872
described in Chapters 'Reference: Rational Numbers', 'Reference: Abelian
873
Number Fields', and 'Reference: Finite Fields'. Row vectors and matrices are
874
described in more detail in Chapters 'Reference: Row Vectors'
875
and 'Reference: Matrices'. Vector spaces are described in
876
Chapter 'Reference: Vector Spaces', further matrix related structures are
877
described in Chapters 'Reference: Matrix Groups', 'Reference: Algebras',
878
and 'Reference: Lie Algebras'.
879
880
You will find more list operations in Chapter 'Reference: Lists'.
881
882
Records and functions for records are described in detail in
883
Chapter 'Reference: Records'.
884
885
886