Sophie

Sophie

distrib > * > 2010.0 > * > by-pkgid > 0c1f9463f03451b5503f0c33beb88a98 > files > 981

gap-system-4.4.12-5mdv2010.0.x86_64.rpm

  
  2 Implementing circle objects
  
  In  this  chapter  we  explain  how  the GAP system may be extended with new
  objects  using  the  circle  multiplication  as  an  example.  We follow the
  guidelines  given  in  the last two chapters of the GAP Programming Tutorial
  and refer to them for more details.
  
  
  2.1 First attempts
  
  Of course, having two ring elements, you can straightforwardly compute their
  circle  product  defined as r * s = r + s + rs. You can do this in a command
  line, and it is a trivial task to write a simplest function of two arguments
  that will do this:
  
  ---------------------------  Example  ----------------------------
    
    gap> CircleMultiplication := function(a,b)
    >      return a+b+a*b;
    >    end;
    function( a, b ) ... end
    gap> CircleMultiplication(2,3); 
    11
    gap> CircleMultiplication( ZmodnZObj(2,8), ZmodnZObj(5,8) );      
    ZmodnZObj( 1, 8 )
    
  ------------------------------------------------------------------
  
  However,  there  is  no check whether both arguments belong to the same ring
  and  whether  they  are  ring  elements at all, so it is easy to obtain some
  meaningless results:
  
  ---------------------------  Example  ----------------------------
    
    gap> CircleMultiplication( 3, ZmodnZObj(3,8) );
    ZmodnZObj( 7, 8 )
    gap> CircleMultiplication( [1], [2,3] );
    [ 5, 5 ]
    
  ------------------------------------------------------------------
  
  You  can  include  some tests for arguments, and maybe the best way of doing
  this  would  be  declaring  a  new  operation  for  two  ring  elements, and
  installing  the  previous function as a method for this operation. This will
  check automatically if the arguments are ring elements from the common ring:
  
  ---------------------------  Example  ----------------------------
    
    gap> DeclareOperation( "BetterCircleMultiplication",                             
    >      [IsRingElement,IsRingElement] );
    gap> InstallMethod( BetterCircleMultiplication,
    >      IsIdenticalObj,
    >      [IsRingElement,IsRingElement],  
    >      CircleMultiplication );
    gap> BetterCircleMultiplication(2,3);
    11
    gap> BetterCircleMultiplication( ZmodnZObj(2,8), ZmodnZObj(5,8) );
    ZmodnZObj( 1, 8 )
    
  ------------------------------------------------------------------
  
  Nevertheless,  the  functionality gained from such operation would be rather
  limited.  You  will  not  be  able  to  compute circle product via the infix
  operator  *,  and,  moreover,  you  will  not be able to create higher level
  objects   such   as  semigroups  and  groups  with  respect  to  the  circle
  multiplication.
  
  In  order  to  "integrate"  the  circle  multiplication into the GAP library
  properly, instead of defining new operations for existing objects, we should
  define  new  objects  for which the infix operator * will perform the circle
  multiplication. This approach is explained in the next two sections.
  
  
  2.2 Defining circle objects
  
  Thus,  we  are  going to implement circle objects, for which we can envisage
  the following functionality:
  
  ---------------------------  Example  ----------------------------
    
    gap> CircleObject( 2 ) * CircleObject( 3 );                       
    CircleObject( 11 )
    
  ------------------------------------------------------------------
  
  First  we need to distinguish these new objects from other GAP objects. This
  is  done  via  the  type  of the objects, that is mainly determined by their
  category, representation and family.
  
  We  start  with  declaring  the  category IsCircleObject as a subcategory of
  IsMultiplicativeElementWithInverse.  Thus,  each  circle  object will "know"
  that  it  is  IsMultiplicativeElementWithInverse,  and  this  will  make  it
  possible  to apply to circle objects such operations as One and Inverse (the
  latter is allowed to return fail for a given circle object).
  
  ---------------------------  Example  ----------------------------
    
    gap> DeclareCategory( "IsCircleObject", IsMultiplicativeElementWithInverse );
    
  ------------------------------------------------------------------
  
  Further  we  would  like to create semigroups and groups generated by circle
  objects. Such structures will be collections of circle objects, so they will
  be  in  the  category  CategoryCollections(  IsCircleObject  ).  This is why
  immediately  after  we declare the underlying category of circle objects, we
  need also to declare the category of their collections:
  
  ---------------------------  Example  ----------------------------
    
    gap> DeclareCategoryCollections( "IsCircleObject" );
    
  ------------------------------------------------------------------
  
  On the next step we should think about the internal representation of circle
  objects.  A  natural  way would be to store the underlying ring element in a
  list-like  structure at its first position. We do not foresee any other data
  that  we need to store internally in the circle object. This is quite common
  situation,  so  we may define first IsPositionalObjectOneSlotRep that is the
  list-like  representation  with  only  one  position  in  the list, and then
  declare  a synonym IsDefaultCircleObject that means that we are dealing with
  a circle object in one-slot representation:
  
  ---------------------------  Example  ----------------------------
    
    gap> DeclareRepresentation( "IsPositionalObjectOneSlotRep",
    >     IsPositionalObjectRep, [ 1 ] );
    gap> DeclareSynonym( "IsDefaultCircleObject",
    >     IsCircleObject and IsPositionalObjectOneSlotRep );
    
  ------------------------------------------------------------------
  
  Until  now  we are still unable to create circle objects, because we did not
  specify  to which family they will belong. Naturally, having a ring, we want
  to  have  all circle objects for elements of this ring in the same family to
  be  able  to  multiply  them,  and  we expect circle objects for elements of
  different  rings  to be placed in different families. Thus, it would be nice
  to  establish  one-to-one correspondence between the family of ring elements
  and   a  family  of  circle  elements  for  this  ring.  We  can  store  the
  corresponding  circle family as an attribute of the ring elements family. To
  do this first we declare an attribute CircleFamily for families:
  
  ---------------------------  Example  ----------------------------
    
    gap> DeclareAttribute( "CircleFamily", IsFamily );
    
  ------------------------------------------------------------------
  
  Now  we  install  the  method that stores the corresponding circle family in
  this attribute:
  
  ---------------------------  Example  ----------------------------
    
    gap> InstallMethod( CircleFamily,
    >     "for a family",
    >     [ IsFamily ],
    >     function( Fam )
    >     local F;
    >   # create the family of circle elements
    >   F:= NewFamily( "CircleFamily(...)", IsCircleObject );
    >   if HasCharacteristic( Fam ) then
    >     SetCharacteristic( F, Characteristic( Fam ) );
    >   fi;
    >   # store the type of objects in the output
    >   F!.CircleType:= NewType( F, IsDefaultCircleObject );
    >   # Return the circle family
    >   return F;
    > end );
    
  ------------------------------------------------------------------
  
  Similarly,  we  want  one-to-one  correspondence between circle elements and
  underlying  ring  elements.  We declare an attribute CircleObject for a ring
  element,  and  then  install the method to create new circle object from the
  ring  element.  This  method  takes  the  family  of the ring element, finds
  corresponding circle family, extracts from it the type of circle objects and
  finally creates the new circle object of that type:
  
  ---------------------------  Example  ----------------------------
    
    gap> DeclareAttribute( "CircleObject", IsRingElement );
    gap> InstallMethod( CircleObject,
    >     "for a ring element",
    >     [ IsRingElement ],
    >     obj -> Objectify( CircleFamily( FamilyObj( obj ) )!.CircleType,
    >                       [ Immutable( obj ) ] ) );
    
  ------------------------------------------------------------------
  
  Only after entering all code above we are able to create some circle object.
  However,  it is displayed just as <object>, though we can get the underlying
  ring element using the "!" operator:
  
  ---------------------------  Example  ----------------------------
    
    gap> a:=CircleObject(2);
    <object>
    gap> a![1];
    2
    
  ------------------------------------------------------------------
  
  We can check that the intended relation between families holds:
  
  ---------------------------  Example  ----------------------------
    
    gap> FamilyObj( CircleObject ( 2 ) ) = CircleFamily( FamilyObj( 2 ) );
    true
    
  ------------------------------------------------------------------
  
  We  can not multiply circle objects yet. But before implementing this, first
  let us improve the output by installing the method for PrintObj:
  
  ---------------------------  Example  ----------------------------
    
    gap> InstallMethod( PrintObj,
    >     "for object in `IsCircleObject'",
    >     [ IsDefaultCircleObject ],
    >     function( obj )
    >     Print( "CircleObject( ", obj![1], " )" );
    >     end );
    
  ------------------------------------------------------------------
  
  This  method  will be used by Print function, and also by View, since we did
  not  install  special  method for ViewObj for circle objects. As a result of
  this installation, the output became more meaningful:
  
  ---------------------------  Example  ----------------------------
    
    gap> a;
    CircleObject( 2 )
    
  ------------------------------------------------------------------
  
  We  need  to  avoid  the  usage  of  "!" operator, which, in general, is not
  recommended  to  the  user  (for  example, if GAP developers will change the
  internal  representation of some object, all GAP functions that deal with it
  must  be  adjusted appropriately, while if the user's code had direct access
  to  that  representation  via  "!", an error may occur). To do this, we wrap
  getting the first component of a circle object in the following operation:
  
  ---------------------------  Example  ----------------------------
    
    gap> DeclareOperation("UnderlyingRingElement", [ IsCircleObject] );
    gap> InstallMethod( UnderlyingRingElement,
    >     "for a circle object", 
    >     [ IsCircleObject],
    >     obj -> obj![1] );
    gap> UnderlyingRingElement(a);
    2
    
  ------------------------------------------------------------------
  
  
  2.3 Installing operations for circle objects
  
  Now we are finally able to install circle multiplication as a default method
  for  the  multiplication of circle objects, and perform the computation that
  we envisaged in the beginning:
  
  ---------------------------  Example  ----------------------------
    
    gap> InstallMethod( \*,
    >     "for two objects in `IsCircleObject'",
    >     IsIdenticalObj,
    >     [ IsDefaultCircleObject, IsDefaultCircleObject ],
    >     function( a, b )
    >     return CircleObject( a![1] + b![1] + a![1]*b![1] );
    >     end );
    gap> CircleObject(2)*CircleObject(3);
    CircleObject( 11 )
    
  ------------------------------------------------------------------
  
  However,  this  functionality  is  not  enough  to form semigroups or groups
  generated by circle elements. We need to be able to check whether two circle
  objects  are equal, and we need to define ordering for them (for example, to
  be  able  to  form  sets  of  circle  elements).  Since we already have both
  operations  for  underlying  ring  elements,  this  can  be implemented in a
  straightforward way:
  
  ---------------------------  Example  ----------------------------
    
    gap> InstallMethod( \=,
    >     "for two objects in `IsCircleObject'",
    >     IsIdenticalObj,
    >     [ IsDefaultCircleObject, IsDefaultCircleObject ],
    >     function( a, b )
    >     return a![1] = b![1];
    >     end );
    gap> InstallMethod( \<,
    >     "for two objects in `IsCircleObject'",
    >     IsIdenticalObj,
    >     [ IsDefaultCircleObject, IsDefaultCircleObject ],
    >     function( a, b )
    >     return a![1] < b![1];
    >     end );
    
  ------------------------------------------------------------------
  
  Further,  zero  element  of the ring plays a role of the neutral element for
  the  circle  multiplication, and we add this knowledge to our code in a form
  of  a method for OneOp that returns circle object for the corresponding zero
  object:
  
  ---------------------------  Example  ----------------------------
    
    gap> InstallMethod( OneOp,
    >     "for an object in `IsCircleObject'",
    >     [ IsDefaultCircleObject ],
    >     a -> CircleObject( Zero( a![1] ) ) );
    gap> One(a);
    CircleObject( 0 )
    
  ------------------------------------------------------------------
  
  Now we are already able to create monoids generated by circle objects:
  
  ---------------------------  Example  ----------------------------
    
    gap> S:=Monoid(a);
    <monoid with 1 generator>
    gap> One(S);
    CircleObject( 0 )
    gap> S:=Monoid( CircleObject( ZmodnZObj( 2,8) ) );
    <monoid with 1 generator>
    gap> Size(S);
    2
    gap> AsList(S);
    [ CircleObject( ZmodnZObj( 0, 8 ) ), CircleObject( ZmodnZObj( 2, 8 ) ) ]
    
  ------------------------------------------------------------------
  
  Finally,  to  generate  groups using circle objects, we need to add a method
  for  the InverseOp. In our implementation we will assume that the underlying
  ring  is  a subring of the ring with one, thus, if the circle inverse for an
  element x exists, than it can be computed as -x(1+x)^-1:
  
  ---------------------------  Example  ----------------------------
    
    gap> InstallMethod( InverseOp,
    >     "for an object in `IsCircleObject'",
    >     [ IsDefaultCircleObject ],
    >     function( a )
    >     local x;
    >     x := Inverse( One( a![1] ) + a![1] );
    >     if x = fail then
    >       return fail;
    >     else
    >       return CircleObject( -a![1] * x );
    >     fi;
    >     end );
    gap> CircleObject(-2)^-1;                
    CircleObject( -2 )
    gap> CircleObject(2)^-1; 
    CircleObject( -2/3 )
    
  ------------------------------------------------------------------
  
  The  last  method  already  makes  it possible to create groups generated by
  circle objects (the warning may be ignored):
  
  ---------------------------  Example  ----------------------------
    
    gap> Group( CircleObject(2) );                       
    #I  default `IsGeneratorsOfMagmaWithInverses' method returns `true' for 
    [ CircleObject( 2 ) ]
    <group with 1 generators>
    gap> G:=Group( [CircleObject( ZmodnZObj( 2,8 ) )  ]);
    #I  default `IsGeneratorsOfMagmaWithInverses' method returns `true' for 
    [ CircleObject( ZmodnZObj( 2, 8 ) ) ]
    <group with 1 generators>
    gap> Size(G);
    2
    gap> AsList(G);
    [ CircleObject( ZmodnZObj( 0, 8 ) ), CircleObject( ZmodnZObj( 2, 8 ) ) ]
    
  ------------------------------------------------------------------
  
  The   GAP   code   used   in   this  Chapter,  is  contained  in  the  files
  circle/lib/circle.gd  and  circle/lib/circle.gi.  We  also refer to last two
  chapters   of   the   GAP  Programming  Tutorial  for  another  examples  of
  implementing new GAP objects and further details.