CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

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

| Download

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

Views: 418346
1
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
2
<!-- %% -->
3
<!-- %W introduc.tex GAP documentation Thomas Breuer -->
4
<!-- %W & Frank Celler -->
5
<!-- %W & Martin Schönert -->
6
<!-- %W & Heiko Theißen -->
7
<!-- %% -->
8
<!-- %H @(#)<M>Id: introduc.tex,v 4.32 2005/05/05 13:22:50 gap Exp </M> -->
9
<!-- %% -->
10
<!-- %Y Copyright 1997, Lehrstuhl D für Mathematik, RWTH Aachen, Germany -->
11
<!-- %% -->
12
<!-- %% This file contains a tutorial introduction to GAP. -->
13
<!-- %% -->
14
<P/>
15
16
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
17
<Chapter Label="A First Session with GAP">
18
<Heading>A First Session with &GAP;</Heading>
19
<P/>
20
This tutorial introduces you to the &GAP; system. It is written with
21
users in mind who have just managed to start &GAP; for the first time on
22
their computer and want to learn the basic facts about &GAP; by playing
23
around with some instructive examples. Therefore, this tutorial contains
24
at many places examples consisting of several lines of input (which
25
you should type on your terminal) followed by the corresponding output (
26
which &GAP; produces as an answer to your input).
27
<P/>
28
We encourage you to actually run through these examples on your
29
computer. This will support your feeling for &GAP; as a tool, which is
30
the leading aim of this tutorial. Do not believe any statement in it as
31
long as you cannot verify it for your own version of &GAP;. You will
32
learn to distinguish between small deviations of the behavior of your
33
personal &GAP; from the printed examples and serious nonsense.
34
<P/>
35
Since the printing routines of &GAP; are in some sense machine dependent
36
you will for instance encounter a different layout of the printed objects
37
in different environments. But the contents should always be the same.
38
In case you encounter serious nonsense it is highly recommended that you
39
send a bug report to <Email>[email protected]</Email>.
40
<P/>
41
The examples in this tutorial should explain everything you have to
42
know in order to be able to use &GAP;. The reference manual then
43
gives a more systematic treatment of the various types of objects that
44
&GAP; can manipulate. It seems desirable neither to start this
45
systematic course with the most elementary (and most boring)
46
structures, nor to confront you with all the complex data types before
47
you know how they are composed from elementary structures. For this
48
reason this tutorial wants to provide you with a basic understanding
49
of &GAP; objects, on which the reference manual will then build when
50
it explains everything in detail. So after having mastered this
51
tutorial, you can immediately plunge into the exciting parts of &GAP;
52
and only read detailed information about elementary things (in the
53
reference manual) when you really need them.
54
<P/>
55
Each chapter of this tutorial contains a section with references to the
56
reference manual at the end.
57
<P/>
58
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
59
<Section Label="Starting and Leaving GAP">
60
<Heading>Starting and Leaving &GAP;</Heading>
61
<P/>
62
<Index Key="starting GAP">starting &GAP;</Index>
63
<Index Key="leaving GAP">leaving &GAP;</Index>
64
<Index Key="quit"><K>quit</K></Index>
65
If the program is correctly installed then you usually start &GAP; by
66
simply
67
typing <C>gap</C> at the prompt of your operating system followed by the
68
<B>Return</B> key, sometimes this is also called the <B>Newline</B> key.
69
<P/>
70
<Log><![CDATA[
71
$ gap
72
]]></Log>
73
<P/>
74
&GAP; answers your request with its beautiful banner and then it shows
75
its own prompt <C>gap></C> asking you for further input.
76
(You can avoid the banner with the command line option <C>-b</C>;
77
more command line options are described in
78
Section&nbsp;<Ref Sect="Command Line Options" BookName="ref"/>.)
79
<P/>
80
<Example><![CDATA[
81
gap>
82
]]></Example>
83
<P/>
84
The usual way to end a &GAP; session is to type <C>quit;</C> at the <C>gap></C>
85
prompt. Do not omit the semicolon!
86
<P/>
87
<Log><![CDATA[
88
gap> quit;
89
$
90
]]></Log>
91
<P/>
92
On some systems you could type <B>Ctrl-D</B> to yield the same effect.
93
In any situation &GAP; is ended by typing <B>Ctrl-C</B> twice within a
94
second. Here as always, a combination like <B>Ctrl-D</B> means that you have
95
to press the <B>D</B> key while you hold down the <B>Ctrl</B> key.
96
<P/>
97
On some systems (for example the Apple Macintosh) minor changes might be
98
necessary. This is explained in &GAP; installation instructions (see the
99
<F>INSTALL</F> file in the &GAP; root directory, or the &GAP; website).
100
<!-- TODO: put URL -->
101
<P/>
102
<Index>whitespace</Index>
103
In most places <E>whitespace</E> characters (i.e. <B>Space</B>s, <B>Tab</B>s
104
and <B>Return</B>s) are insignificant for the meaning of &GAP; input.
105
Identifiers and keywords must however not contain any whitespace.
106
On the other hand,
107
sometimes there must be whitespace around identifiers and keywords to
108
separate them from each other and from numbers. We will use whitespace to
109
format more complicated commands for better readability.
110
<P/>
111
<Index>comments</Index>
112
A <E>comment</E> in &GAP; starts with the symbol <C>#</C> and continues to the
113
end of the line. Comments are treated like whitespace by &GAP;. We use
114
comments in the printed examples in this tutorial to explain certain
115
lines of input or output.
116
<!-- % These comments will be printed in a normal font for better readability, -->
117
<!-- % they start with the symbol&nbsp;#. -->
118
</Section>
119
120
121
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
122
<Section Label="Loading Source Code from a File">
123
<Heading>Loading Source Code from a File</Heading>
124
125
<Index>loading source code from a file</Index>
126
<Index>reading source code from a file</Index>
127
<Index Key="Read"><C>Read</C></Index>
128
The most convenient way of creating larger pieces of &GAP; code is to
129
write them to some text file.
130
For this purpose you can simply use your favorite text editor.
131
You can load such a file into &GAP; using the
132
<Ref Func="Read" BookName="ref"/> function:
133
<P/>
134
<Log><![CDATA[
135
gap> Read("../../GAPProgs/Example.g");
136
]]></Log>
137
<P/>
138
You can either give the full absolute path name of the source file or
139
its relative path name from the &GAP; root directory (the directory
140
containing <F>bin/</F>, <F>doc/</F>, <F>lib/</F>, etc.).
141
142
</Section>
143
144
145
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
146
<Section Label="The Read Evaluate Print Loop">
147
<Heading>The Read Evaluate Print Loop</Heading>
148
149
<Index>read evaluate print loop</Index>
150
&GAP; is an interactive system. It continuously executes a
151
read evaluate print loop. Each expression you type at the keyboard is
152
read by &GAP;, evaluated, and then the result is shown.
153
<P/>
154
The interactive nature of &GAP; allows you to type an expression at the
155
keyboard and see its value immediately. You can define a function and
156
apply it to arguments to see how it works. You may even write whole
157
programs containing lots of functions and test them without leaving the
158
program.
159
<P/>
160
When your program is large it will be more convenient to write it on a
161
file and then read that file into &GAP;. Preparing your functions in a
162
file has several advantages. You can compose your functions more
163
carefully in a file (with your favorite text editor), you can correct
164
errors without retyping the whole function and you can keep a copy for
165
later use. Moreover you can write lots of comments into the program text,
166
which are ignored by &GAP;, but are very useful for human readers of
167
your program text. &GAP; treats input from a file in the same way that
168
it treats input from the keyboard. Further details can be found in
169
section <Ref Func="Read" BookName="ref"/>.
170
<P/>
171
A simple calculation with &GAP; is as easy as one can imagine. You type
172
the problem just after the prompt, terminate it with a semicolon and then
173
pass the problem to the program with the <B>Return</B> key. For example, to
174
multiply the difference between 9 and 7 by the sum of 5 and 6, that is to
175
calculate <M>(9 - 7) * (5 + 6)</M>, you type exactly this last sequence of
176
symbols followed by <C>;</C> and <B>Return</B>.
177
<P/>
178
<Example><![CDATA[
179
gap> (9 - 7) * (5 + 6);
180
22
181
gap>
182
]]></Example>
183
<P/>
184
Then &GAP; echoes the result 22 on the next line and shows with the
185
prompt that it is ready for the next problem. Henceforth, we will no
186
longer print this additional prompt.
187
<P/>
188
<Index>line editing</Index>
189
If you make a mistake while typing the line,
190
but <E>before</E> typing the final <B>Return</B>,
191
you can use the <B>Delete</B> key (or sometimes <B>Backspace</B> key)
192
to delete the last typed character.
193
You can also move the cursor back and forward in the line with <B>Ctrl-B</B>
194
and <B>Ctrl-F</B> and insert or delete characters anywhere in the line.
195
The line editing commands are fully described
196
in section&nbsp;<Ref Sect="Line Editing" BookName="ref"/>.
197
<P/>
198
If you did omit the semicolon at the end of the line but have already
199
typed <B>Return</B>, then &GAP; has read everything you typed, but does
200
not know that the command is complete. The program is waiting for
201
further input and indicates this with a partial prompt <C>></C>. This
202
problem is solved by simply typing the missing semicolon on the next
203
line of input. Then the result is printed and the normal prompt
204
returns.
205
<P/>
206
<Example><![CDATA[
207
gap> (9 - 7) * (5 + 6)
208
> ;
209
22
210
]]></Example>
211
<P/>
212
So the input can consist of several lines, and &GAP; prints a partial
213
prompt <C>></C> in each input line except the first, until the command is
214
completed with a semicolon.
215
(&GAP; may already evaluate part of the input when <B>Return</B> is typed,
216
so for long calculations it might take some time until the partial prompt
217
appears.)
218
Whenever you see the partial prompt and you cannot decide what &GAP; is
219
still waiting for, then you have to type semicolons until the normal
220
prompt returns.
221
In every situation the exact meaning of the prompt <C>gap></C> is that the
222
program is waiting for a new problem.
223
<P/>
224
But even if you mistyped the command more seriously, you do not have to
225
type it all again. Suppose you mistyped or forgot the last closing
226
parenthesis. Then your command is syntactically incorrect and &GAP; will
227
notice it, incapable of computing the desired result.
228
<P/>
229
<Log><![CDATA[
230
gap> (9 - 7) * (5 + 6;
231
Syntax error: ) expected
232
(9 - 7) * (5 + 6;
233
^
234
]]></Log>
235
<P/>
236
<Index>line editing</Index>
237
Instead of the result an error message occurs indicating the place where
238
an unexpected symbol occurred with an arrow sign <C>^</C> under it. As a
239
computer program cannot know what your intentions really were, this is
240
only a hint. But in this case &GAP; is right by claiming that there
241
should be a closing parenthesis before the semicolon. Now you can type
242
<B>Ctrl-P</B> to recover the last line of input. It will be written after the
243
prompt with the cursor in the first position. Type <B>Ctrl-E</B> to take the
244
cursor to the end of the line, then <B>Ctrl-B</B> to move the cursor one
245
character back. The cursor is now on the position of the semicolon. Enter
246
the missing parenthesis by simply typing <C>)</C>. Now the line is correct and
247
may be passed to &GAP; by hitting the <B>Return</B> key. Note that for this
248
action it is not necessary to move the cursor past the last character of
249
the input line.
250
<P/>
251
Each line of commands you type is sent to &GAP; for evaluation by
252
pressing <B>Return</B> regardless of the position of the cursor in that line.
253
We will no longer mention the <B>Return</B> key from now on.
254
<P/>
255
<Index>break loops</Index>
256
Sometimes a syntax error will cause &GAP; to enter a <E>break loop</E>. This
257
is indicated by the special prompt <C>brk></C>. If another syntax error occurs
258
while &GAP; is in a break loop, the prompt will change to <C>brk_02></C>,
259
<C>brk_03></C> and so on. You can leave the current break loop and exit to the
260
next outer one by either typing <C>quit;</C> or by hitting <B>Ctrl-D</B>.
261
Eventually &GAP; will return to its normal state and show its normal
262
prompt <C>gap></C> again.
263
264
</Section>
265
266
267
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
268
<Section Label="Constants and Operators">
269
<Heading>Constants and Operators</Heading>
270
271
<Index>constants</Index><Index>operators</Index>
272
In an expression like <C>(9 - 7) * (5 + 6)</C> the constants <C>5</C>, <C>6</C>, <C>7</C>,
273
and <C>9</C> are being composed by the operators <C>+</C>, <C>*</C> and <C>-</C> to result in
274
a new value.
275
<P/>
276
There are three kinds of operators in &GAP;, arithmetical operators,
277
comparison operators, and logical operators. You have already seen that
278
it is possible to form the sum, the difference, and the product of two
279
integer values. There are some more operators applicable to integers in
280
&GAP;. Of course integers may be divided by each other, possibly
281
resulting in noninteger rational values.
282
<P/>
283
<Example><![CDATA[
284
gap> 12345/25;
285
2469/5
286
]]></Example>
287
<P/>
288
Note that the numerator and denominator are divided by their greatest
289
common divisor and that the result is uniquely represented as a division
290
instruction.
291
<P/>
292
The next self-explanatory example demonstrates negative numbers.
293
<P/>
294
<Example><![CDATA[
295
gap> -3; 17 - 23;
296
-3
297
-6
298
]]></Example>
299
<P/>
300
The exponentiation operator is written as <C>^</C>. This operation in
301
particular might lead to very large numbers. This is no problem for
302
&GAP; as it can handle numbers of (almost) any size.
303
<P/>
304
<Example><![CDATA[
305
gap> 3^132;
306
955004950796825236893190701774414011919935138974343129836853841
307
]]></Example>
308
<P/>
309
The <K>mod</K> operator allows you to compute one value modulo another.
310
<P/>
311
<Example><![CDATA[
312
gap> 17 mod 3;
313
2
314
]]></Example>
315
<P/>
316
Note that there must be whitespace around the keyword <K>mod</K> in this
317
example since <C>17mod3</C> or <C>17mod</C> would be interpreted as identifiers.
318
The whitespace around operators that do not consist of letters, e.g.,
319
the operators <C>*</C> and <C>-</C>, is not necessary.
320
<P/>
321
&GAP; knows a precedence between operators that may be overridden by
322
parentheses.
323
<P/>
324
<Example><![CDATA[
325
gap> (9 - 7) * 5 = 9 - 7 * 5;
326
false
327
]]></Example>
328
<P/>
329
Besides these arithmetical operators there are comparison operators in &GAP;.
330
A comparison results in a <E>boolean value</E> which is another kind
331
of constant.
332
The comparison operators <C>=</C>, <C>&lt;></C>, <C>&lt;</C>, <C>&lt;=</C>,
333
<C>></C> and <C>>=</C>, test for equality, inequality, less than,
334
less than or equal, greater than and greater than or equal, respectively.
335
<P/>
336
<Example><![CDATA[
337
gap> 10^5 < 10^4;
338
false
339
]]></Example>
340
<P/>
341
The boolean values <K>true</K> and <K>false</K> can be manipulated via
342
logical operators, i.&nbsp;e., the unary operator <K>not</K> and the
343
binary operators <K>and</K> and <K>or</K>.
344
Of course boolean values can be compared, too.
345
<P/>
346
<Example><![CDATA[
347
gap> not true; true and false; true or false;
348
false
349
false
350
true
351
gap> 10 > 0 and 10 < 100;
352
true
353
]]></Example>
354
<P/>
355
Another important type of constants in &GAP; are <E>permutations</E>. They
356
are written in cycle notation and they can be multiplied.
357
<P/>
358
<Example><![CDATA[
359
gap> (1,2,3);
360
(1,2,3)
361
gap> (1,2,3) * (1,2);
362
(2,3)
363
]]></Example>
364
<P/>
365
The inverse of the permutation <C>(1,2,3)</C> is denoted by <C>(1,2,3)^-1</C>.
366
Moreover the caret operator <C>^</C> is used to determine the image of a point
367
under a permutation and to conjugate one permutation by another.
368
<P/>
369
<Example><![CDATA[
370
gap> (1,2,3)^-1;
371
(1,3,2)
372
gap> 2^(1,2,3);
373
3
374
gap> (1,2,3)^(1,2);
375
(1,3,2)
376
]]></Example>
377
<P/>
378
The various other constants that &GAP; can deal with will be introduced
379
when they are used, for example there are elements of finite fields
380
such as <C>Z(8)</C>, and complex roots of unity such as <C>E(4)</C>.
381
<P/>
382
The last type of constants we want to mention here are the
383
<E>characters</E>, which are simply objects in &GAP; that represent arbitrary
384
characters from the character set of the operating system. Character
385
literals can be entered in &GAP; by enclosing the character in
386
<E>singlequotes</E> <C>'</C>.
387
<P/>
388
<Example><![CDATA[
389
gap> 'a';
390
'a'
391
gap> '*';
392
'*'
393
]]></Example>
394
<P/>
395
There are no operators defined for characters except that characters can
396
be compared.
397
<P/>
398
<!-- %% Summary %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
399
In this section you have seen that values may be preceded by unary
400
operators and combined by binary operators placed between the operands.
401
There are rules for precedence which may be overridden by parentheses. A
402
comparison results in a boolean value. Boolean values are combined via
403
logical operators. Moreover you have seen that &GAP; handles numbers of
404
arbitrary size. Numbers and boolean values are constants. There are
405
other types of constants in &GAP; like permutations. You are now in a
406
position to use &GAP; as a simple desktop calculator.
407
408
</Section>
409
410
411
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
412
<Section Label="Variables versus Objects">
413
<Heading>Variables versus Objects</Heading>
414
415
<Index>variables</Index><Index>assignment</Index><Index>identifier</Index><Index>objects</Index>
416
<Index Subkey="vs. variables">objects</Index>
417
The constants described in the last section are specified by certain
418
combinations of digits and minus signs (in the case of integers) or
419
digits, commas and parentheses (in the case of permutations). These
420
sequences of characters always have the same meaning to &GAP;. On the
421
other hand, there are <E>variables</E>, specified by a sequence of letters and
422
digits (including at least one letter), and their meaning depends on what
423
has been assigned to them. An <E>assignment</E> is done by a &GAP; command
424
<C><A>sequence_of_letters_and_digits</A> := <A>meaning</A></C>, where the sequence on
425
the left hand side is called the <E>identifier</E> of the variable and it serves
426
as its name. The meaning on the right hand side can be a constant like an
427
integer or a permutation, but it can also be almost any other &GAP;
428
object. From now on, we will use the term <E>object</E> to denote something
429
that can be assigned to a variable.
430
<P/>
431
There must be no whitespace between the <C>:</C> and the <C>=</C> in the assignment
432
operator. Also do not confuse the assignment operator with the single
433
equality sign <C>=</C> which in &GAP; is only used for the test of equality.
434
<P/>
435
<Example><![CDATA[
436
gap> a:= (9 - 7) * (5 + 6);
437
22
438
gap> a;
439
22
440
gap> a * (a + 1);
441
506
442
gap> a = 10;
443
false
444
gap> a:= 10;
445
10
446
gap> a * (a + 1);
447
110
448
]]></Example>
449
<P/>
450
After an assignment the assigned object is echoed on the next line. The
451
printing of the object of a statement may be in every case prevented by
452
typing a double semicolon.
453
<P/>
454
<Example><![CDATA[
455
gap> w:= 2;;
456
]]></Example>
457
<P/>
458
After the assignment the variable evaluates to that object if evaluated.
459
Thus it is possible to refer to that object by the name of the variable
460
in any situation.
461
<P/>
462
This is in fact the whole secret of an assignment. An identifier is bound
463
to an object and from this moment points to that object. Nothing more.
464
This binding is changed by the next assignment to that identifier. An
465
identifier does not denote a block of memory as in some other programming
466
languages. It simply points to an object, which has been given its place
467
in memory by the &GAP; storage manager. This place may change during a
468
&GAP; session, but that doesn't bother the identifier. <E>The identifier
469
points to the object, not to a place in the memory.</E>
470
<P/>
471
For the same reason it is not the identifier that has a type but the
472
object. This means on the other hand that the identifier <C>a</C> which now
473
is bound to an integer object may in the same session point to any other
474
object regardless of its type.
475
<P/>
476
Identifiers may be sequences of letters and digits containing at least
477
one letter. For example <C>abc</C> and <C>a0bc1</C> are valid identifiers. But
478
also <C>123a</C> is a valid identifier as it cannot be confused with any
479
number. Just <C>1234</C> indicates the number 1234 and cannot be at the same
480
time the name of a variable.
481
<P/>
482
Since &GAP; distinguishes upper and lower case, <C>a1</C> and <C>A1</C> are
483
different identifiers. Keywords such as <K>quit</K> must not be used as
484
identifiers. You will see more keywords in the following sections.
485
<P/>
486
In the remaining part of this manual we will ignore the difference
487
between variables, their names (identifiers), and the objects they point
488
to. It may be useful to think from time to time about what is really
489
meant by terms such as <Q>the integer <C>w</C></Q>.
490
<P/>
491
There are some predefined variables coming with &GAP;. Many of them you
492
will find in the remaining chapters of this manual, since functions are
493
also referred to via identifiers.
494
<P/>
495
You can get an overview of <E>all</E> &GAP; variables by entering
496
<C>NamesGVars()</C>. Many of these are predefined. If you are interested in
497
the variables you have defined yourself in the current &GAP; session,
498
you can enter <C>NamesUserGVars()</C>.
499
<P/>
500
<Log><![CDATA[
501
gap> NamesUserGVars();
502
[ "a", "w" ]
503
]]></Log>
504
<P/>
505
This seems to be the right place to state the following rule:
506
The name of every global variable in the &GAP; library starts with a
507
<E>capital letter</E>.
508
Thus if you choose only names starting with a small letter for your own
509
variables you will not attempt to overwrite any predefined variable.
510
(Note that most of the predefined variables are read-only,
511
and trying to change their values will result in an error message.)
512
<P/>
513
There are some further interesting variables one of which will be
514
introduced now.
515
<P/>
516
<Index Key="last"><C>last</C></Index>
517
<Index Key="last2"><C>last2</C></Index>
518
<Index Key="last3"><C>last3</C></Index>
519
Whenever &GAP; returns an object by printing it on the next line this
520
object is assigned to the variable <C>last</C>. So if you computed
521
<P/>
522
<Example><![CDATA[
523
gap> (9 - 7) * (5 + 6);
524
22
525
]]></Example>
526
<P/>
527
and forgot to assign the object to the variable <C>a</C> for further use, you
528
can still do it by the following assignment.
529
<P/>
530
<Example><![CDATA[
531
gap> a:= last;
532
22
533
]]></Example>
534
<P/>
535
Moreover there are variables <C>last2</C> and <C>last3</C>, you can guess their values.
536
<P/>
537
<!-- %% Summary %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
538
In this section you have seen how to assign objects to variables. These
539
objects can later be accessed through the name of the variable, its
540
identifier. You have also encountered the useful concept of the <C>last</C>
541
variables storing the latest returned objects. And you have learned that
542
a double semicolon prevents the result of a statement from being printed.
543
544
</Section>
545
546
547
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
548
<Section Label="Objects vs. Elements">
549
<Heading>Objects vs. Elements</Heading>
550
551
<Index Subkey="vs. elements">objects</Index>
552
<Index>elements</Index>
553
In the last section we mentioned that every object is given a certain
554
place in memory by the &GAP; storage manager (although that place may
555
change in the course of a &GAP; session). In this sense, objects at
556
different places in memory are never equal, and if the object pointed to
557
by the variable <C>a</C> (to be more precise, the variable with identifier
558
<C>a</C>) is equal to the object pointed to by the variable <C>b</C>, then we
559
should better say that they are not only equal but <E>identical</E>. &GAP;
560
provides the function <Ref Func="IsIdenticalObj" BookName="ref"/>
561
to test whether this is the case.
562
<P/>
563
<Example><![CDATA[
564
gap> a:= (1,2);; IsIdenticalObj( a, a );
565
true
566
gap> b:= (1,2);; IsIdenticalObj( a, b );
567
false
568
gap> b:= a;; IsIdenticalObj( a, b );
569
true
570
]]></Example>
571
<P/>
572
<Index Key="IsIdenticalObj"><C>IsIdenticalObj</C></Index>
573
<P/>
574
As the above example indicates, &GAP;
575
objects <A>a</A> and <A>b</A> can be unequal although they are equal from a
576
mathematical point of view, i.e., although we should have <A>a</A> = <A>b</A>. It
577
may be that the objects <A>a</A> and <A>b</A> are stored in different places in
578
memory, or it may be that we have an equivalence relation defined on the
579
set of objects under which <A>a</A> and <A>b</A> belong to the same equivalence
580
class. For example, if <M><A>a</A> = x^3</M> and <M><A>b</A> = x^{{-5}}</M> are words in
581
the finitely presented group <M>\langle x \mid x^2 = 1 \rangle</M>,
582
we would have <A>a</A> = <A>b</A> in that group.
583
<P/>
584
&GAP; uses the equality operator <C>=</C> to denote such a mathematical
585
equality, <E>not</E> the identity of objects. Hence we often have <C><A>a</A> = <A>b</A></C>
586
although <C>IsIdenticalObj( <A>a</A>, <A>b</A> ) = false</C>. The operator <C>=</C> defines
587
an equivalence relation on the set of all &GAP; objects, and we call the
588
corresponding equivalence classes <E>elements</E>. Phrasing it differently,
589
the same element may be represented by various &GAP; objects.
590
<P/>
591
Non-trivial examples of elements that are represented by different
592
objects (objects that really look different, not ones that are merely
593
stored in different memory places) will occur only when we will be
594
considering composite objects such as lists or domains.
595
596
</Section>
597
598
599
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
600
<Section Label="About Functions">
601
<Heading>About Functions</Heading>
602
603
A program written in the &GAP; language is called a <E>function</E>.
604
Functions are special &GAP; objects. Most of them behave like
605
mathematical functions. They are applied to objects and will return a
606
new object depending on the input.
607
The function <Ref Func="Factorial" BookName="ref"/>, for
608
example, can be applied to an integer and will return the factorial of
609
this integer.
610
<P/>
611
<Example><![CDATA[
612
gap> Factorial(17);
613
355687428096000
614
]]></Example>
615
<P/>
616
Applying a function to arguments means to write the arguments in
617
parentheses following the function. Several arguments are separated by
618
commas, as for the function
619
<Ref Func="Gcd" BookName="ref"/> which computes the greatest common
620
divisor of two integers.
621
<P/>
622
<Example><![CDATA[
623
gap> Gcd(1234, 5678);
624
2
625
]]></Example>
626
<P/>
627
There are other functions that do not return an object but only
628
produce a side effect, for example changing one of their arguments.
629
These functions are sometimes called procedures.
630
The function <Ref Func="Print" BookName="ref"/>
631
is only called for the side effect of printing something on the screen.
632
<P/>
633
<Example><![CDATA[
634
gap> Print(1234, "\n");
635
1234
636
]]></Example>
637
<P/>
638
In order to be able to compose arbitrary text with
639
<Ref Func="Print" BookName="ref"/>, this function
640
itself will not produce a line break after printing. Thus we had another
641
newline character <C>"\n"</C> printed to start a new line.
642
<P/>
643
Some functions will both change an argument and return an object such as
644
the function <Ref Func="Sortex" BookName="ref"/> that sorts a list
645
and returns the permutation of
646
the list elements that it has performed. You will not understand right
647
now what it means to change an object. We will return to this subject
648
several times in the next sections.
649
<P/>
650
<Index>maps-to operator</Index>
651
A comfortable way to define a function yourself is the <E>maps-to</E> operator
652
<C>-></C> consisting of a minus sign and a greater sign with no whitespace
653
between them. The function <C>cubed</C> which maps a number to its cube is
654
defined on the following line.
655
<P/>
656
<Example><![CDATA[
657
gap> cubed:= x -> x^3;
658
function( x ) ... end
659
]]></Example>
660
<P/>
661
After the function has been defined, it can now be applied.
662
<P/>
663
<Example><![CDATA[
664
gap> cubed(5);
665
125
666
]]></Example>
667
<P/>
668
More complicated functions, especially functions with more than one
669
argument cannot be defined in this way.
670
You will see how to write your own &GAP; functions in
671
Section&nbsp;<Ref Sect="Writing Functions"/>.
672
<P/>
673
<!-- %% Summary %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
674
In this section you have seen &GAP; objects of type function. You have
675
learned how to apply a function to arguments. This yields as result a
676
new object or a side effect. A side effect may change an argument of the
677
function. Moreover you have seen an easy way to define a function in
678
&GAP; with the maps-to operator.
679
680
</Section>
681
682
683
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
684
<Section Label="Help">
685
<Heading>Help</Heading>
686
687
The content of the &GAP; manuals is also available as on-line help. A
688
&GAP; session loads a long list of index entries. This typically contains
689
all chapter and section headers, all names of documented functions,
690
operations and so on, as well as some explicit index entries defined in the
691
manuals.
692
<P/>
693
The format of a query is as follows.
694
<P/>
695
<C>?[<A>book</A>:][?]<A>topic</A></C>
696
<P/>
697
A simple example would be to type <C>?help</C> at the &GAP; prompt. If there is
698
a single section with index entry <A>topic</A> then this is displayed directly.
699
<P/>
700
If there are several matches you get an overview like in the example below.
701
<P/>
702
<Log><![CDATA[
703
gap> ?sets
704
Help: several entries match this topic - type ?2 to get match [2]
705
706
[1] Tutorial: Sets
707
[2] Reference: Sets
708
[3] Reference: sets
709
[4] Reference: Sets of Subgroups
710
[5] Reference: setstabilizer
711
]]></Log>
712
<P/>
713
&GAP;'s manuals consist of several <E>books</E>, which are indicated before the
714
colon in the list above. A help query can be restricted to one book by using
715
the optional <A>book</A>: part. For example <C>?tut : sets</C> will display the first
716
of these help sections. More precisely, the parts of the string <A>book</A> which
717
are separated by white space are interpreted as beginnings of the first
718
words in the name of the book. Try <C>?books</C> to see the list of available
719
books and their names.
720
<P/>
721
The search for a matching <A>topic</A> (and optional <A>book</A>) is done <E>case
722
insensitively</E>. If there is another <C>?</C> before the <A>topic</A>, then a
723
<E>substring search</E> for <A>topic</A> is performed on all index entries. Otherwise
724
the parts of <A>topic</A> which are separated by white space are considered as
725
<E>beginnings of the first words</E> in an index entry.
726
<P/>
727
White space is normalized in the search string (and the index entries).
728
<P/>
729
<E>Examples.</E> All the following queries lead to the chapter of the reference
730
manual which explains the use of &GAP;'s help system in more detail.
731
<P/>
732
<Log><![CDATA[
733
gap> ?Reference: The Help System
734
gap> ? REF : t h s
735
gap> ?ref:? help system
736
]]></Log>
737
<P/>
738
The query <C>??sets</C> shows all help sections in all books whose index entries
739
contain the substring <C>sets</C>.
740
<P/>
741
As mentioned in the example above a complete list of commands for the help
742
system is available in Section&nbsp;<C>?Ref: The Help System</C> of the reference
743
manual. In particular there are commands to browse through the help
744
sections, see&nbsp;<C>?Ref: Browsing through the Sections</C> and there is a way to
745
influence the way <E>how</E> the help sections are displayed, see&nbsp;<C>?Ref:
746
SetHelpViewer</C>. For example you can use an external pager program, a Web
747
browser, <C>dvi</C>-previewer and/or <C>pdf</C>-viewer for reading &GAP;'s online
748
help.
749
750
</Section>
751
752
753
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
754
<Section Label="Further Information introducing the System">
755
<Heading>Further Information introducing the System</Heading>
756
757
For large amounts of input data, it might be advisable to write your
758
input first into a file, and then read this into &GAP;;
759
see&nbsp;<Ref Func="Read" BookName="ref"/>, <Ref Func="Edit" BookName="ref"/> for this.
760
<P/>
761
The definition of the &GAP; syntax can be looked up in
762
Chapter&nbsp;<Ref Chap="The Programming Language" BookName="ref"/>.
763
A complete list of command line editing facilities is found
764
in Section&nbsp;<Ref Sect="Line Editing" BookName="ref"/>.
765
The break loop is described in Section&nbsp;<Ref Sect="Break Loops" BookName="ref"/>.
766
<P/>
767
Operators are explained in more detail in Sections&nbsp;<Ref Sect="Expressions" BookName="ref"/> and
768
<Ref Sect="Comparisons" BookName="ref"/>.
769
You will find more information about boolean values in
770
Chapters&nbsp;<Ref Chap="Booleans" BookName="ref"/> and <Ref Chap="Boolean Lists" BookName="ref"/>.
771
Permutations are described in Chapter&nbsp;<Ref Chap="Permutations" BookName="ref"/> and characters
772
in Chapter&nbsp;<Ref Chap="Strings and Characters" BookName="ref"/>.
773
<P/>
774
Variables and assignments are described in more detail in&nbsp;<Ref Sect="Variables" BookName="ref"/>
775
and <Ref Sect="Assignments" BookName="ref"/>.
776
A complete list of keywords is contained in&nbsp;<Ref Sect="Keywords" BookName="ref"/>.
777
<P/>
778
More about functions can be found in&nbsp;<Ref Sect="Function Calls" BookName="ref"/>
779
and&nbsp;<Ref Sect="Procedure Calls" BookName="ref"/>.
780
781
</Section>
782
</Chapter>
783
784
785
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
786
<!-- %% -->
787
<!-- %E -->
788
789
790