Python library for arbitrary-precision floating-point arithmetic
npx @tessl/cli install tessl/pypi-mpmath@1.3.0A comprehensive Python library for arbitrary-precision floating-point arithmetic. mpmath provides a complete mathematical computing environment with support for real numbers, complex numbers, interval arithmetic, matrices, and advanced mathematical functions with configurable precision levels.
mpmath enables computational mathematics with arbitrary precision in Python, serving researchers, engineers, and scientists who need exact numerical results beyond standard floating-point limitations. Key features include:
mpmath is designed for applications requiring mathematical rigor, including symbolic computation, numerical analysis, mathematical research, and educational purposes where exact results are essential.
pip install mpmathimport mpmathFor multiprecision arithmetic:
from mpmath import mp, mpf, mpcFor convenience access to functions:
from mpmath import *import mpmath
from mpmath import mp, mpf, mpc
# Set precision to 50 decimal places
mp.dps = 50
# Create high-precision numbers
x = mpf('1.23456789012345678901234567890')
y = mpf('2.71828182845904523536028747135')
# Basic arithmetic
result = x + y
print(f"Sum: {result}")
# Mathematical functions with high precision
pi_precise = mp.pi
e_precise = mp.e
sqrt_result = mp.sqrt(2)
print(f"π with 50 digits: {pi_precise}")
print(f"√2 with 50 digits: {sqrt_result}")
# Complex numbers
z = mpc('1+2j')
w = mpc('3-4j')
complex_result = z * w
print(f"Complex multiplication: {complex_result}")
# Advanced functions
gamma_result = mp.gamma(0.5) # Should equal √π
bessel_result = mp.besselj(0, 1)
print(f"Γ(0.5) = {gamma_result}")
print(f"J₀(1) = {bessel_result}")mpmath is built around context objects that manage precision settings and provide mathematical functions:
The library provides three main number types:
All mathematical functions support both real and complex arguments where mathematically meaningful, and precision can be adjusted globally or locally using context managers.
Basic number types, arithmetic operations, precision control, and utility functions. These form the foundation for all mathematical computations in mpmath.
class mpf:
def __init__(self, x): ...
class mpc:
def __init__(self, real, imag=0): ...
def mpmath.convert(x): ...
def extraprec(n): ...
def workprec(n): ...Standard mathematical functions including exponentials, logarithms, trigonometric functions, hyperbolic functions, and basic operations on complex numbers.
def sqrt(x): ...
def exp(x): ...
def log(x): ...
def sin(x): ...
def cos(x): ...
def tan(x): ...Advanced mathematical functions including gamma functions, zeta functions, Bessel functions, hypergeometric functions, elliptic functions, and other special functions from mathematical physics and number theory.
def gamma(x): ...
def zeta(s): ...
def besselj(n, x): ...
def hyper(a_s, b_s, z): ...
def ellipk(m): ...
def erf(x): ...Matrix operations, linear system solving, eigenvalue computations, matrix decompositions, and matrix functions supporting arbitrary precision arithmetic.
class matrix:
def __init__(self, *args): ...
def lu_solve(A, b): ...
def qr(A): ...
def eig(A): ...
def det(A): ...
def norm(x): ...Numerical integration, differentiation, root finding, optimization, series summation, and differential equation solving with high precision.
def quad(f, a, b): ...
def diff(f, x): ...
def findroot(f, x0): ...
def nsum(f, n1, n2): ...
def taylor(f, x, n): ...High-precision mathematical constants including π, e, and various special constants from number theory and analysis.
mp.pi # π
mp.e # Euler's number
mp.euler # Euler-Mascheroni constant γ
mp.catalan # Catalan's constant
mp.phi # Golden ratio φElliptic functions, modular forms, theta functions, and q-analog functions for advanced mathematical applications including algebraic geometry, number theory, and mathematical physics.
def jacobi(kind, u, m): ...
def jtheta(n, z, q): ...
def kleinj(tau): ...
def eta(tau): ...
def qp(a, q, n): ...
def qhyper(a_s, b_s, q, z): ...Elliptic Functions and Modular Forms
Plotting and visualization functions for mathematical functions, complex analysis, and numerical data with arbitrary precision support.
def plot(f, xlim=[-5, 5]): ...
def cplot(f, re=[-2, 2], im=[-2, 2]): ...
def splot(f, u=[-2, 2], v=[-2, 2]): ...Tools for identifying mathematical constants, finding integer relations, polynomial fitting, and discovering mathematical patterns in numerical data.
def pslq(x, tol=None): ...
def identify(x, constants=None): ...
def findpoly(roots, tol=None): ...Standard waveform functions and signal processing utilities for mathematical modeling and engineering applications.
def squarew(t, period=1): ...
def trianglew(t, period=1): ...
def sawtoothw(t, period=1): ...
def sigmoid(x): ...class mpf:
"""
Multiprecision floating-point number.
Args:
x: Number to convert (int, float, str, or another mpf)
"""
def __init__(self, x): ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
class mpc:
"""
Multiprecision complex number.
Args:
real: Real part (number or string)
imag: Imaginary part (number or string), default 0
"""
def __init__(self, real, imag=0): ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
class matrix:
"""
Matrix supporting arbitrary precision arithmetic.
Args:
*args: Matrix dimensions or initial data
"""
def __init__(self, *args): ...
def __getitem__(self, key): ...
def __setitem__(self, key, value): ...class MPContext:
"""Multiprecision arithmetic context."""
dps: int # Decimal precision
prec: int # Binary precision
pretty: bool # Pretty printing mode
def workprec(self, n): ...
def workdps(self, n): ...
def extraprec(self, n): ...
class FPContext:
"""Fast double-precision floating-point context."""
pass
class MPIntervalContext:
"""Interval arithmetic context."""
pass