In this chapter we explain how the **GAP** system may be extended with new objects using the circle multiplication as an example. We follow the guidelines given in the **GAP** Reference Manual (see Reference: Creating New Objects and subsequent chapters), to which we refer for more details.

Of course, having two ring elements, you can straightforwardly compute their circle product defined as \( r \cdot s = r + s + rs \). You can do this in a command line, and it is a trivial task to write a simplest function of two arguments that will do this:

gap> CircleMultiplication := function(a,b) > return a+b+a*b; > end; function( a, b ) ... end gap> CircleMultiplication(2,3); 11 gap> CircleMultiplication( ZmodnZObj(2,8), ZmodnZObj(5,8) ); ZmodnZObj( 1, 8 )

However, there is no check whether both arguments belong to the same ring and whether they are ring elements at all, so it is easy to obtain some meaningless results:

gap> CircleMultiplication( 3, ZmodnZObj(3,8) ); ZmodnZObj( 7, 8 ) gap> CircleMultiplication( [1], [2,3] ); [ 5, 5 ]

You can include some tests for arguments, and maybe the best way of doing this would be declaring a new operation for two ring elements, and installing the previous function as a method for this operation. This will check automatically if the arguments are ring elements from the common ring:

gap> DeclareOperation( "BetterCircleMultiplication", > [IsRingElement,IsRingElement] ); gap> InstallMethod( BetterCircleMultiplication, > IsIdenticalObj, > [IsRingElement,IsRingElement], > CircleMultiplication ); gap> BetterCircleMultiplication(2,3); 11 gap> BetterCircleMultiplication( ZmodnZObj(2,8), ZmodnZObj(5,8) ); ZmodnZObj( 1, 8 )

Nevertheless, the functionality gained from such operation would be rather limited. You will not be able to compute circle product via the infix operator `*`

, and, moreover, you will not be able to create higher level objects such as semigroups and groups with respect to the circle multiplication.

In order to "integrate" the circle multiplication into the **GAP** library properly, instead of defining *new* operations for existing objects, we should define *new* objects for which the infix operator `*`

will perform the circle multiplication. This approach is explained in the next two sections.

Thus, we are going to implement *circle objects*, for which we can envisage the following functionality:

gap> CircleObject( 2 ) * CircleObject( 3 ); CircleObject( 11 )

First we need to distinguish these new objects from other **GAP** objects. This is done via the *type* of the objects, that is mainly determined by their *category*, *representation* and *family*.

We start with declaring the category `IsCircleObject`

as a subcategory of `IsAssociativeElement>`

and `IsMultiplicativeElementWithInverse`

. Thus, each circle object will "know" that it is `IsAssociativeElement`

and `IsMultiplicativeElementWithInverse`

, and this will make it possible to apply to circle objects such operations as `One`

and `Inverse`

(the latter is allowed to return `fail`

for a given circle object), and construct semigroups generated by circle objects.

gap> DeclareCategory( "IsMyCircleObject", > IsAssociativeElement and IsMultiplicativeElementWithInverse );

Further we would like to create semigroups and groups generated by circle objects. Such structures will be *collections* of circle objects, so they will be in the category `CategoryCollections( IsCircleObject )`

. This is why immediately after we declare the underlying category of circle objects, we need also to declare the category of their collections:

gap> DeclareCategoryCollections( "IsMyCircleObject" );

On the next step we should think about the internal representation of circle objects. A natural way would be to store the underlying ring element in a list-like structure at its first position. We do not foresee any other data that we need to store internally in the circle object. This is quite common situation, so we may define first `IsPositionalObjectOneSlotRep`

that is the list-like representation with only one position in the list, and then declare a synonym `IsDefaultCircleObject`

that means that we are dealing with a circle object in one-slot representation:

gap> DeclareRepresentation( "IsMyPositionalObjectOneSlotRep", > IsPositionalObjectRep, [ 1 ] ); gap> DeclareSynonym( "IsMyDefaultCircleObject", > IsMyCircleObject and IsMyPositionalObjectOneSlotRep );

Until now we are still unable to create circle objects, because we did not specify to which family they will belong. Naturally, having a ring, we want to have all circle objects for elements of this ring in the same family to be able to multiply them, and we expect circle objects for elements of different rings to be placed in different families. Thus, it would be nice to establish one-to-one correspondence between the family of ring elements and a family of circle elements for this ring. We can store the corresponding circle family as an attribute of the ring elements family. To do this first we declare an attribute `CircleFamily`

for families:

gap> DeclareAttribute( "MyCircleFamily", IsFamily );

Now we install the method that stores the corresponding circle family in this attribute:

gap> InstallMethod( MyCircleFamily, > "for a family", > [ IsFamily ], > function( Fam ) > local F; > # create the family of circle elements > F:= NewFamily( "MyCircleFamily(...)", IsMyCircleObject ); > if HasCharacteristic( Fam ) then > SetCharacteristic( F, Characteristic( Fam ) ); > fi; > # store the type of objects in the output > F!.MyCircleType:= NewType( F, IsMyDefaultCircleObject ); > # Return the circle family > return F; > end );

Similarly, we want one-to-one correspondence between circle elements and underlying ring elements. We declare an attribute `CircleObject`

for a ring element, and then install the method to create new circle object from the ring element. This method takes the family of the ring element, finds corresponding circle family, extracts from it the type of circle objects and finally creates the new circle object of that type:

gap> DeclareAttribute( "MyCircleObject", IsRingElement ); gap> InstallMethod( MyCircleObject, > "for a ring element", > [ IsRingElement ], > obj -> Objectify( MyCircleFamily( FamilyObj( obj ) )!.MyCircleType, > [ Immutable( obj ) ] ) );

Only after entering all code above we are able to create some circle object. However, it is displayed just as `<object>`

, though we can get the underlying ring element using the "!" operator:

gap> a:=MyCircleObject(2); <object> gap> a![1]; 2

We can check that the intended relation between families holds:

gap> FamilyObj( MyCircleObject ( 2 ) ) = MyCircleFamily( FamilyObj( 2 ) ); true

We can not multiply circle objects yet. But before implementing this, first let us improve the output by installing the method for `PrintObj`

:

gap> InstallMethod( PrintObj, > "for object in `IsMyCircleObject'", > [ IsMyDefaultCircleObject ], > function( obj ) > Print( "MyCircleObject( ", obj![1], " )" ); > end );

This method will be used by `Print`

function, and also by `View`

, since we did not install special method for `ViewObj`

for circle objects. As a result of this installation, the output became more meaningful:

gap> a; MyCircleObject( 2 )

We need to avoid the usage of "!" operator, which, in general, is not recommended to the user (for example, if **GAP** developers will change the internal representation of some object, all **GAP** functions that deal with it must be adjusted appropriately, while if the user's code had direct access to that representation via "!", an error may occur). To do this, we wrap getting the first component of a circle object in the following operation:

gap> DeclareAttribute("UnderlyingRingElement", IsMyCircleObject ); gap> InstallMethod( UnderlyingRingElement, > "for a circle object", > [ IsMyCircleObject], > obj -> obj![1] ); gap> UnderlyingRingElement(a); 2

Now we are finally able to install circle multiplication as a default method for the multiplication of circle objects, and perform the computation that we envisaged in the beginning:

gap> InstallMethod( \*, > "for two objects in `IsMyCircleObject'", > IsIdenticalObj, > [ IsMyDefaultCircleObject, IsMyDefaultCircleObject ], > function( a, b ) > return MyCircleObject( a![1] + b![1] + a![1]*b![1] ); > end ); gap> MyCircleObject(2)*MyCircleObject(3); MyCircleObject( 11 )

However, this functionality is not enough to form semigroups or groups generated by circle elements. We need to be able to check whether two circle objects are equal, and we need to define ordering for them (for example, to be able to form sets of circle elements). Since we already have both operations for underlying ring elements, this can be implemented in a straightforward way:

gap> InstallMethod( \=, > "for two objects in `IsMyCircleObject'", > IsIdenticalObj, > [ IsMyDefaultCircleObject, IsMyDefaultCircleObject ], > function( a, b ) > return a![1] = b![1]; > end ); gap> InstallMethod( \<, > "for two objects in `IsMyCircleObject'", > IsIdenticalObj, > [ IsMyDefaultCircleObject, IsMyDefaultCircleObject ], > function( a, b ) > return a![1] < b![1]; > end );

Further, zero element of the ring plays a role of the neutral element for the circle multiplication, and we add this knowledge to our code in a form of a method for `OneOp`

that returns circle object for the corresponding zero object:

gap> InstallMethod( OneOp, > "for an object in `IsMyCircleObject'", > [ IsMyDefaultCircleObject ], > a -> MyCircleObject( Zero( a![1] ) ) ); gap> One(a); MyCircleObject( 0 )

Now we are already able to create monoids generated by circle objects:

gap> S:=Monoid(a); <commutative monoid with 1 generator> gap> One(S); MyCircleObject( 0 ) gap> S:=Monoid( MyCircleObject( ZmodnZObj( 2,8) ) ); <commutative monoid with 1 generator> gap> Size(S); 2 gap> AsList(S); [ MyCircleObject( ZmodnZObj( 0, 8 ) ), MyCircleObject( ZmodnZObj( 2, 8 ) ) ]

Finally, to generate groups using circle objects, we need to add a method for the `InverseOp`

. In our implementation we will assume that the underlying ring is a subring of the ring with one, thus, if the circle inverse for an element \(x\) exists, than it can be computed as \(-x(1+x)^{-1}\):

gap> InstallMethod( InverseOp, > "for an object in `IsMyCircleObject'", > [ IsMyDefaultCircleObject ], > function( a ) > local x; > x := Inverse( One( a![1] ) + a![1] ); > if x = fail then > return fail; > else > return MyCircleObject( -a![1] * x ); > fi; > end ); gap> MyCircleObject(-2)^-1; MyCircleObject( -2 ) gap> MyCircleObject(2)^-1; MyCircleObject( -2/3 )

The last method already makes it possible to create groups generated by circle objects (the warning may be ignored):

gap> Group( MyCircleObject(2) ); #I default `IsGeneratorsOfMagmaWithInverses' method returns `true' for [ MyCircleObject( 2 ) ] <group with 1 generators> gap> G:=Group( [MyCircleObject( ZmodnZObj( 2,8 ) ) ]); #I default `IsGeneratorsOfMagmaWithInverses' method returns `true' for [ MyCircleObject( ZmodnZObj( 2, 8 ) ) ] <group with 1 generators> gap> Size(G); 2 gap> AsList(G); [ MyCircleObject( ZmodnZObj( 0, 8 ) ), MyCircleObject( ZmodnZObj( 2, 8 ) ) ]

The **GAP** code used in this Chapter, is contained in the files `circle/lib/circle.gd`

and `circle/lib/circle.gi`

(without `My`

in identifiers). For more examples of implementing new **GAP** objects and further details see Reference: Creating New Objects and subsequent chapters in the **GAP** Reference Manual.

generated by GAPDoc2HTML