Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
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
Project: cocalc-sagemath-dev-slelievre
Views: 4183461[1X3 [33X[0;0YLists and Records[133X[101X23[33X[0;0YModern mathematics, especially algebra, is based on set theory. When sets4are represented in a computer, they inadvertently turn into lists. That's5why we start our survey of the various objects [5XGAP[105X can handle with a6description of lists and their manipulation. [5XGAP[105X regards sets as a special7kind of lists, namely as lists without holes or duplicates whose entries are8ordered with respect to the precedence relation [10X<[110X.[133X910[33X[0;0YAfter the introduction of the basic manipulations with lists in [14X3.1[114X, some11difficulties concerning identity and mutability of lists are discussed12in [14X3.2[114X and [14X3.3[114X. Sets, ranges, row vectors, and matrices are introduced as13special kinds of lists in [14X3.4[114X, [14X3.5[114X, [14X3.8[114X. Handy list operations are shown14in [14X3.7[114X. Finally we explain how to use records in [14X3.9[114X.[133X151617[1X3.1 [33X[0;0YPlain Lists[133X[101X1819[33X[0;0YA [13Xlist[113X is a collection of objects separated by commas and enclosed in20brackets. Let us for example construct the list [10Xprimes[110X of the first ten21prime numbers.[133X2223[4X[32X Example [32X[104X24[4X[25Xgap>[125X [27Xprimes:= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];[127X[104X25[4X[28X[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ][128X[104X26[4X[32X[104X2728[33X[0;0YThe next two primes are 31 and 37. They may be appended to the existing list29by the function [10XAppend[110X which takes the existing list as its first and30another list as a second argument. The second argument is appended to the31list [10Xprimes[110X and no value is returned. Note that by appending another list32the object [10Xprimes[110X is changed.[133X3334[4X[32X Example [32X[104X35[4X[25Xgap>[125X [27XAppend(primes, [31, 37]);[127X[104X36[4X[25Xgap>[125X [27Xprimes;[127X[104X37[4X[28X[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 ][128X[104X38[4X[32X[104X3940[33X[0;0YYou can as well add single new elements to existing lists by the function41[10XAdd[110X which takes the existing list as its first argument and a new element as42its second argument. The new element is added to the list [10Xprimes[110X and again43no value is returned but the list [10Xprimes[110X is changed.[133X4445[4X[32X Example [32X[104X46[4X[25Xgap>[125X [27XAdd(primes, 41);[127X[104X47[4X[25Xgap>[125X [27Xprimes;[127X[104X48[4X[28X[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 ][128X[104X49[4X[32X[104X5051[33X[0;0YSingle elements of a list are referred to by their position in the list. To52get the value of the seventh prime, that is the seventh entry in our list53[10Xprimes[110X, you simply type[133X5455[4X[32X Example [32X[104X56[4X[25Xgap>[125X [27Xprimes[7];[127X[104X57[4X[28X17[128X[104X58[4X[32X[104X5960[33X[0;0YThis value can be handled like any other value, for example multiplied by 261or assigned to a variable. On the other hand this mechanism allows one to62assign a value to a position in a list. So the next prime 43 may be inserted63in the list directly after the last occupied position of [10Xprimes[110X. This last64occupied position is returned by the function [10XLength[110X.[133X6566[4X[32X Example [32X[104X67[4X[25Xgap>[125X [27XLength(primes);[127X[104X68[4X[28X13[128X[104X69[4X[25Xgap>[125X [27Xprimes[14]:= 43;[127X[104X70[4X[28X43[128X[104X71[4X[25Xgap>[125X [27Xprimes;[127X[104X72[4X[28X[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 ][128X[104X73[4X[32X[104X7475[33X[0;0YNote that this operation again has changed the object [10Xprimes[110X. The next76position after the end of a list is not the only position capable of taking77a new value. If you know that 71 is the 20th prime, you can enter it right78now in the 20th position of [10Xprimes[110X. This will result in a list with holes79which is however still a list and now has length 20.[133X8081[4X[32X Example [32X[104X82[4X[25Xgap>[125X [27Xprimes[20]:= 71;[127X[104X83[4X[28X71[128X[104X84[4X[25Xgap>[125X [27Xprimes;[127X[104X85[4X[28X[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ][128X[104X86[4X[25Xgap>[125X [27XLength(primes);[127X[104X87[4X[28X20[128X[104X88[4X[32X[104X8990[33X[0;0YThe list itself however must exist before a value can be assigned to a91position of the list. This list may be the empty list [10X[ ][110X.[133X9293[4X[32X Example [32X[104X94[4X[25Xgap>[125X [27Xlll[1]:= 2;[127X[104X95[4X[28XError, Variable: 'lll' must have a value[128X[104X96[4X[28X[128X[104X97[4X[28X[128X[104X98[4X[25Xgap>[125X [27Xlll:= []; lll[1]:= 2;[127X[104X99[4X[28X[ ][128X[104X100[4X[28X2[128X[104X101[4X[32X[104X102103[33X[0;0YOf course existing entries of a list can be changed by this mechanism, too.104We will not do it here because [10Xprimes[110X then may no longer be a list of105primes. Try for yourself to change the 17 in the list into a 9.[133X106107[33X[0;0YTo get the position of 17 in the list [10Xprimes[110X use the function [2XPosition[102X108([14XReference: Position[114X) which takes the list as its first argument and the109element as its second argument and returns the position of the first110occurrence of the element 17 in the list [10Xprimes[110X. If the element is not111contained in the list then [2XPosition[102X ([14XReference: Position[114X) will return the112special object [9Xfail[109X.[133X113114[4X[32X Example [32X[104X115[4X[25Xgap>[125X [27XPosition(primes, 17);[127X[104X116[4X[28X7[128X[104X117[4X[25Xgap>[125X [27XPosition(primes, 20);[127X[104X118[4X[28Xfail[128X[104X119[4X[32X[104X120121[33X[0;0YIn all of the above changes to the list [10Xprimes[110X, the list has been122automatically resized. There is no need for you to tell [5XGAP[105X how big you want123a list to be. This is all done dynamically.[133X124125[33X[0;0YIt is not necessary for the objects collected in a list to be of the same126type.[133X127128[4X[32X Example [32X[104X129[4X[25Xgap>[125X [27Xlll:= [true, "This is a String",,, 3];[127X[104X130[4X[28X[ true, "This is a String",,, 3 ][128X[104X131[4X[32X[104X132133[33X[0;0YIn the same way a list may be part of another list.[133X134135[4X[32X Example [32X[104X136[4X[25Xgap>[125X [27Xlll[3]:= [4,5,6];; lll;[127X[104X137[4X[28X[ true, "This is a String", [ 4, 5, 6 ],, 3 ][128X[104X138[4X[32X[104X139140[33X[0;0YA list may even be part of itself.[133X141142[4X[32X Example [32X[104X143[4X[25Xgap>[125X [27Xlll[4]:= lll;[127X[104X144[4X[28X[ true, "This is a String", [ 4, 5, 6 ], ~, 3 ][128X[104X145[4X[32X[104X146147[33X[0;0YNow the tilde in the fourth position of [10Xlll[110X denotes the object that is148currently printed. Note that the result of the last operation is the actual149value of the object [10Xlll[110X on the right hand side of the assignment. In fact it150is identical to the value of the whole list [10Xlll[110X on the left hand side of the151assignment.[133X152153[33X[0;0YA [13Xstring[113X is a special type of list, namely a dense list of [13Xcharacters[113X, where154[13Xdense[113X means that the list has no holes. Here, [13Xcharacters[113X are special [5XGAP[105X155objects representing an element of the character set of the operating156system. The input of printable characters is by enclosing them in single157quotes [10X'[110X. A string literal can either be entered as the list of characters158or by writing the characters between doublequotes [10X"[110X. Strings are handled159specially by [2XPrint[102X ([14XReference: Print[114X). You can learn much more about strings160in the reference manual.[133X161162[4X[32X Example [32X[104X163[4X[25Xgap>[125X [27Xs1 := ['H','a','l','l','o',' ','w','o','r','l','d','.'];[127X[104X164[4X[28X"Hallo world."[128X[104X165[4X[25Xgap>[125X [27Xs1 = "Hallo world.";[127X[104X166[4X[28Xtrue[128X[104X167[4X[25Xgap>[125X [27Xs1[7];[127X[104X168[4X[28X'w'[128X[104X169[4X[32X[104X170171[33X[0;0YSublists of lists can easily be extracted and assigned using the operator172[10X[3Xlist[103X[10X{ [3Xpositions[103X[10X }[110X.[133X173174[4X[32X Example [32X[104X175[4X[25Xgap>[125X [27Xsl := lll{ [ 1, 2, 3 ] };[127X[104X176[4X[28X[ true, "This is a String", [ 4, 5, 6 ] ][128X[104X177[4X[25Xgap>[125X [27Xsl{ [ 2, 3 ] } := [ "New String", false ];[127X[104X178[4X[28X[ "New String", false ][128X[104X179[4X[25Xgap>[125X [27Xsl;[127X[104X180[4X[28X[ true, "New String", false ][128X[104X181[4X[32X[104X182183[33X[0;0YThis way you get a new list whose [22Xi[122X-th entry is that element of the original184list whose position is the [22Xi[122X-th entry of the argument in the curly braces.[133X185186187[1X3.2 [33X[0;0YIdentical Lists[133X[101X188189[33X[0;0YThis second section about lists is dedicated to the subtle difference190between [13Xequality[113X and [13Xidentity[113X of lists. It is really important to understand191this difference in order to understand how complex data structures are192realized in [5XGAP[105X. This section applies to all [5XGAP[105X objects that have193subobjects, e.g., to lists and to records. After reading the section [14X3.9[114X194about records you should return to this section and translate it into the195record context.[133X196197[33X[0;0YTwo lists are [13Xequal[113X if all their entries are equal. This means that the198equality operator [10X=[110X returns [9Xtrue[109X for the comparison of two lists if and only199if these two lists are of the same length and for each position the values200in the respective lists are equal.[133X201202[4X[32X Example [32X[104X203[4X[25Xgap>[125X [27Xnumbers := primes;; numbers = primes;[127X[104X204[4X[28Xtrue[128X[104X205[4X[32X[104X206207[33X[0;0YWe assigned the list [10Xprimes[110X to the variable [10Xnumbers[110X and, of course they are208equal as they have both the same length and the same entries. Now we will209change the third number to 4 and compare the result again with [10Xprimes[110X.[133X210211[4X[32X Example [32X[104X212[4X[25Xgap>[125X [27Xnumbers[3]:= 4;; numbers = primes;[127X[104X213[4X[28Xtrue[128X[104X214[4X[32X[104X215216[33X[0;0YYou see that [10Xnumbers[110X and [10Xprimes[110X are still equal, check this by printing the217value of [10Xprimes[110X. The list [10Xprimes[110X is no longer a list of primes! What has218happened? The truth is that the lists [10Xprimes[110X and [10Xnumbers[110X are not only equal219but they are also [13Xidentical[113X. [10Xprimes[110X and [10Xnumbers[110X are two variables pointing220to the same list. If you change the value of the subobject [10Xnumbers[3][110X of221[10Xnumbers[110X this will also change [10Xprimes[110X. Variables do [13Xnot[113X point to a certain222block of storage memory but they do point to an object that occupies storage223memory. So the assignment [10Xnumbers := primes[110X did [13Xnot[113X create a new list in a224different place of memory but only created the new name [10Xnumbers[110X for the same225old list of primes.[133X226227[33X[0;0YFrom this we see that [13Xthe same object can have several names.[113X[133X228229[33X[0;0YIf you want to change a list with the contents of [10Xprimes[110X independently from230[10Xprimes[110X you will have to make a copy of [10Xprimes[110X by the function [10XShallowCopy[110X231which takes an object as its argument and returns a copy of the argument.232(We will first restore the old value of [10Xprimes[110X.)[133X233234[4X[32X Example [32X[104X235[4X[25Xgap>[125X [27Xprimes[3]:= 5;; primes;[127X[104X236[4X[28X[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ][128X[104X237[4X[25Xgap>[125X [27Xnumbers:= ShallowCopy(primes);; numbers = primes;[127X[104X238[4X[28Xtrue[128X[104X239[4X[25Xgap>[125X [27Xnumbers[3]:= 4;; numbers = primes;[127X[104X240[4X[28Xfalse[128X[104X241[4X[32X[104X242243[33X[0;0YNow [10Xnumbers[110X is no longer equal to [10Xprimes[110X and [10Xprimes[110X still is a list of244primes. Check this by printing the values of [10Xnumbers[110X and [10Xprimes[110X.[133X245246[33X[0;0YLists and records can be changed this way because [5XGAP[105X objects of these types247have subobjects. To clarify this statement consider the following248assignments.[133X249250[4X[32X Example [32X[104X251[4X[25Xgap>[125X [27Xi:= 1;; j:= i;; i:= i+1;; [127X[104X252[4X[32X[104X253254[33X[0;0YBy adding 1 to [10Xi[110X the value of [10Xi[110X has changed. What happens to [10Xj[110X? After the255second statement [10Xj[110X points to the same object as [10Xi[110X, namely to the integer 1.256The addition does [13Xnot[113X change the object [10X1[110X but creates a new object according257to the instruction [10Xi+1[110X. It is actually the assignment that changes the value258of [10Xi[110X. Therefore [10Xj[110X still points to the object [10X1[110X. Integers (like permutations259and booleans) have no subobjects. Objects of these types cannot be changed260but can only be replaced by other objects. And a replacement does not change261the values of other variables. In the above example an assignment of a new262value to the variable [10Xnumbers[110X would also not change the value of [10Xprimes[110X.[133X263264[33X[0;0YFinally try the following examples and explain the results.[133X265266[4X[32X Example [32X[104X267[4X[25Xgap>[125X [27Xl:= [];; l:= [l];[127X[104X268[4X[28X[ [ ] ][128X[104X269[4X[25Xgap>[125X [27Xl[1]:= l;[127X[104X270[4X[28X[ ~ ][128X[104X271[4X[32X[104X272273[33X[0;0YNow return to Section [14X3.1[114X and find out whether the functions [2XAdd[102X ([14XReference:274Add[114X) and [2XAppend[102X ([14XReference: Append[114X) change their arguments.[133X275276277[1X3.3 [33X[0;0YImmutability[133X[101X278279[33X[0;0Y[5XGAP[105X has a mechanism that protects lists against changes like the ones that280have bothered us in Section [14X3.2[114X. The function [2XImmutable[102X ([14XReference:281Immutable[114X) takes as argument a list and returns an immutable copy of it,282i.e., a list which looks exactly like the old one, but has two extra283properties: (1) The new list is immutable, i.e., the list itself and its284subobjects cannot be changed. (2) In constructing the copy, every part of285the list that can be changed has been copied, so that changes to the old286list will not affect the new one. In other words, the new list has no287mutable subobjects in common with the old list.[133X288289[4X[32X Example [32X[104X290[4X[25Xgap>[125X [27Xlist := [ 1, 2, "three", [ 4 ] ];; copy := Immutable( list );;[127X[104X291[4X[25Xgap>[125X [27Xlist[3][5] := 'w';; list; copy;[127X[104X292[4X[28X[ 1, 2, "threw", [ 4 ] ][128X[104X293[4X[28X[ 1, 2, "three", [ 4 ] ][128X[104X294[4X[25Xgap>[125X [27Xcopy[3][5] := 'w';[127X[104X295[4X[28XLists Assignment: <list> must be a mutable list[128X[104X296[4X[28Xnot in any function[128X[104X297[4X[28XEntering break read-eval-print loop ...[128X[104X298[4X[28Xyou can 'quit;' to quit to outer loop, or[128X[104X299[4X[28Xyou can 'return;' and ignore the assignment to continue[128X[104X300[4X[26Xbrk>[126X [27Xquit;[127X[104X301[4X[32X[104X302303[33X[0;0YAs a consequence of these rules, in the immutable copy of a list which304contains an already immutable list as subobject, this immutable subobject305need not be copied, because it is unchangeable. Immutable lists are useful306in many complex [5XGAP[105X objects, for example as generator lists of groups. By307making them immutable, [5XGAP[105X ensures that no generators can be added to the308list, removed or exchanged. Such changes would of course lead to serious309inconsistencies with other knowledge that may already have been calculated310for the group.[133X311312[33X[0;0YA converse function to [2XImmutable[102X ([14XReference: Immutable[114X) is [2XShallowCopy[102X313([14XReference: ShallowCopy[114X), which produces a new mutable list whose [22Xi[122X-th entry314is the [22Xi[122X-th entry of the old list. The single entries are not copied, they315are just placed in the new list. If the old list is immutable, and hence the316list entries are immutable themselves, the result of [2XShallowCopy[102X ([14XReference:317ShallowCopy[114X) is mutable only on the top level.[133X318319[33X[0;0YIt should be noted that also other objects than lists can appear in mutable320or immutable form. Records (see Section [14X3.9[114X) provide another example.[133X321322323[1X3.4 [33X[0;0YSets[133X[101X324325[33X[0;0Y[5XGAP[105X knows several special kinds of lists. A [13Xset[113X in [5XGAP[105X is a list that326contains no holes (such a list is called [13Xdense[113X) and whose elements are327strictly sorted w.r.t. [10X<[110X; in particular, a set cannot contain duplicates.328(More precisely, the elements of a set in [5XGAP[105X are required to lie in the329same [13Xfamily[113X, but roughly this means that they can be compared using the [10X<[110X330operator.)[133X331332[33X[0;0YThis provides a natural model for mathematical sets whose elements are given333by an explicit enumeration.[133X334335[33X[0;0Y[5XGAP[105X also calls a set a [13Xstrictly sorted list[113X, and the function [2XIsSSortedList[102X336([14XReference: IsSSortedList[114X) tests whether a given list is a set. It returns a337boolean value. For almost any list whose elements are contained in the same338family, there exists a corresponding set. This set is constructed by the339function [2XSet[102X ([14XReference: Set[114X) which takes the list as its argument and340returns a set obtained from this list by ignoring holes and duplicates and341by sorting the elements.[133X342343[33X[0;0YThe elements of the sets used in the examples of this section are strings.[133X344345[4X[32X Example [32X[104X346[4X[25Xgap>[125X [27Xfruits:= ["apple", "strawberry", "cherry", "plum"];[127X[104X347[4X[28X[ "apple", "strawberry", "cherry", "plum" ][128X[104X348[4X[25Xgap>[125X [27XIsSSortedList(fruits);[127X[104X349[4X[28Xfalse[128X[104X350[4X[25Xgap>[125X [27Xfruits:= Set(fruits);[127X[104X351[4X[28X[ "apple", "cherry", "plum", "strawberry" ][128X[104X352[4X[32X[104X353354[33X[0;0YNote that the original list [10Xfruits[110X is not changed by the function [2XSet[102X355([14XReference: Set[114X). We have to make a new assignment to the variable [10Xfruits[110X in356order to make it a set.[133X357358[33X[0;0YThe operator [9Xin[109X is used to test whether an object is an element of a set. It359returns a boolean value [9Xtrue[109X or [9Xfalse[109X.[133X360361[4X[32X Example [32X[104X362[4X[25Xgap>[125X [27X"apple" in fruits;[127X[104X363[4X[28Xtrue[128X[104X364[4X[25Xgap>[125X [27X"banana" in fruits;[127X[104X365[4X[28Xfalse[128X[104X366[4X[32X[104X367368[33X[0;0YThe operator [9Xin[109X can also be applied to ordinary lists. It is however much369faster to perform a membership test for sets since sets are always sorted370and a binary search can be used instead of a linear search. New elements may371be added to a set by the function [2XAddSet[102X ([14XReference: AddSet[114X) which takes the372set [10Xfruits[110X as its first argument and an element as its second argument and373adds the element to the set if it wasn't already there. Note that the object374[10Xfruits[110X is changed.[133X375376[4X[32X Example [32X[104X377[4X[25Xgap>[125X [27XAddSet(fruits, "banana");[127X[104X378[4X[25Xgap>[125X [27Xfruits; # The banana is inserted in the right place.[127X[104X379[4X[28X[ "apple", "banana", "cherry", "plum", "strawberry" ][128X[104X380[4X[25Xgap>[125X [27XAddSet(fruits, "apple");[127X[104X381[4X[25Xgap>[125X [27Xfruits; # fruits has not changed.[127X[104X382[4X[28X[ "apple", "banana", "cherry", "plum", "strawberry" ][128X[104X383[4X[32X[104X384385[33X[0;0YNote that inserting new elements into a set with [2XAddSet[102X ([14XReference: AddSet[114X)386is usually more expensive than simply adding new elements at the end of a387list.[133X388389[33X[0;0YSets can be intersected by the function [2XIntersection[102X ([14XReference:390Intersection[114X) and united by the function [2XUnion[102X ([14XReference: Union[114X) which both391take two sets as their arguments and return the intersection resp. union of392the two sets as a new object.[133X393394[4X[32X Example [32X[104X395[4X[25Xgap>[125X [27Xbreakfast:= ["tea", "apple", "egg"];[127X[104X396[4X[28X[ "tea", "apple", "egg" ][128X[104X397[4X[25Xgap>[125X [27XIntersection(breakfast, fruits);[127X[104X398[4X[28X[ "apple" ][128X[104X399[4X[32X[104X400401[33X[0;0YThe arguments of the functions [2XIntersection[102X ([14XReference: Intersection[114X) and402[2XUnion[102X ([14XReference: Union[114X) could be ordinary lists, while their result is403always a set. Note that in the preceding example at least one argument of404[2XIntersection[102X ([14XReference: Intersection[114X) was not a set. The functions405[2XIntersectSet[102X ([14XReference: IntersectSet[114X) and [2XUniteSet[102X ([14XReference: UniteSet[114X)406also form the intersection resp. union of two sets. They will however not407return the result but change their first argument to be the result. Try them408carefully.[133X409410411[1X3.5 [33X[0;0YRanges[133X[101X412413[33X[0;0YA [13Xrange[113X is a finite arithmetic progression of integers. This is another414special kind of list. A range is described by the first two values and the415last value of the arithmetic progression which are given in the form416[10X[[3Xfirst[103X[10X,[3Xsecond[103X[10X..[3Xlast[103X[10X][110X. In the usual case of an ascending list of consecutive417integers the second entry may be omitted.[133X418419[4X[32X Example [32X[104X420[4X[25Xgap>[125X [27X[1..999999]; # a range of almost a million numbers[127X[104X421[4X[28X[ 1 .. 999999 ][128X[104X422[4X[25Xgap>[125X [27X[1, 2..999999]; # this is equivalent[127X[104X423[4X[28X[ 1 .. 999999 ][128X[104X424[4X[25Xgap>[125X [27X[1, 3..999999]; # here the step is 2[127X[104X425[4X[28X[ 1, 3 .. 999999 ][128X[104X426[4X[25Xgap>[125X [27XLength( last );[127X[104X427[4X[28X500000[128X[104X428[4X[25Xgap>[125X [27X[ 999999, 999997 .. 1 ];[127X[104X429[4X[28X[ 999999, 999997 .. 1 ][128X[104X430[4X[32X[104X431432[33X[0;0YThis compact printed representation of a fairly long list corresponds to a433compact internal representation. The function [2XIsRange[102X ([14XReference: IsRange[114X)434tests whether an object is a range, the function [2XConvertToRangeRep[102X435([14XReference: ConvertToRangeRep[114X) changes the representation of a list that is436in fact a range to this compact internal representation.[133X437438[4X[32X Example [32X[104X439[4X[25Xgap>[125X [27Xa:= [-2,-1,0,1,2,3,4,5];[127X[104X440[4X[28X[ -2, -1, 0, 1, 2, 3, 4, 5 ][128X[104X441[4X[25Xgap>[125X [27XIsRange( a );[127X[104X442[4X[28Xtrue[128X[104X443[4X[25Xgap>[125X [27XConvertToRangeRep( a );; a;[127X[104X444[4X[28X[ -2 .. 5 ][128X[104X445[4X[25Xgap>[125X [27Xa[1]:= 0;; IsRange( a );[127X[104X446[4X[28Xfalse[128X[104X447[4X[32X[104X448449[33X[0;0YNote that this change of representation does [13Xnot[113X change the value of the450list [10Xa[110X. The list [10Xa[110X still behaves in any context in the same way as it would451have in the long representation.[133X452453454[1X3.6 [33X[0;0YFor and While Loops[133X[101X455456[33X[0;0YGiven a list [10Xpp[110X of permutations we can form their product by means of a [9Xfor[109X457loop instead of writing down the product explicitly.[133X458459[4X[32X Example [32X[104X460[4X[25Xgap>[125X [27Xpp:= [ (1,3,2,6,8)(4,5,9), (1,6)(2,7,8), (1,5,7)(2,3,8,6),[127X[104X461[4X[25X>[125X [27X (1,8,9)(2,3,5,6,4), (1,9,8,6,3,4,7,2)];;[127X[104X462[4X[25Xgap>[125X [27Xprod:= (); [127X[104X463[4X[28X()[128X[104X464[4X[25Xgap>[125X [27Xfor p in pp do[127X[104X465[4X[25X>[125X [27X prod:= prod*p; [127X[104X466[4X[25X>[125X [27X od;[127X[104X467[4X[25Xgap>[125X [27Xprod; [127X[104X468[4X[28X(1,8,4,2,3,6,5,9)[128X[104X469[4X[32X[104X470471[33X[0;0YFirst a new variable [10Xprod[110X is initialized to the identity permutation [10X()[110X.472Then the loop variable [10Xp[110X takes as its value one permutation after the other473from the list [10Xpp[110X and is multiplied with the present value of [10Xprod[110X resulting474in a new value which is then assigned to [10Xprod[110X.[133X475476[33X[0;0YThe [9Xfor[109X loop has the following syntax[133X477478[33X[0;0Y[9Xfor[109X [3Xvar[103X [9Xin[109X [3Xlist[103X [9Xdo[109X [3Xstatements[103X [9Xod[109X[10X;[110X[133X479480[33X[0;0YThe effect of the [9Xfor[109X loop is to execute the [3Xstatements[103X for every element of481the [3Xlist[103X. A [9Xfor[109X loop is a statement and therefore terminated by a semicolon.482The list of [3Xstatements[103X is enclosed by the keywords [9Xdo[109X and [9Xod[109X (reverse [9Xdo[109X). A483[9Xfor[109X loop returns no value. Therefore we had to ask explicitly for the value484of [10Xprod[110X in the preceding example.[133X485486[33X[0;0YThe [9Xfor[109X loop can loop over any kind of list, even a list with holes. In many487programming languages the [9Xfor[109X loop has the form[133X488489[33X[0;0Y[10Xfor [3Xvar[103X[10X from [3Xfirst[103X[10X to [3Xlast[103X[10X do [3Xstatements[103X[10X od;[110X[133X490491[33X[0;0YIn [5XGAP[105X this is merely a special case of the general [9Xfor[109X loop as defined492above where the [3Xlist[103X in the loop body is a range (see [14X3.5[114X):[133X493494[33X[0;0Y[9Xfor[109X [3Xvar[103X [9Xin[109X [10X[[3Xfirst[103X[10X..[3Xlast[103X[10X][110X [9Xdo[109X [3Xstatements[103X [9Xod[109X[10X;[110X[133X495496[33X[0;0YYou can for instance loop over a range to compute the factorial [22X15![122X of the497number [22X15[122X in the following way.[133X498499[4X[32X Example [32X[104X500[4X[25Xgap>[125X [27Xff:= 1;[127X[104X501[4X[28X1[128X[104X502[4X[25Xgap>[125X [27Xfor i in [1..15] do[127X[104X503[4X[25X>[125X [27X ff:= ff * i;[127X[104X504[4X[25X>[125X [27X od;[127X[104X505[4X[25Xgap>[125X [27Xff;[127X[104X506[4X[28X1307674368000[128X[104X507[4X[32X[104X508509[33X[0;0YThe [9Xwhile[109X loop has the following syntax[133X510511[33X[0;0Y[9Xwhile[109X [3Xcondition[103X [9Xdo[109X [3Xstatements[103X [9Xod[109X[10X;[110X[133X512513[33X[0;0YThe [9Xwhile[109X loop loops over the [3Xstatements[103X as long as the [3Xcondition[103X evaluates514to [9Xtrue[109X. Like the [9Xfor[109X loop the [9Xwhile[109X loop is terminated by the keyword [9Xod[109X515followed by a semicolon.[133X516517[33X[0;0YWe can use our list [10Xprimes[110X to perform a very simple factorization. We begin518by initializing a list [10Xfactors[110X to the empty list. In this list we want to519collect the prime factors of the number 1333. Remember that a list has to520exist before any values can be assigned to positions of the list. Then we521will loop over the list [10Xprimes[110X and test for each prime whether it divides522the number. If it does we will divide the number by that prime, add it to523the list [10Xfactors[110X and continue.[133X524525[4X[32X Example [32X[104X526[4X[25Xgap>[125X [27Xn:= 1333;;[127X[104X527[4X[25Xgap>[125X [27Xfactors:= [];;[127X[104X528[4X[25Xgap>[125X [27Xfor p in primes do[127X[104X529[4X[25X>[125X [27X while n mod p = 0 do[127X[104X530[4X[25X>[125X [27X n:= n/p;[127X[104X531[4X[25X>[125X [27X Add(factors, p);[127X[104X532[4X[25X>[125X [27X od;[127X[104X533[4X[25X>[125X [27X od;[127X[104X534[4X[25Xgap>[125X [27Xfactors;[127X[104X535[4X[28X[ 31, 43 ][128X[104X536[4X[25Xgap>[125X [27Xn;[127X[104X537[4X[28X1[128X[104X538[4X[32X[104X539540[33X[0;0YAs [10Xn[110X now has the value 1 all prime factors of 1333 have been found and541[10Xfactors[110X contains a complete factorization of 1333. This can of course be542verified by multiplying 31 and 43.[133X543544[33X[0;0YThis loop may be applied to arbitrary numbers in order to find prime545factors. But as [10Xprimes[110X is not a complete list of all primes this loop may546fail to find all prime factors of a number greater than 2000, say. You can547try to improve it in such a way that new primes are added to the list [10Xprimes[110X548if needed.[133X549550[33X[0;0YYou have already seen that list objects may be changed. This of course also551holds for the list in a loop body. In most cases you have to be careful not552to change this list, but there are situations where this is quite useful.553The following example shows a quick way to determine the primes smaller than5541000 by a sieve method. Here we will make use of the function [10XUnbind[110X to555delete entries from a list, and the [10Xif[110X statement covered in [14X4.2[114X.[133X556557[4X[32X Example [32X[104X558[4X[25Xgap>[125X [27Xprimes:= [];;[127X[104X559[4X[25Xgap>[125X [27Xnumbers:= [2..1000];;[127X[104X560[4X[25Xgap>[125X [27Xfor p in numbers do[127X[104X561[4X[25X>[125X [27X Add(primes, p);[127X[104X562[4X[25X>[125X [27X for n in numbers do[127X[104X563[4X[25X>[125X [27X if n mod p = 0 then[127X[104X564[4X[25X>[125X [27X Unbind(numbers[n-1]);[127X[104X565[4X[25X>[125X [27X fi;[127X[104X566[4X[25X>[125X [27X od;[127X[104X567[4X[25X>[125X [27X od;[127X[104X568[4X[32X[104X569570[33X[0;0YThe inner loop removes all entries from [10Xnumbers[110X that are divisible by the571last detected prime [10Xp[110X. This is done by the function [10XUnbind[110X which deletes the572binding of the list position [10Xnumbers[n-1][110X to the value [10Xn[110X so that afterwards573[10Xnumbers[n-1][110X no longer has an assigned value. The next element encountered574in [10Xnumbers[110X by the outer loop necessarily is the next prime.[133X575576[33X[0;0YIn a similar way it is possible to enlarge the list which is looped over.577This yields a nice and short orbit algorithm for the action of a group, for578example.[133X579580[33X[0;0YMore about [9Xfor[109X and [9Xwhile[109X loops can be found in the sections [14X'Reference:581While'[114X and [14X'Reference: For'[114X.[133X582583584[1X3.7 [33X[0;0YList Operations[133X[101X585586[33X[0;0YThere is a more comfortable way than that given in the previous section to587compute the product of a list of numbers or permutations.[133X588589[4X[32X Example [32X[104X590[4X[25Xgap>[125X [27XProduct([1..15]);[127X[104X591[4X[28X1307674368000[128X[104X592[4X[25Xgap>[125X [27XProduct(pp);[127X[104X593[4X[28X(1,8,4,2,3,6,5,9)[128X[104X594[4X[32X[104X595596[33X[0;0YThe function [2XProduct[102X ([14XReference: Product[114X) takes a list as its argument and597computes the product of the elements of the list. This is possible whenever598a multiplication of the elements of the list is defined. So [2XProduct[102X599([14XReference: Product[114X) executes a loop over all elements of the list.[133X600601[33X[0;0YThere are other often used loops available as functions. Guess what the602function [2XSum[102X ([14XReference: Sum[114X) does. The function [2XList[102X ([14XReference: Lists[114X) may603take a list and a function as its arguments. It will then apply the function604to each element of the list and return the corresponding list of results. A605list of cubes is produced as follows with the function [10Xcubed[110X from Section [14X4[114X.[133X606607[4X[32X Example [32X[104X608[4X[25Xgap>[125X [27Xcubed:= x -> x^3;;[127X[104X609[4X[25Xgap>[125X [27XList([2..10], cubed);[127X[104X610[4X[28X[ 8, 27, 64, 125, 216, 343, 512, 729, 1000 ][128X[104X611[4X[32X[104X612613[33X[0;0YTo add all these cubes we might apply the function [2XSum[102X ([14XReference: Sum[114X) to614the last list. But we may as well give the function [10Xcubed[110X to [2XSum[102X ([14XReference:615Sum[114X) as an additional argument.[133X616617[4X[32X Example [32X[104X618[4X[25Xgap>[125X [27XSum(last) = Sum([2..10], cubed);[127X[104X619[4X[28Xtrue[128X[104X620[4X[32X[104X621622[33X[0;0YThe primes less than 30 can be retrieved out of the list [10Xprimes[110X from623Section [14X3.1[114X by the function [2XFiltered[102X ([14XReference: Filtered[114X). This function624takes the list [10Xprimes[110X and a property as its arguments and will return the625list of those elements of [10Xprimes[110X which have this property. Such a property626will be represented by a function that returns a boolean value. In this627example the property of being less than 30 can be represented by the628function [10Xx -> x < 30[110X since [10Xx < 30[110X will evaluate to [9Xtrue[109X for values [10Xx[110X less629than 30 and to [9Xfalse[109X otherwise.[133X630631[4X[32X Example [32X[104X632[4X[25Xgap>[125X [27XFiltered(primes, x -> x < 30);[127X[104X633[4X[28X[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ][128X[104X634[4X[32X[104X635636[33X[0;0YWe have already mentioned the operator [10X{ }[110X that forms sublists. It takes a637list of positions as its argument and will return the list of elements from638the original list corresponding to these positions.[133X639640[4X[32X Example [32X[104X641[4X[25Xgap>[125X [27Xprimes{ [1 .. 10] };[127X[104X642[4X[28X[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ][128X[104X643[4X[32X[104X644645[33X[0;0YFinally we mention the function [2XForAll[102X ([14XReference: ForAll[114X) that checks646whether a property holds for all elements of a list. It takes as its647arguments a list and a function that returns a boolean value. [2XForAll[102X648([14XReference: ForAll[114X) checks whether the function returns [9Xtrue[109X for all649elements of the list.[133X650651[4X[32X Example [32X[104X652[4X[25Xgap>[125X [27Xlist:= [ 1, 2, 3, 4 ];;[127X[104X653[4X[25Xgap>[125X [27XForAll( list, x -> x > 0 );[127X[104X654[4X[28Xtrue[128X[104X655[4X[25Xgap>[125X [27XForAll( list, x -> x in primes );[127X[104X656[4X[28Xfalse[128X[104X657[4X[32X[104X658659[33X[0;0YYou will find more predefined [9Xfor[109X loops in chapter [14X'Reference: Lists'[114X.[133X660661662[1X3.8 [33X[0;0YVectors and Matrices[133X[101X663664[33X[0;0YThis section describes how [5XGAP[105X uses lists to represent row vectors and665matrices. A [13Xrow vector[113X is a dense list of elements from a common field. A666[13Xmatrix[113X is a dense list of row vectors over a common field and of equal667length.[133X668669[4X[32X Example [32X[104X670[4X[25Xgap>[125X [27Xv:= [3, 6, 2, 5/2];; IsRowVector(v);[127X[104X671[4X[28Xtrue[128X[104X672[4X[32X[104X673674[33X[0;0YRow vectors may be added and multiplied by scalars from their field.675Multiplication of row vectors of equal length results in their scalar676product.[133X677678[4X[32X Example [32X[104X679[4X[25Xgap>[125X [27X2 * v; v * 1/3;[127X[104X680[4X[28X[ 6, 12, 4, 5 ][128X[104X681[4X[28X[ 1, 2, 2/3, 5/6 ][128X[104X682[4X[25Xgap>[125X [27Xv * v; # the scalar product of `v' with itself[127X[104X683[4X[28X221/4[128X[104X684[4X[32X[104X685686[33X[0;0YNote that the expression [10Xv * 1/3[110X is actually evaluated by first multiplying687[10Xv[110X by 1 (which yields again [10Xv[110X) and by then dividing by 3. This is also an688allowed scalar operation. The expression [10Xv/3[110X would result in the same value.[133X689690[33X[0;0YSuch arithmetical operations (if the results are again vectors) result in691[13Xmutable[113X vectors except if the operation is binary and both operands are692immutable; thus the vectors shown in the examples above are all mutable.[133X693694[33X[0;0YSo if you want to produce a mutable list with 100 entries equal to 25, you695can simply say [10X25 + 0 * [ 1 .. 100 ][110X. Note that ranges are also vectors696(over the rationals), and that [10X[ 1 .. 100 ][110X is mutable.[133X697698[33X[0;0YA matrix is a dense list of row vectors of equal length.[133X699700[4X[32X Example [32X[104X701[4X[25Xgap>[125X [27Xm:= [[1,-1, 1],[127X[104X702[4X[25X>[125X [27X [2, 0,-1],[127X[104X703[4X[25X>[125X [27X [1, 1, 1]];[127X[104X704[4X[28X[ [ 1, -1, 1 ], [ 2, 0, -1 ], [ 1, 1, 1 ] ][128X[104X705[4X[25Xgap>[125X [27Xm[2][1];[127X[104X706[4X[28X2[128X[104X707[4X[32X[104X708709[33X[0;0YSyntactically a matrix is a list of lists. So the number 2 in the second row710and the first column of the matrix [10Xm[110X is referred to as the first element of711the second element of the list [10Xm[110X via [10Xm[2][1][110X.[133X712713[33X[0;0YA matrix may be multiplied by scalars, row vectors and other matrices. (If714the row vectors and matrices involved in such a multiplication do not have715suitable dimensions then the [21Xmissing[121X entries are treated as zeros, so the716results may look unexpectedly in such cases.)[133X717718[4X[32X Example [32X[104X719[4X[25Xgap>[125X [27X[1, 0, 0] * m;[127X[104X720[4X[28X[ 1, -1, 1 ][128X[104X721[4X[25Xgap>[125X [27X[1, 0, 0, 2] * m;[127X[104X722[4X[28X[ 1, -1, 1 ][128X[104X723[4X[25Xgap>[125X [27Xm * [1, 0, 0];[127X[104X724[4X[28X[ 1, 2, 1 ][128X[104X725[4X[25Xgap>[125X [27Xm * [1, 0, 0, 2];[127X[104X726[4X[28X[ 1, 2, 1 ][128X[104X727[4X[32X[104X728729[33X[0;0YNote that multiplication of a row vector with a matrix will result in a730linear combination of the rows of the matrix, while multiplication of a731matrix with a row vector results in a linear combination of the columns of732the matrix. In the latter case the row vector is considered as a column733vector.[133X734735[33X[0;0YA vector or matrix of integers can also be multiplied with a finite field736scalar and vice versa. Such products result in a matrix over the finite737field with the integers mapped into the finite field in the obvious way.738Finite field matrices are nicer to read when they are [10XDisplay[110Xed rather than739[10XPrint[110Xed. (Here we write [10XZ(q)[110X to denote a primitive root of the finite field740with [10Xq[110X elements.)[133X741742[4X[32X Example [32X[104X743[4X[25Xgap>[125X [27XDisplay( m * One( GF(5) ) );[127X[104X744[4X[28X 1 4 1[128X[104X745[4X[28X 2 . 4[128X[104X746[4X[28X 1 1 1[128X[104X747[4X[25Xgap>[125X [27XDisplay( m^2 * Z(2) + m * Z(4) );[127X[104X748[4X[28Xz = Z(4)[128X[104X749[4X[28X z^1 z^1 z^2[128X[104X750[4X[28X 1 1 z^2[128X[104X751[4X[28X z^1 z^1 z^2[128X[104X752[4X[32X[104X753754[33X[0;0YSubmatrices can easily be extracted using the expression [10X[3Xmat[103X[10X{[3Xrows[103X[10X}{[3Xcolumns[103X[10X}[110X.755They can also be assigned to, provided the big matrix is mutable (which it756is not if it is the result of an arithmetical operation, see above).[133X757758[4X[32X Example [32X[104X759[4X[25Xgap>[125X [27Xsm := m{ [ 1, 2 ] }{ [ 2, 3 ] };[127X[104X760[4X[28X[ [ -1, 1 ], [ 0, -1 ] ][128X[104X761[4X[25Xgap>[125X [27Xsm{ [ 1, 2 ] }{ [2] } := [[-2],[0]];; sm;[127X[104X762[4X[28X[ [ -1, -2 ], [ 0, 0 ] ][128X[104X763[4X[32X[104X764765[33X[0;0YThe first curly brackets contain the selection of rows, the second that of766columns.[133X767768[33X[0;0YMatrices appear not only in linear algebra, but also as group elements,769provided they are invertible. Here we have the opportunity to meet a770group-theoretical function, namely [2XOrder[102X ([14XReference: Order[114X), which computes771the order of a group element.[133X772773[4X[32X Example [32X[104X774[4X[25Xgap>[125X [27XOrder( m * One( GF(5) ) );[127X[104X775[4X[28X8[128X[104X776[4X[25Xgap>[125X [27XOrder( m );[127X[104X777[4X[28Xinfinity[128X[104X778[4X[32X[104X779780[33X[0;0YFor matrices whose entries are more complex objects, for example rational781functions, [5XGAP[105X's [2XOrder[102X ([14XReference: Order[114X) methods might not be able to prove782that the matrix has infinite order, and one gets the following warning.[133X783784[4X[32X Example [32X[104X785[4X[28X#I Order: warning, order of <mat> might be infinite[128X[104X786[4X[32X[104X787788[33X[0;0YIn such a case, if the order of the matrix really is infinite, you will have789to interrupt [5XGAP[105X by pressing [10X[3Xctl[103X[10X-C[110X (followed by [10X[3Xctl[103X[10X-D[110X or [10Xquit;[110X to leave the790break loop).[133X791792[33X[0;0YTo prove that the order of [10Xm[110X is infinite, we also could look at the minimal793polynomial of [10Xm[110X over the rationals.[133X794795[4X[32X Example [32X[104X796[4X[25Xgap>[125X [27Xf:= MinimalPolynomial( Rationals, m );; Factors( f );[127X[104X797[4X[28X[ x_1-2, x_1^2+3 ][128X[104X798[4X[32X[104X799800[33X[0;0Y[2XFactors[102X ([14XReference: Factors[114X) returns a list of irreducible factors of the801polynomial [10Xf[110X. The first irreducible factor [22XX-2[122X reveals that 2 is an802eigenvalue of [10Xm[110X, hence its order cannot be finite.[133X803804805[1X3.9 [33X[0;0YPlain Records[133X[101X806807[33X[0;0YA record provides another way to build new data structures. Like a list a808record contains subobjects. In a record the elements, the so-called [13Xrecord809components[113X, are not indexed by numbers but by names.[133X810811[33X[0;0YIn this section you will see how to define and how to use records. Records812are changed by assignments to record components or by unbinding record813components.[133X814815[33X[0;0YInitially a record is defined as a comma separated list of assignments to816its record components.[133X817818[4X[32X Example [32X[104X819[4X[25Xgap>[125X [27Xdate:= rec(year:= 1997,[127X[104X820[4X[25X>[125X [27X month:= "Jul",[127X[104X821[4X[25X>[125X [27X day:= 14);[127X[104X822[4X[28Xrec( day := 14, month := "Jul", year := 1997 )[128X[104X823[4X[32X[104X824825[33X[0;0YThe value of a record component is accessible by the record name and the826record component name separated by one dot as the record component selector.[133X827828[4X[32X Example [32X[104X829[4X[25Xgap>[125X [27Xdate.year;[127X[104X830[4X[28X1997[128X[104X831[4X[32X[104X832833[33X[0;0YAssignments to new record components are possible in the same way. The834record is automatically resized to hold the new component.[133X835836[4X[32X Example [32X[104X837[4X[25Xgap>[125X [27Xdate.time:= rec(hour:= 19, minute:= 23, second:= 12);[127X[104X838[4X[28Xrec( hour := 19, minute := 23, second := 12 )[128X[104X839[4X[25Xgap>[125X [27Xdate;[127X[104X840[4X[28Xrec( day := 14, month := "Jul", [128X[104X841[4X[28X time := rec( hour := 19, minute := 23, second := 12 ), year := 1997 )[128X[104X842[4X[32X[104X843844[33X[0;0YRecords are objects that may be changed. An assignment to a record component845changes the original object. The remarks made in Sections [14X3.2[114X and [14X3.3[114X about846identity and mutability of lists are also true for records.[133X847848[33X[0;0YSometimes it is interesting to know which components of a certain record are849bound. This information is available from the function [2XRecNames[102X ([14XReference:850RecNames[114X), which takes a record as its argument and returns a list of names851of all bound components of this record as a list of strings.[133X852853[4X[32X Example [32X[104X854[4X[25Xgap>[125X [27XRecNames(date);[127X[104X855[4X[28X[ "time", "year", "month", "day" ][128X[104X856[4X[32X[104X857858[33X[0;0YNow return to Sections [14X3.2[114X and [14X3.3[114X and find out what these sections mean for859records.[133X860861862[1X3.10 [33X[0;0YFurther Information about Lists[133X[101X863864[33X[0;0Y(The following cross-references point to the [5XGAP[105X Reference Manual.)[133X865866[33X[0;0YYou will find more about lists, sets, and ranges in Chapter [14X'Reference:867Lists'[114X, in particular more about identical lists in Section [14X'Reference:868Identical Lists'[114X. A more detailed description of strings is contained in869Chapter [14X'Reference: Strings and Characters'[114X. Fields are described in870Chapter [14X'Reference: Fields and Division Rings'[114X, some known fields in [5XGAP[105X are871described in Chapters [14X'Reference: Rational Numbers'[114X, [14X'Reference: Abelian872Number Fields'[114X, and [14X'Reference: Finite Fields'[114X. Row vectors and matrices are873described in more detail in Chapters [14X'Reference: Row Vectors'[114X874and [14X'Reference: Matrices'[114X. Vector spaces are described in875Chapter [14X'Reference: Vector Spaces'[114X, further matrix related structures are876described in Chapters [14X'Reference: Matrix Groups'[114X, [14X'Reference: Algebras'[114X,877and [14X'Reference: Lie Algebras'[114X.[133X878879[33X[0;0YYou will find more list operations in Chapter [14X'Reference: Lists'[114X.[133X880881[33X[0;0YRecords and functions for records are described in detail in882Chapter [14X'Reference: Records'[114X.[133X883884885886