Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{mpz} --- |
Fred Drake | 76991ec | 1999-02-20 05:20:49 +0000 | [diff] [blame] | 2 | GNU arbitrary magnitude integers} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 76991ec | 1999-02-20 05:20:49 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{mpz} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 5 | \modulesynopsis{Interface to the GNU MP library for arbitrary |
| 6 | precision arithmetic.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 3e43d83 | 1995-08-10 14:21:49 +0000 | [diff] [blame] | 9 | This is an optional module. It is only available when Python is |
| 10 | configured to include it, which requires that the GNU MP software is |
| 11 | installed. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 12 | \index{MP, GNU library} |
| 13 | \index{arbitrary precision integers} |
| 14 | \index{integer!arbitrary precision} |
Guido van Rossum | 3e43d83 | 1995-08-10 14:21:49 +0000 | [diff] [blame] | 15 | |
| 16 | This module implements the interface to part of the GNU MP library, |
| 17 | which defines arbitrary precision integer and rational number |
| 18 | arithmetic routines. Only the interfaces to the \emph{integer} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 19 | (\function{mpz_*()}) routines are provided. If not stated |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 20 | otherwise, the description in the GNU MP documentation can be applied. |
| 21 | |
Fred Drake | 9b134bf | 1998-09-10 18:42:55 +0000 | [diff] [blame] | 22 | Support for rational numbers\index{rational numbers} can be |
Fred Drake | fcf94d4 | 1999-04-22 20:55:59 +0000 | [diff] [blame] | 23 | implemented in Python. For an example, see the |
| 24 | \module{Rat}\withsubitem{(demo module)}{\ttindex{Rat}} module, provided as |
Fred Drake | 9b134bf | 1998-09-10 18:42:55 +0000 | [diff] [blame] | 25 | \file{Demos/classes/Rat.py} in the Python source distribution. |
| 26 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 27 | In general, \dfn{mpz}-numbers can be used just like other standard |
Fred Drake | 9b134bf | 1998-09-10 18:42:55 +0000 | [diff] [blame] | 28 | Python numbers, e.g., you can use the built-in operators like \code{+}, |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 29 | \code{*}, etc., as well as the standard built-in functions like |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 30 | \function{abs()}, \function{int()}, \ldots, \function{divmod()}, |
| 31 | \function{pow()}. \strong{Please note:} the \emph{bitwise-xor} |
| 32 | operation has been implemented as a bunch of \emph{and}s, |
| 33 | \emph{invert}s and \emph{or}s, because the library lacks an |
| 34 | \cfunction{mpz_xor()} function, and I didn't need one. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 35 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 36 | You create an mpz-number by calling the function \function{mpz()} (see |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 37 | below for an exact description). An mpz-number is printed like this: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 38 | \code{mpz(\var{value})}. |
| 39 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 41 | \begin{funcdesc}{mpz}{value} |
| 42 | Create a new mpz-number. \var{value} can be an integer, a long, |
| 43 | another mpz-number, or even a string. If it is a string, it is |
| 44 | interpreted as an array of radix-256 digits, least significant digit |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 45 | first, resulting in a positive number. See also the \method{binary()} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 46 | method, described below. |
| 47 | \end{funcdesc} |
| 48 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 49 | \begin{datadesc}{MPZType} |
| 50 | The type of the objects returned by \function{mpz()} and most other |
| 51 | functions in this module. |
| 52 | \end{datadesc} |
| 53 | |
| 54 | |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 55 | A number of \emph{extra} functions are defined in this module. Non |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 56 | mpz-arguments are converted to mpz-values first, and the functions |
| 57 | return mpz-numbers. |
| 58 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 59 | \begin{funcdesc}{powm}{base, exponent, modulus} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 60 | Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If |
| 61 | \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 62 | \C{} library function, this version can handle negative exponents. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 63 | \end{funcdesc} |
| 64 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 65 | \begin{funcdesc}{gcd}{op1, op2} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 66 | Return the greatest common divisor of \var{op1} and \var{op2}. |
| 67 | \end{funcdesc} |
| 68 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 69 | \begin{funcdesc}{gcdext}{a, b} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 70 | Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that |
| 71 | \code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}. |
| 72 | \end{funcdesc} |
| 73 | |
| 74 | \begin{funcdesc}{sqrt}{op} |
| 75 | Return the square root of \var{op}. The result is rounded towards zero. |
| 76 | \end{funcdesc} |
| 77 | |
| 78 | \begin{funcdesc}{sqrtrem}{op} |
| 79 | Return a tuple \code{(\var{root}, \var{remainder})}, such that |
| 80 | \code{\var{root}*\var{root} + \var{remainder} == \var{op}}. |
| 81 | \end{funcdesc} |
| 82 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 83 | \begin{funcdesc}{divm}{numerator, denominator, modulus} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 84 | Returns a number \var{q} such that |
| 85 | \code{\var{q} * \var{denominator} \%{} \var{modulus} == |
| 86 | \var{numerator}}. One could also implement this function in Python, |
| 87 | using \function{gcdext()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 88 | \end{funcdesc} |
| 89 | |
| 90 | An mpz-number has one method: |
| 91 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 92 | \begin{methoddesc}[mpz]{binary}{} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 93 | Convert this mpz-number to a binary string, where the number has been |
| 94 | stored as an array of radix-256 digits, least significant digit first. |
| 95 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 96 | The mpz-number must have a value greater than or equal to zero, |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 97 | otherwise \exception{ValueError} will be raised. |
| 98 | \end{methoddesc} |
Fred Drake | b40501b | 2001-12-15 18:37:24 +0000 | [diff] [blame^] | 99 | |
| 100 | |
| 101 | \begin{seealso} |
| 102 | \seetitle[http://gmpy.sourceforge.net/]{General Multiprecision Python}{ |
| 103 | This project is building new numeric types to allow |
| 104 | arbitrary-precision arithmetic in Python. Their first |
| 105 | efforts are also based on the GNU MP library.} |
| 106 | \end{seealso} |