Debian Codegraph @

doc/tut/chap2_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 2: A First Session with GAP</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="chap2" 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="chap1_mj.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap3_mj.html">[Next Chapter]</a>&nbsp; </div>
24
25 <p id="mathjaxlink" class="pcenter"><a href="chap2.html">[MathJax off]</a></p>
26 <p><a id="X7A3C7AE07D6848E3" name="X7A3C7AE07D6848E3"></a></p>
27 <div class="ChapSects"><a href="chap2_mj.html#X7A3C7AE07D6848E3">2 <span class="Heading">A First Session with <strong class="pkg">GAP</strong></span></a>
28 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X84063FD486665C97">2.1 <span class="Heading">Starting and Leaving <strong class="pkg">GAP</strong></span></a>
29 </span>
30 </div>
31 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X7B8D5EC27E33AF6D">2.2 <span class="Heading">Loading Source Code from a File</span></a>
32 </span>
33 </div>
34 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X7C7388A683D15599">2.3 <span class="Heading">The Read Evaluate Print Loop</span></a>
35 </span>
36 </div>
37 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X86CA8242832F5F71">2.4 <span class="Heading">Constants and Operators</span></a>
38 </span>
39 </div>
40 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X82F47EA88347AC2D">2.5 <span class="Heading">Variables versus Objects</span></a>
41 </span>
42 </div>
43 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X7F36AE347AD3CDEC">2.6 <span class="Heading">Objects vs. Elements</span></a>
44 </span>
45 </div>
46 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X796E055D86856B42">2.7 <span class="Heading">About Functions</span></a>
47 </span>
48 </div>
49 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X7A7EADB77FF38BC9">2.8 <span class="Heading">Help</span></a>
50 </span>
51 </div>
52 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap2_mj.html#X798515EF7D762548">2.9 <span class="Heading">Further Information introducing the System</span></a>
53 </span>
54 </div>
55 </div>
56
57 <h3>2 <span class="Heading">A First Session with <strong class="pkg">GAP</strong></span></h3>
58
59 <p>This tutorial introduces you to the <strong class="pkg">GAP</strong> system. It is written with users in mind who have just managed to start <strong class="pkg">GAP</strong> for the first time on their computer and want to learn the basic facts about <strong class="pkg">GAP</strong> by playing around with some instructive examples. Therefore, this tutorial contains at many places examples consisting of several lines of input (which you should type on your terminal) followed by the corresponding output ( which <strong class="pkg">GAP</strong> produces as an answer to your input).</p>
60
61 <p>We encourage you to actually run through these examples on your computer. This will support your feeling for <strong class="pkg">GAP</strong> as a tool, which is the leading aim of this tutorial. Do not believe any statement in it as long as you cannot verify it for your own version of <strong class="pkg">GAP</strong>. You will learn to distinguish between small deviations of the behavior of your personal <strong class="pkg">GAP</strong> from the printed examples and serious nonsense.</p>
62
63 <p>Since the printing routines of <strong class="pkg">GAP</strong> are in some sense machine dependent you will for instance encounter a different layout of the printed objects in different environments. But the contents should always be the same. In case you encounter serious nonsense it is highly recommended that you send a bug report to <span class="URL"><a href="mailto:[email protected]">[email protected]</a></span>.</p>
64
65 <p>The examples in this tutorial should explain everything you have to know in order to be able to use <strong class="pkg">GAP</strong>. The reference manual then gives a more systematic treatment of the various types of objects that <strong class="pkg">GAP</strong> can manipulate. It seems desirable neither to start this systematic course with the most elementary (and most boring) structures, nor to confront you with all the complex data types before you know how they are composed from elementary structures. For this reason this tutorial wants to provide you with a basic understanding of <strong class="pkg">GAP</strong> objects, on which the reference manual will then build when it explains everything in detail. So after having mastered this tutorial, you can immediately plunge into the exciting parts of <strong class="pkg">GAP</strong> and only read detailed information about elementary things (in the reference manual) when you really need them.</p>
66
67 <p>Each chapter of this tutorial contains a section with references to the reference manual at the end.</p>
68
69 <p><a id="X84063FD486665C97" name="X84063FD486665C97"></a></p>
70
71 <h4>2.1 <span class="Heading">Starting and Leaving <strong class="pkg">GAP</strong></span></h4>
72
73 <p>If the program is correctly installed then you usually start <strong class="pkg">GAP</strong> by simply typing <code class="code">gap</code> at the prompt of your operating system followed by the <strong class="button">Return</strong> key, sometimes this is also called the <strong class="button">Newline</strong> key.</p>
74
75
76 <div class="example"><pre>
77 $ gap
78 </pre></div>
79
80 <p><strong class="pkg">GAP</strong> answers your request with its beautiful banner and then it shows its own prompt <code class="code">gap&gt;</code> asking you for further input. (You can avoid the banner with the command line option <code class="code">-b</code>; more command line options are described in Section <span class="RefLink">Reference: Command Line Options</span>.)</p>
81
82
83 <div class="example"><pre>
84 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"></span>
85 </pre></div>
86
87 <p>The usual way to end a <strong class="pkg">GAP</strong> session is to type <code class="code">quit;</code> at the <code class="code">gap&gt;</code> prompt. Do not omit the semicolon!</p>
88
89
90 <div class="example"><pre>
91 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">quit;</span>
92 $
93 </pre></div>
94
95 <p>On some systems you could type <strong class="button">Ctrl-D</strong> to yield the same effect. In any situation <strong class="pkg">GAP</strong> is ended by typing <strong class="button">Ctrl-C</strong> twice within a second. Here as always, a combination like <strong class="button">Ctrl-D</strong> means that you have to press the <strong class="button">D</strong> key while you hold down the <strong class="button">Ctrl</strong> key.</p>
96
97 <p>On some systems minor changes might be necessary. This is explained in <strong class="pkg">GAP</strong> installation instructions (see the <code class="file">INSTALL</code> file in the <strong class="pkg">GAP</strong> root directory, or the <strong class="pkg">GAP</strong> website).</p>
98
99 <p>In most places <em>whitespace</em> characters (i.e. <strong class="button">Space</strong>s, <strong class="button">Tab</strong>s and <strong class="button">Return</strong>s) are insignificant for the meaning of <strong class="pkg">GAP</strong> input. Identifiers and keywords must however not contain any whitespace. On the other hand, sometimes there must be whitespace around identifiers and keywords to separate them from each other and from numbers. We will use whitespace to format more complicated commands for better readability.</p>
100
101 <p>A <em>comment</em> in <strong class="pkg">GAP</strong> starts with the symbol <code class="code">#</code> and continues to the end of the line. Comments are treated like whitespace by <strong class="pkg">GAP</strong>. We use comments in the printed examples in this tutorial to explain certain lines of input or output.</p>
102
103 <p><a id="X7B8D5EC27E33AF6D" name="X7B8D5EC27E33AF6D"></a></p>
104
105 <h4>2.2 <span class="Heading">Loading Source Code from a File</span></h4>
106
107 <p>The most convenient way of creating larger pieces of <strong class="pkg">GAP</strong> code is to write them to some text file. For this purpose you can simply use your favorite text editor. You can load such a file into <strong class="pkg">GAP</strong> using the <code class="func">Read</code> (<span class="RefLink">Reference: Read</span>) function:</p>
108
109
110 <div class="example"><pre>
111 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Read("../../GAPProgs/Example.g");</span>
112 </pre></div>
113
114 <p>You can either give the full absolute path name of the source file or its relative path name from the current directory.</p>
115
116 <p><a id="X7C7388A683D15599" name="X7C7388A683D15599"></a></p>
117
118 <h4>2.3 <span class="Heading">The Read Evaluate Print Loop</span></h4>
119
120 <p><strong class="pkg">GAP</strong> is an interactive system. It continuously executes a read evaluate print loop. Each expression you type at the keyboard is read by <strong class="pkg">GAP</strong>, evaluated, and then the result is shown.</p>
121
122 <p>The interactive nature of <strong class="pkg">GAP</strong> allows you to type an expression at the keyboard and see its value immediately. You can define a function and apply it to arguments to see how it works. You may even write whole programs containing lots of functions and test them without leaving the program.</p>
123
124 <p>When your program is large it will be more convenient to write it on a file and then read that file into <strong class="pkg">GAP</strong>. Preparing your functions in a file has several advantages. You can compose your functions more carefully in a file (with your favorite text editor), you can correct errors without retyping the whole function and you can keep a copy for later use. Moreover you can write lots of comments into the program text, which are ignored by <strong class="pkg">GAP</strong>, but are very useful for human readers of your program text. <strong class="pkg">GAP</strong> treats input from a file in the same way that it treats input from the keyboard. Further details can be found in section <code class="func">Read</code> (<span class="RefLink">Reference: Read</span>).</p>
125
126 <p>A simple calculation with <strong class="pkg">GAP</strong> is as easy as one can imagine. You type the problem just after the prompt, terminate it with a semicolon and then pass the problem to the program with the <strong class="button">Return</strong> key. For example, to multiply the difference between 9 and 7 by the sum of 5 and 6, that is to calculate <span class="SimpleMath">\((9 - 7) * (5 + 6)\)</span>, you type exactly this last sequence of symbols followed by <code class="code">;</code> and <strong class="button">Return</strong>.</p>
127
128
129 <div class="example"><pre>
130 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(9 - 7) * (5 + 6);</span>
131 22
132 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"></span>
133 </pre></div>
134
135 <p>Then <strong class="pkg">GAP</strong> echoes the result 22 on the next line and shows with the prompt that it is ready for the next problem. Henceforth, we will no longer print this additional prompt.</p>
136
137 <p>If you make a mistake while typing the line, but <em>before</em> typing the final <strong class="button">Return</strong>, you can use the <strong class="button">Delete</strong> key (or sometimes <strong class="button">Backspace</strong> key) to delete the last typed character. You can also move the cursor back and forward in the line with <strong class="button">Ctrl-B</strong> and <strong class="button">Ctrl-F</strong> and insert or delete characters anywhere in the line. The line editing commands are fully described in section <span class="RefLink">Reference: Line Editing</span>.</p>
138
139 <p>If you did omit the semicolon at the end of the line but have already typed <strong class="button">Return</strong>, then <strong class="pkg">GAP</strong> has read everything you typed, but does not know that the command is complete. The program is waiting for further input and indicates this with a partial prompt <code class="code">&gt;</code>. This problem is solved by simply typing the missing semicolon on the next line of input. Then the result is printed and the normal prompt returns.</p>
140
141
142 <div class="example"><pre>
143 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(9 - 7) * (5 + 6)</span>
144 <span class="GAPprompt">&gt;</span> <span class="GAPinput">;</span>
145 22
146 </pre></div>
147
148 <p>So the input can consist of several lines, and <strong class="pkg">GAP</strong> prints a partial prompt <code class="code">&gt;</code> in each input line except the first, until the command is completed with a semicolon. (<strong class="pkg">GAP</strong> may already evaluate part of the input when <strong class="button">Return</strong> is typed, so for long calculations it might take some time until the partial prompt appears.) Whenever you see the partial prompt and you cannot decide what <strong class="pkg">GAP</strong> is still waiting for, then you have to type semicolons until the normal prompt returns. In every situation the exact meaning of the prompt <code class="code">gap&gt;</code> is that the program is waiting for a new problem.</p>
149
150 <p>But even if you mistyped the command more seriously, you do not have to type it all again. Suppose you mistyped or forgot the last closing parenthesis. Then your command is syntactically incorrect and <strong class="pkg">GAP</strong> will notice it, incapable of computing the desired result.</p>
151
152
153 <div class="example"><pre>
154 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(9 - 7) * (5 + 6;</span>
155 Syntax error: ) expected
156 (9 - 7) * (5 + 6;
157 ^
158 </pre></div>
159
160 <p>Instead of the result an error message occurs indicating the place where an unexpected symbol occurred with an arrow sign <code class="code">^</code> under it. As a computer program cannot know what your intentions really were, this is only a hint. But in this case <strong class="pkg">GAP</strong> is right by claiming that there should be a closing parenthesis before the semicolon. Now you can type <strong class="button">Ctrl-P</strong> to recover the last line of input. It will be written after the prompt with the cursor in the first position. Type <strong class="button">Ctrl-E</strong> to take the cursor to the end of the line, then <strong class="button">Ctrl-B</strong> to move the cursor one character back. The cursor is now on the position of the semicolon. Enter the missing parenthesis by simply typing <code class="code">)</code>. Now the line is correct and may be passed to <strong class="pkg">GAP</strong> by hitting the <strong class="button">Return</strong> key. Note that for this action it is not necessary to move the cursor past the last character of the input line.</p>
161
162 <p>Each line of commands you type is sent to <strong class="pkg">GAP</strong> for evaluation by pressing <strong class="button">Return</strong> regardless of the position of the cursor in that line. We will no longer mention the <strong class="button">Return</strong> key from now on.</p>
163
164 <p>Sometimes a syntax error will cause <strong class="pkg">GAP</strong> to enter a <em>break loop</em>. This is indicated by the special prompt <code class="code">brk&gt;</code>. If another syntax error occurs while <strong class="pkg">GAP</strong> is in a break loop, the prompt will change to <code class="code">brk_2&gt;</code>, <code class="code">brk_3&gt;</code> and so on. You can leave the current break loop and exit to the next outer one by either typing <code class="code">quit;</code> or by hitting <strong class="button">Ctrl-D</strong>. Eventually <strong class="pkg">GAP</strong> will return to its normal state and show its normal prompt <code class="code">gap&gt;</code> again.</p>
165
166 <p><a id="X86CA8242832F5F71" name="X86CA8242832F5F71"></a></p>
167
168 <h4>2.4 <span class="Heading">Constants and Operators</span></h4>
169
170 <p>In an expression like <code class="code">(9 - 7) * (5 + 6)</code> the constants <code class="code">5</code>, <code class="code">6</code>, <code class="code">7</code>, and <code class="code">9</code> are being composed by the operators <code class="code">+</code>, <code class="code">*</code> and <code class="code">-</code> to result in a new value.</p>
171
172 <p>There are three kinds of operators in <strong class="pkg">GAP</strong>, arithmetical operators, comparison operators, and logical operators. You have already seen that it is possible to form the sum, the difference, and the product of two integer values. There are some more operators applicable to integers in <strong class="pkg">GAP</strong>. Of course integers may be divided by each other, possibly resulting in noninteger rational values.</p>
173
174
175 <div class="example"><pre>
176 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">12345/25;</span>
177 2469/5
178 </pre></div>
179
180 <p>Note that the numerator and denominator are divided by their greatest common divisor and that the result is uniquely represented as a division instruction.</p>
181
182 <p>The next self-explanatory example demonstrates negative numbers.</p>
183
184
185 <div class="example"><pre>
186 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">-3; 17 - 23;</span>
187 -3
188 -6
189 </pre></div>
190
191 <p>The exponentiation operator is written as <code class="code">^</code>. This operation in particular might lead to very large numbers. This is no problem for <strong class="pkg">GAP</strong> as it can handle numbers of (almost) any size.</p>
192
193
194 <div class="example"><pre>
195 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">3^132;</span>
196 955004950796825236893190701774414011919935138974343129836853841
197 </pre></div>
198
199 <p>The <code class="keyw">mod</code> operator allows you to compute one value modulo another.</p>
200
201
202 <div class="example"><pre>
203 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">17 mod 3;</span>
204 2
205 </pre></div>
206
207 <p>Note that there must be whitespace around the keyword <code class="keyw">mod</code> in this example since <code class="code">17mod3</code> or <code class="code">17mod</code> would be interpreted as identifiers. The whitespace around operators that do not consist of letters, e.g., the operators <code class="code">*</code> and <code class="code">-</code>, is not necessary.</p>
208
209 <p><strong class="pkg">GAP</strong> knows a precedence between operators that may be overridden by parentheses.</p>
210
211
212 <div class="example"><pre>
213 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(9 - 7) * 5 = 9 - 7 * 5;</span>
214 false
215 </pre></div>
216
217 <p>Besides these arithmetical operators there are comparison operators in <strong class="pkg">GAP</strong>. A comparison results in a <em>boolean value</em> which is another kind of constant. The comparison operators <code class="code">=</code>, <code class="code">&lt;&gt;</code>, <code class="code">&lt;</code>, <code class="code">&lt;=</code>, <code class="code">&gt;</code> and <code class="code">&gt;=</code>, test for equality, inequality, less than, less than or equal, greater than and greater than or equal, respectively.</p>
218
219
220 <div class="example"><pre>
221 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">10^5 &lt; 10^4;</span>
222 false
223 </pre></div>
224
225 <p>The boolean values <code class="keyw">true</code> and <code class="keyw">false</code> can be manipulated via logical operators, i. e., the unary operator <code class="keyw">not</code> and the binary operators <code class="keyw">and</code> and <code class="keyw">or</code>. Of course boolean values can be compared, too.</p>
226
227
228 <div class="example"><pre>
229 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">not true; true and false; true or false;</span>
230 false
231 false
232 true
233 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">10 &gt; 0 and 10 &lt; 100;</span>
234 true
235 </pre></div>
236
237 <p>Another important type of constants in <strong class="pkg">GAP</strong> are <em>permutations</em>. They are written in cycle notation and they can be multiplied.</p>
238
239
240 <div class="example"><pre>
241 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(1,2,3);</span>
242 (1,2,3)
243 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(1,2,3) * (1,2);</span>
244 (2,3)
245 </pre></div>
246
247 <p>The inverse of the permutation <code class="code">(1,2,3)</code> is denoted by <code class="code">(1,2,3)^-1</code>. Moreover the caret operator <code class="code">^</code> is used to determine the image of a point under a permutation and to conjugate one permutation by another.</p>
248
249
250 <div class="example"><pre>
251 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(1,2,3)^-1;</span>
252 (1,3,2)
253 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">2^(1,2,3);</span>
254 3
255 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(1,2,3)^(1,2);</span>
256 (1,3,2)
257 </pre></div>
258
259 <p>The various other constants that <strong class="pkg">GAP</strong> can deal with will be introduced when they are used, for example there are elements of finite fields such as <code class="code">Z(8)</code>, and complex roots of unity such as <code class="code">E(4)</code>.</p>
260
261 <p>The last type of constants we want to mention here are the <em>characters</em>, which are simply objects in <strong class="pkg">GAP</strong> that represent arbitrary characters from the character set of the operating system. Character literals can be entered in <strong class="pkg">GAP</strong> by enclosing the character in <em>singlequotes</em> <code class="code">'</code>.</p>
262
263
264 <div class="example"><pre>
265 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">'a';</span>
266 'a'
267 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">'*';</span>
268 '*'
269 </pre></div>
270
271 <p>There are no operators defined for characters except that characters can be compared.</p>
272
273 <p>In this section you have seen that values may be preceded by unary operators and combined by binary operators placed between the operands. There are rules for precedence which may be overridden by parentheses. A comparison results in a boolean value. Boolean values are combined via logical operators. Moreover you have seen that <strong class="pkg">GAP</strong> handles numbers of arbitrary size. Numbers and boolean values are constants. There are other types of constants in <strong class="pkg">GAP</strong> like permutations. You are now in a position to use <strong class="pkg">GAP</strong> as a simple desktop calculator.</p>
274
275 <p><a id="X82F47EA88347AC2D" name="X82F47EA88347AC2D"></a></p>
276
277 <h4>2.5 <span class="Heading">Variables versus Objects</span></h4>
278
279 <p>The constants described in the last section are specified by certain combinations of digits and minus signs (in the case of integers) or digits, commas and parentheses (in the case of permutations). These sequences of characters always have the same meaning to <strong class="pkg">GAP</strong>. On the other hand, there are <em>variables</em>, specified by a sequence of letters and digits (including at least one letter), and their meaning depends on what has been assigned to them. An <em>assignment</em> is done by a <strong class="pkg">GAP</strong> command <code class="code"><var class="Arg">sequence_of_letters_and_digits</var> := <var class="Arg">meaning</var></code>, where the sequence on the left hand side is called the <em>identifier</em> of the variable and it serves as its name. The meaning on the right hand side can be a constant like an integer or a permutation, but it can also be almost any other <strong class="pkg">GAP</strong> object. From now on, we will use the term <em>object</em> to denote something that can be assigned to a variable.</p>
280
281 <p>There must be no whitespace between the <code class="code">:</code> and the <code class="code">=</code> in the assignment operator. Also do not confuse the assignment operator with the single equality sign <code class="code">=</code> which in <strong class="pkg">GAP</strong> is only used for the test of equality.</p>
282
283
284 <div class="example"><pre>
285 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= (9 - 7) * (5 + 6);</span>
286 22
287 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a;</span>
288 22
289 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a * (a + 1);</span>
290 506
291 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a = 10;</span>
292 false
293 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= 10;</span>
294 10
295 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a * (a + 1);</span>
296 110
297 </pre></div>
298
299 <p>After an assignment the assigned object is echoed on the next line. The printing of the object of a statement may be in every case prevented by typing a double semicolon.</p>
300
301
302 <div class="example"><pre>
303 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">w:= 2;;</span>
304 </pre></div>
305
306 <p>After the assignment the variable evaluates to that object if evaluated. Thus it is possible to refer to that object by the name of the variable in any situation.</p>
307
308 <p>This is in fact the whole secret of an assignment. An identifier is bound to an object and from this moment points to that object. Nothing more. This binding is changed by the next assignment to that identifier. An identifier does not denote a block of memory as in some other programming languages. It simply points to an object, which has been given its place in memory by the <strong class="pkg">GAP</strong> storage manager. This place may change during a <strong class="pkg">GAP</strong> session, but that doesn't bother the identifier. <em>The identifier points to the object, not to a place in the memory.</em></p>
309
310 <p>For the same reason it is not the identifier that has a type but the object. This means on the other hand that the identifier <code class="code">a</code> which now is bound to an integer object may in the same session point to any other object regardless of its type.</p>
311
312 <p>Identifiers may be sequences of letters and digits containing at least one letter. For example <code class="code">abc</code> and <code class="code">a0bc1</code> are valid identifiers. But also <code class="code">123a</code> is a valid identifier as it cannot be confused with any number. Just <code class="code">1234</code> indicates the number 1234 and cannot be at the same time the name of a variable.</p>
313
314 <p>Since <strong class="pkg">GAP</strong> distinguishes upper and lower case, <code class="code">a1</code> and <code class="code">A1</code> are different identifiers. Keywords such as <code class="keyw">quit</code> must not be used as identifiers. You will see more keywords in the following sections.</p>
315
316 <p>In the remaining part of this manual we will ignore the difference between variables, their names (identifiers), and the objects they point to. It may be useful to think from time to time about what is really meant by terms such as <q>the integer <code class="code">w</code></q>.</p>
317
318 <p>There are some predefined variables coming with <strong class="pkg">GAP</strong>. Many of them you will find in the remaining chapters of this manual, since functions are also referred to via identifiers.</p>
319
320 <p>You can get an overview of <em>all</em> <strong class="pkg">GAP</strong> variables by entering <code class="code">NamesGVars()</code>. Many of these are predefined. If you are interested in the variables you have defined yourself in the current <strong class="pkg">GAP</strong> session, you can enter <code class="code">NamesUserGVars()</code>.</p>
321
322
323 <div class="example"><pre>
324 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NamesUserGVars();</span>
325 [ "a", "w" ]
326 </pre></div>
327
328 <p>This seems to be the right place to state the following rule: The name of every global variable in the <strong class="pkg">GAP</strong> library starts with a <em>capital letter</em>. Thus if you choose only names starting with a small letter for your own variables you will not attempt to overwrite any predefined variable. (Note that most of the predefined variables are read-only, and trying to change their values will result in an error message.)</p>
329
330 <p>There are some further interesting variables one of which will be introduced now.</p>
331
332 <p>Whenever <strong class="pkg">GAP</strong> returns an object by printing it on the next line this object is assigned to the variable <code class="code">last</code>. So if you computed</p>
333
334
335 <div class="example"><pre>
336 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">(9 - 7) * (5 + 6);</span>
337 22
338 </pre></div>
339
340 <p>and forgot to assign the object to the variable <code class="code">a</code> for further use, you can still do it by the following assignment.</p>
341
342
343 <div class="example"><pre>
344 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= last;</span>
345 22
346 </pre></div>
347
348 <p>Moreover there are variables <code class="code">last2</code> and <code class="code">last3</code>, you can guess their values.</p>
349
350 <p>In this section you have seen how to assign objects to variables. These objects can later be accessed through the name of the variable, its identifier. You have also encountered the useful concept of the <code class="code">last</code> variables storing the latest returned objects. And you have learned that a double semicolon prevents the result of a statement from being printed.</p>
351
352 <p><a id="X7F36AE347AD3CDEC" name="X7F36AE347AD3CDEC"></a></p>
353
354 <h4>2.6 <span class="Heading">Objects vs. Elements</span></h4>
355
356 <p>In the last section we mentioned that every object is given a certain place in memory by the <strong class="pkg">GAP</strong> storage manager (although that place may change in the course of a <strong class="pkg">GAP</strong> session). In this sense, objects at different places in memory are never equal, and if the object pointed to by the variable <code class="code">a</code> (to be more precise, the variable with identifier <code class="code">a</code>) is equal to the object pointed to by the variable <code class="code">b</code>, then we should better say that they are not only equal but <em>identical</em>. <strong class="pkg">GAP</strong> provides the function <code class="func">IsIdenticalObj</code> (<span class="RefLink">Reference: IsIdenticalObj</span>) to test whether this is the case.</p>
357
358
359 <div class="example"><pre>
360 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= (1,2);; IsIdenticalObj( a, a );</span>
361 true
362 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">b:= (1,2);; IsIdenticalObj( a, b );</span>
363 false
364 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">b:= a;; IsIdenticalObj( a, b );</span>
365 true
366 </pre></div>
367
368 <p>As the above example indicates, <strong class="pkg">GAP</strong> objects <var class="Arg">a</var> and <var class="Arg">b</var> can be unequal although they are equal from a mathematical point of view, i.e., although we should have <var class="Arg">a</var> = <var class="Arg">b</var>. It may be that the objects <var class="Arg">a</var> and <var class="Arg">b</var> are stored in different places in memory, or it may be that we have an equivalence relation defined on the set of objects under which <var class="Arg">a</var> and <var class="Arg">b</var> belong to the same equivalence class. For example, if <span class="SimpleMath">\(\textit{a} = x^3\)</span> and <span class="SimpleMath">\(\textit{b} = x^{{-5}}\)</span> are words in the finitely presented group <span class="SimpleMath">\(\langle x \mid x^2 = 1 \rangle\)</span>, we would have <var class="Arg">a</var> = <var class="Arg">b</var> in that group.</p>
369
370 <p><strong class="pkg">GAP</strong> uses the equality operator <code class="code">=</code> to denote such a mathematical equality, <em>not</em> the identity of objects. Hence we often have <code class="code"><var class="Arg">a</var> = <var class="Arg">b</var></code> although <code class="code">IsIdenticalObj( <var class="Arg">a</var>, <var class="Arg">b</var> ) = false</code>. The operator <code class="code">=</code> defines an equivalence relation on the set of all <strong class="pkg">GAP</strong> objects, and we call the corresponding equivalence classes <em>elements</em>. Phrasing it differently, the same element may be represented by various <strong class="pkg">GAP</strong> objects.</p>
371
372 <p>Non-trivial examples of elements that are represented by different objects (objects that really look different, not ones that are merely stored in different memory places) will occur only when we will be considering composite objects such as lists or domains.</p>
373
374 <p><a id="X796E055D86856B42" name="X796E055D86856B42"></a></p>
375
376 <h4>2.7 <span class="Heading">About Functions</span></h4>
377
378 <p>A program written in the <strong class="pkg">GAP</strong> language is called a <em>function</em>. Functions are special <strong class="pkg">GAP</strong> objects. Most of them behave like mathematical functions. They are applied to objects and will return a new object depending on the input. The function <code class="func">Factorial</code> (<span class="RefLink">Reference: Factorial</span>), for example, can be applied to an integer and will return the factorial of this integer.</p>
379
380
381 <div class="example"><pre>
382 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Factorial(17);</span>
383 355687428096000
384 </pre></div>
385
386 <p>Applying a function to arguments means to write the arguments in parentheses following the function. Several arguments are separated by commas, as for the function <code class="func">Gcd</code> (<span class="RefLink">Reference: Gcd</span>) which computes the greatest common divisor of two integers.</p>
387
388
389 <div class="example"><pre>
390 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Gcd(1234, 5678);</span>
391 2
392 </pre></div>
393
394 <p>There are other functions that do not return an object but only produce a side effect, for example changing one of their arguments. These functions are sometimes called procedures. The function <code class="func">Print</code> (<span class="RefLink">Reference: Print</span>) is only called for the side effect of printing something on the screen.</p>
395
396
397 <div class="example"><pre>
398 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Print(1234, "\n");</span>
399 1234
400 </pre></div>
401
402 <p>In order to be able to compose arbitrary text with <code class="func">Print</code> (<span class="RefLink">Reference: Print</span>), this function itself will not produce a line break after printing. Thus we had another newline character <code class="code">"\n"</code> printed to start a new line.</p>
403
404 <p>Some functions will both change an argument and return an object such as the function <code class="func">Sortex</code> (<span class="RefLink">Reference: Sortex</span>) that sorts a list and returns the permutation of the list elements that it has performed. You will not understand right now what it means to change an object. We will return to this subject several times in the next sections.</p>
405
406 <p>A comfortable way to define a function yourself is the <em>maps-to</em> operator <code class="code">-&gt;</code> consisting of a minus sign and a greater sign with no whitespace between them. The function <code class="code">cubed</code> which maps a number to its cube is defined on the following line.</p>
407
408
409 <div class="example"><pre>
410 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">cubed:= x -&gt; x^3;</span>
411 function( x ) ... end
412 </pre></div>
413
414 <p>After the function has been defined, it can now be applied.</p>
415
416
417 <div class="example"><pre>
418 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">cubed(5);</span>
419 125
420 </pre></div>
421
422 <p>More complicated functions, especially functions with more than one argument cannot be defined in this way. You will see how to write your own <strong class="pkg">GAP</strong> functions in Section <a href="chap4_mj.html#X7D81680E7BB09F45"><span class="RefLink">4.1</span></a>.</p>
423
424 <p>In this section you have seen <strong class="pkg">GAP</strong> objects of type function. You have learned how to apply a function to arguments. This yields as result a new object or a side effect. A side effect may change an argument of the function. Moreover you have seen an easy way to define a function in <strong class="pkg">GAP</strong> with the maps-to operator.</p>
425
426 <p><a id="X7A7EADB77FF38BC9" name="X7A7EADB77FF38BC9"></a></p>
427
428 <h4>2.8 <span class="Heading">Help</span></h4>
429
430 <p>The content of the <strong class="pkg">GAP</strong> manuals is also available as on-line help. A <strong class="pkg">GAP</strong> session loads a long list of index entries. This typically contains all chapter and section headers, all names of documented functions, operations and so on, as well as some explicit index entries defined in the manuals.</p>
431
432 <p>The format of a query is as follows.</p>
433
434 <p><code class="code">?[<var class="Arg">book</var>:][?]<var class="Arg">topic</var></code></p>
435
436 <p>A simple example would be to type <code class="code">?help</code> at the <strong class="pkg">GAP</strong> prompt. If there is a single section with index entry <var class="Arg">topic</var> then this is displayed directly.</p>
437
438 <p>If there are several matches you get an overview like in the example below.</p>
439
440
441 <div class="example"><pre>
442 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">?sets</span>
443 Help: several entries match this topic - type ?2 to get match [2]
444
445 [1] Tutorial: Sets
446 [2] Reference: Sets
447 [3] Reference: sets
448 [4] Reference: Sets of Subgroups
449 [5] Reference: setstabilizer
450 </pre></div>
451
452 <p><strong class="pkg">GAP</strong>'s manuals consist of several <em>books</em>, which are indicated before the colon in the list above. A help query can be restricted to one book by using the optional <var class="Arg">book</var>: part. For example <code class="code">?tut : sets</code> will display the first of these help sections. More precisely, the parts of the string <var class="Arg">book</var> which are separated by white space are interpreted as beginnings of the first words in the name of the book. Try <code class="code">?books</code> to see the list of available books and their names.</p>
453
454 <p>The search for a matching <var class="Arg">topic</var> (and optional <var class="Arg">book</var>) is done <em>case insensitively</em>. If there is another <code class="code">?</code> before the <var class="Arg">topic</var>, then a <em>substring search</em> for <var class="Arg">topic</var> is performed on all index entries. Otherwise the parts of <var class="Arg">topic</var> which are separated by white space are considered as <em>beginnings of the first words</em> in an index entry.</p>
455
456 <p>White space is normalized in the search string (and the index entries).</p>
457
458 <p><em>Examples.</em> All the following queries lead to the chapter of the reference manual which explains the use of <strong class="pkg">GAP</strong>'s help system in more detail.</p>
459
460
461 <div class="example"><pre>
462 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">?Reference: The Help System</span>
463 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">? REF : t h s</span>
464 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">?ref:? help system</span>
465 </pre></div>
466
467 <p>The query <code class="code">??sets</code> shows all help sections in all books whose index entries contain the substring <code class="code">sets</code>.</p>
468
469 <p>As mentioned in the example above a complete list of commands for the help system is available in Section <code class="code">?Ref: The Help System</code> of the reference manual. In particular there are commands to browse through the help sections, see <code class="code">?Ref: Browsing through the Sections</code> and there is a way to influence the way <em>how</em> the help sections are displayed, see <code class="code">?Ref: SetHelpViewer</code>. For example you can use an external pager program, a Web browser, <code class="code">dvi</code>-previewer and/or <code class="code">pdf</code>-viewer for reading <strong class="pkg">GAP</strong>'s online help.</p>
470
471 <p><a id="X798515EF7D762548" name="X798515EF7D762548"></a></p>
472
473 <h4>2.9 <span class="Heading">Further Information introducing the System</span></h4>
474
475 <p>For large amounts of input data, it might be advisable to write your input first into a file, and then read this into <strong class="pkg">GAP</strong>; see <code class="func">Read</code> (<span class="RefLink">Reference: Read</span>), <code class="func">Edit</code> (<span class="RefLink">Reference: Edit</span>) for this.</p>
476
477 <p>The definition of the <strong class="pkg">GAP</strong> syntax can be looked up in Chapter <span class="RefLink">Reference: The Programming Language</span>. A complete list of command line editing facilities is found in Section <span class="RefLink">Reference: Line Editing</span>. The break loop is described in Section <span class="RefLink">Reference: Break Loops</span>.</p>
478
479 <p>Operators are explained in more detail in Sections <span class="RefLink">Reference: Expressions</span> and <span class="RefLink">Reference: Comparisons</span>. You will find more information about boolean values in Chapters <span class="RefLink">Reference: Booleans</span> and <span class="RefLink">Reference: Boolean Lists</span>. Permutations are described in Chapter <span class="RefLink">Reference: Permutations</span> and characters in Chapter <span class="RefLink">Reference: Strings and Characters</span>.</p>
480
481 <p>Variables and assignments are described in more detail in <span class="RefLink">Reference: Variables</span> and <span class="RefLink">Reference: Assignments</span>. A complete list of keywords is contained in <span class="RefLink">Reference: Keywords</span>.</p>
482
483 <p>More about functions can be found in <span class="RefLink">Reference: Function Calls</span> and <span class="RefLink">Reference: Procedure Calls</span>.</p>
484
485
486 <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="chap1_mj.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap3_mj.html">[Next Chapter]</a>&nbsp; </div>
487
488
489 <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>
490
491 <hr />
492 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
493 </body>
494 </html>