mpmath


Python library for arbitrary-precision floating-point arithmetic

Status

2014-04-28: the main website for mpmath is now http://mpmath.org/. The Google Code project site is deprecated.

The mpmath source code is now hosted on github: https://github.com/fredrik-johansson/mpmath/

Issues can be reported at https://github.com/fredrik-johansson/mpmath/issues

2013-12-31: mpmath 0.18 released. See the changelog for details

(Older news)

About mpmath

Mpmath is a pure-Python library for multiprecision floating-point arithmetic. It provides an extensive set of transcendental functions, unlimited exponent sizes, complex numbers, interval arithmetic, numerical integration and differentiation, root-finding, linear algebra, and much more. Almost any calculation can be performed just as well at 10-digit or 1000-digit precision, and in many cases mpmath implements asymptotically fast algorithms that scale well for extremely high precision work. Mpmath internally uses Python's builtin long integers by default, but automatically switches to http://gmplib.org/'>GMP/http://www.mpir.org/'>MPIR for much faster high-precision arithmetic if gmpy is installed or if mpmath is imported from within Sage.

Mpmath is free (BSD license) and easy to install or include in other software due to being written entirely in Python with no additional required dependencies. It runs on Python 2.5 or higher, including Python 3.x. It can be used as a library, interactively via the Python interpreter, or via SymPy which uses it for numerical evaluation of symbolic expressions. Mpmath is also a standard component of Sage which uses it for special function evaluation.

If matplotlib is available, mpmath also provides a convenient plotting interface. The three images below were generated by the commands plot([fresnels, fresnelc]), cplot(fresnels), and the mandelbrot.py script. See the function gallery for more images.

http://mpmath.googlecode.com/svn/doc/frontpic.png

Documentation

The documentation provides installation instructions and extensive interactive examples.

Main documentation, 0.18 release (HTML)

Previous versions: 0.17 0.16, 0.15, 0.14, 0.13 0.12

List of features

Arithmetic: * Real and complex numbers with arbitrary precision * Unlimited exponent sizes / magnitudes * Support for infinities and not-a-numbers * Directed rounding * Real and complex interval arithmetic * Matrices with arbitrary-precision real, complex or interval elements

Special functions: * Elementary functions (sqrt, exp, log, trigonometric, hyperbolic, inverse trig and hyperbolic) * Standard mathematical constants: pi, e, the golden ratio, Euler's constant (gamma) * Less standard constants: Catalan's, Apery's, Khinchin's and Glaisher's constants * Lambert W function (all branches) * Error function (erf), imaginary and complementary error functions; inverse error function; normal distribution functions * Gamma functions (complete and incomplete), factorials, double factorials and binomial coefficients, log gamma function; complete and incomplete beta functions * Fibonacci numbers * Barnes G-function, super- and hyperfactorials * Polygamma functions * Riemann zeta function, Hurwitz zeta function, Riemann-Siegel and related functions; evaluation with the Riemann-Siegel expansion; Riemann zeta zeros * Bernoulli numbers (fast numerical and exact computation of large Bernoulli numbers), Bernoulli polynomials, Euler numbers and polynomials * Polylogarithms, Clausen functions * Stieltjes constants * Bessel functions; Hankel, Struve, Kelvin, Whittaker, Airy, Coulomb functions; Bessel function zeros; parabolic cylinder functions * Exponential and trigonometric integrals * Arithmetic-geometric mean * Complete and incomplete elliptic integrals (Legendre and Carlsen forms) * Jacobi elliptic functions and Jacobi theta functions * Jacobi, Legendre and Chebyshev and other orthogonal polynomials; associated Legendre functions; spherical harmonics * Generalized hypergeometric functions; the Meijer G-function; Borel regularized hypergeometric series; bilateral series; 2D hypergeometric series (Appell, Horn, Kampé de Fériet functions) * q-factorials and q-hypergeometric series

Calculus and other general high-level mathematics: * Numerical integration (regular, double/triple integrals, oscillatory) * Numerical differentiation and differintegration (arbitrary orders); partial derivatives * Limits and summation of infinite series (with convergence acceleration) * Multidimensional series * Root-finding (1D and multidimensional; secant method, bisection, modified Newton's method, and other algorithms) * Polynomial evaluation and polynomial root-finding * Chebyshev approximation * ODE solvers * Fourier and Taylor series * Integer relation detection (constant recognition) * Linear algebra functions (linear system solving, LU factorization, matrix inverse, matrix norms, matrix exponentials/logarithms/square roots, eigenvalues, singular values, QR factorization)

High-precision examples

A few different ways to compute pi (here to 50 decimal places): directly, as special values of various functions, using quadrature of the unit circle, quadrature of a Gaussian integral, accelerated summation of the slowly convergent Leibniz series, a limit of a sequence, and root-finding. To test mpmath, you can paste the following directly into an interactive Python session:

from mpmath import * mp.dps = 50 print pi print 2*asinh(j).imag print gamma(0.5)**2 print sqrt(6*zeta(2)) print quad(lambda x: 4*sqrt(1-x**2), [0, 1]) print quad(lambda x: exp(-x**2), [-inf, inf]) ** 2 print nsum(lambda n: 4*(-1)**n/(2*n+1), [0, inf]) print limit(lambda n: 2**(4*n+1)*factorial(n)**4/(2*n+1)/factorial(2*n)**2, inf) print findroot(sin, 3.14)

Each command should print 3.1415926535897932384626433832795028841971693993751.

Some additional demo scripts are available: * pidigits.py -- print thousands of digits of pi in any base * manydigits.py -- outputs solutions to Many Digits competition problems * taylor.py -- educational interval arithmetic

Some example computations with high precision: * The golden ratio to 100,000 digits (computed in 4 seconds, in pure Python mode) * Glaisher's constant to 20,000 digits (computed in 1 hour) * Khinchin's constant to 10,000 digits (computed in 11 minutes) * 2500 Bernoulli numbers with 64 digits (computed in 6 seconds)

Projects using mpmath

  • Sage includes mpmath
  • SymPy and SympyCore, Python computer algebra systems, use mpmath for numerical evaluation
  • hcpy, an RPN calculator
  • pytaylor, Taylor models for Python using mpmath and sympy
  • NZMATH uses mpmath for auxiliary calculations in its ECPP code

Research using mpmath / citing mpmath

See the page citing mpmath.

Project Information

Labels:
python floating-point arbitrary-precision bignum arithmetic mathematics math multiprecision