The mpmath setup files can be downloaded from the mpmath download page or the Python Package Index. Download the source package (available as both .zip and .tar.gz), extract it, open the extracted directory, and run
python setup.py install
If you are using Windows, you can download the binary installer
from the mpmath website or the Python Package Index. Run the installer and follow the instructions.
If you have setuptools installed, you can download and install mpmath in one step by running:
python -m easy_install mpmath
If you have an old version of mpmath installed already, you may have to pass easy_install the -U flag to force an upgrade.
Debian and Ubuntu users can install mpmath with
sudo apt-get install python-mpmath
Mpmath is provided in the “Science” repository for all recent versions of openSUSE. To add this repository to the YAST software management tool, see http://en.opensuse.org/Add_Package_Repositories_to_YaST and use one of the following repository URLs:
See http://code.google.com/p/mpmath/source/checkout for instructions on how to check out the mpmath Subversion repository. The source code can also be browsed online from the Google Code page.
After the setup has completed, you should be able to fire up the interactive Python interpreter and do the following:
>>> from mpmath import * >>> mp.dps = 50 >>> print mpf(2) ** mpf('0.5') 1.4142135623730950488016887242096980785696718753769 >>> print 2*pi 6.2831853071795864769252867665590057683943387987502
Note: if you have mpmath 0.6 or earlier installed, you may have to either first uninstall the old version, or manually delete the obsolete mpmath/lib directory. Otherwise this directory will be imported erroneously.
By default, mpmath uses Python integers internally. If gmpy version 1.03 or later is installed on your system, mpmath will automatically detect it and transparently use gmpy integers intead. This makes mpmath much faster, especially at high precision (approximately above 100 digits).
To verify that mpmath uses gmpy, check the internal MODE variable is not equal to ‘python’:
>>> import mpmath.settings >>> mpmath.settings.MODE 'gmpy'
The gmpy mode can be disabled by setting the MPMATH_NOGMPY environment variable. Note that the mode cannot be switched during runtime; mpmath must be re-imported for this change to take effect.
It is recommended that you run mpmath’s full set of unit tests to make sure everything works. The tests are located in the tests subdirectory of the main mpmath directory. They can be run in the interactive interpreter using the runtests() function:
import mpmath mpmath.runtests()
Alternatively, they can be run from the tests directory via
The tests should finish in about a minute. If you have psyco installed, the tests can also be run with
python runtests.py -psyco
which will cut the running time in half.
To run the tests with support for gmpy disabled, use
python runtests.py -nogmpy
To enable extra diagnostics, use
python runtests.py -strict
If you downloaded the source package, the text source for these documentation pages is included in the doc directory. The documentation can be compiled to pretty HTML using Sphinx. Go to the doc directory and run
You can also test that all the interactive examples in the documentation work by running
and by running the individual .py files in the mpmath source.
(The doctests may take several minutes.)
Finally, some additional demo scripts are available in the demo directory included in the source package.