or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-arithmetic.mdelementary-functions.mdelliptic-modular-functions.mdindex.mdlinear-algebra.mdmathematical-constants.mdnumerical-calculus.mdpattern-recognition.mdsignal-processing.mdspecial-functions.mdvisualization.md
tile.json

tessl/pypi-mpmath

Python library for arbitrary-precision floating-point arithmetic

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/mpmath@1.3.x

To install, run

npx @tessl/cli install tessl/pypi-mpmath@1.3.0

index.mddocs/

mpmath

A 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.

Overview

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:

  • Arbitrary Precision Arithmetic: Perform calculations with hundreds or thousands of decimal places
  • Comprehensive Mathematical Functions: Complete implementation of elementary functions, special functions, and advanced mathematical operations
  • Multiple Number Systems: Support for real numbers (mpf), complex numbers (mpc), and interval arithmetic (mpi)
  • Scientific Computing Tools: Numerical integration, differentiation, root finding, linear algebra, and differential equations
  • Mathematical Constants: High-precision values of π, e, and other important mathematical constants
  • Specialized Functions: Gamma functions, Bessel functions, elliptic functions, hypergeometric functions, and more
  • Visualization Support: Built-in plotting capabilities for mathematical functions and complex analysis
  • Flexible Precision Control: Dynamic precision adjustment with context managers for optimal performance

mpmath is designed for applications requiring mathematical rigor, including symbolic computation, numerical analysis, mathematical research, and educational purposes where exact results are essential.

Package Information

  • Package Name: mpmath
  • Language: Python
  • Installation: pip install mpmath

Core Imports

import mpmath

For multiprecision arithmetic:

from mpmath import mp, mpf, mpc

For convenience access to functions:

from mpmath import *

Basic Usage

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}")

Architecture

mpmath is built around context objects that manage precision settings and provide mathematical functions:

  • MPContext (mp): Multiprecision arithmetic with configurable precision
  • FPContext (fp): Fast double-precision floating-point arithmetic
  • MPIntervalContext (iv): Interval arithmetic for rigorous numerical computation

The library provides three main number types:

  • mpf: Multiprecision floating-point numbers
  • mpc: Multiprecision complex numbers
  • mpi: Multiprecision intervals

All mathematical functions support both real and complex arguments where mathematically meaningful, and precision can be adjusted globally or locally using context managers.

Capabilities

Core Number Types and Arithmetic

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): ...

Core Arithmetic

Elementary Functions

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): ...

Elementary Functions

Special Functions

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): ...

Special Functions

Linear Algebra and Matrices

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): ...

Linear Algebra

Numerical Calculus

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): ...

Numerical Calculus

Mathematical Constants

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 φ

Mathematical Constants

Elliptic Functions and Modular Forms

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

Visualization and Plotting

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]): ...

Visualization and Plotting

Pattern Recognition and Mathematical Identification

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): ...

Pattern Recognition

Signal Processing and Waveforms

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): ...

Signal Processing

Types

Core Types

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): ...

Context Types

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