gmpy2 is a C-coded Python extension module that supports multiple-precision arithmetic. gmpy2 is the successor to the original gmpy module. The gmpy module only supported the GMP multiple-precision library. gmpy2 adds support for the MPFR (correctly rounded real floating-point arithmetic) and MPC (correctly rounded complex floating-point arithmetic) libraries. gmpy2 also updates the API and naming conventions to be more consistent and support the additional functionality.
The following libraries are supported:
GMP for integer and rational arithmetic
Home page: http://gmplib.org
MPIR is based on the GMP library but adds support for Microsoft’s Visual Studio compiler. It is used to create the Windows binaries.
Home page: http://www.mpir.org
MPFR for correctly rounded real floating-point arithmetic
Home page: http://www.mpfr.org
MPC for correctly rounded complex floating-point arithmetic
Home page: http://mpc.multiprecision.org
Generalized Lucas sequences and primality tests are based on the following code:
mpz_lucas: http://sourceforge.net/projects/mpzlucas/
Pre-compiled versions of gmpy2 are available at PyPi . Please select the installer that corresponds to the version of Python installed on your computer. Note that either a 32 or 64-bit version of Python can be installed on a 64-bit version of Windows. If you get an error message stating that Python could not be found in the registry, you have the wrong version of the gmpy2 installer.
gmpy2 has only been tested with recent versions of GMP, MPFR and MPC. Specifically, for integer and rational support, gmpy2 requires GMP 5.1.x or later. To support multiple-precision floating point arithmetic, MPFR 3.1.x or later is required. MPC 1.0.1 or later is required for complex arithmetic.
You will need to install the development libraries for Python, GMP, MPFR, and MPC. Different Linux distributions may the development packages differently. Typical names are libpython-dev, libgmp-dev, libmpfr-dev, and libmpc-dev.
If your system includes recent versions of GMP, MPFR and MPC, and you have the development libraries installed, compiling should be as simple as:
cd <gmpy2 source directory>
python setup.py build
sudo python setup.py install
If this fails, read on.
If your Linux distribution does not support recent versions of GMP, MPFR and MPC, you will need to compile your own versions. To avoid any possible conflict with existing libraries on your system, it is recommended to use a directory not normally used by your distribution. setup.py will automatically search the following directories for the required libraries:
- /opt/local
- /opt
- /usr/local
- /usr
- /sw
If you can’t use one of these directories, you can use a directory located in your home directory. The examples will use /home/<username>/local. If you use one of standard directories (say /opt/local), then you won’t need to specify –prefix=/home/case/local to setup.py but you will need to specify the prefix when compiling GMP, MPFR, and MPC.
Please substitute your actual user name for <username>.
Create the desired destination directory for GMP, MPFR, and MPC.
$ mkdir /home/<username>/local
Download and un-tar the GMP source code. Change to the GMP source directory and compile GMP.
$ cd /home/<username>/local/src/gmp-6.0.0
$ ./configure --prefix=/home/<username>/local
$ make
$ make check
$ make install
Download and un-tar the MPFR source code. Change to the MPFR source directory and compile MPFR.
$ cd /home/<username>/local/src/mpfr-3.1.2
$ ./configure --prefix=/home/<username>/local --with-gmp=/home/<username>/local
$ make
$ make check
$ make install
Download and un-tar the MPC source code. Change to the MPC source directory and compile MPC.
$ cd /home/<username>/local/src/mpc-1.0.2
$ ./configure --prefix=/home/<username>/local --with-gmp=/home/<username>/local --with-mpfr=/home/<username>/local
$ make
$ make check
$ make install
Compile gmpy2 and specify the location of GMP, MPFR and MPC. The location of the GMP, MPFR, and MPC libraries is embedded into the gmpy2 library so the new versions of GMP, MPFR, and MPC do not need to be installed the system library directories. The prefix directory is added to the beginning of the directories that are checked so it will be found first.
$ python setup.py install --prefix=/home/<username>/local
If you get a “permission denied” error message, you may need to use:
$ python setup.py build --prefix=/home/<username>/local
$ sudo python setup.py install --prefix=/home/<username>/local