Python library for arbitrary-precision floating-point arithmetic
—
Plotting and visualization functions for mathematical functions, complex analysis, and numerical data. These functions provide high-quality visualizations with arbitrary precision support.
Plot real-valued functions with high precision and customizable appearance.
def plot(f, xlim=[-5, 5], ylim=None, points=200, file=None, **kwargs):
"""
Plot a real-valued function of one variable.
Args:
f: Function to plot (callable)
xlim: x-axis range as [xmin, xmax] (default: [-5, 5])
ylim: y-axis range as [ymin, ymax] (auto if None)
points: Number of sample points (default: 200)
file: Output file name (optional)
**kwargs: Additional plotting options
Returns:
Plot object (displays plot if in interactive mode)
"""Visualize complex functions using color mapping and domain coloring techniques.
def cplot(f, re=[-2, 2], im=[-2, 2], points=300, color=None, verbose=False, file=None, **kwargs):
"""
Plot a complex function using domain coloring.
Args:
f: Complex function to plot (callable)
re: Real axis range as [re_min, re_max] (default: [-2, 2])
im: Imaginary axis range as [im_min, im_max] (default: [-2, 2])
points: Number of sample points per axis (default: 300)
color: Color mapping function (optional)
verbose: Print progress information (default: False)
file: Output file name (optional)
**kwargs: Additional plotting options
Returns:
Complex plot object with color-coded magnitude and phase
"""Create 3D surface plots for functions of two variables.
def splot(f, u=[-2, 2], v=[-2, 2], points=[50, 50], keep_aspect=True, wireframe=False, file=None, **kwargs):
"""
Create 3D surface plot for function of two variables.
Args:
f: Function f(x,y) to plot (callable)
u: Range for first variable as [u_min, u_max] (default: [-2, 2])
v: Range for second variable as [v_min, v_max] (default: [-2, 2])
points: Number of sample points as [n_u, n_v] or single int (default: [50, 50])
keep_aspect: Maintain aspect ratio (default: True)
wireframe: Use wireframe rendering (default: False)
file: Output file name (optional)
**kwargs: Additional plotting options
Returns:
3D surface plot object
"""import mpmath
from mpmath import mp
# Set precision
mp.dps = 25
# Example 1: Plot a simple function
def f(x):
return mp.sin(x) * mp.exp(-x/5)
# Create a basic plot
mp.plot(f, xlim=[-2*mp.pi, 2*mp.pi])
# Plot with custom styling
mp.plot(f, xlim=[0, 4*mp.pi], ylim=[-0.5, 1.1], points=500,
file='damped_sine.png')
# Example 2: Plot special functions
def gamma_plot(x):
try:
return mp.gamma(x)
except:
return mp.nan
# Plot gamma function with restricted y-range
mp.plot(gamma_plot, xlim=[-4, 4], ylim=[-10, 10], points=1000)
# Example 3: Complex function plotting
def complex_sin(z):
return mp.sin(z)
# Domain coloring plot of sin(z)
mp.cplot(complex_sin, re=[-mp.pi, mp.pi], im=[-mp.pi/2, mp.pi/2],
points=400, file='complex_sine.png')
# Plot a more complex function
def rational_func(z):
return (z**3 - 1) / (z**2 + 1)
mp.cplot(rational_func, re=[-3, 3], im=[-3, 3], points=500)
# Example 4: Plot Riemann zeta function
def zeta_plot(z):
try:
return mp.zeta(z)
except:
return mp.nan
# Critical strip visualization
mp.cplot(zeta_plot, re=[-1, 2], im=[-20, 20], points=600,
file='riemann_zeta.png')
# Example 5: Surface plotting
def surface_func(x, y):
return mp.sin(mp.sqrt(x**2 + y**2)) / mp.sqrt(x**2 + y**2 + 1)
# Create 3D surface plot
mp.splot(surface_func, u=[-5, 5], v=[-5, 5], points=[60, 60])
# Wireframe version
mp.splot(surface_func, u=[-3, 3], v=[-3, 3], points=[40, 40],
wireframe=True, file='surface_wireframe.png')
# Example 6: Plotting Bessel functions
def bessel_family(x):
return [mp.besselj(n, x) for n in range(4)]
# Plot multiple Bessel functions
for n in range(4):
mp.plot(lambda x: mp.besselj(n, x), xlim=[0, 20], ylim=[-0.5, 0.6])
# Example 7: Plot elliptic functions
def jacobi_sn(x):
return mp.jacobi('sn', x, 0.5)
def jacobi_cn(x):
return mp.jacobi('cn', x, 0.5)
def jacobi_dn(x):
return mp.jacobi('dn', x, 0.5)
# Plot Jacobi elliptic functions
mp.plot(jacobi_sn, xlim=[0, 4*mp.ellipk(0.5)], points=400)
mp.plot(jacobi_cn, xlim=[0, 4*mp.ellipk(0.5)], points=400)
mp.plot(jacobi_dn, xlim=[0, 4*mp.ellipk(0.5)], points=400)
# Example 8: Complex analysis - visualize analytic continuation
def gamma_complex(z):
try:
return mp.gamma(z)
except:
return mp.inf
# Visualize gamma function in complex plane
mp.cplot(gamma_complex, re=[-4, 4], im=[-4, 4], points=500,
file='gamma_complex.png')
# Example 9: Plot eigenvalue problems
# Visualize characteristic polynomial
def char_poly(lam):
# Example: characteristic polynomial of 2x2 matrix
return lam**2 - 3*lam + 2
mp.plot(char_poly, xlim=[-1, 4], ylim=[-2, 3])
# Example 10: Advanced surface plots with mathematical functions
def riemann_surface(x, y):
z = x + 1j*y
try:
return mp.re(mp.sqrt(z)) # Real part of sqrt(z)
except:
return 0
mp.splot(riemann_surface, u=[-2, 2], v=[-2, 2], points=[50, 50],
file='riemann_surface_real.png')
# Imaginary part
def riemann_surface_imag(x, y):
z = x + 1j*y
try:
return mp.im(mp.sqrt(z)) # Imaginary part of sqrt(z)
except:
return 0
mp.splot(riemann_surface_imag, u=[-2, 2], v=[-2, 2], points=[50, 50],
file='riemann_surface_imag.png')
print("Visualization examples completed!")
print("Check generated PNG files for visual results.")The mpmath plotting functions provide several advanced features:
These visualization tools are particularly valuable for:
The arbitrary precision support ensures that even functions with challenging numerical properties can be visualized accurately, making these tools invaluable for mathematical research and education.