home *** CD-ROM | disk | FTP | other *** search
/ Math Solutions 1995 October / Math_Solutions_CD-ROM_Walnut_Creek_October_1995.iso / pc / mac / discrete / lib / rational.g < prev    next >
Encoding:
Text File  |  1993-05-05  |  7.9 KB  |  296 lines

  1. #############################################################################
  2. ##
  3. #A  rational.g                  GAP library                  Martin Schoenert
  4. ##
  5. #A  @(#)$Id: rational.g,v 3.6 1993/02/12 11:59:52 martin Rel $
  6. ##
  7. #Y  Copyright 1990-1992,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
  8. ##
  9. ##  This file contains  those  functions  that  mainly  deal  with rationals.
  10. ##
  11. #H  $Log: rational.g,v $
  12. #H  Revision 3.6  1993/02/12  11:59:52  martin
  13. #H  added 'RationalOps.FastPolynomial'
  14. #H
  15. #H  Revision 3.5  1992/12/16  19:47:27  martin
  16. #H  replaced quoted record names with escaped ones
  17. #H
  18. #H  Revision 3.4  1992/11/16  12:23:44  fceller
  19. #H  added Laurent polynomials
  20. #H
  21. #H  Revision 3.3  1992/06/17  07:06:04  fceller
  22. #H  moved '<somedomain>.operations.Polynomial' function to "<somedomain>.g"
  23. #H
  24. #H  Revision 3.2  1992/06/01  07:48:16  fceller
  25. #H  added read of "polyrat.g"
  26. #H
  27. #H  Revision 3.1  1992/02/06  11:47:31  martin
  28. #H  removed 'InverseMod' and added 'QuotientMod'
  29. #H
  30. #H  Revision 3.0  1991/12/27  15:00:00  martin
  31. #H  initial revision under RCS
  32. #H
  33. ##
  34.  
  35.  
  36. #############################################################################
  37. ##
  38. #F  Rationals . . . . . . . . . . . . . . . . . . . . . .  field of rationals
  39. #F  RationalsOps  . . . . . . . . . . . . operations record for the rationals
  40. ##
  41. RationalsOps := Copy( FieldOps );
  42.  
  43. Rationals := rec(
  44.     isDomain                    := true,
  45.     isField                     := true,
  46.     isCyclotomicField           := true,
  47.  
  48.     char                        := 0,
  49.     generators                  := [ 1 ],
  50.     zero                        := 0,
  51.     one                         := 1,
  52.     name                        := "Rationals",
  53.  
  54.     size                        := "infinity",
  55.     isFinite                    := false,
  56.     degree                      := 1,
  57.  
  58.     field                       := 0,
  59.     dimension                   := 1,
  60.     base                        := [ 1 ],
  61.     automorphisms               := [ e -> e ],
  62.  
  63.     operations                  := RationalsOps
  64. );
  65.  
  66.  
  67. #############################################################################
  68. ##
  69. #F  RationalsOps.Field(<elms>)  . . . . . . field generated by some rationals
  70. ##
  71. RationalsOps.Field := function ( elms )
  72.     return Rationals;
  73. end;
  74.  
  75.  
  76. #############################################################################
  77. ##
  78. #F  RationalsOps.DefaultField(<elms>) . . . . default field of some rationals
  79. ##
  80. RationalsOps.DefaultField := function ( elms )
  81.     return Rationals;
  82. end;
  83.  
  84.  
  85. #############################################################################
  86. ##
  87. #F  RationalsOps.\in(<x>,<Rationals>)  . . . . membership test for rationals
  88. ##
  89. RationalsOps.\in := function ( x, Rationals )
  90.     return IsRat( x );
  91. end;
  92.  
  93.  
  94. #############################################################################
  95. ##
  96. #F  RationalsOps.Random(<Rationals>)  . . . . . . . . . . . . random rational
  97. ##
  98. RationalsOps.Random := function ( Rationals )
  99.     local    num, den;
  100.     num := Random( Integers );
  101.     repeat
  102.         den := Random( Integers );
  103.     until den <> 0;
  104.     return num / den;
  105. end;
  106.  
  107.  
  108. #############################################################################
  109. ##
  110. #F  RationalOps.Conjugates(<Rationals>,<x>) . . . .  conjugates of a rational
  111. ##
  112. RationalsOps.Conjugates := function ( Rationals, x )
  113.     return [ x ];
  114. end;
  115.  
  116.  
  117. #############################################################################
  118. ##
  119. #F  RationalsOps.AsGroup(<Rationals>) . . . . . . . . . view the rationals as
  120. #F                                                       multiplicative group
  121. ##
  122. RationalsOps.AsGroup := function ( Rationals )
  123.     Error("the multiplicative group of Q is not finitely generated");
  124. end;
  125.  
  126.  
  127. #############################################################################
  128. ##
  129. #F  RationalsOps.AsAdditiveGroup(<Rationals>) . . . . . view the rationals as
  130. #F                                                             additive group
  131. ##
  132. RationalsOps.AsAdditiveGroup := function ( Rationals )
  133.     Error("the additive group of Q is not finitely generated");
  134. end;
  135.  
  136.  
  137. #############################################################################
  138. ##
  139. #F  RationalsOps.AsRing(<Rationals>)  . . . . . .  view the rationals as ring
  140. ##
  141. #N  23-Oct-91 martin this should be 'FieldOps.AsRing'
  142. ##
  143. RationalsAsRingOps := Copy( RingOps );
  144.  
  145. RationalsAsRingOps.\in := RationalsOps.\in;
  146.  
  147. RationalsAsRingOps.Random := RationalsOps.Random;
  148.  
  149. RationalsAsRingOps.Quotient := function ( R, r, s )
  150.     return r/s;
  151. end;
  152.  
  153. RationalsAsRingOps.IsUnit := function ( R, r )
  154.     return r <> R.zero;
  155. end;
  156.  
  157. RationalsAsRingOps.Units := function ( R )
  158.     return AsGroup( R.field );
  159. end;
  160.  
  161. RationalsAsRingOps.IsAssociated := function ( R, r, s )
  162.     return (r = R.zero) = (s = R.zero);
  163. end;
  164.  
  165. RationalsAsRingOps.StandardAssociate := function ( R, r )
  166.     if r = R.zero  then
  167.         return R.zero;
  168.     else
  169.         return R.one;
  170.     fi;
  171. end;
  172.  
  173. RationalsOps.AsRing := function ( Rationals )
  174.  
  175.     return rec(
  176.         isDomain                := true,
  177.         isRing                  := true,
  178.  
  179.         zero                    := 0,
  180.         one                     := 1,
  181.  
  182.         isFinite                := false,
  183.         size                    := "infinity",
  184.         isCommutativeRing       := true,
  185.         isIntegralRing          := true,
  186.         field                   := Rationals,
  187.  
  188.         operations              := RationalsAsRingOps
  189.     );
  190. end;
  191.  
  192.  
  193. #############################################################################
  194. ##
  195. #F  RationalsOps.PolynomialRing( <R> )  . . . . . . . .  full polynomial ring
  196. ##
  197. RationalsOps.PolynomialRing := function( R )
  198.     return RationalsPolynomials;
  199. end;
  200.  
  201.  
  202. #############################################################################
  203. ##
  204. #F  RationalsOps.Polynomial( <R>, <coeffs>, <val> ) . . . polynomial over <R>
  205. ##
  206. RationalsOps.Polynomial := function( R, coeffs, val )
  207.     local  i,  k,  l,  c;
  208.  
  209.     # remove leading zeros
  210.     k := Length( coeffs );
  211.     while 0 < k and coeffs[k] = R.zero  do
  212.         k := k - 1;
  213.     od;
  214.  
  215.     # remove trailing zeros
  216.     i := 0;
  217.     while i < k and coeffs[i+1] = R.zero  do
  218.     i := i + 1;
  219.     od;
  220.  
  221.     # now really remove zeros
  222.     c := [];
  223.     for l  in [ i+1 .. k ]  do
  224.     c[l-i] := coeffs[l];
  225.     od;
  226.     if i < k  then
  227.         val := val + i;
  228.     else
  229.         val := 0;
  230.     fi;
  231.     IsVector(c);
  232.  
  233.     # return polynomial
  234.     if val < 0  then
  235.         return rec( coefficients := c,
  236.                     baseRing     := R,
  237.                     isPolynomial := true,
  238.             valuation    := val,
  239.                     domain       := LaurentPolynomials,
  240.                     operations   := RationalsPolynomialOps );
  241.     else
  242.         return rec( coefficients := c,
  243.                     baseRing     := R,
  244.                     isPolynomial := true,
  245.             valuation    := val,
  246.                     domain       := RationalsPolynomials,
  247.                     operations   := RationalsPolynomialOps );
  248.     fi;
  249.  
  250. end;
  251.  
  252.  
  253. #############################################################################
  254. ##
  255. #F  RationalsOps.FastPolynomial( <R>, <coeffs>, <val> ) . polynomial over <R>
  256. ##
  257. ##  This function will *not* copy or check <coeffs>.
  258. ##
  259. RationalsOps.FastPolynomial := function( R, coeffs, val )
  260.  
  261.     # return polynomial
  262.     if val < 0  then
  263.         return rec( coefficients := coeffs,
  264.                     baseRing     := R,
  265.                     isPolynomial := true,
  266.             valuation    := val,
  267.                     domain       := LaurentPolynomials,
  268.                     operations   := RationalsPolynomialOps );
  269.     else
  270.         return rec( coefficients := coeffs,
  271.                     baseRing     := R,
  272.                     isPolynomial := true,
  273.             valuation    := val,
  274.                     domain       := RationalsPolynomials,
  275.                     operations   := RationalsPolynomialOps );
  276.     fi;
  277.  
  278. end;
  279.  
  280.  
  281. #############################################################################
  282. ##
  283. #E  Emacs . . . . . . . . . . . . . . . . . . . . . . . local emacs variables
  284. ##
  285. ##  Local Variables:
  286. ##  mode:               outline
  287. ##  outline-regexp:     "#F\\|#V\\|#E"
  288. ##  fill-column:        73
  289. ##  fill-prefix:        "##  "
  290. ##  eval:               (hide-body)
  291. ##  End:
  292. ##
  293.  
  294.  
  295.  
  296.