Debian Codegraph @

doc/tut/chap3_mj.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
7 <head>
8 <script type="text/javascript"
9 src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
10 </script>
11 <title>GAP (tut) - Chapter 3: Lists and Records</title>
12 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
13 <meta name="generator" content="GAPDoc2HTML" />
14 <link rel="stylesheet" type="text/css" href="manual.css" />
15 <script src="manual.js" type="text/javascript"></script>
16 <script type="text/javascript">overwriteStyle();</script>
17 </head>
18 <body class="chap3" onload="jscontent()">
19
20
21 <div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a> <a href="chap1_mj.html">1</a> <a href="chap2_mj.html">2</a> <a href="chap3_mj.html">3</a> <a href="chap4_mj.html">4</a> <a href="chap5_mj.html">5</a> <a href="chap6_mj.html">6</a> <a href="chap7_mj.html">7</a> <a href="chap8_mj.html">8</a> <a href="chapBib_mj.html">Bib</a> <a href="chapInd_mj.html">Ind</a> </div>
22
23 <div class="chlinkprevnexttop">&nbsp;<a href="chap0_mj.html">[Top of Book]</a>&nbsp; <a href="chap0_mj.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap2_mj.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap4_mj.html">[Next Chapter]</a>&nbsp; </div>
24
25 <p id="mathjaxlink" class="pcenter"><a href="chap3.html">[MathJax off]</a></p>
26 <p><a id="X7863DAFD78E5DFB6" name="X7863DAFD78E5DFB6"></a></p>
27 <div class="ChapSects"><a href="chap3_mj.html#X7863DAFD78E5DFB6">3 <span class="Heading">Lists and Records</span></a>
28 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X81ECC2077D88E112">3.1 <span class="Heading">Plain Lists</span></a>
29 </span>
30 </div>
31 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X7DD65BEA7EDB0CD7">3.2 <span class="Heading">Identical Lists</span></a>
32 </span>
33 </div>
34 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X8614C8207D57AD7A">3.3 <span class="Heading">Immutability</span></a>
35 </span>
36 </div>
37 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X83BE0C20875DD285">3.4 <span class="Heading">Sets</span></a>
38 </span>
39 </div>
40 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X79596BDE7CAF8491">3.5 <span class="Heading">Ranges</span></a>
41 </span>
42 </div>
43 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X7CC5B0347F8EC820">3.6 <span class="Heading">For and While Loops</span></a>
44 </span>
45 </div>
46 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X879CFE0985DBA041">3.7 <span class="Heading">List Operations</span></a>
47 </span>
48 </div>
49 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X851A7DD07866323F">3.8 <span class="Heading">Vectors and Matrices</span></a>
50 </span>
51 </div>
52 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X7CF7BBD58135B981">3.9 <span class="Heading">Plain Records</span></a>
53 </span>
54 </div>
55 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap3_mj.html#X800A5E8681E28E2D">3.10 <span class="Heading">Further Information about Lists</span></a>
56 </span>
57 </div>
58 </div>
59
60 <h3>3 <span class="Heading">Lists and Records</span></h3>
61
62 <p>Modern mathematics, especially algebra, is based on set theory. When sets are represented in a computer, they inadvertently turn into lists. That's why we start our survey of the various objects <strong class="pkg">GAP</strong> can handle with a description of lists and their manipulation. <strong class="pkg">GAP</strong> regards sets as a special kind of lists, namely as lists without holes or duplicates whose entries are ordered with respect to the precedence relation <code class="code">&lt;</code>.</p>
63
64 <p>After the introduction of the basic manipulations with lists in <a href="chap3_mj.html#X81ECC2077D88E112"><span class="RefLink">3.1</span></a>, some difficulties concerning identity and mutability of lists are discussed in <a href="chap3_mj.html#X7DD65BEA7EDB0CD7"><span class="RefLink">3.2</span></a> and <a href="chap3_mj.html#X8614C8207D57AD7A"><span class="RefLink">3.3</span></a>. Sets, ranges, row vectors, and matrices are introduced as special kinds of lists in <a href="chap3_mj.html#X83BE0C20875DD285"><span class="RefLink">3.4</span></a>, <a href="chap3_mj.html#X79596BDE7CAF8491"><span class="RefLink">3.5</span></a>, <a href="chap3_mj.html#X851A7DD07866323F"><span class="RefLink">3.8</span></a>. Handy list operations are shown in <a href="chap3_mj.html#X879CFE0985DBA041"><span class="RefLink">3.7</span></a>. Finally we explain how to use records in <a href="chap3_mj.html#X7CF7BBD58135B981"><span class="RefLink">3.9</span></a>.</p>
65
66 <p><a id="X81ECC2077D88E112" name="X81ECC2077D88E112"></a></p>
67
68 <h4>3.1 <span class="Heading">Plain Lists</span></h4>
69
70 <p>A <em>list</em> is a collection of objects separated by commas and enclosed in brackets. Let us for example construct the list <code class="code">primes</code> of the first ten prime numbers.</p>
71
72
73 <div class="example"><pre>
74 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes:= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];</span>
75 [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]
76 </pre></div>
77
78 <p>The next two primes are 31 and 37. They may be appended to the existing list by the function <code class="code">Append</code> which takes the existing list as its first and another list as a second argument. The second argument is appended to the list <code class="code">primes</code> and no value is returned. Note that by appending another list the object <code class="code">primes</code> is changed.</p>
79
80
81 <div class="example"><pre>
82 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Append(primes, [31, 37]);</span>
83 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes;</span>
84 [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 ]
85 </pre></div>
86
87 <p>You can as well add single new elements to existing lists by the function <code class="code">Add</code> which takes the existing list as its first argument and a new element as its second argument. The new element is added to the list <code class="code">primes</code> and again no value is returned but the list <code class="code">primes</code> is changed.</p>
88
89
90 <div class="example"><pre>
91 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Add(primes, 41);</span>
92 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes;</span>
93 [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 ]
94 </pre></div>
95
96 <p>Single elements of a list are referred to by their position in the list. To get the value of the seventh prime, that is the seventh entry in our list <code class="code">primes</code>, you simply type</p>
97
98
99 <div class="example"><pre>
100 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes[7];</span>
101 17
102 </pre></div>
103
104 <p>This value can be handled like any other value, for example multiplied by 2 or assigned to a variable. On the other hand this mechanism allows one to assign a value to a position in a list. So the next prime 43 may be inserted in the list directly after the last occupied position of <code class="code">primes</code>. This last occupied position is returned by the function <code class="code">Length</code>.</p>
105
106
107 <div class="example"><pre>
108 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Length(primes);</span>
109 13
110 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes[14]:= 43;</span>
111 43
112 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes;</span>
113 [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 ]
114 </pre></div>
115
116 <p>Note that this operation again has changed the object <code class="code">primes</code>. The next position after the end of a list is not the only position capable of taking a new value. If you know that 71 is the 20th prime, you can enter it right now in the 20th position of <code class="code">primes</code>. This will result in a list with holes which is however still a list and now has length 20.</p>
117
118
119 <div class="example"><pre>
120 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes[20]:= 71;</span>
121 71
122 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes;</span>
123 [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ]
124 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Length(primes);</span>
125 20
126 </pre></div>
127
128 <p>The list itself however must exist before a value can be assigned to a position of the list. This list may be the empty list <code class="code">[ ]</code>.</p>
129
130
131 <div class="example"><pre>
132 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">lll[1]:= 2;</span>
133 Error, Variable: 'lll' must have a value
134
135
136 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">lll:= []; lll[1]:= 2;</span>
137 [ ]
138 2
139 </pre></div>
140
141 <p>Of course existing entries of a list can be changed by this mechanism, too. We will not do it here because <code class="code">primes</code> then may no longer be a list of primes. Try for yourself to change the 17 in the list into a 9.</p>
142
143 <p>To get the position of 17 in the list <code class="code">primes</code> use the function <code class="func">Position</code> (<span class="RefLink">Reference: Position</span>) which takes the list as its first argument and the element as its second argument and returns the position of the first occurrence of the element 17 in the list <code class="code">primes</code>. If the element is not contained in the list then <code class="func">Position</code> (<span class="RefLink">Reference: Position</span>) will return the special object <code class="keyw">fail</code>.</p>
144
145
146 <div class="example"><pre>
147 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Position(primes, 17);</span>
148 7
149 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Position(primes, 20);</span>
150 fail
151 </pre></div>
152
153 <p>In all of the above changes to the list <code class="code">primes</code>, the list has been automatically resized. There is no need for you to tell <strong class="pkg">GAP</strong> how big you want a list to be. This is all done dynamically.</p>
154
155 <p>It is not necessary for the objects collected in a list to be of the same type.</p>
156
157
158 <div class="example"><pre>
159 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">lll:= [true, "This is a String",,, 3];</span>
160 [ true, "This is a String",,, 3 ]
161 </pre></div>
162
163 <p>In the same way a list may be part of another list.</p>
164
165
166 <div class="example"><pre>
167 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">lll[3]:= [4,5,6];; lll;</span>
168 [ true, "This is a String", [ 4, 5, 6 ],, 3 ]
169 </pre></div>
170
171 <p>A list may even be part of itself.</p>
172
173
174 <div class="example"><pre>
175 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">lll[4]:= lll;</span>
176 [ true, "This is a String", [ 4, 5, 6 ], ~, 3 ]
177 </pre></div>
178
179 <p>Now the tilde in the fourth position of <code class="code">lll</code> denotes the object that is currently printed. Note that the result of the last operation is the actual value of the object <code class="code">lll</code> on the right hand side of the assignment. In fact it is identical to the value of the whole list <code class="code">lll</code> on the left hand side of the assignment.</p>
180
181 <p>A <em>string</em> is a special type of list, namely a dense list of <em>characters</em>, where <em>dense</em> means that the list has no holes. Here, <em>characters</em> are special <strong class="pkg">GAP</strong> objects representing an element of the character set of the operating system. The input of printable characters is by enclosing them in single quotes <code class="code">'</code>. A string literal can either be entered as the list of characters or by writing the characters between doublequotes <code class="code">"</code>. Strings are handled specially by <code class="func">Print</code> (<span class="RefLink">Reference: Print</span>). You can learn much more about strings in the reference manual.</p>
182
183
184 <div class="example"><pre>
185 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s1 := ['H','a','l','l','o',' ','w','o','r','l','d','.'];</span>
186 "Hallo world."
187 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s1 = "Hallo world.";</span>
188 true
189 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s1[7];</span>
190 'w'
191 </pre></div>
192
193 <p>Sublists of lists can easily be extracted and assigned using the operator <code class="code"><var class="Arg">list</var>{ <var class="Arg">positions</var> }</code>.</p>
194
195
196 <div class="example"><pre>
197 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sl := lll{ [ 1, 2, 3 ] };</span>
198 [ true, "This is a String", [ 4, 5, 6 ] ]
199 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sl{ [ 2, 3 ] } := [ "New String", false ];</span>
200 [ "New String", false ]
201 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sl;</span>
202 [ true, "New String", false ]
203 </pre></div>
204
205 <p>This way you get a new list whose <span class="SimpleMath">\(i\)</span>-th entry is that element of the original list whose position is the <span class="SimpleMath">\(i\)</span>-th entry of the argument in the curly braces.</p>
206
207 <p><a id="X7DD65BEA7EDB0CD7" name="X7DD65BEA7EDB0CD7"></a></p>
208
209 <h4>3.2 <span class="Heading">Identical Lists</span></h4>
210
211 <p>This second section about lists is dedicated to the subtle difference between <em>equality</em> and <em>identity</em> of lists. It is really important to understand this difference in order to understand how complex data structures are realized in <strong class="pkg">GAP</strong>. This section applies to all <strong class="pkg">GAP</strong> objects that have subobjects, e.g., to lists and to records. After reading the section <a href="chap3_mj.html#X7CF7BBD58135B981"><span class="RefLink">3.9</span></a> about records you should return to this section and translate it into the record context.</p>
212
213 <p>Two lists are <em>equal</em> if all their entries are equal. This means that the equality operator <code class="code">=</code> returns <code class="keyw">true</code> for the comparison of two lists if and only if these two lists are of the same length and for each position the values in the respective lists are equal.</p>
214
215
216 <div class="example"><pre>
217 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">numbers := primes;; numbers = primes;</span>
218 true
219 </pre></div>
220
221 <p>We assigned the list <code class="code">primes</code> to the variable <code class="code">numbers</code> and, of course they are equal as they have both the same length and the same entries. Now we will change the third number to 4 and compare the result again with <code class="code">primes</code>.</p>
222
223
224 <div class="example"><pre>
225 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">numbers[3]:= 4;; numbers = primes;</span>
226 true
227 </pre></div>
228
229 <p>You see that <code class="code">numbers</code> and <code class="code">primes</code> are still equal, check this by printing the value of <code class="code">primes</code>. The list <code class="code">primes</code> is no longer a list of primes! What has happened? The truth is that the lists <code class="code">primes</code> and <code class="code">numbers</code> are not only equal but they are also <em>identical</em>. <code class="code">primes</code> and <code class="code">numbers</code> are two variables pointing to the same list. If you change the value of the subobject <code class="code">numbers[3]</code> of <code class="code">numbers</code> this will also change <code class="code">primes</code>. Variables do <em>not</em> point to a certain block of storage memory but they do point to an object that occupies storage memory. So the assignment <code class="code">numbers := primes</code> did <em>not</em> create a new list in a different place of memory but only created the new name <code class="code">numbers</code> for the same old list of primes.</p>
230
231 <p>From this we see that <em>the same object can have several names.</em></p>
232
233 <p>If you want to change a list with the contents of <code class="code">primes</code> independently from <code class="code">primes</code> you will have to make a copy of <code class="code">primes</code> by the function <code class="code">ShallowCopy</code> which takes an object as its argument and returns a copy of the argument. (We will first restore the old value of <code class="code">primes</code>.)</p>
234
235
236 <div class="example"><pre>
237 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes[3]:= 5;; primes;</span>
238 [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ]
239 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">numbers:= ShallowCopy(primes);; numbers = primes;</span>
240 true
241 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">numbers[3]:= 4;; numbers = primes;</span>
242 false
243 </pre></div>
244
245 <p>Now <code class="code">numbers</code> is no longer equal to <code class="code">primes</code> and <code class="code">primes</code> still is a list of primes. Check this by printing the values of <code class="code">numbers</code> and <code class="code">primes</code>.</p>
246
247 <p>Lists and records can be changed this way because <strong class="pkg">GAP</strong> objects of these types have subobjects. To clarify this statement consider the following assignments.</p>
248
249
250 <div class="example"><pre>
251 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">i:= 1;; j:= i;; i:= i+1;;</span>
252 </pre></div>
253
254 <p>By adding 1 to <code class="code">i</code> the value of <code class="code">i</code> has changed. What happens to <code class="code">j</code>? After the second statement <code class="code">j</code> points to the same object as <code class="code">i</code>, namely to the integer 1. The addition does <em>not</em> change the object <code class="code">1</code> but creates a new object according to the instruction <code class="code">i+1</code>. It is actually the assignment that changes the value of <code class="code">i</code>. Therefore <code class="code">j</code> still points to the object <code class="code">1</code>. Integers (like permutations and booleans) have no subobjects. Objects of these types cannot be changed but can only be replaced by other objects. And a replacement does not change the values of other variables. In the above example an assignment of a new value to the variable <code class="code">numbers</code> would also not change the value of <code class="code">primes</code>.</p>
255
256 <p>Finally try the following examples and explain the results.</p>
257
258
259 <div class="example"><pre>
260 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= [];; l:= [l];</span>
261 [ [ ] ]
262 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[1]:= l;</span>
263 [ ~ ]
264 </pre></div>
265
266 <p>Now return to Section <a href="chap3_mj.html#X81ECC2077D88E112"><span class="RefLink">3.1</span></a> and find out whether the functions <code class="func">Add</code> (<span class="RefLink">Reference: Add</span>) and <code class="func">Append</code> (<span class="RefLink">Reference: Append</span>) change their arguments.</p>
267
268 <p><a id="X8614C8207D57AD7A" name="X8614C8207D57AD7A"></a></p>
269
270 <h4>3.3 <span class="Heading">Immutability</span></h4>
271
272 <p><strong class="pkg">GAP</strong> has a mechanism that protects lists against changes like the ones that have bothered us in Section <a href="chap3_mj.html#X7DD65BEA7EDB0CD7"><span class="RefLink">3.2</span></a>. The function <code class="func">Immutable</code> (<span class="RefLink">Reference: Immutable</span>) takes as argument a list and returns an immutable copy of it, i.e., a list which looks exactly like the old one, but has two extra properties: (1) The new list is immutable, i.e., the list itself and its subobjects cannot be changed. (2) In constructing the copy, every part of the list that can be changed has been copied, so that changes to the old list will not affect the new one. In other words, the new list has no mutable subobjects in common with the old list.</p>
273
274
275 <div class="example"><pre>
276 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list := [ 1, 2, "three", [ 4 ] ];; copy := Immutable( list );;</span>
277 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list[3][5] := 'w';; list; copy;</span>
278 [ 1, 2, "threw", [ 4 ] ]
279 [ 1, 2, "three", [ 4 ] ]
280 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">copy[3][5] := 'w';</span>
281 List Assignment: &lt;list&gt; must be a mutable list
282 not in any function
283 Entering break read-eval-print loop ...
284 you can 'quit;' to quit to outer loop, or
285 you can 'return;' and ignore the assignment to continue
286 <span class="GAPbrkprompt">brk&gt;</span> <span class="GAPinput">quit;</span>
287 </pre></div>
288
289 <p>As a consequence of these rules, in the immutable copy of a list which contains an already immutable list as subobject, this immutable subobject need not be copied, because it is unchangeable. Immutable lists are useful in many complex <strong class="pkg">GAP</strong> objects, for example as generator lists of groups. By making them immutable, <strong class="pkg">GAP</strong> ensures that no generators can be added to the list, removed or exchanged. Such changes would of course lead to serious inconsistencies with other knowledge that may already have been calculated for the group.</p>
290
291 <p>A converse function to <code class="func">Immutable</code> (<span class="RefLink">Reference: Immutable</span>) is <code class="func">ShallowCopy</code> (<span class="RefLink">Reference: ShallowCopy</span>), which produces a new mutable list whose <span class="SimpleMath">\(i\)</span>-th entry is the <span class="SimpleMath">\(i\)</span>-th entry of the old list. The single entries are not copied, they are just placed in the new list. If the old list is immutable, and hence the list entries are immutable themselves, the result of <code class="func">ShallowCopy</code> (<span class="RefLink">Reference: ShallowCopy</span>) is mutable only on the top level.</p>
292
293 <p>It should be noted that also other objects than lists can appear in mutable or immutable form. Records (see Section <a href="chap3_mj.html#X7CF7BBD58135B981"><span class="RefLink">3.9</span></a>) provide another example.</p>
294
295 <p><a id="X83BE0C20875DD285" name="X83BE0C20875DD285"></a></p>
296
297 <h4>3.4 <span class="Heading">Sets</span></h4>
298
299 <p><strong class="pkg">GAP</strong> knows several special kinds of lists. A <em>set</em> in <strong class="pkg">GAP</strong> is a list that contains no holes (such a list is called <em>dense</em>) and whose elements are strictly sorted w.r.t. <code class="code">&lt;</code>; in particular, a set cannot contain duplicates. (More precisely, the elements of a set in <strong class="pkg">GAP</strong> are required to lie in the same <em>family</em>, but roughly this means that they can be compared using the <code class="code">&lt;</code> operator.)</p>
300
301 <p>This provides a natural model for mathematical sets whose elements are given by an explicit enumeration.</p>
302
303 <p><strong class="pkg">GAP</strong> also calls a set a <em>strictly sorted list</em>, and the function <code class="func">IsSSortedList</code> (<span class="RefLink">Reference: IsSSortedList</span>) tests whether a given list is a set. It returns a boolean value. For almost any list whose elements are contained in the same family, there exists a corresponding set. This set is constructed by the function <code class="func">Set</code> (<span class="RefLink">Reference: Set</span>) which takes the list as its argument and returns a set obtained from this list by ignoring holes and duplicates and by sorting the elements.</p>
304
305 <p>The elements of the sets used in the examples of this section are strings.</p>
306
307
308 <div class="example"><pre>
309 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fruits:= ["apple", "strawberry", "cherry", "plum"];</span>
310 [ "apple", "strawberry", "cherry", "plum" ]
311 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsSSortedList(fruits);</span>
312 false
313 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fruits:= Set(fruits);</span>
314 [ "apple", "cherry", "plum", "strawberry" ]
315 </pre></div>
316
317 <p>Note that the original list <code class="code">fruits</code> is not changed by the function <code class="func">Set</code> (<span class="RefLink">Reference: Set</span>). We have to make a new assignment to the variable <code class="code">fruits</code> in order to make it a set.</p>
318
319 <p>The operator <code class="keyw">in</code> is used to test whether an object is an element of a set. It returns a boolean value <code class="keyw">true</code> or <code class="keyw">false</code>.</p>
320
321
322 <div class="example"><pre>
323 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">"apple" in fruits;</span>
324 true
325 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">"banana" in fruits;</span>
326 false
327 </pre></div>
328
329 <p>The operator <code class="keyw">in</code> can also be applied to ordinary lists. It is however much faster to perform a membership test for sets since sets are always sorted and a binary search can be used instead of a linear search. New elements may be added to a set by the function <code class="func">AddSet</code> (<span class="RefLink">Reference: AddSet</span>) which takes the set <code class="code">fruits</code> as its first argument and an element as its second argument and adds the element to the set if it wasn't already there. Note that the object <code class="code">fruits</code> is changed.</p>
330
331
332 <div class="example"><pre>
333 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AddSet(fruits, "banana");</span>
334 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fruits; # The banana is inserted in the right place.</span>
335 [ "apple", "banana", "cherry", "plum", "strawberry" ]
336 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AddSet(fruits, "apple");</span>
337 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fruits; # fruits has not changed.</span>
338 [ "apple", "banana", "cherry", "plum", "strawberry" ]
339 </pre></div>
340
341 <p>Note that inserting new elements into a set with <code class="func">AddSet</code> (<span class="RefLink">Reference: AddSet</span>) is usually more expensive than simply adding new elements at the end of a list.</p>
342
343 <p>Sets can be intersected by the function <code class="func">Intersection</code> (<span class="RefLink">Reference: Intersection</span>) and united by the function <code class="func">Union</code> (<span class="RefLink">Reference: Union</span>) which both take two sets as their arguments and return the intersection resp. union of the two sets as a new object.</p>
344
345
346 <div class="example"><pre>
347 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">breakfast:= ["tea", "apple", "egg"];</span>
348 [ "tea", "apple", "egg" ]
349 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Intersection(breakfast, fruits);</span>
350 [ "apple" ]
351 </pre></div>
352
353 <p>The arguments of the functions <code class="func">Intersection</code> (<span class="RefLink">Reference: Intersection</span>) and <code class="func">Union</code> (<span class="RefLink">Reference: Union</span>) could be ordinary lists, while their result is always a set. Note that in the preceding example at least one argument of <code class="func">Intersection</code> (<span class="RefLink">Reference: Intersection</span>) was not a set. The functions <code class="func">IntersectSet</code> (<span class="RefLink">Reference: IntersectSet</span>) and <code class="func">UniteSet</code> (<span class="RefLink">Reference: UniteSet</span>) also form the intersection resp. union of two sets. They will however not return the result but change their first argument to be the result. Try them carefully.</p>
354
355 <p><a id="X79596BDE7CAF8491" name="X79596BDE7CAF8491"></a></p>
356
357 <h4>3.5 <span class="Heading">Ranges</span></h4>
358
359 <p>A <em>range</em> is a finite arithmetic progression of integers. This is another special kind of list. A range is described by the first two values and the last value of the arithmetic progression which are given in the form <code class="code">[<var class="Arg">first</var>,<var class="Arg">second</var>..<var class="Arg">last</var>]</code>. In the usual case of an ascending list of consecutive integers the second entry may be omitted.</p>
360
361
362 <div class="example"><pre>
363 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[1..999999]; # a range of almost a million numbers</span>
364 [ 1 .. 999999 ]
365 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[1, 2..999999]; # this is equivalent</span>
366 [ 1 .. 999999 ]
367 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[1, 3..999999]; # here the step is 2</span>
368 [ 1, 3 .. 999999 ]
369 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Length( last );</span>
370 500000
371 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 999999, 999997 .. 1 ];</span>
372 [ 999999, 999997 .. 1 ]
373 </pre></div>
374
375 <p>This compact printed representation of a fairly long list corresponds to a compact internal representation. The function <code class="func">IsRange</code> (<span class="RefLink">Reference: IsRange</span>) tests whether an object is a range, the function <code class="func">ConvertToRangeRep</code> (<span class="RefLink">Reference: ConvertToRangeRep</span>) changes the representation of a list that is in fact a range to this compact internal representation.</p>
376
377
378 <div class="example"><pre>
379 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= [-2,-1,0,1,2,3,4,5];</span>
380 [ -2, -1, 0, 1, 2, 3, 4, 5 ]
381 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRange( a );</span>
382 true
383 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ConvertToRangeRep( a );; a;</span>
384 [ -2 .. 5 ]
385 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a[1]:= 0;; IsRange( a );</span>
386 false
387 </pre></div>
388
389 <p>Note that this change of representation does <em>not</em> change the value of the list <code class="code">a</code>. The list <code class="code">a</code> still behaves in any context in the same way as it would have in the long representation.</p>
390
391 <p><a id="X7CC5B0347F8EC820" name="X7CC5B0347F8EC820"></a></p>
392
393 <h4>3.6 <span class="Heading">For and While Loops</span></h4>
394
395 <p>Given a list <code class="code">pp</code> of permutations we can form their product by means of a <code class="keyw">for</code> loop instead of writing down the product explicitly.</p>
396
397
398 <div class="example"><pre>
399 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">pp:= [ (1,3,2,6,8)(4,5,9), (1,6)(2,7,8), (1,5,7)(2,3,8,6),</span>
400 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> (1,8,9)(2,3,5,6,4), (1,9,8,6,3,4,7,2)];;</span>
401 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">prod:= ();</span>
402 ()
403 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">for p in pp do</span>
404 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> prod:= prod*p;</span>
405 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
406 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">prod;</span>
407 (1,8,4,2,3,6,5,9)
408 </pre></div>
409
410 <p>First a new variable <code class="code">prod</code> is initialized to the identity permutation <code class="code">()</code>. Then the loop variable <code class="code">p</code> takes as its value one permutation after the other from the list <code class="code">pp</code> and is multiplied with the present value of <code class="code">prod</code> resulting in a new value which is then assigned to <code class="code">prod</code>.</p>
411
412 <p>The <code class="keyw">for</code> loop has the following syntax</p>
413
414 <p><code class="keyw">for</code> <var class="Arg">var</var> <code class="keyw">in</code> <var class="Arg">list</var> <code class="keyw">do</code> <var class="Arg">statements</var> <code class="keyw">od</code><code class="code">;</code></p>
415
416 <p>The effect of the <code class="keyw">for</code> loop is to execute the <var class="Arg">statements</var> for every element of the <var class="Arg">list</var>. A <code class="keyw">for</code> loop is a statement and therefore terminated by a semicolon. The list of <var class="Arg">statements</var> is enclosed by the keywords <code class="keyw">do</code> and <code class="keyw">od</code> (reverse <code class="keyw">do</code>). A <code class="keyw">for</code> loop returns no value. Therefore we had to ask explicitly for the value of <code class="code">prod</code> in the preceding example.</p>
417
418 <p>The <code class="keyw">for</code> loop can loop over any kind of list, even a list with holes. In many programming languages the <code class="keyw">for</code> loop has the form</p>
419
420 <p><code class="code">for <var class="Arg">var</var> from <var class="Arg">first</var> to <var class="Arg">last</var> do <var class="Arg">statements</var> od;</code></p>
421
422 <p>In <strong class="pkg">GAP</strong> this is merely a special case of the general <code class="keyw">for</code> loop as defined above where the <var class="Arg">list</var> in the loop body is a range (see <a href="chap3_mj.html#X79596BDE7CAF8491"><span class="RefLink">3.5</span></a>):</p>
423
424 <p><code class="keyw">for</code> <var class="Arg">var</var> <code class="keyw">in</code> <code class="code">[<var class="Arg">first</var>..<var class="Arg">last</var>]</code> <code class="keyw">do</code> <var class="Arg">statements</var> <code class="keyw">od</code><code class="code">;</code></p>
425
426 <p>You can for instance loop over a range to compute the factorial <span class="SimpleMath">\(15!\)</span> of the number <span class="SimpleMath">\(15\)</span> in the following way.</p>
427
428
429 <div class="example"><pre>
430 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ff:= 1;</span>
431 1
432 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">for i in [1..15] do</span>
433 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> ff:= ff * i;</span>
434 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
435 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ff;</span>
436 1307674368000
437 </pre></div>
438
439 <p>The <code class="keyw">while</code> loop has the following syntax</p>
440
441 <p><code class="keyw">while</code> <var class="Arg">condition</var> <code class="keyw">do</code> <var class="Arg">statements</var> <code class="keyw">od</code><code class="code">;</code></p>
442
443 <p>The <code class="keyw">while</code> loop loops over the <var class="Arg">statements</var> as long as the <var class="Arg">condition</var> evaluates to <code class="keyw">true</code>. Like the <code class="keyw">for</code> loop the <code class="keyw">while</code> loop is terminated by the keyword <code class="keyw">od</code> followed by a semicolon.</p>
444
445 <p>We can use our list <code class="code">primes</code> to perform a very simple factorization. We begin by initializing a list <code class="code">factors</code> to the empty list. In this list we want to collect the prime factors of the number 1333. Remember that a list has to exist before any values can be assigned to positions of the list. Then we will loop over the list <code class="code">primes</code> and test for each prime whether it divides the number. If it does we will divide the number by that prime, add it to the list <code class="code">factors</code> and continue.</p>
446
447
448 <div class="example"><pre>
449 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">n:= 1333;;</span>
450 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">factors:= [];;</span>
451 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">for p in primes do</span>
452 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> while n mod p = 0 do</span>
453 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> n:= n/p;</span>
454 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> Add(factors, p);</span>
455 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
456 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
457 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">factors;</span>
458 [ 31, 43 ]
459 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">n;</span>
460 1
461 </pre></div>
462
463 <p>As <code class="code">n</code> now has the value 1 all prime factors of 1333 have been found and <code class="code">factors</code> contains a complete factorization of 1333. This can of course be verified by multiplying 31 and 43.</p>
464
465 <p>This loop may be applied to arbitrary numbers in order to find prime factors. But as <code class="code">primes</code> is not a complete list of all primes this loop may fail to find all prime factors of a number greater than 2000, say. You can try to improve it in such a way that new primes are added to the list <code class="code">primes</code> if needed.</p>
466
467 <p>You have already seen that list objects may be changed. This of course also holds for the list in a loop body. In most cases you have to be careful not to change this list, but there are situations where this is quite useful. The following example shows a quick way to determine the primes smaller than 1000 by a sieve method. Here we will make use of the function <code class="code">Unbind</code> to delete entries from a list, and the <code class="code">if</code> statement covered in <a href="chap4_mj.html#X801EE07E839B31B2"><span class="RefLink">4.2</span></a>.</p>
468
469
470 <div class="example"><pre>
471 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes:= [];;</span>
472 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">numbers:= [2..1000];;</span>
473 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">for p in numbers do</span>
474 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> Add(primes, p);</span>
475 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> for n in numbers do</span>
476 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> if n mod p = 0 then</span>
477 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> Unbind(numbers[n-1]);</span>
478 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> fi;</span>
479 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
480 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
481 </pre></div>
482
483 <p>The inner loop removes all entries from <code class="code">numbers</code> that are divisible by the last detected prime <code class="code">p</code>. This is done by the function <code class="code">Unbind</code> which deletes the binding of the list position <code class="code">numbers[n-1]</code> to the value <code class="code">n</code> so that afterwards <code class="code">numbers[n-1]</code> no longer has an assigned value. The next element encountered in <code class="code">numbers</code> by the outer loop necessarily is the next prime.</p>
484
485 <p>In a similar way it is possible to enlarge the list which is looped over. This yields a nice and short orbit algorithm for the action of a group, for example.</p>
486
487 <p>More about <code class="keyw">for</code> and <code class="keyw">while</code> loops can be found in the sections <span class="RefLink">Reference: While</span> and <span class="RefLink">Reference: For</span>.</p>
488
489 <p><a id="X879CFE0985DBA041" name="X879CFE0985DBA041"></a></p>
490
491 <h4>3.7 <span class="Heading">List Operations</span></h4>
492
493 <p>There is a more comfortable way than that given in the previous section to compute the product of a list of numbers or permutations.</p>
494
495
496 <div class="example"><pre>
497 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Product([1..15]);</span>
498 1307674368000
499 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Product(pp);</span>
500 (1,8,4,2,3,6,5,9)
501 </pre></div>
502
503 <p>The function <code class="func">Product</code> (<span class="RefLink">Reference: Product</span>) takes a list as its argument and computes the product of the elements of the list. This is possible whenever a multiplication of the elements of the list is defined. So <code class="func">Product</code> (<span class="RefLink">Reference: Product</span>) executes a loop over all elements of the list.</p>
504
505 <p>There are other often used loops available as functions. Guess what the function <code class="func">Sum</code> (<span class="RefLink">Reference: Sum</span>) does. The function <code class="func">List</code> (<span class="RefLink">Reference: list and non-list difference</span>) may take a list and a function as its arguments. It will then apply the function to each element of the list and return the corresponding list of results. A list of cubes is produced as follows with the function <code class="code">cubed</code> from Section <a href="chap4_mj.html#X86FA580F8055B274"><span class="RefLink">4</span></a>.</p>
506
507
508 <div class="example"><pre>
509 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">cubed:= x -&gt; x^3;;</span>
510 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([2..10], cubed);</span>
511 [ 8, 27, 64, 125, 216, 343, 512, 729, 1000 ]
512 </pre></div>
513
514 <p>To add all these cubes we might apply the function <code class="func">Sum</code> (<span class="RefLink">Reference: Sum</span>) to the last list. But we may as well give the function <code class="code">cubed</code> to <code class="func">Sum</code> (<span class="RefLink">Reference: Sum</span>) as an additional argument.</p>
515
516
517 <div class="example"><pre>
518 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sum(last) = Sum([2..10], cubed);</span>
519 true
520 </pre></div>
521
522 <p>The primes less than 30 can be retrieved out of the list <code class="code">primes</code> from Section <a href="chap3_mj.html#X81ECC2077D88E112"><span class="RefLink">3.1</span></a> by the function <code class="func">Filtered</code> (<span class="RefLink">Reference: Filtered</span>). This function takes the list <code class="code">primes</code> and a property as its arguments and will return the list of those elements of <code class="code">primes</code> which have this property. Such a property will be represented by a function that returns a boolean value. In this example the property of being less than 30 can be represented by the function <code class="code">x -&gt; x &lt; 30</code> since <code class="code">x &lt; 30</code> will evaluate to <code class="keyw">true</code> for values <code class="code">x</code> less than 30 and to <code class="keyw">false</code> otherwise.</p>
523
524
525 <div class="example"><pre>
526 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Filtered(primes, x -&gt; x &lt; 30);</span>
527 [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]
528 </pre></div>
529
530 <p>We have already mentioned the operator <code class="code">{ }</code> that forms sublists. It takes a list of positions as its argument and will return the list of elements from the original list corresponding to these positions.</p>
531
532
533 <div class="example"><pre>
534 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">primes{ [1 .. 10] };</span>
535 [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]
536 </pre></div>
537
538 <p>Finally we mention the function <code class="func">ForAll</code> (<span class="RefLink">Reference: ForAll</span>) that checks whether a property holds for all elements of a list. It takes as its arguments a list and a function that returns a boolean value. <code class="func">ForAll</code> (<span class="RefLink">Reference: ForAll</span>) checks whether the function returns <code class="keyw">true</code> for all elements of the list.</p>
539
540
541 <div class="example"><pre>
542 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list:= [ 1, 2, 3, 4 ];;</span>
543 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAll( list, x -&gt; x &gt; 0 );</span>
544 true
545 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAll( list, x -&gt; x in primes );</span>
546 false
547 </pre></div>
548
549 <p>You will find more predefined <code class="keyw">for</code> loops in chapter <span class="RefLink">Reference: Lists</span>.</p>
550
551 <p><a id="X851A7DD07866323F" name="X851A7DD07866323F"></a></p>
552
553 <h4>3.8 <span class="Heading">Vectors and Matrices</span></h4>
554
555 <p>This section describes how <strong class="pkg">GAP</strong> uses lists to represent row vectors and matrices. A <em>row vector</em> is a dense list of elements from a common field. A <em>matrix</em> is a dense list of row vectors over a common field and of equal length.</p>
556
557
558 <div class="example"><pre>
559 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">v:= [3, 6, 2, 5/2];; IsRowVector(v);</span>
560 true
561 </pre></div>
562
563 <p>Row vectors may be added and multiplied by scalars from their field. Multiplication of row vectors of equal length results in their scalar product.</p>
564
565
566 <div class="example"><pre>
567 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">2 * v; v * 1/3;</span>
568 [ 6, 12, 4, 5 ]
569 [ 1, 2, 2/3, 5/6 ]
570 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">v * v; # the scalar product of `v' with itself</span>
571 221/4
572 </pre></div>
573
574 <p>Note that the expression <code class="code">v * 1/3</code> is actually evaluated by first multiplying <code class="code">v</code> by 1 (which yields again <code class="code">v</code>) and by then dividing by 3. This is also an allowed scalar operation. The expression <code class="code">v/3</code> would result in the same value.</p>
575
576 <p>Such arithmetical operations (if the results are again vectors) result in <em>mutable</em> vectors except if the operation is binary and both operands are immutable; thus the vectors shown in the examples above are all mutable.</p>
577
578 <p>So if you want to produce a mutable list with 100 entries equal to 25, you can simply say <code class="code">25 + 0 * [ 1 .. 100 ]</code>. Note that ranges are also vectors (over the rationals), and that <code class="code">[ 1 .. 100 ]</code> is mutable.</p>
579
580 <p>A matrix is a dense list of row vectors of equal length.</p>
581
582
583 <div class="example"><pre>
584 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m:= [[1,-1, 1],</span>
585 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> [2, 0,-1],</span>
586 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> [1, 1, 1]];</span>
587 [ [ 1, -1, 1 ], [ 2, 0, -1 ], [ 1, 1, 1 ] ]
588 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m[2][1];</span>
589 2
590 </pre></div>
591
592 <p>Syntactically a matrix is a list of lists. So the number 2 in the second row and the first column of the matrix <code class="code">m</code> is referred to as the first element of the second element of the list <code class="code">m</code> via <code class="code">m[2][1]</code>.</p>
593
594 <p>A matrix may be multiplied by scalars, row vectors and other matrices. (If the row vectors and matrices involved in such a multiplication do not have suitable dimensions then the <q>missing</q> entries are treated as zeros, so the results may look unexpectedly in such cases.)</p>
595
596
597 <div class="example"><pre>
598 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[1, 0, 0] * m;</span>
599 [ 1, -1, 1 ]
600 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[1, 0, 0, 2] * m;</span>
601 [ 1, -1, 1 ]
602 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m * [1, 0, 0];</span>
603 [ 1, 2, 1 ]
604 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m * [1, 0, 0, 2];</span>
605 [ 1, 2, 1 ]
606 </pre></div>
607
608 <p>Note that multiplication of a row vector with a matrix will result in a linear combination of the rows of the matrix, while multiplication of a matrix with a row vector results in a linear combination of the columns of the matrix. In the latter case the row vector is considered as a column vector.</p>
609
610 <p>A vector or matrix of integers can also be multiplied with a finite field scalar and vice versa. Such products result in a matrix over the finite field with the integers mapped into the finite field in the obvious way. Finite field matrices are nicer to read when they are <code class="code">Display</code>ed rather than <code class="code">Print</code>ed. (Here we write <code class="code">Z(q)</code> to denote a primitive root of the finite field with <code class="code">q</code> elements.)</p>
611
612
613 <div class="example"><pre>
614 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Display( m * One( GF(5) ) );</span>
615 1 4 1
616 2 . 4
617 1 1 1
618 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Display( m^2 * Z(2) + m * Z(4) );</span>
619 z = Z(4)
620 z^1 z^1 z^2
621 1 1 z^2
622 z^1 z^1 z^2
623 </pre></div>
624
625 <p>Submatrices can easily be extracted using the expression <code class="code"><var class="Arg">mat</var>{<var class="Arg">rows</var>}{<var class="Arg">columns</var>}</code>. They can also be assigned to, provided the big matrix is mutable (which it is not if it is the result of an arithmetical operation, see above).</p>
626
627
628 <div class="example"><pre>
629 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sm := m{ [ 1, 2 ] }{ [ 2, 3 ] };</span>
630 [ [ -1, 1 ], [ 0, -1 ] ]
631 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sm{ [ 1, 2 ] }{ [2] } := [[-2],[0]];; sm;</span>
632 [ [ -1, -2 ], [ 0, 0 ] ]
633 </pre></div>
634
635 <p>The first curly brackets contain the selection of rows, the second that of columns.</p>
636
637 <p>Matrices appear not only in linear algebra, but also as group elements, provided they are invertible. Here we have the opportunity to meet a group-theoretical function, namely <code class="func">Order</code> (<span class="RefLink">Reference: Order</span>), which computes the order of a group element.</p>
638
639
640 <div class="example"><pre>
641 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Order( m * One( GF(5) ) );</span>
642 8
643 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Order( m );</span>
644 infinity
645 </pre></div>
646
647 <p>For matrices whose entries are more complex objects, for example rational functions, <strong class="pkg">GAP</strong>'s <code class="func">Order</code> (<span class="RefLink">Reference: Order</span>) methods might not be able to prove that the matrix has infinite order, and one gets the following warning.</p>
648
649
650 <div class="example"><pre>
651 #I Order: warning, order of &lt;mat&gt; might be infinite
652 </pre></div>
653
654 <p>In such a case, if the order of the matrix really is infinite, you will have to interrupt <strong class="pkg">GAP</strong> by pressing <code class="code"><var class="Arg">ctl</var>-C</code> (followed by <code class="code"><var class="Arg">ctl</var>-D</code> or <code class="code">quit;</code> to leave the break loop).</p>
655
656 <p>To prove that the order of <code class="code">m</code> is infinite, we also could look at the minimal polynomial of <code class="code">m</code> over the rationals.</p>
657
658
659 <div class="example"><pre>
660 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">f:= MinimalPolynomial( Rationals, m );; Factors( f );</span>
661 [ x_1-2, x_1^2+3 ]
662 </pre></div>
663
664 <p><code class="func">Factors</code> (<span class="RefLink">Reference: Factors</span>) returns a list of irreducible factors of the polynomial <code class="code">f</code>. The first irreducible factor <span class="SimpleMath">\(X-2\)</span> reveals that 2 is an eigenvalue of <code class="code">m</code>, hence its order cannot be finite.</p>
665
666 <p><a id="X7CF7BBD58135B981" name="X7CF7BBD58135B981"></a></p>
667
668 <h4>3.9 <span class="Heading">Plain Records</span></h4>
669
670 <p>A record provides another way to build new data structures. Like a list a record contains subobjects. In a record the elements, the so-called <em>record components</em>, are not indexed by numbers but by names.</p>
671
672 <p>In this section you will see how to define and how to use records. Records are changed by assignments to record components or by unbinding record components.</p>
673
674 <p>Initially a record is defined as a comma separated list of assignments to its record components.</p>
675
676
677 <div class="example"><pre>
678 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">date:= rec(year:= 1997,</span>
679 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> month:= "Jul",</span>
680 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> day:= 14);</span>
681 rec( day := 14, month := "Jul", year := 1997 )
682 </pre></div>
683
684 <p>The value of a record component is accessible by the record name and the record component name separated by one dot as the record component selector.</p>
685
686
687 <div class="example"><pre>
688 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">date.year;</span>
689 1997
690 </pre></div>
691
692 <p>Assignments to new record components are possible in the same way. The record is automatically resized to hold the new component.</p>
693
694
695 <div class="example"><pre>
696 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">date.time:= rec(hour:= 19, minute:= 23, second:= 12);</span>
697 rec( hour := 19, minute := 23, second := 12 )
698 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">date;</span>
699 rec( day := 14, month := "Jul",
700 time := rec( hour := 19, minute := 23, second := 12 ), year := 1997 )
701 </pre></div>
702
703 <p>Records are objects that may be changed. An assignment to a record component changes the original object. The remarks made in Sections <a href="chap3_mj.html#X7DD65BEA7EDB0CD7"><span class="RefLink">3.2</span></a> and <a href="chap3_mj.html#X8614C8207D57AD7A"><span class="RefLink">3.3</span></a> about identity and mutability of lists are also true for records.</p>
704
705 <p>Sometimes it is interesting to know which components of a certain record are bound. This information is available from the function <code class="func">RecNames</code> (<span class="RefLink">Reference: RecNames</span>), which takes a record as its argument and returns a list of names of all bound components of this record as a list of strings.</p>
706
707
708 <div class="example"><pre>
709 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RecNames(date);</span>
710 [ "time", "year", "month", "day" ]
711 </pre></div>
712
713 <p>Now return to Sections <a href="chap3_mj.html#X7DD65BEA7EDB0CD7"><span class="RefLink">3.2</span></a> and <a href="chap3_mj.html#X8614C8207D57AD7A"><span class="RefLink">3.3</span></a> and find out what these sections mean for records.</p>
714
715 <p><a id="X800A5E8681E28E2D" name="X800A5E8681E28E2D"></a></p>
716
717 <h4>3.10 <span class="Heading">Further Information about Lists</span></h4>
718
719 <p>(The following cross-references point to the <strong class="pkg">GAP</strong> Reference Manual.)</p>
720
721 <p>You will find more about lists, sets, and ranges in Chapter <span class="RefLink">Reference: Lists</span>, in particular more about identical lists in Section <span class="RefLink">Reference: Identical Lists</span>. A more detailed description of strings is contained in Chapter <span class="RefLink">Reference: Strings and Characters</span>. Fields are described in Chapter <span class="RefLink">Reference: Fields and Division Rings</span>, some known fields in <strong class="pkg">GAP</strong> are described in Chapters <span class="RefLink">Reference: Rational Numbers</span>, <span class="RefLink">Reference: Abelian Number Fields</span>, and <span class="RefLink">Reference: Finite Fields</span>. Row vectors and matrices are described in more detail in Chapters <span class="RefLink">Reference: Row Vectors</span> and <span class="RefLink">Reference: Matrices</span>; note that <strong class="pkg">GAP</strong> supports also linear algebra for objects which are <em>not</em> lists, see Chapter <span class="RefLink">Reference: Vector and Matrix Objects</span>. Vector spaces are described in Chapter <span class="RefLink">Reference: Vector Spaces</span>, further matrix related structures are described in Chapters <span class="RefLink">Reference: Matrix Groups</span>, <span class="RefLink">Reference: Algebras</span>, and <span class="RefLink">Reference: Lie Algebras</span>.</p>
722
723 <p>You will find more list operations in Chapter <span class="RefLink">Reference: Lists</span>.</p>
724
725 <p>Records and functions for records are described in detail in Chapter <span class="RefLink">Reference: Records</span>.</p>
726
727
728 <div class="chlinkprevnextbot">&nbsp;<a href="chap0_mj.html">[Top of Book]</a>&nbsp; <a href="chap0_mj.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap2_mj.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap4_mj.html">[Next Chapter]</a>&nbsp; </div>
729
730
731 <div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a> <a href="chap1_mj.html">1</a> <a href="chap2_mj.html">2</a> <a href="chap3_mj.html">3</a> <a href="chap4_mj.html">4</a> <a href="chap5_mj.html">5</a> <a href="chap6_mj.html">6</a> <a href="chap7_mj.html">7</a> <a href="chap8_mj.html">8</a> <a href="chapBib_mj.html">Bib</a> <a href="chapInd_mj.html">Ind</a> </div>
732
733 <hr />
734 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
735 </body>
736 </html>