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[1X8 [33X[0;0YOperations and Methods[133X[101X234[1X8.1 [33X[0;0YAttributes[133X[101X56[33X[0;0YIn the preceding chapters, we have seen how to obtain information about7mathematical objects in [5XGAP[105X: We have to pass the object as an argument to a8function. For example, if [3XG[103X is a group one can call [10XSize( [3XG[103X[10X )[110X, and the9function will return a value, in our example an integer which is the size of10[3XG[103X. Computing the size of a group generally requires a substantial amount of11work, therefore it seems desirable to store the size somewhere once it has12been calculated. You should imagine that [5XGAP[105X stores the size in some place13associated with the object [3XG[103X when [10XSize( [3XG[103X[10X )[110X is executed for the first time,14and if this function call is executed again later, the size is simply looked15up and returned, without further computation.[133X1617[33X[0;0YThis means that the behavior of the function [2XSize[102X ([14XReference: Size[114X) has to18depend on whether the size for the argument [3XG[103X is already known, and if not,19that the size must be stored after it has been calculated. These two extra20tasks are done by two other functions that accompany [10XSize( [3XG[103X[10X )[110X, namely the21[13Xtester[113X [10XHasSize( [3XG[103X[10X )[110X and the [13Xsetter[113X [10XSetSize( [3XG[103X[10X, [3Xsize[103X[10X )[110X. The tester returns22[9Xtrue[109X or [9Xfalse[109X according to whether [3XG[103X has already stored its size, and the23setter puts [3Xsize[103X into a place from where [3XG[103X can directly look it up. The24function [2XSize[102X ([14XReference: Size[114X) itself is called the [13Xgetter[113X, and from the25preceding discussion we see that there must really be at least two [13Xmethods[113X26for the getter: One method is used when the tester returns [9Xfalse[109X; it is the27method which first does the real computation and then executes the setter28with the computed value. A second method is used when the tester returns29[9Xtrue[109X; it simply returns the stored value. This second method is also called30the [13Xsystem getter[113X. [5XGAP[105X functions for which several methods can be available31are called [13Xoperations[113X, so [2XSize[102X ([14XReference: Size[114X) is an example of an32operation.[133X3334[4X[32X Example [32X[104X35[4X[25Xgap>[125X [27XG := Group(List([1..3], i-> Random(SymmetricGroup(53))));;[127X[104X36[4X[25Xgap>[125X [27XSize( G ); time > 0; # the time may of course vary on your machine[127X[104X37[4X[28X4274883284060025564298013753389399649690343788366813724672000000000000[128X[104X38[4X[28Xtrue[128X[104X39[4X[25Xgap>[125X [27XSize( G ); time;[127X[104X40[4X[28X4274883284060025564298013753389399649690343788366813724672000000000000[128X[104X41[4X[28X0[128X[104X42[4X[32X[104X4344[33X[0;0YThe convenient thing for the user is that [5XGAP[105X automatically chooses the45right method for the getter, i.e., it calls a real-work getter at most once46and the system getter in all subsequent occurrences. [13XAt most once[113X because47the value of a function call like [10XSize( [3XG[103X[10X )[110X can also be set for [3XG[103X before the48getter is called at all; for example, one can call the setter directly if49one knows the size.[133X5051[33X[0;0YThe size of a group is an example of a class of things which in [5XGAP[105X are52called [13Xattributes[113X. Every attribute in [5XGAP[105X is represented by a triple of a53getter, a setter and a tester. When a new attribute is declared, all three54functions are created together and the getter contains references to the55other two. This is necessary because when the getter is called, it must56first consult the tester, and perhaps execute the setter in the end.57Therefore the getter could be implemented as follows:[133X5859[4X[32X Example [32X[104X60[4X[28Xgetter := function( obj )[128X[104X61[4X[28Xlocal value;[128X[104X62[4X[28X if tester( obj ) then[128X[104X63[4X[28X value := system_getter( obj );[128X[104X64[4X[28X else[128X[104X65[4X[28X value := real_work_getter( obj );[128X[104X66[4X[28X setter( obj, value );[128X[104X67[4X[28X fi;[128X[104X68[4X[28X return value;[128X[104X69[4X[28Xend;[128X[104X70[4X[32X[104X7172[33X[0;0YThe only function which depends on the mathematical nature of the attribute73is the real-work getter, and this is of course what the programmer of an74attribute has to install. In both cases, the getter returns the same value,75which we also call the value of the attribute (properly: the value of the76attribute for the object [10Xobj[110X). By the way: The names for setter and tester77of an attribute are always composed from the prefix [10XSet[110X resp. [10XHas[110X and the78name of the getter.[133X7980[33X[0;0YAs a (not typical) example, note that the [5XGAP[105X function [2XRandom[102X ([14XReference:81Random[114X), although it takes only one argument, is of course [13Xnot[113X an attribute,82because otherwise the first random element of a group would be stored by the83setter and returned over and over again by the system getter every time84[2XRandom[102X ([14XReference: Random[114X) is called in the sequel.[133X8586[33X[0;0YThere is a general important rule about attributes: [13XOnce the value of an87attribute for an object has been set, it cannot be reset, i.e., it cannot be88changed any more.[113X This is achieved by having two methods not only for the89getter but also for the setter: If an object already has an attribute value90stored, i.e., if the tester returns [9Xtrue[109X, the setter simply does nothing.[133X9192[4X[32X Example [32X[104X93[4X[25Xgap>[125X [27XG := SymmetricGroup(8);; Size(G);[127X[104X94[4X[28X40320[128X[104X95[4X[25Xgap>[125X [27XSetSize( G, 0 ); Size( G );[127X[104X96[4X[28X40320[128X[104X97[4X[32X[104X9899[33X[0;0Y[13XSummary.[113X In this section we have introduced attributes as triples of getter,100setter and tester and we have explained how these three functions work101together behind the scene to provide automatic storage and look-up of values102that have once been calculated. We have seen that there can be several103methods for the same function among which [5XGAP[105X automatically selects an104appropriate one.[133X105106107[1X8.2 [33X[0;0YProperties and Filters[133X[101X108109[33X[0;0YCertain attributes, like [2XIsAbelian[102X ([14XReference: IsAbelian[114X), are110boolean-valued. Such attributes are known to [5XGAP[105X as [13Xproperties[113X, because111their values are stored in a slightly different way. A property also has a112getter, a setter and a tester, but in this case, the getter as well as the113tester returns a boolean value. Therefore [5XGAP[105X stores both values in the same114way, namely as bits in a boolean list, thereby treating property getters and115all testers (of attributes or properties) uniformly. These boolean-valued116functions are called [13Xfilters[113X. You can imagine a filter as a switch which is117set either to [9Xtrue[109X or to [9Xfalse[109X. For every [5XGAP[105X object there is a boolean list118which has reserved a bit for every filter [5XGAP[105X knows about. Strictly119speaking, there is one bit for every [13Xsimple filter[113X, and these simple filters120can be combined with [10Xand[110X to form other filters (which are then [9Xtrue[109X if and121only if all the corresponding bits are set to [9Xtrue[109X). For example, the filter122[10XIsPermGroup and IsSolvableGroup[110X is made up from several simple filters.[133X123124[33X[0;0YSince they allow only two values, the bits which represent filters can be125compared very quickly, and the scheme by which [5XGAP[105X chooses the method, e.g.,126for a getter or a setter (as we have seen in the previous section), is127mostly based on the examination of filters, not on the examination of other128attribute values. Details of this [13Xmethod selection[113X are described in129chapter [14X'Reference: Method Selection'[114X.[133X130131[33X[0;0YWe only present the following rule of thumb here: Each installed method for132an attribute, say [2XSize[102X ([14XReference: Size[114X), has a [21Xrequired filter[121X, which is133made up from certain simple filters which must yield [9Xtrue[109X for the argument134[3Xobj[103X for this method to be applicable. To execute a call of [10XSize( [3Xobj[103X[10X )[110X, [5XGAP[105X135selects among all applicable methods the one whose required filter combines136the most simple filters; the idea behind is that the more an algorithm137requires of [3Xobj[103X, the more efficient it is expected to be. For example, if138[3Xobj[103X is a permutation group that is not (known to be) solvable, a method with139required filter [10XIsPermGroup and IsSolvableGroup[110X is not applicable, whereas a140method with required filter [2XIsPermGroup[102X ([14XReference: IsPermGroup[114X) can be141chosen. On the other hand, if [3Xobj[103X was known to be solvable, the method with142required filter [10XIsPermGroup and IsSolvableGroup[110X would be preferred to the143one with required filter [2XIsPermGroup[102X ([14XReference: IsPermGroup[114X).[133X144145[33X[0;0YIt may happen that a method is applicable for a given argument but cannot146compute the desired value. In such cases, the method will execute the147statement [10XTryNextMethod();[110X, and [5XGAP[105X calls the next applicable method. For148example, [Sim90] describes an algorithm to compute the size of a solvable149permutation group, which can be used also to decide whether or not a150permutation group is solvable. Suppose that the function [10Xsize_solvable[110X151implements this algorithm, and that is returns the order of the group if it152is solvable and [9Xfail[109X otherwise. Then we can install the following method for153[2XSize[102X ([14XReference: Size[114X) with required filter [2XIsPermGroup[102X ([14XReference:154IsPermGroup[114X).[133X155156[4X[32X Example [32X[104X157[4X[28Xfunction( G )[128X[104X158[4X[28Xlocal value;[128X[104X159[4X[28X value := size_solvable( G );[128X[104X160[4X[28X if value <> fail then return value;[128X[104X161[4X[28X else TryNextMethod(); fi;[128X[104X162[4X[28Xend;[128X[104X163[4X[32X[104X164165[33X[0;0YThis method can then be tried on every permutation group (whether known to166be solvable or not), and it would include a mandatory solvability test.[133X167168[33X[0;0YIf no applicable method (or no next applicable method) is found, [5XGAP[105X stops169with an error message of the form[133X170171[4X[32X Example [32X[104X172[4X[28XError, no method found! For debugging hints type ?Recovery from NoMethodFound[128X[104X173[4X[28XError, no 1st choice method found for `Size' on 1 arguments called from[128X[104X174[4X[28X... lines deleted here ...[128X[104X175[4X[32X[104X176177[33X[0;0YYou would get an error message as above if you asked for [10XSize( 1 )[110X. The178message simply says that there is no method installed for calculating the179size of [10X1[110X. Section [14X'Reference: Recovery from NoMethodFound-Errors'[114X contains180more information on how to deal with these messages.[133X181182[33X[0;0Y[13XSummary.[113X In this section we have introduced properties as special183attributes, and filters as the general concept behind property getters and184attribute testers. The values of the filters of an object govern how the185object is treated in the selection of methods for operations.[133X186187188[1X8.3 [33X[0;0YImmediate and True Methods[133X[101X189190[33X[0;0YIn the example in Section [14X8.2[114X, we have mentioned that the operation [2XSize[102X191([14XReference: Size[114X) has a method for solvable permutation groups that is so192far superior to the method for general permutation groups that it seems193worthwhile to try it even if nothing is known about solvability of the group194of which the [2XSize[102X ([14XReference: Size[114X) is to be calculated. There are other195examples where certain methods are even [21Xcheaper[121X to execute. For example, if196the size of a group is known it is easy to check whether it is odd, and if197so, the Feit-Thompson theorem allows us to set [2XIsSolvableGroup[102X ([14XReference:198IsSolvableGroup[114X) to [9Xtrue[109X for this group. [5XGAP[105X utilizes this celebrated199theorem by having an [13Ximmediate method[113X for [2XIsSolvableGroup[102X ([14XReference:200IsSolvableGroup[114X) with required filter [10XHasSize[110X which checks parity of the201size and either sets [2XIsSolvableGroup[102X ([14XReference: IsSolvableGroup[114X) or does202nothing, i.e., calls [10XTryNextMethod()[110X. These immediate methods are executed203automatically for an object whenever the value of a filter changes, so204solvability of a group will automatically be detected when an odd size has205been calculated for it (and therefore the value of [10XHasSize[110X for that group206has changed to [9Xtrue[109X).[133X207208[33X[0;0YSome methods are even more immediate, because they do not require any209calculation at all: They allow a filter to be set if another filter is also210set. In other words, they model a mathematical implication like [10XIsGroup and211IsCyclic[110X implies [10XIsSolvableGroup[110X and such implications can be installed in212[5XGAP[105X as [13Xtrue methods[113X. To execute true methods, [5XGAP[105X only needs to do some213bookkeeping with its filters, therefore true methods are much faster than214immediate methods.[133X215216[33X[0;0YHow immediate and true methods are installed is described in [14X'Reference:217Immediate Methods'[114X and [14X'Reference: Logical Implications'[114X.[133X218219220[1X8.4 [33X[0;0YOperations and Method Selection[133X[101X221222[33X[0;0YThe method selection is not only used to select methods for attribute223getters but also for arbitrary [13Xoperations[113X, which can have more than one224argument. In this case, there is a required filter for each argument (which225must yield [9Xtrue[109X for the corresponding arguments).[133X226227[33X[0;0YAdditionally, a method with at least two arguments may require a certain228relation between the arguments, which is expressed in terms of the [13Xfamilies[113X229of the arguments. For example, the methods for [10XConjugateGroup( [3Xgrp[103X[10X, [3Xelm[103X[10X )[110X230require that [3Xelm[103X lies in the family of elements from which [3Xgrp[103X is made,231i.e., that the family of [3Xelm[103X equals the [21Xelements family[121X of [3Xgrp[103X.[133X232233[33X[0;0YFor permutation groups, the situation is quite easy: all permutations form234one family, [2XPermutationsFamily[102X ([14XReference: PermutationsFamily[114X), and each235collection of permutations, for example each permutation group, each coset236of a permutation group, or each dense list of permutations, lies in237[10XCollectionsFamily( PermutationsFamily )[110X.[133X238239[33X[0;0YFor other kinds of group elements, the situation can be different. Every240call of [2XFreeGroup[102X ([14XReference: FreeGroup[114X) constructs a new family of free241group elements. [5XGAP[105X refuses to compute [10XOne( FreeGroup( 1 ) ) * One(242FreeGroup( 1 ) )[110X because the two operands of the multiplication lie in243different families and no method is installed for this case.[133X244245[33X[0;0YFor further information on family relations, see [14X'Reference: Families'[114X.[133X246247[33X[0;0YIf you want to know which properties are already known for an object [3Xobj[103X, or248which properties are known to be true, you can use the functions249[10XKnownPropertiesOfObject( [3Xobj[103X[10X )[110X resp. [10XKnownTruePropertiesOfObject( [3Xobj[103X[10X )[110X.250This will print a list of names of properties. These names are also the251identifiers of the property getters, by which you can retrieve the value of252the properties (and confirm that they are really [9Xtrue[109X). Analogously, there253is the function [2XKnownAttributesOfObject[102X ([14XReference: KnownAttributesOfObject[114X)254which lists the names of the known attributes, leaving out the properties.[133X255256[33X[0;0YSince [5XGAP[105X lets you know what it already knows about an object, it is only257natural that it also lets you know what methods it considers applicable for258a certain method, and in what order it will try them (in case259[10XTryNextMethod()[110X occurs). [10XApplicableMethod( [3Xopr[103X[10X, [ arg_1, arg_2, ... ] )[110X260returns the first applicable method for the call [10X[3Xopr[103X[10X( arg_1, arg_2, ... )[110X.261More generally, [10XApplicableMethod( [3Xopr[103X[10X, [ ... ], 0, [3Xnr[103X[10X )[110X returns the [3Xnr[103Xth262applicable method (i.e., the one that would be chosen after [22X[3Xnr[103X-1[122X calls of263[10XTryNextMethod[110X) and if [3Xnr[103X[10X = "all"[110X, the sorted list of all applicable methods264is returned. For details, see [14X'Reference: Applicable Methods and Method265Selection'[114X.[133X266267[33X[0;0YIf you want to see which methods are chosen for certain operations while [5XGAP[105X268code is being executed, you can call the function [2XTraceMethods[102X ([14XReference:269TraceMethods for a list of operations[114X) with a list of these operations as270arguments.[133X271272[4X[32X Example [32X[104X273[4X[25Xgap>[125X [27XTraceMethods( [ Size ] );[127X[104X274[4X[25Xgap>[125X [27Xg:= Group( (1,2,3), (1,2) );; Size( g );[127X[104X275[4X[28X#I Size: for a permutation group[128X[104X276[4X[28X#I Setter(Size): system setter[128X[104X277[4X[28X#I Size: system getter[128X[104X278[4X[28X#I Size: system getter[128X[104X279[4X[28X6[128X[104X280[4X[32X[104X281282[33X[0;0YThe system getter is called once to fetch the freshly computed value for283returning to the user. The second call is triggered by an immediate method.284To find out by which, we can trace the immediate methods by saying285[10XTraceImmediateMethods( true )[110X.[133X286287[4X[32X Example [32X[104X288[4X[25Xgap>[125X [27XTraceImmediateMethods( true );[127X[104X289[4X[25Xgap>[125X [27Xg:= Group( (1,2,3), (1,2) );;[127X[104X290[4X[28X#I immediate: Size[128X[104X291[4X[28X#I immediate: IsCyclic[128X[104X292[4X[28X#I immediate: IsCommutative[128X[104X293[4X[28X#I immediate: IsTrivial[128X[104X294[4X[25Xgap>[125X [27XSize( g );[127X[104X295[4X[28X#I Size: for a permutation group[128X[104X296[4X[28X#I immediate: IsNonTrivial[128X[104X297[4X[28X#I immediate: Size[128X[104X298[4X[28X#I immediate: IsFreeAbelian[128X[104X299[4X[28X#I immediate: IsTorsionFree[128X[104X300[4X[28X#I immediate: IsNonTrivial[128X[104X301[4X[28X#I immediate: GeneralizedPcgs[128X[104X302[4X[28X#I Setter(Size): system setter[128X[104X303[4X[28X#I Size: system getter[128X[104X304[4X[28X#I immediate: IsPerfectGroup[128X[104X305[4X[28X#I Size: system getter[128X[104X306[4X[28X#I immediate: IsEmpty[128X[104X307[4X[28X6[128X[104X308[4X[25Xgap>[125X [27XTraceImmediateMethods( false );[127X[104X309[4X[25Xgap>[125X [27XUntraceMethods( [ Size ] );[127X[104X310[4X[32X[104X311312[33X[0;0YThe last two lines switch off tracing again. We now see that the system313getter was called by the immediate method for [2XIsPerfectGroup[102X ([14XReference:314IsPerfectGroup[114X). Also the above-mentioned immediate method for315[2XIsSolvableGroup[102X ([14XReference: IsSolvableGroup[114X) was not used because the316solvability of [10Xg[110X was already found out during the size calculation (cf. the317example in Section [14X8.2[114X).[133X318319[33X[0;0Y[13XSummary.[113X In this section and the last we have looked some more behind the320scenes and seen that [5XGAP[105X automatically executes immediate and true methods321to deduce information about objects that is cheaply available. We have seen322how this can be supervised by tracing the methods.[133X323324325326