<h3>2 <span class="Heading">Implementing circle objects</span></h3>
<p>In this chapter we explain how the <strong class="pkg">GAP</strong> system may be extended with new objects using the circle multiplication as an example. We follow the guidelines given in the <strong class="pkg">GAP</strong> Reference Manual (see <a href="../../../doc/ref/chap79_mj.html#X83548994805AD1C9"><span class="RefLink">Reference: Creating New Objects</span></a> and subsequent chapters), to which we refer for more details.</p>
<p>Of course, having two ring elements, you can straightforwardly compute their circle product defined as <span class="SimpleMath">\( r \cdot s = r + s + rs \)</span>. 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:</p>
<p>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:</p>
<p>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:</p>
<p>Nevertheless, the functionality gained from such operation would be rather limited. You will not be able to compute circle product via the infix operator <code class="code">*</code>, and, moreover, you will not be able to create higher level objects such as semigroups and groups with respect to the circle multiplication.</p>
<p>In order to "integrate" the circle multiplication into the <strong class="pkg">GAP</strong> library properly, instead of defining <em>new</em> operations for existing objects, we should define <em>new</em> objects for which the infix operator <code class="code">*</code> will perform the circle multiplication. This approach is explained in the next two sections.</p>
<p>First we need to distinguish these new objects from other <strong class="pkg">GAP</strong> objects. This is done via the <em>type</em> of the objects, that is mainly determined by their <em>category</em>, <em>representation</em> and <em>family</em>.</p>
<p>We start with declaring the category <code class="code">IsCircleObject</code> as a subcategory of <code class="code">IsAssociativeElement></code> and <code class="code">IsMultiplicativeElementWithInverse</code>. Thus, each circle object will "know" that it is <code class="code">IsAssociativeElement</code> and <code class="code">IsMultiplicativeElementWithInverse</code>, and this will make it possible to apply to circle objects such operations as <code class="code">One</code> and <code class="code">Inverse</code> (the latter is allowed to return <code class="keyw">fail</code> for a given circle object), and construct semigroups generated by circle objects.</p>
<p>Further we would like to create semigroups and groups generated by circle objects. Such structures will be <em>collections</em> of circle objects, so they will be in the category <code class="code">CategoryCollections( IsCircleObject )</code>. This is why immediately after we declare the underlying category of circle objects, we need also to declare the category of their collections:</p>
<p>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 <code class="code">IsPositionalObjectOneSlotRep</code> that is the list-like representation with only one position in the list, and then declare a synonym <code class="code">IsDefaultCircleObject</code> that means that we are dealing with a circle object in one-slot representation:</p>
<p>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 <code class="code">CircleFamily</code> for families:</p>
<p>Now we install the method that stores the corresponding circle family in this attribute:</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( MyCircleFamily,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for a family",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsFamily ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( Fam )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> local F;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> # create the family of circle elements</span>
<span class="GAPprompt">></span> <span class="GAPinput"> F:= NewFamily( "MyCircleFamily(...)", IsMyCircleObject );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if HasCharacteristic( Fam ) then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> SetCharacteristic( F, Characteristic( Fam ) );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> # store the type of objects in the output</span>
<span class="GAPprompt">></span> <span class="GAPinput"> F!.MyCircleType:= NewType( F, IsMyDefaultCircleObject );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> # Return the circle family</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return F;</span>
<span class="GAPprompt">></span> <span class="GAPinput">end );</span>
</pre></div>
<p>Similarly, we want one-to-one correspondence between circle elements and underlying ring elements. We declare an attribute <code class="code">CircleObject</code> 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:</p>
<p>Only after entering all code above we are able to create some circle object. However, it is displayed just as <code class="code"><object></code>, though we can get the underlying ring element using the "!" operator:</p>
<p>We can not multiply circle objects yet. But before implementing this, first let us improve the output by installing the method for <code class="code">PrintObj</code>:</p>
<p>This method will be used by <code class="code">Print</code> function, and also by <code class="code">View</code>, since we did not install special method for <code class="code">ViewObj</code> for circle objects. As a result of this installation, the output became more meaningful:</p>
<p>We need to avoid the usage of "!" operator, which, in general, is not recommended to the user (for example, if <strong class="pkg">GAP</strong> developers will change the internal representation of some object, all <strong class="pkg">GAP</strong> 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:
<h4>2.3 <span class="Heading">Installing operations for circle objects</span></h4>
<p>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:</p>
<p>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:</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( \=,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for two objects in `IsMyCircleObject'",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> IsIdenticalObj,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsMyDefaultCircleObject, IsMyDefaultCircleObject ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( a, b )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return a![1] = b![1];</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( \<,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for two objects in `IsMyCircleObject'",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> IsIdenticalObj,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsMyDefaultCircleObject, IsMyDefaultCircleObject ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( a, b )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return a![1] < b![1];</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end );</span>
</pre></div>
<p>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 <code class="code">OneOp</code> that returns circle object for the corresponding zero object:</p>
<p>Finally, to generate groups using circle objects, we need to add a method for the <code class="code">InverseOp</code>. 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 <span class="SimpleMath">\(x\)</span> exists, than it can be computed as <span class="SimpleMath">\(-x(1+x)^{-1}\)</span>:</p>
<p>The <strong class="pkg">GAP</strong> code used in this Chapter, is contained in the files <code class="file">circle/lib/circle.gd</code> and <code class="file">circle/lib/circle.gi</code> (without <code class="code">My</code> in identifiers). For more examples of implementing new <strong class="pkg">GAP</strong> objects and further details see <a href="../../../doc/ref/chap79_mj.html#X83548994805AD1C9"><span class="RefLink">Reference: Creating New Objects</span></a> and subsequent chapters in the <strong class="pkg">GAP</strong> Reference Manual.</p>
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.