<Chapter Label="Objects">
<Heading>Implementing circle objects</Heading>
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 <Ref Chap="Creating New Objects" BookName="ref"/> 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 <M> r \cdot s = r + s + rs </M>. 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:
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:
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:
Nevertheless, the functionality gained from such operation would be rather
limited. You will not be able to compute circle product via the infix
operator <C>*</C>, and, moreover, you will not be able to create higher
level objects such as semigroups and groups with respect to the circle
multiplication.<P/>
In order to "integrate" the circle multiplication into the &GAP; library
properly, instead of defining <E>new</E> operations for existing objects,
we should define <E>new</E> objects for which the infix operator <C>*</C>
will perform the circle multiplication. This approach is explained in the
next two sections.
</Section>
<Section Label="ObjectsDefining">
<Heading>Defining circle objects</Heading>
Thus, we are going to implement <E>circle objects</E>,
for which we can envisage the following functionality:
First we need to distinguish these new objects from other &GAP; objects.
This is done via the <E>type</E> of the objects, that is mainly determined
by their <E>category</E>, <E>representation</E> and <E>family</E>.
<P/>
We start with declaring the category <C>IsCircleObject</C> as a subcategory
of <C>IsAssociativeElement></C> and <C>IsMultiplicativeElementWithInverse</C>.
Thus, each circle object will "know" that it is <C>IsAssociativeElement</C>
and <C>IsMultiplicativeElementWithInverse</C>, and this will make it possible
to apply to circle objects such operations as <C>One</C> and <C>Inverse</C>
(the latter is allowed to return <K>fail</K> for a given circle object), and
construct semigroups generated by circle objects.
<P/>
Further we would like to create semigroups and groups generated by circle
objects. Such structures will be <E>collections</E> of circle objects, so
they will be in the category <C>CategoryCollections( IsCircleObject )</C>.
This is why immediately after we declare the underlying category of circle
objects, we need also to declare the category of their collections:
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 <C>IsPositionalObjectOneSlotRep</C>
that is the list-like representation with only one position in the list, and
then declare a synonym <C>IsDefaultCircleObject</C> that means that we are
dealing with a circle object in one-slot representation:
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 <C>CircleFamily</C> for families:
Now we install the method that stores the corresponding
circle family in this attribute:
<Example>
<![CDATA[
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 );
]]>
</Example>
Similarly, we want one-to-one correspondence between
circle elements and underlying ring elements. We declare
an attribute <C>CircleObject</C> 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:
Only after entering all code above we are able to create some circle object.
However, it is displayed just as <C><object></C>, though we can get
the underlying ring element using the "!" operator:
This method will be used by <C>Print</C> function, and also
by <C>View</C>, since we did not install special method for
<C>ViewObj</C> for circle objects. As a result of this installation,
the output became more meaningful:
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:
<Section Label="ObjectsOperations">
<Heading>Installing operations for circle objects</Heading>
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:
<Example>
<![CDATA[
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 )
]]>
</Example>
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:
<Example>
<![CDATA[
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 );
]]>
</Example>
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 <C>OneOp</C> that returns circle object
for the corresponding zero object:
Finally, to generate groups using circle objects, we need to add a method
for the <C>InverseOp</C>. 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 <M>x</M> exists, than it can be computed as
<M>-x(1+x)^{-1}</M>:
<Example>
<![CDATA[
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 )
]]>
</Example>
The last method already makes it possible to create groups generated by
circle objects (the warning may be ignored):
The &GAP; code used in this Chapter, is contained in the files
<File>circle/lib/circle.gd</File> and <File>circle/lib/circle.gi</File>
(without <C>My</C> in identifiers).
For more examples of implementing new &GAP; objects and further details
see <Ref Chap="Creating New Objects" BookName="ref"/> and subsequent
chapters in the &GAP; Reference Manual.
</Section>
</Chapter>
¤ Dauer der Verarbeitung: 0.27 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.