Quellcode-Bibliothek chap81.html
Sprache: HTML
|
|
| products/Sources/formale Sprachen/GAP/doc/ref/chap81.html |
 |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>GAP (ref) - Chapter 81: An Example – Residue Class Rings</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="generator" content="GAPDoc2HTML" />
<link rel="stylesheet" type="text/css" href="manual.css" />
<script src="manual.js" type="text/javascript"></script>
<script type="text/javascript">overwriteStyle();</script>
</head>
<body class="chap81" onload="jscontent()">
<div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
<div class="chlinkprevnexttop"> <a href="chap0.html">[Top of Book]</a> <a href="chap0.html#contents">[Contents]</a> <a href="chap80.html">[Previous Chapter]</a> <a href="chap82.html">[Next Chapter]</a> </div>
<p id="mathjaxlink" class="pcenter"><a href="chap81_mj.html">[MathJax on]</a></p>
<p><a id="X8125CC6A87409887" name="X8125CC6A87409887"></a></p>
<div class="ChapSects"><a href="chap81.html#X8125CC6A87409887">81 <span class="Heading">An Example – Residue Class Rings</span></a>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap81.html#X81008A74838A792E">81.1 <span class="Heading">A First Attempt to Implement Elements of Residue Class Rings</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap81.html#X78B6425787FDB0E5">81.2 <span class="Heading">Why Proceed in a Different Way?</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap81.html#X85B914DD81732492">81.3 <span class="Heading">A Second Attempt to Implement Elements of Residue Class Rings</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap81.html#X83127B258512C436">81.4 <span class="Heading">Compatibility of Residue Class Rings with Prime Fields</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap81.html#X81CA1C7087A815DE">81.5 <span class="Heading">Further Improvements in Implementing Residue Class Rings</span></a>
</span>
</div>
</div>
<h3>81 <span class="Heading">An Example – Residue Class Rings</span></h3>
<p>In this chapter, we give an example how <strong class="pkg">GAP</strong> can be extended by new data structures and new functionality. In order to focus on the issues of the implementation, the mathematics in the example chosen is trivial. Namely, we will discuss computations with elements of residue class rings <span class="SimpleMath">ℤ / nℤ</span>.</p>
<p>The first attempt is straightforward (see Section <a href="chap81.html#X81008A74838A792E"><span class="RefLink">81.1</span></a>), it deals with the implementation of the necessary arithmetic operations. Section <a href="chap81.html#X78B6425787FDB0E5"><span class="RefLink">81.2</span></a> deals with the question why it might be useful to use an approach that involves creating a new data structure and integrating the algorithms dealing with these new <strong class="pkg">GAP</strong> objects into the system. Section <a href="chap81.html#X85B914DD81732492"><span class="RefLink">81.3</span></a> shows how this can be done in our example, and Section <a href="chap81.html#X83127B258512C436"><span class="RefLink">81.4</span></a>, the question of further compatibility of the new objects with known <strong class="pkg">GAP</strong> objects is discussed. Finally, Section <a href="chap81.html#X81CA1C7087A815DE"><span class="RefLink">81.5</span></a> gives some hints how to improve the implementation presented before.</p>
<p><a id="X81008A74838A792E" name="X81008A74838A792E"></a></p>
<h4>81.1 <span class="Heading">A First Attempt to Implement Elements of Residue Class Rings</span></h4>
<p>Suppose we want to do computations with elements of a ring <span class="SimpleMath">ℤ / nℤ</span>, where <span class="SimpleMath">n</span> is a positive integer.</p>
<p>First we have to decide how to represent the element <span class="SimpleMath">k + nℤ</span> in <strong class="pkg">GAP</strong>. If the modulus <span class="SimpleMath">n</span> is fixed then we can use the integer <span class="SimpleMath">k</span>. More precisely, we can use any integer <span class="SimpleMath">k' such that k - k'</span> is a multiple of <span class="SimpleMath">n</span>. If different moduli are likely to occur then using a list of the form <span class="SimpleMath">[ k, n ]</span>, or a record of the form <code class="code">rec( residue := <var class="Arg">k</var>, modulus := <var class="Arg">n</var> )</code> is more appropriate. In the following, let us assume the list representation <span class="SimpleMath">[ k, n ]</span> is chosen. Moreover, we decide that the residue <span class="SimpleMath">k</span> in all such lists satisfies <span class="SimpleMath">0 ≤ k < n</span>, i.e., the result of adding two residue classes represented by <span class="SimpleMath">[ k_1, n ]</span> and <span class="SimpleMath">[ k_2, n ]</span> (of course with same modulus <span class="SimpleMath">n</span>) will be <span class="SimpleMath">[ k, n ]</span> with <span class="SimpleMath">k_1 + k_2</span> congruent to <span class="SimpleMath">k</span> modulo <span class="SimpleMath">n</span> and <span class="SimpleMath">0 ≤ k < n</span>.</p>
<p>Now we can implement the arithmetic operations for residue classes. Note that the result of the <code class="keyw">mod</code> operator is normalized as required. The division by a noninvertible residue class results in <code class="keyw">fail</code>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">resclass_sum := function( c1, c2 )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if c1[2] <> c2[2] then Error( "different moduli" ); fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return [ ( c1[1] + c2[1] ) mod c1[2], c1[2] ];</span>
<span class="GAPprompt">></span> <span class="GAPinput">end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">resclass_diff := function( c1, c2 )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if c1[2] <> c2[2] then Error( "different moduli" ); fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return [ ( c1[1] - c2[1] ) mod c1[2], c1[2] ];</span>
<span class="GAPprompt">></span> <span class="GAPinput">end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">resclass_prod := function( c1, c2 )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if c1[2] <> c2[2] then Error( "different moduli" ); fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return [ ( c1[1] * c2[1] ) mod c1[2], c1[2] ];</span>
<span class="GAPprompt">></span> <span class="GAPinput">end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">resclass_quo := function( c1, c2 )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> local quo;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if c1[2] <> c2[2] then Error( "different moduli" ); fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> quo:= QuotientMod( c1[1], c2[1], c1[2] );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if quo <> fail then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> quo:= [ quo, c1[2] ];</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return quo;</span>
<span class="GAPprompt">></span> <span class="GAPinput">end;;</span>
</pre></div>
<p>With these functions, we can in principle compute with residue classes.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">list:= List( [ 0 .. 3 ], k -> [ k, 4 ] );</span>
[ [ 0, 4 ], [ 1, 4 ], [ 2, 4 ], [ 3, 4 ] ]
<span class="GAPprompt">gap></span> <span class="GAPinput">resclass_sum( list[2], list[4] );</span>
[ 0, 4 ]
<span class="GAPprompt">gap></span> <span class="GAPinput">resclass_diff( list[1], list[2] );</span>
[ 3, 4 ]
<span class="GAPprompt">gap></span> <span class="GAPinput">resclass_prod( list[2], list[4] );</span>
[ 3, 4 ]
<span class="GAPprompt">gap></span> <span class="GAPinput">resclass_prod( list[3], list[4] );</span>
[ 2, 4 ]
<span class="GAPprompt">gap></span> <span class="GAPinput">List( list, x -> resclass_quo( list[2], x ) );</span>
[ fail, [ 1, 4 ], fail, [ 3, 4 ] ]
</pre></div>
<p><a id="X78B6425787FDB0E5" name="X78B6425787FDB0E5"></a></p>
<h4>81.2 <span class="Heading">Why Proceed in a Different Way?</span></h4>
<p>It depends on the computations we intended to do with residue classes whether or not the implementation described in the previous section is satisfactory for us.</p>
<p>Probably we are mainly interested in more complex data structures than the residue classes themselves, for example in matrix algebras or matrix groups over a ring such as <span class="SimpleMath">ℤ / 4ℤ</span>. For this, we need functions to add, multiply, invert etc. matrices of residue classes. Of course this is not a difficult task, but it requires to write additional <strong class="pkg">GAP</strong> code.</p>
<p>And when we have implemented the arithmetic operations for matrices of residue classes, we might be interested in domain operations such as computing the order of a matrix group over <span class="SimpleMath">ℤ / 4ℤ</span>, a Sylow <span class="SimpleMath">2</span> subgroup, and so on. The problem is that a residue class represented as a pair <span class="SimpleMath">[ k, n ]</span> is not regarded as a group element by <strong class="pkg">GAP</strong>. We have not yet discussed how a matrix of residue classes shall be represented, but if we choose the obvious representation of a list of lists of our residue classes then also this is not a valid group element in <strong class="pkg">GAP</strong>. Hence we cannot apply the function <code class="func">Group</code> (<a href="chap39.html#X7D7B075385435151"><span class="RefLink">39.2-1</span></a>) to create a group of residue classes or a group of matrices of residue classes. This is because <strong class="pkg">GAP</strong> assumes that group elements can be multiplied via the infix operator <code class="code">*</code> (equivalently, via the operation <code class="func">\*</code> (<a href="chap31.html#X8481C9B97B214C23"><span class="RefLink">31.12-1</span></a>)). Note that in fact the multiplication of two lists <span class="SimpleMath">[ k_1, n ]</span>, <span class="SimpleMath">[ k_2, n ]</span> is defined, but we have <span class="SimpleMath">[ k_1, n ] * [ k_2, n ] = k_1 * k_2 + n * n</span>, the standard scalar product of two row vectors of same length. That is, the multiplication with <code class="code">*</code> is not compatible with the function <code class="code">resclass_prod</code> introduced in the previous section. Similarly, ring elements are assumed to be added via the infix operator <code class="code">+</code>; the addition of residue classes is not compatible with the available addition of row vectors.</p>
<p>What we have done in the previous section can be described as implementation of a <q>standalone</q> arithmetic for residue classes. In order to use the machinery of the <strong class="pkg">GAP</strong> library for creating higher level objects such as matrices, polynomials, or domains over residue class rings, we have to <q>integrate</q> this implementation into the <strong class="pkg">GAP</strong> library. The key step will be to create a new kind of <strong class="pkg">GAP</strong> objects. This will be done in the following sections; there we assume that residue classes and residue class rings are not yet available in <strong class="pkg">GAP</strong>; in fact they are available, and their implementation is very close to what is described here.</p>
<p><a id="X85B914DD81732492" name="X85B914DD81732492"></a></p>
<h4>81.3 <span class="Heading">A Second Attempt to Implement Elements of Residue Class Rings</span></h4>
<p>Faced with the problem to implement elements of the rings <span class="SimpleMath">ℤ / nℤ</span>, we must define the <em>types</em> of these elements as far as is necessary to distinguish them from other <strong class="pkg">GAP</strong> objects.</p>
<p>As is described in Chapter <a href="chap13.html#X7E8202627B421DB1"><span class="RefLink">13</span></a>, the type of an object comprises several aspects of information about this object; the <em>family</em> determines the relation of the object to other objects, the <em>categories</em> determine what operations the object admits, the <em>representation</em> determines how an object is actually represented, and the <em>attributes</em> describe knowledge about the object.</p>
<p>First of all, we must decide about the <em>family</em> of each residue class. A natural way to do this is to put the elements of each ring <span class="SimpleMath">ℤ / nℤ</span> into a family of their own. This means that for example elements of <span class="SimpleMath">ℤ / 3ℤ</span> and <span class="SimpleMath">ℤ / 9ℤ</span> lie in different families. So the only interesting relation between the families of two residue classes is equality; binary arithmetic operations with two residue classes will be admissible only if their families are equal. Note that in the naive approach in Section <a href="chap81.html#X81008A74838A792E"><span class="RefLink">81.1</span></a>, we had to take care of different moduli by a check in each function; these checks may disappear in the new approach because of our choice of families.</p>
<p>Note that we do not need to tell <strong class="pkg">GAP</strong> anything about the above decision concerning the families of the objects that we are going to implement, that is, the <em>declaration part</em> (see <a href="chap79.html#X7837CA9A83D93B38"><span class="RefLink">79.11</span></a>) of the little <strong class="pkg">GAP</strong> package we are writing contains nothing about the distribution of the new objects into families. (The actual construction of a family happens in the function <code class="code">MyZmodnZ</code> shown below.)</p>
<p>Second, we want to describe methods to add or multiply two elements in <span class="SimpleMath">ℤ / nℤ</span>, and these methods shall be not applicable to other <strong class="pkg">GAP</strong> objects. The natural way to do this is to create a new <em>category</em> in which all elements of all rings <span class="SimpleMath">ℤ / nℤ</span> lie. This is done as follows.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">DeclareCategory( "IsMyZmodnZObj", IsScalar );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">cat:= CategoryCollections( IsMyZmodnZObj );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">cat:= CategoryCollections( cat );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">cat:= CategoryCollections( cat );;</span>
</pre></div>
<p>So all elements in the rings <span class="SimpleMath">ℤ / nℤ</span> will lie in the category <code class="code">IsMyZmodnZObj</code>, which is a subcategory of <code class="func">IsScalar</code> (<a href="chap31.html#X8113834E84FD0435"><span class="RefLink">31.14-20</span></a>). The latter means that one can add, subtract, multiply and divide two such elements that lie in the same family, with the obvious restriction that the second operand of a division must be invertible. (The name <code class="code">IsMyZmodnZObj</code> is chosen because <code class="func">IsZmodnZObj</code> (<a href="chap14.html#X7D0107DD79753901"><span class="RefLink">14.5-4</span></a>) is already defined in <strong class="pkg">GAP</strong>, for an implementation of residue classes that is very similar to the one developed in this manual chapter. Using this different name, one can simply enter the <strong class="pkg">GAP</strong> code of this chapter into a <strong class="pkg">GAP</strong> session, either interactively or by reading a file with this code, and experiment after each step whether the expected behaviour has been achieved, and what is still missing.)</p>
<p>The next lines of <strong class="pkg">GAP</strong> code above create the categories <code class="code">CategoryCollections( IsMyZmodnZObj )</code> and two higher levels of collections categories of this, which will be needed later; it is important to create these categories before collections of the objects in <code class="code">IsMyZmodnZObj</code> actually arise.</p>
<p>Note that the only difference between <code class="func">DeclareCategory</code> (<a href="chap13.html#X879DE2A17A6C6E92"><span class="RefLink">13.3-5</span></a>) and <code class="func">NewCategory</code> (<a href="chap13.html#X87F68F887B44DBBD"><span class="RefLink">13.3-4</span></a>) is that in a call to <code class="func">DeclareCategory</code> (<a href="chap13.html#X879DE2A17A6C6E92"><span class="RefLink">13.3-5</span></a>), a variable corresponding to the first argument is set to the new category, and this variable is read-only. The same holds for <code class="func">DeclareRepresentation</code> (<a href="chap13.html#X7C81FB2682AE54CD"><span class="RefLink">13.4-5</span></a>) and <code class="func">NewRepresentation</code> (<a href="chap13.html#X7CC8106F809E15CF"><span class="RefLink">13.4-4</span></a>) etc.</p>
<p>There is no analogue of categories in the implementation in Section <a href="chap81.html#X81008A74838A792E"><span class="RefLink">81.1</span></a>, since there it was not necessary to distinguish residue classes from other <strong class="pkg">GAP</strong> objects. Note that the functions there assumed that their arguments were residue classes, and the user was responsible not to call them with other arguments. Thus an important aspect of types is to describe arguments of functions explicitly.</p>
<p>Third, we must decide about the <em>representation</em> of our objects. This is something we know already from Section <a href="chap81.html#X81008A74838A792E"><span class="RefLink">81.1</span></a>, where we chose a list of length two. Here we may choose between two essentially different representations for the new <strong class="pkg">GAP</strong> objects, namely as <q>component object</q> (record-like) or <q>positional object</q> (list-like). We decide to store the modulus of each residue class in its family, and to encode the element <span class="SimpleMath">k + nℤ</span> by the unique residue in the range <span class="SimpleMath">[ 0 .. n-1 ]</span> that is congruent to <span class="SimpleMath">k</span> modulo <span class="SimpleMath">n</span>, and the object itself is chosen to be a positional object with this residue at the first and only position (see <a href="chap79.html#X834893D07FAA6FD2"><span class="RefLink">79.3</span></a>).</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">DeclareRepresentation("IsMyModulusRep", IsPositionalObjectRep, [1]);</span>
</pre></div>
<p>The fourth ingredients of a type, <em>attributes</em>, are usually of minor importance for element objects. In particular, we do not need to introduce special attributes for residue classes.</p>
<p>Having defined what the new objects shall look like, we now declare a global function (see <a href="chap79.html#X7837CA9A83D93B38"><span class="RefLink">79.11</span></a>), to create an element when family and residue are given.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">DeclareGlobalFunction( "MyZmodnZObj" );</span>
</pre></div>
<p>Now we have declared what we need, and we can start to implement the missing methods resp. functions; so the following command belongs to the <em>implementation part</em> of our package (see <a href="chap79.html#X7837CA9A83D93B38"><span class="RefLink">79.11</span></a>).</p>
<p>The probably most interesting function is the one to construct a residue class.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallGlobalFunction( MyZmodnZObj, function( Fam, residue )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return Objectify( NewType( Fam, IsMyZmodnZObj and IsMyModulusRep ),</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ residue mod Fam!.modulus ] );</span>
<span class="GAPprompt">></span> <span class="GAPinput">end );</span>
</pre></div>
<p>Note that we normalize <code class="code">residue</code> explicitly using <code class="keyw">mod</code>; we assumed that the modulus is stored in <code class="code">Fam</code>, so we must take care of this below. If <code class="code">Fam</code> is a family of residue classes, and <code class="code">residue</code> is an integer, <code class="code">MyZmodnZObj</code> returns the corresponding object in the family <code class="code">Fam</code>, which lies in the category <code class="code">IsMyZmodnZObj</code> and in the representation <code class="code">IsMyModulusRep</code>.</p>
<p><code class="code">MyZmodnZObj</code> needs an appropriate family as first argument, so let us see how to get our hands on this. Of course we could write a handy function to create such a family for given modulus, but we choose another way. In fact we do not really want to call <code class="code">MyZmodnZObj</code> explicitly when we want to create residue classes. For example, if we want to enter a matrix of residues then usually we start with a matrix of corresponding integers, and it is more elegant to do the conversion via multiplying the matrix with the identity of the required ring <span class="SimpleMath">ℤ / nℤ</span>; this is also done for the conversion of integral matrices to finite field matrices. (Note that we will have to install a method for this.) So it is often sufficient to access this identity, for example via <code class="code">One( MyZmodnZ( <var class="Arg">n</var> ) )</code>, where <code class="code">MyZmodnZ</code> returns a domain representing the ring <span class="SimpleMath">ℤ / nℤ</span> when called with the argument <span class="SimpleMath">n</span>. We decide that constructing this ring is a natural place where the creation of the family can be hidden, and implement the function. (Note that the declaration belongs to the declaration part, and the installation belongs to the implementation part, see <a href="chap79.html#X7837CA9A83D93B38"><span class="RefLink">79.11</span></a>).</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">DeclareGlobalFunction( "MyZmodnZ" );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallGlobalFunction( MyZmodnZ, function( n )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> local F, R;</span>
<span class="GAPprompt">></span> <span class="GAPinput"></span>
<span class="GAPprompt">></span> <span class="GAPinput"> if not IsPosInt( n ) then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> Error( "<n> must be a positive integer" );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"></span>
<span class="GAPprompt">></span> <span class="GAPinput"> # Construct the family of element objects of our ring.</span>
<span class="GAPprompt">></span> <span class="GAPinput"> F:= NewFamily( Concatenation( "MyZmod", String( n ), "Z" ),</span>
<span class="GAPprompt">></span> <span class="GAPinput"> IsMyZmodnZObj );</span>
<span class="GAPprompt">></span> <span class="GAPinput"></span>
<span class="GAPprompt">></span> <span class="GAPinput"> # Install the data.</span>
<span class="GAPprompt">></span> <span class="GAPinput"> F!.modulus:= n;</span>
<span class="GAPprompt">></span> <span class="GAPinput"></span>
<span class="GAPprompt">></span> <span class="GAPinput"> # Make the domain.</span>
<span class="GAPprompt">></span> <span class="GAPinput"> R:= RingWithOneByGenerators( [ MyZmodnZObj( F, 1 ) ] );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> SetIsWholeFamily( R, true );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> SetName( R, Concatenation( "(Integers mod ", String(n), ")" ) );</span>
<span class="GAPprompt">></span> <span class="GAPinput"></span>
<span class="GAPprompt">></span> <span class="GAPinput"> # Return the ring.</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return R;</span>
<span class="GAPprompt">></span> <span class="GAPinput">end );</span>
</pre></div>
<p>Note that the modulus <code class="code">n</code> is stored in the component <code class="code">modulus</code> of the family, as is assumed by <code class="code">MyZmodnZ</code>. Thus it is not necessary to store the modulus in each element. When storing <code class="code">n</code> with the <code class="code">!.</code> operator as value of the component <code class="code">modulus</code>, we used that all families are in fact represented as component objects (see <a href="chap79.html#X866E223484649E5A"><span class="RefLink">79.2</span></a>).</p>
<p>We see that we can use <code class="func">RingWithOneByGenerators</code> (<a href="chap56.html#X851115EC79B8C393"><span class="RefLink">56.3-3</span></a>) to construct a ring with one if we have the appropriate generators. The construction via <code class="func">RingWithOneByGenerators</code> (<a href="chap56.html#X851115EC79B8C393"><span class="RefLink">56.3-3</span></a>) makes sure that <code class="func">IsRingWithOne</code> (<a href="chap56.html#X7E601FBD8020A0F3"><span class="RefLink">56.3-1</span></a>) (and <code class="func">IsRing</code> (<a href="chap56.html#X80FD843C8221DAC9"><span class="RefLink">56.1-1</span></a>)) is <code class="keyw">true</code> for each output of <code class="code">MyZmodnZ</code>. So the main problem is to create the identity element of the ring, which in our case suffices to generate the ring. In order to create this element via <code class="code">MyZmodnZObj</code>, we have to construct its family first, at each call of <code class="code">MyZmodnZ</code>.</p>
<p>Also note that we may enter known information about the ring. Here we store that it contains the whole family of elements; this is useful for example when we want to check the membership of an element in the ring, which can be decided from the type of the element if the ring contains its whole elements family. Giving a name to the ring causes that it will be printed via printing the name. (By the way: This name <code class="code">(Integers mod <var class="Arg">n</var>)</code> looks like a call to <code class="func">\mod</code> (<a href="chap31.html#X8481C9B97B214C23"><span class="RefLink">31.12-1</span></a>) with the arguments <code class="func">Integers</code> (<a href="chap14.html#X853DF11B80068ED5"><span class="RefLink">14</span></a>) and <var class="Arg">n</var>; a construction of the ring via this call seems to be more natural than by calling <code class="code">MyZmodnZ</code>; later we shall install a <code class="func">\mod</code> (<a href="chap31.html#X8481C9B97B214C23"><span class="RefLink">31.12-1</span></a>) method in order to admit this construction.)</p>
<p>Now we can read the above code into <strong class="pkg">GAP</strong>, and the following works already.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">R:= MyZmodnZ( 4 );</span>
(Integers mod 4)
<span class="GAPprompt">gap></span> <span class="GAPinput">IsRing( R );</span>
true
<span class="GAPprompt">gap></span> <span class="GAPinput">gens:= GeneratorsOfRingWithOne( R );</span>
[ <object> ]
</pre></div>
<p>But of course this means just to ask for the information we have explicitly stored in the ring. Already the questions whether the ring is finite and how many elements it has, cannot be answered by <strong class="pkg">GAP</strong>. Clearly we know the answers, and we could store them in the ring, by setting the value of the property <code class="func">IsFinite</code> (<a href="chap30.html#X808A4061809A6E67"><span class="RefLink">30.4-2</span></a>) to <code class="keyw">true</code> and the value of the attribute <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>) to <var class="Arg">n</var> (the argument of the call to <code class="code">MyZmodnZ</code>). If we do not want to do so then <strong class="pkg">GAP</strong> could only try to find out the number of elements of the ring via forming the closure of the generators under addition and multiplication, but up to now, <strong class="pkg">GAP</strong> does not know how to add or multiply two elements of our ring.</p>
<p>So we must install some methods for arithmetic and other operations if the elements are to behave as we want.</p>
<p>We start with a method for showing elements nicely on the screen. There are different operations for this purpose. One of them is <code class="func">PrintObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>), which is called for each argument in an explicit call to <code class="func">Print</code> (<a href="chap6.html#X7AFA64D97A1F39A3"><span class="RefLink">6.3-4</span></a>). Another one is <code class="func">ViewObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>), which is called in the read-eval-print loop for each object. <code class="func">ViewObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) shall produce short and human readable information about the object in question, whereas <code class="func">PrintObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) shall produce information that may be longer and is (if reasonable) readable by <strong class="pkg">GAP</strong>. We cannot satisfy the latter requirement for a <code class="func">PrintObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) method because there is no way to make a family <strong class="pkg">GAP</strong> readable. So we decide to display the expression <code class="code">( k mod n )</code> for an object that is given by the residue <code class="code">k</code> and the modulus <code class="code">n</code>, which would be fine as a <code class="func">ViewObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) method. Since the default for <code class="func">ViewObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) is to call <code class="func">PrintObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>), and since no other <code class="func">ViewObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) method is applicable to our elements, we need only a <code class="func">PrintObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) method.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( PrintObj,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for element in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsMyZmodnZObj and IsMyModulusRep ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( x )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> Print( "( ", x![1], " mod ", FamilyObj(x)!.modulus, " )" );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end );</span>
</pre></div>
<p>So we installed a method for the operation <code class="func">PrintObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) (first argument), and we gave it a suitable information message (second argument), see <a href="chap7.html#X80848FF486BD6F9F"><span class="RefLink">7.2-1</span></a> and <a href="chap7.html#X7D43A2D885B37739"><span class="RefLink">7.3</span></a> for applications of this information string. The third argument tells <strong class="pkg">GAP</strong> that the method is applicable for objects that lie in the category <code class="code">IsMyZmodnZObj</code> and in the representation <code class="code">IsMyModulusRep</code>. and the fourth argument is the method itself. More details about <code class="func">InstallMethod</code> (<a href="chap78.html#X837EFDAB7BEF290B"><span class="RefLink">78.3-1</span></a>) can be found in <a href="chap78.html#X795EE8257848B438"><span class="RefLink">78.3</span></a>.</p>
<p>Note that the requirement <code class="code">IsMyModulusRep</code> for the argument <code class="code">x</code> allows us to access the residue as <code class="code">x![1]</code>. Since the family of <code class="code">x</code> has the component <code class="code">modulus</code> bound if it is constructed by <code class="code">MyZmodnZ</code>, we may access this component. We check whether the method installation has some effect.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">gens;</span>
[ ( 1 mod 4 ) ]
</pre></div>
<p>Next we install methods for the comparison operations. Note that we can assume that the residues in the representation chosen are normalized.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( \=,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for two elements in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> IsIdenticalObj,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [IsMyZmodnZObj and IsMyModulusRep, IsMyZmodnZObj and IsMyModulusRep],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( x, y ) return x![1] = y![1]; end );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( \<,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for two elements in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> IsIdenticalObj,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [IsMyZmodnZObj and IsMyModulusRep, IsMyZmodnZObj and IsMyModulusRep],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( x, y ) return x![1] < y![1]; end );</span>
</pre></div>
<p>The third argument used in these installations specifies the required relation between the families of the arguments (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). This argument of a method installation, if present, is a function that shall be applied to the families of the arguments. <code class="func">IsIdenticalObj</code> (<a href="chap12.html#X7961183378DFB902"><span class="RefLink">12.5-1</span></a>) means that the methods are applicable only if both arguments lie in the same family. (In installations for unary methods, obviously no relation is required, so this argument is left out there.)</p>
<p>Up to now, we see no advantage of the new approach over the one in Section <a href="chap81.html#X81008A74838A792E"><span class="RefLink">81.1</span></a>. For a residue class represented as <code class="code">[ <var class="Arg">k</var>, <var class="Arg">n</var> ]</code>, the way it is printed on the screen is sufficient, and equality and comparison of lists are good enough to define equality and comparison of residue classes if needed. But this is not the case in other situations. For example, if we would have decided that the residue <var class="Arg">k</var> need not be normalized then we would have needed functions in Section <a href="chap81.html#X81008A74838A792E"><span class="RefLink">81.1</span></a> that compute whether two residue classes are equal, and which of two residue classes is regarded as larger than another. Note that we are free to define what <q>larger</q> means for objects that are newly introduced.</p>
<p>Next we install methods for the arithmetic operations, first for the additive structure.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( \+,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for two elements in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> IsIdenticalObj,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [IsMyZmodnZObj and IsMyModulusRep, IsMyZmodnZObj and IsMyModulusRep],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( x, y )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return MyZmodnZObj( FamilyObj( x ), x![1] + y![1] );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( ZeroOp,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for element in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsMyZmodnZObj ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> x -> MyZmodnZObj( FamilyObj( x ), 0 ) );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( AdditiveInverseOp,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for element in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsMyZmodnZObj and IsMyModulusRep ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> x -> MyZmodnZObj( FamilyObj( x ), AdditiveInverse( x![1] ) ) );</span>
</pre></div>
<p>Here the new approach starts to pay off. The method for the operation <code class="func">\+</code> (<a href="chap31.html#X8481C9B97B214C23"><span class="RefLink">31.12-1</span></a>) allows us to use the infix operator <code class="code">+</code> for residue classes. The method for <code class="func">ZeroOp</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>) is used when we call this operation or the attribute <code class="func">Zero</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>) explicitly, and <code class="func">ZeroOp</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>) it is also used when we ask for <code class="code">0 * <var class="Arg">rescl</var></code>, where <var class="Arg">rescl</var> is a residue class.</p>
<p>(Note that <code class="func">Zero</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>) and <code class="func">ZeroOp</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>) are distinguished because <code class="code">0 * <var class="Arg">obj</var></code> is guaranteed to return a <em>mutable</em> result whenever a mutable version of this result exists in <strong class="pkg">GAP</strong> –for example if <var class="Arg">obj</var> is a matrix– whereas <code class="func">Zero</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>) is an attribute and therefore returns <em>immutable</em> results; for our example there is no difference since the residue classes are always immutable, nevertheless we have to install the method for <code class="func">ZeroOp</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>). The same holds for <code class="func">AdditiveInverse</code> (<a href="chap31.html#X84BB723C81D55D63"><span class="RefLink">31.10-9</span></a>), <code class="func">One</code> (<a href="chap31.html#X8046262384895B2A"><span class="RefLink">31.10-2</span></a>), and <code class="func">Inverse</code> (<a href="chap31.html#X78EE524E83624057"><span class="RefLink">31.10-8</span></a>).)</p>
<p>Similarly, <code class="func">AdditiveInverseOp</code> (<a href="chap31.html#X84BB723C81D55D63"><span class="RefLink">31.10-9</span></a>) can be either called directly or via the unary <code class="code">-</code> operator; so we can compute the additive inverse of the residue class <var class="Arg">rescl</var> as <code class="code">-<var class="Arg">rescl</var></code>.</p>
<p>It is not necessary to install methods for subtraction, since this is handled via addition of the additive inverse of the second argument if no other method is installed.</p>
<p>Let us try what we can do with the methods that are available now.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">x:= gens[1]; y:= x + x;</span>
( 1 mod 4 )
( 2 mod 4 )
<span class="GAPprompt">gap></span> <span class="GAPinput">0 * x; -x;</span>
( 0 mod 4 )
( 3 mod 4 )
<span class="GAPprompt">gap></span> <span class="GAPinput">y = -y; x = y; x < y; -x < y;</span>
true
false
true
false
</pre></div>
<p>We might want to admit the addition of integers and elements in rings <span class="SimpleMath">ℤ / nℤ</span>, where an integer is implicitly identified with its residue modulo <span class="SimpleMath">n</span>. To achieve this, we install methods to add an integer to an object in <code class="code">IsMyZmodnZObj</code> from the left and from the right.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( \+,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for element in Z/nZ (ModulusRep) and integer",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsMyZmodnZObj and IsMyModulusRep, IsInt ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( x, y )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return MyZmodnZObj( FamilyObj( x ), x![1] + y );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( \+,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for integer and element in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsInt, IsMyZmodnZObj and IsMyModulusRep ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( x, y )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return MyZmodnZObj( FamilyObj( y ), x + y![1] );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end );</span>
</pre></div>
<p>Now we can do also the following.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">2 + x; 7 - x; y - 2;</span>
( 3 mod 4 )
( 2 mod 4 )
( 0 mod 4 )
</pre></div>
<p>Similarly we install the methods dealing with the multiplicative structure. We need methods to multiply two of our objects, and to compute identity and inverse. The operation <code class="func">OneOp</code> (<a href="chap31.html#X8046262384895B2A"><span class="RefLink">31.10-2</span></a>) is called when we ask for <code class="code"><var class="Arg">rescl</var>^0</code>, and <code class="func">InverseOp</code> (<a href="chap31.html#X78EE524E83624057"><span class="RefLink">31.10-8</span></a>) is called when we ask for <code class="code"><var class="Arg">rescl</var>^-1</code>. Note that the method for <code class="func">InverseOp</code> (<a href="chap31.html#X78EE524E83624057"><span class="RefLink">31.10-8</span></a>) returns <code class="keyw">fail</code> if the argument is not invertible.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( \*,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for two elements in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> IsIdenticalObj,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [IsMyZmodnZObj and IsMyModulusRep, IsMyZmodnZObj and IsMyModulusRep],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> function( x, y )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return MyZmodnZObj( FamilyObj( x ), x![1] * y![1] );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( OneOp,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for element in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsMyZmodnZObj ],</span>
<span class="GAPprompt">></span> <span class="GAPinput"> elm -> MyZmodnZObj( FamilyObj( elm ), 1 ) );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
<span class="GAPprompt">gap></span> <span class="GAPinput">InstallMethod( InverseOp,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> "for element in Z/nZ (ModulusRep)",</span>
<span class="GAPprompt">></span> <span class="GAPinput"> [ IsMyZmodnZObj and IsMyModulusRep ],</ | |