Debian Codegraph @

doc/ref/chap82.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
7 <head>
8 <title>GAP (ref) - Chapter 82: An Example – Designing Arithmetic Operations</title>
9 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
10 <meta name="generator" content="GAPDoc2HTML" />
11 <link rel="stylesheet" type="text/css" href="manual.css" />
12 <script src="manual.js" type="text/javascript"></script>
13 <script type="text/javascript">overwriteStyle();</script>
14 </head>
15 <body class="chap82" onload="jscontent()">
16
17
18 <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>
19
20 <div class="chlinkprevnexttop">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap81.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap83.html">[Next Chapter]</a>&nbsp; </div>
21
22 <p id="mathjaxlink" class="pcenter"><a href="chap82_mj.html">[MathJax on]</a></p>
23 <p><a id="X7E485C967A5778C9" name="X7E485C967A5778C9"></a></p>
24 <div class="ChapSects"><a href="chap82.html#X7E485C967A5778C9">82 <span class="Heading">An Example – Designing Arithmetic Operations</span></a>
25 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap82.html#X7EA9422E7ACA7276">82.1 <span class="Heading">New Arithmetic Operations vs. New Objects</span></a>
26 </span>
27 </div>
28 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap82.html#X7BE9D84482B421F9">82.2 <span class="Heading">Designing new Multiplicative Objects</span></a>
29 </span>
30 </div>
31 </div>
32
33 <h3>82 <span class="Heading">An Example – Designing Arithmetic Operations</span></h3>
34
35 <p>In this chapter, we give a –hopefully typical– example of extending <strong class="pkg">GAP</strong> by new objects with prescribed arithmetic operations (for a simple approach that may be useful to get started though does not permit to exploit all potential features, see also <code class="func">ArithmeticElementCreator</code> (<a href="chap80.html#X87A88E3D7F6E2A7C"><span class="RefLink">80.9-1</span></a>)).</p>
36
37 <p><a id="X7EA9422E7ACA7276" name="X7EA9422E7ACA7276"></a></p>
38
39 <h4>82.1 <span class="Heading">New Arithmetic Operations vs. New Objects</span></h4>
40
41 <p>A usual procedure in mathematics is the definition of new operations for given objects; here are a few typical examples. The Lie bracket defines an interesting new multiplicative structure on a given (associative) algebra. Forming a group ring can be viewed as defining a new addition for the elements of the given group, and extending the multiplication to sums of group elements in a natural way. Forming the exterior algebra of a given vector space can be viewed as defining a new multiplication for the vectors in a natural way.</p>
42
43 <p><strong class="pkg">GAP</strong> does <em>not</em> support such a procedure. The main reason for this is that in <strong class="pkg">GAP</strong>, the multiplication in a group, a ring etc. is always written as <code class="code">*</code>, and the addition in a vector space, a ring etc.  is always written as <code class="code">+</code>. Therefore it is not possible to define the Lie bracket as a <q>second multiplication</q> for the elements of a given algebra; in fact, the multiplication in Lie algebras in <strong class="pkg">GAP</strong> is denoted by <code class="code">*</code>. Analogously, constructing the group ring as sketched above is impossible if an addition is already defined for the elements; note the difference between the usual addition of matrices and the addition in the group ring of a matrix group! (See Chapter <a href="chap65.html#X825897DC7A16E07D"><span class="RefLink">65</span></a> for an example.) Similarly, there is already a multiplication defined for row vectors (yielding the standard scalar product), hence these vectors cannot be regarded as elements of the exterior algebra of the space.</p>
44
45 <p>In situations such as the ones mentioned above, <strong class="pkg">GAP</strong>'s way to deal with the structures in question is the following. Instead of defining <em>new</em> operations for the <em>given</em> objects, <em>new</em> objects are created to which the <em>given</em> arithmetic operations <code class="code">*</code> and <code class="code">+</code> are then made applicable.</p>
46
47 <p>With this construction, matrix Lie algebras consist of matrices that are different from the matrices with associative multiplication; technically, the type of a matrix determines how it is multiplied with other matrices (see <code class="func">IsMatrix</code> (<a href="chap24.html#X7E1AE46B862B185F"><span class="RefLink">24.2-1</span></a>)). A matrix with the Lie bracket as its multiplication can be created with the function <code class="func">LieObject</code> (<a href="chap64.html#X87F121978775AF48"><span class="RefLink">64.1-1</span></a>) from a matrix with the usual associative multiplication.</p>
48
49 <p>Group rings (more general: magma rings, see Chapter <a href="chap65.html#X825897DC7A16E07D"><span class="RefLink">65</span></a>) can be constructed with <code class="func">FreeMagmaRing</code> (<a href="chap65.html#X7B9AF0A47F44E4B4"><span class="RefLink">65.1-1</span></a>) from a coefficient ring and a group. The elements of the group are not contained in such a group ring, one has to use an embedding map for creating a group ring element that corresponds to a given group element.</p>
50
51 <p>It should be noted that the <strong class="pkg">GAP</strong> approach to the construction of Lie algebras from associative algebras is generic in the sense that all objects in the filter <code class="func">IsLieObject</code> (<a href="chap64.html#X83E5DD4381D9A65D"><span class="RefLink">64.1-2</span></a>) use the same methods for their addition, multiplication etc., by delegating to the <q>underlying</q> objects of the associative algebra, no matter what these objects actually are. Analogously, also the construction of group rings is generic.</p>
52
53 <p><a id="X7BE9D84482B421F9" name="X7BE9D84482B421F9"></a></p>
54
55 <h4>82.2 <span class="Heading">Designing new Multiplicative Objects</span></h4>
56
57 <p>The goal of this section is to implement objects with a prescribed multiplication. Let us assume that we are given a field <span class="SimpleMath">F</span>, and that we want to define a new multiplication <span class="SimpleMath">*</span> on <span class="SimpleMath">F</span> that is given by <span class="SimpleMath">a * b = a b - a - b + 2</span>; here <span class="SimpleMath">a b</span> denotes the ordinary product in <span class="SimpleMath">F</span>.</p>
58
59 <p>By the discussion in Section <a href="chap82.html#X7EA9422E7ACA7276"><span class="RefLink">82.1</span></a>, we know that we cannot define a new multiplication on <span class="SimpleMath">F</span> itself but have to create new objects.</p>
60
61 <p>We want to distinguish these new objects from all other <strong class="pkg">GAP</strong> objects, in order to describe for example the situation that two of our objects shall be multiplied. This distinction is made via the <em>type</em> of the objects. More precisely, we declare a new <em>filter</em>, a function that will return <code class="keyw">true</code> for our new objects, and <code class="keyw">false</code> for all other <strong class="pkg">GAP</strong> objects. This can be done by calling <code class="func">DeclareFilter</code> (<a href="chap13.html#X846EA18A7D36626C"><span class="RefLink">13.8-2</span></a>), but since our objects will know about the value already when they are constructed, the filter can be created with <code class="func">DeclareCategory</code> (<a href="chap13.html#X879DE2A17A6C6E92"><span class="RefLink">13.3-5</span></a>) or <code class="func">NewCategory</code> (<a href="chap13.html#X87F68F887B44DBBD"><span class="RefLink">13.3-4</span></a>).</p>
62
63
64 <div class="example"><pre>
65 DeclareCategory( "IsMyObject", IsObject );
66 </pre></div>
67
68 <p>The idea is that the new multiplication will be installed only for objects that <q>lie in the category <code class="code">IsMyObject</code></q>.</p>
69
70 <p>The next question is what internal data our new objects store, and how they are accessed. The easiest solution is to store the <q>underlying</q> object from the field <span class="SimpleMath">F</span>. <strong class="pkg">GAP</strong> provides two general possibilities how to store this, namely record-like and list-like structures (for examples, see <a href="chap79.html#X866E223484649E5A"><span class="RefLink">79.2</span></a> and <a href="chap79.html#X834893D07FAA6FD2"><span class="RefLink">79.3</span></a>). We decide to store the data in a list-like structure, at position 1. This <em>representation</em> is declared as follows.</p>
71
72
73 <div class="example"><pre>
74 DeclareRepresentation( "IsMyObjectListRep", IsPositionalObjectRep, [ 1 ] );
75 </pre></div>
76
77 <p>Of course we can argue that this declaration is superfluous because <em>all</em> objects in the category <code class="code">IsMyObject</code> will be represented this way; it is possible to proceed like that, but often (in more complicated situations) it turns out to be useful that several representations are available for <q>the same element</q>.</p>
78
79 <p>For creating the type of our objects, we need to specify to which <em>family</em> (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>) the objects shall belong. For the moment, we need not say anything about relations to other <strong class="pkg">GAP</strong> objects, thus the only requirement is that all new objects lie in the <em>same</em> family; therefore we create a <em>new</em> family. Also we are not interested in properties that some of our objects have and others do not have, thus we need only one type, and store it in a global variable.</p>
80
81
82 <div class="example"><pre>
83 MyType:= NewType( NewFamily( "MyFamily" ),
84 IsMyObject and IsMyObjectListRep );
85 </pre></div>
86
87 <p>The next step is to write a function that creates a new object. It may look as follows.</p>
88
89
90 <div class="example"><pre>
91 MyObject:= val -&gt; Objectify( MyType, [ Immutable( val ) ] );
92 </pre></div>
93
94 <p>Note that we store an <em>immutable copy</em> of the argument in the returned object; without doing so, for example if the argument would be a mutable matrix then the corresponding new object would be changed whenever the matrix is changed (see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a> for more details about mutability).</p>
95
96 <p>Having entered the above <strong class="pkg">GAP</strong> code, we can create some of our objects.</p>
97
98
99 <div class="example"><pre>
100 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= MyObject( 3 ); b:= MyObject( 5 );</span>
101 &lt;object&gt;
102 &lt;object&gt;
103 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a![1]; b![1];</span>
104 3
105 5
106 </pre></div>
107
108 <p>But clearly a lot is missing. Besides the fact that the desired multiplication is not yet installed, we see that also the way how the objects are printed is not satisfactory.</p>
109
110 <p>Let us improve the latter first. There are two <strong class="pkg">GAP</strong> functions <code class="func">View</code> (<a href="chap6.html#X851902C583B84CDC"><span class="RefLink">6.3-3</span></a>) and <code class="func">Print</code> (<a href="chap6.html#X7AFA64D97A1F39A3"><span class="RefLink">6.3-4</span></a>) for showing objects on the screen. <code class="func">View</code> (<a href="chap6.html#X851902C583B84CDC"><span class="RefLink">6.3-3</span></a>) is thought to show a short and human readable form of the object, and <code class="func">Print</code> (<a href="chap6.html#X7AFA64D97A1F39A3"><span class="RefLink">6.3-4</span></a>) is thought to show a not necessarily short form that is <strong class="pkg">GAP</strong> readable whenever this makes sense. We decide to show <code class="code">a</code> as <code class="code"><var class="Arg">3</var></code> by <code class="func">View</code> (<a href="chap6.html#X851902C583B84CDC"><span class="RefLink">6.3-3</span></a>), and to show the construction <code class="code">MyObject( 3 )</code> by <code class="func">Print</code> (<a href="chap6.html#X7AFA64D97A1F39A3"><span class="RefLink">6.3-4</span></a>); the methods are installed for the underlying operations <code class="func">ViewObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) and <code class="func">PrintObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>).</p>
111
112
113 <div class="example"><pre>
114 InstallMethod( ViewObj,
115 "for object in `IsMyObject'",
116 [ IsMyObject and IsMyObjectListRep ],
117 function( obj )
118 Print( "&lt;", obj![1], "&gt;" );
119 end );
120
121 InstallMethod( PrintObj,
122 "for object in `IsMyObject'",
123 [ IsMyObject and IsMyObjectListRep ],
124 function( obj )
125 Print( "MyObject( ", obj![1], " )" );
126 end );
127 </pre></div>
128
129 <p>This is the result of the above installations.</p>
130
131
132 <div class="example"><pre>
133 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a; Print( a, "\n" );</span>
134 &lt;3&gt;
135 MyObject( 3 )
136 </pre></div>
137
138 <p>And now we try to install the multiplication.</p>
139
140
141 <div class="example"><pre>
142 InstallMethod( \*,
143 "for two objects in `IsMyObject'",
144 [ IsMyObject and IsMyObjectListRep,
145 IsMyObject and IsMyObjectListRep ],
146 function( a, b )
147 return MyObject( a![1] * b![1] - a![1] - b![1] + 2 );
148 end );
149 </pre></div>
150
151 <p>When we enter the above code, <strong class="pkg">GAP</strong> runs into an error. This is due to the fact that the operation <code class="func">\*</code> (<a href="chap31.html#X8481C9B97B214C23"><span class="RefLink">31.12-1</span></a>) is declared for two arguments that lie in the category <code class="func">IsMultiplicativeElement</code> (<a href="chap31.html#X797D3B2A7A2B2F53"><span class="RefLink">31.14-10</span></a>). One could circumvent the check whether the method matches the declaration of the operation, by calling <code class="func">InstallOtherMethod</code> (<a href="chap78.html#X7D2C12DB841CE539"><span class="RefLink">78.3-2</span></a>) instead of <code class="func">InstallMethod</code> (<a href="chap78.html#X837EFDAB7BEF290B"><span class="RefLink">78.3-1</span></a>). But it would make sense if our objects would lie in <code class="func">IsMultiplicativeElement</code> (<a href="chap31.html#X797D3B2A7A2B2F53"><span class="RefLink">31.14-10</span></a>), for example because some generic methods for objects with multiplication would be available then, such as powering by positive integers via repeated squaring. So we want that <code class="code">IsMyObject</code> implies <code class="func">IsMultiplicativeElement</code> (<a href="chap31.html#X797D3B2A7A2B2F53"><span class="RefLink">31.14-10</span></a>). The easiest way to achieve such implications is to use the implied filter as second argument of the <code class="func">DeclareCategory</code> (<a href="chap13.html#X879DE2A17A6C6E92"><span class="RefLink">13.3-5</span></a>) call; but since we do not want to start anew, we can also install the implication afterwards.</p>
152
153
154 <div class="example"><pre>
155 InstallTrueMethod( IsMultiplicativeElement, IsMyObject );
156 </pre></div>
157
158 <p>Afterwards, installing the multiplication works without problems. Note that <code class="code">MyType</code> and therefore also <code class="code">a</code> and <code class="code">b</code> are <em>not</em> affected by this implication, so we construct them anew.</p>
159
160
161 <div class="example"><pre>
162 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">MyType:= NewType( NewFamily( "MyFamily" ),</span>
163 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> IsMyObject and IsMyObjectListRep );;</span>
164 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= MyObject( 3 );; b:= MyObject( 5 );;</span>
165 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a*b; a^27;</span>
166 &lt;9&gt;
167 &lt;134217729&gt;
168 </pre></div>
169
170 <p>Powering the new objects by negative integers is not possible yet, because <strong class="pkg">GAP</strong> does not know how to compute the inverse of an element <span class="SimpleMath">a</span>, say, which is defined as the unique element <span class="SimpleMath">a'</span> such that both <span class="SimpleMath">a a'</span> and <span class="SimpleMath">a' a</span> are <q>the unique multiplicative neutral element that belongs to <span class="SimpleMath">a</span></q>.</p>
171
172 <p>And also this neutral element, if it exists, cannot be computed by <strong class="pkg">GAP</strong> in our current situation. It does, however, make sense to ask for the multiplicative neutral element of a given magma, and for inverses of elements in the magma.</p>
173
174 <p>But before we can form domains of our objects, we must define when two objects are regarded as equal; note that this is necessary in order to decide about the uniqueness of neutral and inverse elements. In our situation, equality is defined in the obvious way. For being able to form sets of our objects, also an ordering via <code class="func">\&lt;</code> (<a href="chap31.html#X7EF67D047F03CA6F"><span class="RefLink">31.11-1</span></a>) is defined for them.</p>
175
176
177 <div class="example"><pre>
178 InstallMethod( \=,
179 "for two objects in `IsMyObject'",
180 [ IsMyObject and IsMyObjectListRep,
181 IsMyObject and IsMyObjectListRep ],
182 function( a, b )
183 return a![1] = b![1];
184 end );
185
186 InstallMethod( \&lt;,
187 "for two objects in `IsMyObject'",
188 [ IsMyObject and IsMyObjectListRep,
189 IsMyObject and IsMyObjectListRep ],
190 function( a, b )
191 return a![1] &lt; b![1];
192 end );
193 </pre></div>
194
195 <p>Let us look at an example. We start with finite field elements because then the domains are finite, hence the generic methods for such domains will have a chance to succeed.</p>
196
197
198 <div class="example"><pre>
199 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= MyObject( Z(7) );</span>
200 &lt;Z(7)&gt;
201 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m:= Magma( a );</span>
202 &lt;magma with 1 generator&gt;
203 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">e:= MultiplicativeNeutralElement( m );</span>
204 &lt;Z(7)^2&gt;
205 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">elms:= AsList( m );</span>
206 [ &lt;Z(7)&gt;, &lt;Z(7)^2&gt;, &lt;Z(7)^5&gt; ]
207 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAll( elms, x -&gt; ForAny( elms, y -&gt; x*y = e and y*x = e ) );</span>
208 true
209 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List( elms, x -&gt; First( elms, y -&gt; x*y = e and y*x = e ) );</span>
210 [ &lt;Z(7)^5&gt;, &lt;Z(7)^2&gt;, &lt;Z(7)&gt; ]
211 </pre></div>
212
213 <p>So a multiplicative neutral element exists, in fact all elements in the magma <code class="code">m</code> are invertible. But what about the following.</p>
214
215
216 <div class="example"><pre>
217 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">b:= MyObject( Z(7)^0 ); m:= Magma( a, b );</span>
218 &lt;Z(7)^0&gt;
219 &lt;magma with 2 generators&gt;
220 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">elms:= AsList( m );</span>
221 [ &lt;Z(7)^0&gt;, &lt;Z(7)&gt;, &lt;Z(7)^2&gt;, &lt;Z(7)^5&gt; ]
222 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">e:= MultiplicativeNeutralElement( m );</span>
223 &lt;Z(7)^2&gt;
224 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAll( elms, x -&gt; ForAny( elms, y -&gt; x*y = e and y*x = e ) );</span>
225 false
226 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List( elms, x -&gt; b * x );</span>
227 [ &lt;Z(7)^0&gt;, &lt;Z(7)^0&gt;, &lt;Z(7)^0&gt;, &lt;Z(7)^0&gt; ]
228 </pre></div>
229
230 <p>Here we found a multiplicative neutral element, but the element <code class="code">b</code> does not have an inverse. If an addition would be defined for our elements then we would say that <code class="code">b</code> behaves like a zero element.</p>
231
232 <p>When we started to implement the new objects, we said that we wanted to define the new multiplication for elements of a given field <span class="SimpleMath">F</span>. In principle, the current implementation would admit also something like <code class="code">MyObject( 2 ) * MyObject( Z(7) )</code>. But if we decide that our initial assumption holds, we may define the identity and the inverse of the object <code class="code">&lt;a&gt;</code> as <code class="code">&lt;2*e&gt;</code> and <code class="code">&lt;a/(a-e)&gt;</code>, respectively, where <code class="code">e</code> is the identity element in <span class="SimpleMath">F</span> and <code class="code">/</code> denotes the division in <span class="SimpleMath">F</span>; note that the element <code class="code">&lt;e&gt;</code> is not invertible, and that the above definitions are determined by the multiplication defined for our objects. Further note that after the installations shown below, also <code class="code">One( MyObject( 1 ) )</code> is defined.</p>
233
234 <p>(For technical reasons, we do not install the intended methods for the attributes <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>) but for the operations <code class="func">OneOp</code> (<a href="chap31.html#X8046262384895B2A"><span class="RefLink">31.10-2</span></a>) and <code class="func">InverseOp</code> (<a href="chap31.html#X78EE524E83624057"><span class="RefLink">31.10-8</span></a>). This is because for certain kinds of objects –mainly matrices– one wants to support a method to compute a <em>mutable</em> identity or inverse, and the attribute needs only a method that takes this object, makes it immutable, and then returns this object. As stated above, we only want to deal with immutable objects, so this distinction is not really interesting for us.)</p>
235
236 <p>A more interesting point to note is that we should mark our objects as likely to be invertible, since we add the possibility to invert them. Again, this could have been part of the declaration of <code class="code">IsMyObject</code>, but we may also formulate an implication for the existing category.</p>
237
238
239 <div class="example"><pre>
240 InstallTrueMethod( IsMultiplicativeElementWithInverse, IsMyObject );
241
242 InstallMethod( OneOp,
243 "for an object in `IsMyObject'",
244 [ IsMyObject and IsMyObjectListRep ],
245 a -&gt; MyObject( 2 * One( a![1] ) ) );
246
247 InstallMethod( InverseOp,
248 "for an object in `IsMyObject'",
249 [ IsMyObject and IsMyObjectListRep ],
250 a -&gt; MyObject( a![1] / ( a![1] - One( a![1] ) ) ) );
251 </pre></div>
252
253 <p>Now we can form groups of our (nonzero) elements.</p>
254
255
256 <div class="example"><pre>
257 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">MyType:= NewType( NewFamily( "MyFamily" ),</span>
258 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> IsMyObject and IsMyObjectListRep );;</span>
259 gap&gt;
260 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= MyObject( Z(7) );</span>
261 &lt;Z(7)&gt;
262 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">b:= MyObject( 0*Z(7) ); g:= Group( a, b );</span>
263 &lt;0*Z(7)&gt;
264 &lt;group with 2 generators&gt;
265 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Size( g );</span>
266 6
267 </pre></div>
268
269 <p>We are completely free to define an <em>addition</em> for our elements, a natural one is given by <code class="code">&lt;a&gt; + &lt;b&gt; = &lt;a+b-1&gt;</code>. As we did for the multiplication, we first change <code class="code">IsMyObject</code> such that the additive structure is also known.</p>
270
271
272 <div class="example"><pre>
273 InstallTrueMethod( IsAdditiveElementWithInverse, IsMyObject );
274 </pre></div>
275
276 <p>Next we install the methods for the addition, and those to compute the additive neutral element and the additive inverse.</p>
277
278
279 <div class="example"><pre>
280 InstallMethod( \+,
281 "for two objects in `IsMyObject'",
282 [ IsMyObject and IsMyObjectListRep,
283 IsMyObject and IsMyObjectListRep ],
284 function( a, b )
285 return MyObject( a![1] + b![1] - 1 );
286 end );
287
288 InstallMethod( ZeroOp,
289 "for an object in `IsMyObject'",
290 [ IsMyObject and IsMyObjectListRep ],
291 a -&gt; MyObject( One( a![1] ) ) );
292
293 InstallMethod( AdditiveInverseOp,
294 "for an object in `IsMyObject'",
295 [ IsMyObject and IsMyObjectListRep ],
296 a -&gt; MyObject( a![1] / ( a![1] - One( a![1] ) ) ) );
297 </pre></div>
298
299 <p>Let us try whether the addition works.</p>
300
301
302 <div class="example"><pre>
303 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">MyType:= NewType( NewFamily( "MyFamily" ),</span>
304 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> IsMyObject and IsMyObjectListRep );;</span>
305 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= MyObject( Z(7) );; b:= MyObject( 0*Z(7) );;</span>
306 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m:= AdditiveMagma( a, b );</span>
307 &lt;additive magma with 2 generators&gt;
308 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Size( m );</span>
309 7
310 </pre></div>
311
312 <p>Similar as installing a multiplication automatically makes powering by integers available, multiplication with integers becomes available with the addition.</p>
313
314
315 <div class="example"><pre>
316 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">2 * a;</span>
317 &lt;Z(7)^5&gt;
318 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a+a;</span>
319 &lt;Z(7)^5&gt;
320 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">MyObject( 2*Z(7)^0 ) * a;</span>
321 &lt;Z(7)&gt;
322 </pre></div>
323
324 <p>In particular we see that this multiplication does <em>not</em> coincide with the multiplication of two of our objects, that is, an integer <em>cannot</em> be used as a shorthand for one of the new objects in a multiplication.</p>
325
326 <p>(It should be possible to create a <em>field</em> with the new multiplication and addition. Currently this fails, due to missing methods for computing several kinds of generators from field generators, for computing the characteristic in the case that the family does not know this in advance, for checking with <code class="func">AsField</code> (<a href="chap58.html#X7C193B7D7AFB29BE"><span class="RefLink">58.1-9</span></a>) whether a domain is in fact a field, for computing the closure as a field.)</p>
327
328 <p>It should be emphasized that the mechanism described above may be not suitable for the situation that one wants to consider many different multiplications <q>on the same set of objects</q>, since the installation of a new multiplication requires the declaration of at least one new filter and the installation of several methods. But the design of <strong class="pkg">GAP</strong> is not suitable for such dynamic method installations.</p>
329
330 <p>Turning this argument the other way round, the implementation of the new arithmetics defined by the above multiplication and addition is available for any field <span class="SimpleMath">F</span>, one need not repeat it for each field one is interested in.</p>
331
332 <p>Similar to the above situation, the construction of a magma ring <span class="SimpleMath">RM</span> from a coefficient ring <span class="SimpleMath">R</span> and a magma <span class="SimpleMath">M</span> is implemented only once, since the definition of the arithmetic operations depends only on the given multiplication of <span class="SimpleMath">M</span> and not on <span class="SimpleMath">M</span> itself. So the addition is not implemented for the elements in <span class="SimpleMath">M</span> or –more precisely– for an isomorphic copy. In some sense, the addition is installed <q>for the multiplication</q>, and as mentioned in Section <a href="chap82.html#X7EA9422E7ACA7276"><span class="RefLink">82.1</span></a>, there is only one multiplication <code class="func">\*</code> (<a href="chap31.html#X8481C9B97B214C23"><span class="RefLink">31.12-1</span></a>) in <strong class="pkg">GAP</strong>.</p>
333
334
335 <div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap81.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap83.html">[Next Chapter]</a>&nbsp; </div>
336
337
338 <div class="chlinkbot"><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>
339
340 <hr />
341 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
342 </body>
343 </html>