mpmath

Pure-Python library for arbitrary-precision floating-point arithmetic and numerical computation

Library
PyPI
v1.4.1
1,195stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
86/100Excellent
Development Activity100
Maintenance72
Community84
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
75/100Good
Architecture80
Code Quality82
Innovation78
Learning Curve60

mpmath implements arbitrary-precision real and complex floating-point arithmetic in Python, letting users compute with hundreds or thousands of digits of precision instead of the fixed 53-bit precision of native floats. Beyond basic arithmetic, it provides a comprehensive library of special functions (gamma, zeta, Bessel, hypergeometric, elliptic functions), numerical calculus (integration, differentiation, root-finding, ODE solving), and arbitrary-precision linear algebra.

Written and maintained by Fredrik Johansson, mpmath is a foundational dependency of SymPy (which uses it for numerical evaluation) and is used throughout the scientific Python ecosystem wherever exact-precision or high-precision numerical results are required, such as verifying symbolic results or evaluating special functions to guaranteed accuracy.

What You Get

  • Arbitrary-precision real (mpf) and complex (mpc) floating-point types with configurable precision
  • A large catalog of special functions: gamma, zeta, Bessel, hypergeometric, elliptic integrals, polylogarithms, and more
  • Numerical calculus tools: adaptive quadrature, numerical differentiation, root-finding, and ODE solvers, all at arbitrary precision
  • Arbitrary-precision matrix and linear algebra support via the matrices module
  • Optional interval arithmetic (iv context) and optional acceleration via gmpy2/python-gmp backends

Common Use Cases

  • Verifying or cross-checking symbolic math results numerically (this is exactly how SymPy uses mpmath internally)
  • Evaluating special functions (Bessel, zeta, gamma, elliptic) to high precision for scientific computing
  • Computing constants like pi or e to thousands of digits, or running high-precision numerical experiments
  • Numerically solving equations, integrals, or ODEs where standard 64-bit floats lose accuracy
  • Research and educational use requiring guaranteed numerical precision beyond hardware float limits

Under The Hood

Architecture mpmath is organized around a set of interchangeable numerical “contexts” (ctx_mp.py for the default arbitrary-precision context, ctx_fp.py for a fast fixed-precision float context, ctx_iv.py for interval arithmetic), with libmp/ providing the low-level big-integer-backed floating-point kernel that every context builds on; functions/ and calculus/ layer special functions and numerical algorithms on top of whichever context is active, and matrices/ adds arbitrary-precision linear algebra.

Tech Stack Pure Python by default (no compiled extensions required), with optional acceleration by installing gmpy2 or python-gmp for faster big-integer arithmetic; packaged with modern setuptools/setuptools-scm, targets Python 3.10+, and integrates with numpy/matplotlib only for its optional test and plotting extras.

Code Quality The project has an extensive tests/ directory covering functions, calculus, matrices, and identification routines, plus flake518 linting and pytest-cov coverage tracking configured in develop extras; documentation is unusually thorough for a numerical library, with dedicated .rst pages for basics, contexts, calculus, matrices, and technical internals.

API Design The primary interface mirrors ordinary Python numeric usage (mpmath.mpf('1.5'), mpmath.sqrt(2)) while precision is controlled globally via mpmath.mp.dps, making basic use approachable; deeper features like switching numerical contexts or configuring interval arithmetic require reading the context-specific docs, giving it a moderate learning curve for advanced use.

Used by 5 apps in this directory

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search