I would like to display my complex function in the complex domain, but I have a problem. I get the following error message: cannot create mpf from array
The code is simple and I have tried to show where the problem is. I think that my mpmath
cannot work with complex
in this form to create an array. What can solve the problem of this library with my imaginary part?
import numpy as np
import mpmath as mp
from mpl_toolkits.mplot3d import Axes3D # Needed to create 3D plots
import matplotlib
import matplotlib.pyplot as plt
#%matplotlib notebook
# Uncomment this if using Jupyter
def Laplace_Max(s):
#mp.dps = 100
A= s**(5/4)/ (
0.00480931 +
0.0244077*s**(1/4) +
0.0129056*mp.exp(-35*s)*s**(1/4) +
0.00329167*mp.exp(0.707997*s)*s**(1/4) * mp.gammainc(0.0, 35.708*s,mp.inf, regularized=True) -
0.00530593*mp.gammainc(1.25, 35*s,mp.inf, regularized=True)
)
return A
# Create 1x1 figure with 3 axes
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Define grid of x and w using a step of 0.05
X = W = np.arange(-20.0, 20.0, 0.05)
X, W = np.meshgrid(X,W)
complexVals = X + 1j*W # Convert X/W grid into complex numbers
# Call the function on the complex numbers and extract the real/imag parts
##################PROBLEM IS HERE#############
Y_Z = Laplace_Max(complexVals)
##################PROBLEM IS HERE#############
Y = Y_Z.real
Z = Y_Z.imag
# Create colormap for W
color_dimension = W
minn, maxx = color_dimension.min(), color_dimension.max()
norm = matplotlib.colors.Normalize(minn, maxx)
m = plt.cm.ScalarMappable(norm=norm, cmap='jet')
m.set_array([])
fcolors = m.to_rgba(color_dimension)
# Create surface
ax.plot_surface(X, Y, Z, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False,
linewidth=0, antialiased=False)
# Set axis labels
ax.set_xlabel('X (Re)')
ax.set_ylabel('Y (Re)')
ax.set_zlabel('Z = f(x) (Im)')
# Create colorbar and set title
cbr = plt.colorbar(m)
cbr.ax.set_title('W')
# Show plot with colorbar
plt.show()
Error:
cannot create mpf from array([[ 700. +700.j , 698.25+700.j , 696.5 +700.j , ...,
-694.75+700.j , -696.5 +700.j , -698.25+700.j ],
[ 700. +698.25j, 698.25+698.25j, 696.5 +698.25j, ...,
-694.75+698.25j, -696.5 +698.25j, -698.25+698.25j],
[ 700. +696.5j , 698.25+696.5j , 696.5 +696.5j , ...,
-694.75+696.5j , -696.5 +696.5j , -698.25+696.5j ],
...,
[ 700. -694.75j, 698.25-694.75j, 696.5 -694.75j, ...,
-694.75-694.75j, -696.5 -694.75j, -698.25-694.75j],
[ 700. -696.5j , 698.25-696.5j , 696.5 -696.5j , ...,
-694.75-696.5j , -696.5 -696.5j , -698.25-696.5j ],
[ 700. -698.25j, 698.25-698.25j, 696.5 -698.25j, ...,
-694.75-698.25j, -696.5 -698.25j, -698.25-698.25j]])