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