| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \section{\module{mpz} --- | 
					
						
							| 
									
										
										
										
											1999-02-20 05:20:49 +00:00
										 |  |  |          GNU arbitrary magnitude integers} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-02-20 05:20:49 +00:00
										 |  |  | \declaremodule{builtin}{mpz} | 
					
						
							| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \modulesynopsis{Interface to the GNU MP library for arbitrary | 
					
						
							|  |  |  | precision arithmetic.} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-08-10 14:21:49 +00:00
										 |  |  | This is an optional module.  It is only available when Python is | 
					
						
							|  |  |  | configured to include it, which requires that the GNU MP software is | 
					
						
							|  |  |  | installed. | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \index{MP, GNU library} | 
					
						
							|  |  |  | \index{arbitrary precision integers} | 
					
						
							|  |  |  | \index{integer!arbitrary precision} | 
					
						
							| 
									
										
										
										
											1995-08-10 14:21:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | This module implements the interface to part of the GNU MP library, | 
					
						
							|  |  |  | which defines arbitrary precision integer and rational number | 
					
						
							|  |  |  | arithmetic routines.  Only the interfaces to the \emph{integer} | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | (\function{mpz_*()}) routines are provided. If not stated | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | otherwise, the description in the GNU MP documentation can be applied. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-09-10 18:42:55 +00:00
										 |  |  | Support for rational numbers\index{rational numbers} can be | 
					
						
							| 
									
										
										
										
											1999-04-22 20:55:59 +00:00
										 |  |  | implemented in Python.  For an example, see the | 
					
						
							|  |  |  | \module{Rat}\withsubitem{(demo module)}{\ttindex{Rat}} module, provided as | 
					
						
							| 
									
										
										
										
											1998-09-10 18:42:55 +00:00
										 |  |  | \file{Demos/classes/Rat.py} in the Python source distribution. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | In general, \dfn{mpz}-numbers can be used just like other standard | 
					
						
							| 
									
										
										
										
											1998-09-10 18:42:55 +00:00
										 |  |  | Python numbers, e.g., you can use the built-in operators like \code{+}, | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \code{*}, etc., as well as the standard built-in functions like | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \function{abs()}, \function{int()}, \ldots, \function{divmod()}, | 
					
						
							|  |  |  | \function{pow()}.  \strong{Please note:} the \emph{bitwise-xor} | 
					
						
							|  |  |  | operation has been implemented as a bunch of \emph{and}s, | 
					
						
							|  |  |  | \emph{invert}s and \emph{or}s, because the library lacks an | 
					
						
							|  |  |  | \cfunction{mpz_xor()} function, and I didn't need one. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | You create an mpz-number by calling the function \function{mpz()} (see | 
					
						
							| 
									
										
										
										
											1995-03-13 10:03:32 +00:00
										 |  |  | below for an exact description). An mpz-number is printed like this: | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \code{mpz(\var{value})}. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{mpz}{value} | 
					
						
							|  |  |  |   Create a new mpz-number. \var{value} can be an integer, a long, | 
					
						
							|  |  |  |   another mpz-number, or even a string. If it is a string, it is | 
					
						
							|  |  |  |   interpreted as an array of radix-256 digits, least significant digit | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  |   first, resulting in a positive number. See also the \method{binary()} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   method, described below. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{datadesc}{MPZType} | 
					
						
							|  |  |  |   The type of the objects returned by \function{mpz()} and most other | 
					
						
							|  |  |  |   functions in this module. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-01-14 14:51:31 +00:00
										 |  |  | A number of \emph{extra} functions are defined in this module. Non | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | mpz-arguments are converted to mpz-values first, and the functions | 
					
						
							|  |  |  | return mpz-numbers. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{powm}{base, exponent, modulus} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If | 
					
						
							|  |  |  |   \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  |   \C{} library function, this version can handle negative exponents. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{gcd}{op1, op2} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Return the greatest common divisor of \var{op1} and \var{op2}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{gcdext}{a, b} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that | 
					
						
							|  |  |  |   \code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{sqrt}{op} | 
					
						
							|  |  |  |   Return the square root of \var{op}. The result is rounded towards zero. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{sqrtrem}{op} | 
					
						
							|  |  |  |   Return a tuple \code{(\var{root}, \var{remainder})}, such that | 
					
						
							|  |  |  |   \code{\var{root}*\var{root} + \var{remainder} == \var{op}}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{divm}{numerator, denominator, modulus} | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  |   Returns a number \var{q} such that | 
					
						
							|  |  |  |   \code{\var{q} * \var{denominator} \%{} \var{modulus} == | 
					
						
							|  |  |  |   \var{numerator}}.  One could also implement this function in Python, | 
					
						
							|  |  |  |   using \function{gcdext()}. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An mpz-number has one method: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}[mpz]{binary}{} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Convert this mpz-number to a binary string, where the number has been | 
					
						
							|  |  |  |   stored as an array of radix-256 digits, least significant digit first. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-03-13 10:03:32 +00:00
										 |  |  |   The mpz-number must have a value greater than or equal to zero, | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  |   otherwise \exception{ValueError} will be raised. | 
					
						
							|  |  |  | \end{methoddesc} |