Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

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

Path: gap4r8 / doc / ref / chap12.txt
Views: 415155
1
2
12 Objects and Elements
3
4
An object is anything in GAP that can be assigned to a variable, so nearly
5
everything in GAP is an object.
6
7
Different objects can be regarded as equal with respect to the equivalence
8
relation =, in this case we say that the objects describe the same element.
9
10
11
12.1 Objects
12
13
Nearly all things one deals with in GAP are objects. For example, an integer
14
is an object, as is a list of integers, a matrix, a permutation, a function,
15
a list of functions, a record, a group, a coset or a conjugacy class in a
16
group.
17
18
Examples of things that are not objects are comments which are only lexical
19
constructs, while loops which are only syntactical constructs, and
20
expressions, such as 1 + 1; but note that the value of an expression, in
21
this case the integer 2, is an object.
22
23
Objects can be assigned to variables, and everything that can be assigned to
24
a variable is an object. Analogously, objects can be used as arguments of
25
functions, and can be returned by functions.
26
27
12.1-1 IsObject
28
29
IsObject( obj )  Category
30
31
IsObject returns true if the object obj is an object. Obviously it can never
32
return false.
33
34
It can be used as a filter in InstallMethod (78.2-1) when one of the
35
arguments can be anything.
36
37
38
12.2 Elements as equivalence classes
39
40
The equality operation = defines an equivalence relation on all GAP objects.
41
The equivalence classes are called elements.
42
43
There are basically three reasons to regard different objects as equal.
44
Firstly the same information may be stored in different places. Secondly the
45
same information may be stored in different ways; for example, a polynomial
46
can be stored sparsely or densely. Thirdly different information may be
47
equal modulo a mathematical equivalence relation. For example, in a finitely
48
presented group with the relation a^2 = 1 the different objects a and a^3
49
describe the same element.
50
51
As an example of all three reasons, consider the possibility of storing an
52
integer in several places of the memory, of representing it as a fraction
53
with denominator 1, or of representing it as a fraction with any
54
denominator, and numerator a suitable multiple of the denominator.
55
56
57
12.3 Sets
58
59
In GAP there is no category whose definition corresponds to the mathematical
60
property of being a set, however in the manual we will often refer to an
61
object as a set in order to convey the fact that mathematically, we are
62
thinking of it as a set. In particular, two sets A and B are equal if and
63
only if, x āˆˆ A <=> x āˆˆ B.
64
65
There are two types of object in GAP which exhibit this kind of behaviour
66
with respect to equality, namely domains (see SectionĀ 12.4) and lists whose
67
elements are strictly sorted see IsSSortedList (21.17-4). In general, set in
68
this manual will mean an object of one of these types.
69
70
More precisely: two domains can be compared with {=}, the answer being true
71
if and only if the sets of elements are equal (regardless of any additional
72
structure) and; a domain and a list can be compared with =, the answer being
73
true if and only if the list is equal to the strictly sorted list of
74
elements of the domain.
75
76
A discussion about sorted lists and sets can be found in Section 21.19.
77
78
79
12.4 Domains
80
81
An especially important class of objects in GAP are those whose underlying
82
mathematical abstraction is that of a structured set, for example a group, a
83
conjugacy class, or a vector space. Such objects are called domains. The
84
equality relation between domains is always equality as sets, so that two
85
domains are equal if and only if they contain the same elements.
86
87
Domains play a central role in GAP. In a sense, the only reason that GAP
88
supports objects such as integers and permutations is the wish to form
89
domains of them and compute the properties of those domains.
90
91
Domains are described in ChapterĀ 31.
92
93
94
12.5 Identical Objects
95
96
Two objects that are equal as objects (that is they actually refer to the
97
same area of computer memory) and not only w.r.t. the equality relation =
98
are called identical. Identical objects do of course describe the same
99
element.
100
101
12.5-1 IsIdenticalObj
102
103
IsIdenticalObj( obj1, obj2 )  function
104
105
IsIdenticalObj tests whether the objects obj1 and obj2 are identical (that
106
is they are either equal immediate objects or are both stored at the same
107
location in memory.
108
109
If two copies of a simple constant object (see section 12.6) are created, it
110
is not defined whether GAP will actually store two equal but non-identical
111
objects, or just a single object. For mutable objects, however, it is
112
important to know whether two values refer to identical or non-identical
113
objects, and the documentation of operations that return mutable values
114
should make clear whether the values returned are new, or may be identical
115
to values stored elsewhere.
116
117
 Example 
118
gap> IsIdenticalObj( 10^6, 10^6);
119
true
120
gap> IsIdenticalObj( 10^30, 10^30);
121
false
122
gap> IsIdenticalObj( true, true);
123
true
124

125
126
Generally, one may compute with objects but think of the results in terms of
127
the underlying elements because one is not interested in locations in
128
memory, data formats or information beyond underlying equivalence relations.
129
But there are cases where it is important to distinguish the relations
130
identity and equality. This is best illustrated with an example. (The reader
131
who is not familiar with lists in GAP, in particular element access and
132
assignment, is referred to ChapterĀ 21.)
133
134
 Example 
135
gap> l1:= [ 1, 2, 3 ];; l2:= [ 1, 2, 3 ];;
136
gap> l1 = l2;
137
true
138
gap> IsIdenticalObj( l1, l2 );
139
false
140
gap> l1[3]:= 4;; l1; l2;
141
[ 1, 2, 4 ]
142
[ 1, 2, 3 ]
143
gap> l1 = l2;
144
false
145

146
147
The two lists l1 and l2 are equal but not identical. Thus a change in l1
148
does not affect l2.
149
150
 Example 
151
gap> l1:= [ 1, 2, 3 ];; l2:= l1;;
152
gap> l1 = l2;
153
true
154
gap> IsIdenticalObj( l1, l2 );
155
true
156
gap> l1[3]:= 4;; l1; l2;
157
[ 1, 2, 4 ]
158
[ 1, 2, 4 ]
159
gap> l1 = l2;
160
true
161

162
163
Here, l1 and l2 are identical objects, so changing l1 means a change to l2
164
as well.
165
166
12.5-2 IsNotIdenticalObj
167
168
IsNotIdenticalObj( obj1, obj2 )  function
169
170
tests whether the objects obj1 and obj2 are not identical.
171
172
173
12.6 Mutability and Copyability
174
175
An object in GAP is said to be immutable if its mathematical value (as
176
defined by =) does not change under any operation. More explicitly, suppose
177
a is immutable and O is some operation on a, then if a = b evaluates to true
178
before executing O(a), a = b also evaluates to true afterwards. (Examples
179
for operations O that change mutable objects are Add (21.4-2) and Unbind
180
(21.5-2) which are used to change list objects, see ChapterĀ 21.) An
181
immutable object may change, for example to store new information, or to
182
adopt a more efficient representation, but this does not affect its
183
behaviour under =.
184
185
There are two points here to note. Firstly, operation above refers to the
186
functions and methods which can legitimately be applied to the object, and
187
not the !. operation whereby virtually any aspect of any GAP level object
188
may be changed. The second point which follows from this, is that when
189
implementing new types of objects, it is the programmer's responsibility to
190
ensure that the functions and methods they write never change immutable
191
objects mathematically.
192
193
In fact, most objects with which one deals in GAP are immutable. For
194
instance, the permutation (1,2) will never become a different permutation or
195
a non-permutation (although a variable which previously had (1,2) stored in
196
it may subsequently have some other value).
197
198
For many purposes, however, mutable objects are useful. These objects may be
199
changed to represent different mathematical objects during their life. For
200
example, mutable lists can be changed by assigning values to positions or by
201
unbinding values at certain positions. Similarly, one can assign values to
202
components of a mutable record, or unbind them.
203
204
12.6-1 IsCopyable
205
206
IsCopyable( obj )  Category
207
208
If a mutable form of an object obj can be made in GAP, the object is called
209
copyable. Examples of copyable objects are of course lists and records. A
210
new mutable version of the object can always be obtained by the operation
211
ShallowCopy (12.7-1).
212
213
Objects for which only an immutable form exists in GAP are called constants.
214
Examples of constants are integers, permutations, and domains. Called with a
215
constant as argument, Immutable (12.6-3) and ShallowCopy (12.7-1) return
216
this argument.
217
218
12.6-2 IsMutable
219
220
IsMutable( obj )  Category
221
222
tests whether obj is mutable.
223
224
If an object is mutable then it is also copyable (seeĀ IsCopyable (12.6-1)),
225
and a ShallowCopy (12.7-1) method should be supplied for it. Note that
226
IsMutable must not be implied by another filter, since otherwise Immutable
227
(12.6-3) would be able to create paradoxical objects in the sense that
228
IsMutable for such an object is false but the filter that implies IsMutable
229
is true.
230
231
In many situations, however, one wants to ensure that objects are immutable.
232
For example, take the identity of a matrix group. Since this matrix may be
233
referred to as the identity of the group in several places, it would be
234
fatal to modify its entries, or add or unbind rows. We can obtain an
235
immutable copy of an object with Immutable (12.6-3).
236
237
12.6-3 Immutable
238
239
Immutable( obj )  function
240
241
returns an immutable structural copy (seeĀ StructuralCopy (12.7-2)) of obj in
242
which the subobjects are immutable copies of the subobjects of obj. If obj
243
is immutable then Immutable returns obj itself.
244
245
GAP will complain with an error if one tries to change an immutable object.
246
247
12.6-4 MakeImmutable
248
249
MakeImmutable( obj )  function
250
251
One can turn the (mutable or immutable) object obj into an immutable one
252
with MakeImmutable; note that this also makes all subobjects of obj
253
immutable, so one should call MakeImmutable only if obj and its mutable
254
subobjects are newly created. If one is not sure about this, Immutable
255
(12.6-3) should be used.
256
257
Note that it is not possible to turn an immutable object into a mutable one;
258
only mutable copies can be made (seeĀ 12.7).
259
260
Using Immutable (12.6-3), it is possible to store an immutable identity
261
matrix or an immutable list of generators, and to pass around references to
262
this immutable object safely. Only when a mutable copy is really needed does
263
the actual object have to be duplicated. Compared to the situation without
264
immutable objects, much unnecessary copying is avoided this way. Another
265
advantage of immutability is that lists of immutable objects may remember
266
whether they are sorted (seeĀ 21.19), which is not possible for lists of
267
mutable objects.
268
269
Since the operation Immutable (12.6-3) must work for any object in GAP, it
270
follows that an immutable form of every object must be possible, even if it
271
is not sensible, and user-defined objects must allow for the possibility of
272
becoming immutable without notice.
273
274
275
12.6-5 Mutability of Iterators
276
277
An interesting example of mutable (and thus copyable) objects is provided by
278
iterators, seeĀ 30.8. (Of course an immutable form of an iterator is not very
279
useful, but clearly Immutable (12.6-3) will yield such an object.) Every
280
call of NextIterator (30.8-5) changes a mutable iterator until it is
281
exhausted, and this is the only way to change an iterator. ShallowCopy
282
(12.7-1) for an iterator iter is defined so as to return a mutable iterator
283
that has no mutable data in common with iter, and that behaves equally to
284
iter w.r.t.Ā IsDoneIterator (30.8-4) and (if iter is mutable) NextIterator
285
(30.8-5). Note that this meaning of the shallow copy of an iterator that is
286
returned by ShallowCopy (12.7-1) is not as obvious as for lists and records,
287
and must be explicitly defined.
288
289
290
12.6-6 Mutability of Results of Arithmetic Operations
291
292
Many operations return immutable results, among those in particular
293
attributes (seeĀ 13.5). Examples of attributes are Size (30.4-6), Zero
294
(31.10-3), AdditiveInverse (31.10-9), One (31.10-2), and Inverse (31.10-8).
295
Arithmetic operations, such as the binary infix operations +, -, *, /, ^,
296
mod, the unary -, and operations such as Comm (31.12-3) and LeftQuotient
297
(31.12-2), return mutable results, except if all arguments are immutable. So
298
the product of two matrices or of a vector and a matrix is immutable if and
299
only if the two matrices or both the vector and the matrix are immutable
300
(see alsoĀ 21.11). There is one exception to this rule, which arises where
301
the result is less deeply nested than at least one of the argument, where
302
mutable arguments may sometimes lead to an immutable result. For instance, a
303
mutable matrix with immutable rows, multiplied by an immutable vector gives
304
an immutable vector result. The exact rules are given inĀ 21.11.
305
306
It should be noted that 0 * obj is equivalent to ZeroSM( obj ), -obj is
307
equivalent to AdditiveInverseSM( obj ), obj^0 is equivalent to OneSM( obj),
308
and obj^-1 is equivalent to InverseSM( obj ). The SM stands for same
309
mutability, and indicates that the result is mutable if and only if the
310
argument is mutable.
311
312
The operations ZeroOp (31.10-3), AdditiveInverseOp (31.10-9), OneOp
313
(31.10-2), and InverseOp (31.10-8) return mutable results whenever a mutable
314
version of the result exists, contrary to the attributes Zero (31.10-3),
315
AdditiveInverse (31.10-9), One (31.10-2), and Inverse (31.10-8).
316
317
If one introduces new arithmetic objects then one need not install methods
318
for the attributes One (31.10-2), Zero (31.10-3), etc. The methods for the
319
associated operations OneOp (31.10-2) and ZeroOp (31.10-3) will be called,
320
and then the results made immutable.
321
322
All methods installed for the arithmetic operations must obey the rule about
323
the mutability of the result. This means that one may try to avoid the
324
perhaps expensive creation of a new object if both operands are immutable,
325
and of course no problems of this kind arise at all in the (usual) case that
326
the objects in question do not admit a mutable form, i.e., that these
327
objects are not copyable.
328
329
In a few, relatively low-level algorithms, one wishes to treat a matrix
330
partly as a data structure, and manipulate and change its entries. For this,
331
the matrix needs to be mutable, and the rule that attribute values are
332
immutable is an obstacle. For these situations, a number of additional
333
operations are provided, for example TransposedMatMutable (24.5-6)
334
constructs a mutable matrix (contrary to the attribute TransposedMat
335
(24.5-6)), while TriangulizeMat (24.7-3) modifies a mutable matrix (in
336
place) into upper triangular form.
337
338
Note that being immutable does not forbid an object to store knowledge. For
339
example, if it is found out that an immutable list is strictly sorted then
340
the list may store this information. More precisely, an immutable object may
341
change in any way, provided that it continues to represent the same
342
mathematical object.
343
344
345
12.7 Duplication of Objects
346
347
12.7-1 ShallowCopy
348
349
ShallowCopy( obj )  operation
350
351
ShallowCopy returns a new mutable object equal to its argument, if this is
352
possible. The subobjects of ShallowCopy( obj ) are identical to the
353
subobjects of obj.
354
355
If GAP does not support a mutable form of the immutable object obj
356
(seeĀ 12.6) then ShallowCopy returns obj itself.
357
358
Since ShallowCopy is an operation, the concrete meaning of subobject depends
359
on the type of obj. But for any copyable object obj, the definition should
360
reflect the idea of first level copying.
361
362
The definition of ShallowCopy for lists (in particular for matrices) can be
363
found inĀ 21.7.
364
365
12.7-2 StructuralCopy
366
367
StructuralCopy( obj )  function
368
369
In a few situations, one wants to make a structural copy scp of an object
370
obj. This is defined as follows. scp and obj are identical if obj is
371
immutable. Otherwise, scp is a mutable copy of obj such that each subobject
372
of scp is a structural copy of the corresponding subobject of obj.
373
Furthermore, if two subobjects of obj are identical then also the
374
corresponding subobjects of scp are identical.
375
376
 Example 
377
gap> obj:= [ [ 0, 1 ] ];;
378
gap> obj[2]:= obj[1];;
379
gap> obj[3]:= Immutable( obj[1] );;
380
gap> scp:= StructuralCopy( obj );;
381
gap> scp = obj; IsIdenticalObj( scp, obj );
382
true
383
false
384
gap> IsIdenticalObj( scp[1], obj[1] );
385
false
386
gap> IsIdenticalObj( scp[3], obj[3] );
387
true
388
gap> IsIdenticalObj( scp[1], scp[2] );
389
true
390

391
392
That both ShallowCopy (12.7-1) and StructuralCopy return the argument obj
393
itself if it is not copyable is consistent with this definition, since there
394
is no way to change obj by modifying the result of any of the two functions,
395
because in fact there is no way to change this result at all.
396
397
398
12.8 Other Operations Applicable to any Object
399
400
There are a number of general operations which can be applied, in principle,
401
to any object in GAP. Some of these are documented elsewhere ā€“see String
402
(27.7-6), PrintObj (6.3-5) and Display (6.3-6). Others are mainly somewhat
403
technical.
404
405
12.8-1 SetName
406
407
SetName( obj, name )  operation
408
409
for a suitable object obj sets that object to have name name (a string).
410
411
12.8-2 Name
412
413
Name( obj )  attribute
414
415
returns the name, a string, previously assigned to obj via a call to SetName
416
(12.8-1). The name of an object is used only for viewing the object via this
417
name.
418
419
There are no methods installed for computing names of objects, but the name
420
may be set for suitable objects, using SetName (12.8-1).
421
422
 Example 
423
gap> R := PolynomialRing(Integers,2);
424
Integers[x_1,x_2]
425
gap> SetName(R,"Z[x,y]");
426
gap> R;
427
Z[x,y]
428
gap> Name(R);
429
"Z[x,y]"
430

431
432
12.8-3 InfoText
433
434
InfoText( obj )  attribute
435
436
is a mutable string with information about the object obj. There is no
437
default method to create an info text.
438
439
12.8-4 IsInternallyConsistent
440
441
IsInternallyConsistent( obj )  operation
442
443
For debugging purposes, it may be useful to check the consistency of an
444
object obj that is composed from other (composed) objects.
445
446
There is a default method of IsInternallyConsistent, with rank zero, that
447
returns true. So it is possible (and recommended) to check the consistency
448
of subobjects of obj recursively by IsInternallyConsistent.
449
450
(Note that IsInternallyConsistent is not an attribute.)
451
452
12.8-5 MemoryUsage
453
454
MemoryUsage( obj )  operation
455
456
returns the amount of memory in bytes used by the object obj and its
457
subobjects. Note that in general, objects can reference each other in very
458
difficult ways such that determining the memory usage is a recursive
459
procedure. In particular, computing the memory usage of a complicated
460
structure itself uses some additional memory, which is however no longer
461
used after completion of this operation. This procedure descends into lists
462
and records, positional and component objects, however it does not take into
463
account the type and family objects! For functions, it only takes the memory
464
usage of the function body, not of the local context the function was
465
created in, although the function keeps a reference to that as well!
466
467
468