Python library for arbitrary-precision floating-point arithmetic
npx @tessl/cli install tessl/pypi-mpmath@1.3.00
# mpmath
1
2
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.
3
4
## Overview
5
6
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:
7
8
- **Arbitrary Precision Arithmetic**: Perform calculations with hundreds or thousands of decimal places
9
- **Comprehensive Mathematical Functions**: Complete implementation of elementary functions, special functions, and advanced mathematical operations
10
- **Multiple Number Systems**: Support for real numbers (mpf), complex numbers (mpc), and interval arithmetic (mpi)
11
- **Scientific Computing Tools**: Numerical integration, differentiation, root finding, linear algebra, and differential equations
12
- **Mathematical Constants**: High-precision values of π, e, and other important mathematical constants
13
- **Specialized Functions**: Gamma functions, Bessel functions, elliptic functions, hypergeometric functions, and more
14
- **Visualization Support**: Built-in plotting capabilities for mathematical functions and complex analysis
15
- **Flexible Precision Control**: Dynamic precision adjustment with context managers for optimal performance
16
17
mpmath is designed for applications requiring mathematical rigor, including symbolic computation, numerical analysis, mathematical research, and educational purposes where exact results are essential.
18
19
## Package Information
20
21
- **Package Name**: mpmath
22
- **Language**: Python
23
- **Installation**: `pip install mpmath`
24
25
## Core Imports
26
27
```python
28
import mpmath
29
```
30
31
For multiprecision arithmetic:
32
33
```python
34
from mpmath import mp, mpf, mpc
35
```
36
37
For convenience access to functions:
38
39
```python
40
from mpmath import *
41
```
42
43
## Basic Usage
44
45
```python
46
import mpmath
47
from mpmath import mp, mpf, mpc
48
49
# Set precision to 50 decimal places
50
mp.dps = 50
51
52
# Create high-precision numbers
53
x = mpf('1.23456789012345678901234567890')
54
y = mpf('2.71828182845904523536028747135')
55
56
# Basic arithmetic
57
result = x + y
58
print(f"Sum: {result}")
59
60
# Mathematical functions with high precision
61
pi_precise = mp.pi
62
e_precise = mp.e
63
sqrt_result = mp.sqrt(2)
64
65
print(f"π with 50 digits: {pi_precise}")
66
print(f"√2 with 50 digits: {sqrt_result}")
67
68
# Complex numbers
69
z = mpc('1+2j')
70
w = mpc('3-4j')
71
complex_result = z * w
72
print(f"Complex multiplication: {complex_result}")
73
74
# Advanced functions
75
gamma_result = mp.gamma(0.5) # Should equal √π
76
bessel_result = mp.besselj(0, 1)
77
print(f"Γ(0.5) = {gamma_result}")
78
print(f"J₀(1) = {bessel_result}")
79
```
80
81
## Architecture
82
83
mpmath is built around context objects that manage precision settings and provide mathematical functions:
84
85
- **MPContext (mp)**: Multiprecision arithmetic with configurable precision
86
- **FPContext (fp)**: Fast double-precision floating-point arithmetic
87
- **MPIntervalContext (iv)**: Interval arithmetic for rigorous numerical computation
88
89
The library provides three main number types:
90
91
- **mpf**: Multiprecision floating-point numbers
92
- **mpc**: Multiprecision complex numbers
93
- **mpi**: Multiprecision intervals
94
95
All mathematical functions support both real and complex arguments where mathematically meaningful, and precision can be adjusted globally or locally using context managers.
96
97
## Capabilities
98
99
### Core Number Types and Arithmetic
100
101
Basic number types, arithmetic operations, precision control, and utility functions. These form the foundation for all mathematical computations in mpmath.
102
103
```python { .api }
104
class mpf:
105
def __init__(self, x): ...
106
107
class mpc:
108
def __init__(self, real, imag=0): ...
109
110
def mpmath.convert(x): ...
111
def extraprec(n): ...
112
def workprec(n): ...
113
```
114
115
[Core Arithmetic](./core-arithmetic.md)
116
117
### Elementary Functions
118
119
Standard mathematical functions including exponentials, logarithms, trigonometric functions, hyperbolic functions, and basic operations on complex numbers.
120
121
```python { .api }
122
def sqrt(x): ...
123
def exp(x): ...
124
def log(x): ...
125
def sin(x): ...
126
def cos(x): ...
127
def tan(x): ...
128
```
129
130
[Elementary Functions](./elementary-functions.md)
131
132
### Special Functions
133
134
Advanced mathematical functions including gamma functions, zeta functions, Bessel functions, hypergeometric functions, elliptic functions, and other special functions from mathematical physics and number theory.
135
136
```python { .api }
137
def gamma(x): ...
138
def zeta(s): ...
139
def besselj(n, x): ...
140
def hyper(a_s, b_s, z): ...
141
def ellipk(m): ...
142
def erf(x): ...
143
```
144
145
[Special Functions](./special-functions.md)
146
147
### Linear Algebra and Matrices
148
149
Matrix operations, linear system solving, eigenvalue computations, matrix decompositions, and matrix functions supporting arbitrary precision arithmetic.
150
151
```python { .api }
152
class matrix:
153
def __init__(self, *args): ...
154
155
def lu_solve(A, b): ...
156
def qr(A): ...
157
def eig(A): ...
158
def det(A): ...
159
def norm(x): ...
160
```
161
162
[Linear Algebra](./linear-algebra.md)
163
164
### Numerical Calculus
165
166
Numerical integration, differentiation, root finding, optimization, series summation, and differential equation solving with high precision.
167
168
```python { .api }
169
def quad(f, a, b): ...
170
def diff(f, x): ...
171
def findroot(f, x0): ...
172
def nsum(f, n1, n2): ...
173
def taylor(f, x, n): ...
174
```
175
176
[Numerical Calculus](./numerical-calculus.md)
177
178
### Mathematical Constants
179
180
High-precision mathematical constants including π, e, and various special constants from number theory and analysis.
181
182
```python { .api }
183
mp.pi # π
184
mp.e # Euler's number
185
mp.euler # Euler-Mascheroni constant γ
186
mp.catalan # Catalan's constant
187
mp.phi # Golden ratio φ
188
```
189
190
[Mathematical Constants](./mathematical-constants.md)
191
192
### Elliptic Functions and Modular Forms
193
194
Elliptic functions, modular forms, theta functions, and q-analog functions for advanced mathematical applications including algebraic geometry, number theory, and mathematical physics.
195
196
```python { .api }
197
def jacobi(kind, u, m): ...
198
def jtheta(n, z, q): ...
199
def kleinj(tau): ...
200
def eta(tau): ...
201
def qp(a, q, n): ...
202
def qhyper(a_s, b_s, q, z): ...
203
```
204
205
[Elliptic Functions and Modular Forms](./elliptic-modular-functions.md)
206
207
### Visualization and Plotting
208
209
Plotting and visualization functions for mathematical functions, complex analysis, and numerical data with arbitrary precision support.
210
211
```python { .api }
212
def plot(f, xlim=[-5, 5]): ...
213
def cplot(f, re=[-2, 2], im=[-2, 2]): ...
214
def splot(f, u=[-2, 2], v=[-2, 2]): ...
215
```
216
217
[Visualization and Plotting](./visualization.md)
218
219
### Pattern Recognition and Mathematical Identification
220
221
Tools for identifying mathematical constants, finding integer relations, polynomial fitting, and discovering mathematical patterns in numerical data.
222
223
```python { .api }
224
def pslq(x, tol=None): ...
225
def identify(x, constants=None): ...
226
def findpoly(roots, tol=None): ...
227
```
228
229
[Pattern Recognition](./pattern-recognition.md)
230
231
### Signal Processing and Waveforms
232
233
Standard waveform functions and signal processing utilities for mathematical modeling and engineering applications.
234
235
```python { .api }
236
def squarew(t, period=1): ...
237
def trianglew(t, period=1): ...
238
def sawtoothw(t, period=1): ...
239
def sigmoid(x): ...
240
```
241
242
[Signal Processing](./signal-processing.md)
243
244
## Types
245
246
### Core Types
247
248
```python { .api }
249
class mpf:
250
"""
251
Multiprecision floating-point number.
252
253
Args:
254
x: Number to convert (int, float, str, or another mpf)
255
"""
256
def __init__(self, x): ...
257
def __str__(self) -> str: ...
258
def __repr__(self) -> str: ...
259
260
class mpc:
261
"""
262
Multiprecision complex number.
263
264
Args:
265
real: Real part (number or string)
266
imag: Imaginary part (number or string), default 0
267
"""
268
def __init__(self, real, imag=0): ...
269
def __str__(self) -> str: ...
270
def __repr__(self) -> str: ...
271
272
class matrix:
273
"""
274
Matrix supporting arbitrary precision arithmetic.
275
276
Args:
277
*args: Matrix dimensions or initial data
278
"""
279
def __init__(self, *args): ...
280
def __getitem__(self, key): ...
281
def __setitem__(self, key, value): ...
282
```
283
284
### Context Types
285
286
```python { .api }
287
class MPContext:
288
"""Multiprecision arithmetic context."""
289
dps: int # Decimal precision
290
prec: int # Binary precision
291
pretty: bool # Pretty printing mode
292
293
def workprec(self, n): ...
294
def workdps(self, n): ...
295
def extraprec(self, n): ...
296
297
class FPContext:
298
"""Fast double-precision floating-point context."""
299
pass
300
301
class MPIntervalContext:
302
"""Interval arithmetic context."""
303
pass
304
```