|
|
Status
2008-04-20: mpmath is 0.8 is released. See the changelog and release announcement for details.
About mpmath
Mpmath is a pure-Python library for arbitrary-precision floating-point arithmetic. It implements a large set of mathematical functions and provides utilities for arbitrary-precision numerical differentiation, integration, root-finding, interval arithmetic, and other tasks. It supports unlimited exponent sizes, has full support for complex numbers, and is faster than Python's standard decimal library.
Mpmath is free (BSD license) and easy to install or include in other software due to being written entirely in Python with no additional dependencies.
Documentation
Mpmath documentation (HTML)
Provides installation instructions and extensive interactive examples.
List of features
Arithmetic:
- Real and complex numbers with arbitrary precision
- Unlimited exponent sizes / magnitudes
- Support for infinities and not-a-numbers
- Directed rounding
- Rudimentary interval arithmetic
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)
- Gamma functions (complete and incomplete), factorials and binomial coefficients
- Riemann zeta function and Bernoulli numbers
- Bessel function of the first kind
- Arithmetic-geometric mean
- Complete elliptic integrals
- Jacobi, Legendre and Chebyshev functions
- Generic hypergeometric functions
High-level features:
- Integration
- Differentiation
- Limits and summation of infinite series (with convergence acceleration)
- Root-finding
- Polynomial evaluation and root-finding
- Chebyshev approximation
- Basic ODE solvers
- Integer relation detection (constant recognition)
Demonstration
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 quadts(lambda x: 4*sqrt(1-x**2), 0, 1) print quadts(lambda x: exp(-x**2), -inf, inf) ** 2 print sumsh(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 secant(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)
- Glaisher's constant to 20,000 digits (computed in 16 hours)
- Khinchin's constant to 6,000 digits (computed in 8 hours)
- 2500 Bernoulli numbers with 64 digits (computed in 1 minute)
