Reputation: 121
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]])
Upvotes: 0
Views: 238
Reputation: 231665
It's not the complex
; it's the array.
You can use mp
with python scalars:
In [32]: mp.exp(1)
Out[32]: mpf('2.7182818284590451')
but not arrays:
In [33]: mp.exp(np.array([1]))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [33], in <module>
----> 1 mp.exp(np.array([1]))
File /usr/local/lib/python3.8/dist-packages/mpmath/ctx_mp_python.py:989, in PythonMPContext._wrap_libmp_function.<locals>.f(x, **kwargs)
987 def f(x, **kwargs):
988 if type(x) not in ctx.types:
--> 989 x = ctx.convert(x)
990 prec, rounding = ctx._prec_rounding
991 if kwargs:
File /usr/local/lib/python3.8/dist-packages/mpmath/ctx_mp_python.py:648, in PythonMPContext.convert(ctx, x, strings)
646 if isinstance(x, complex):
647 return ctx.make_mpc((from_float(x.real), from_float(x.imag)))
--> 648 if type(x).__module__ == 'numpy': return ctx.npconvert(x)
649 if isinstance(x, numbers.Rational): # e.g. Fraction
650 try: x = rational.mpq(int(x.numerator), int(x.denominator))
File /usr/local/lib/python3.8/dist-packages/mpmath/ctx_mp_python.py:681, in PythonMPContext.npconvert(ctx, x)
679 if isinstance(x, np.complexfloating):
680 return ctx.make_mpc((from_npfloat(x.real), from_npfloat(x.imag)))
--> 681 raise TypeError("cannot create mpf from " + repr(x))
TypeError: cannot create mpf from array([1])
Again the scalar:
In [34]: mp.exp(np.array([1]).item())
Out[34]: mpf('2.7182818284590451')
not a list either:
In [35]: mp.exp(np.array([1]).tolist())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [35], in <module>
----> 1 mp.exp(np.array([1]).tolist())
File /usr/local/lib/python3.8/dist-packages/mpmath/ctx_mp_python.py:989, in PythonMPContext._wrap_libmp_function.<locals>.f(x, **kwargs)
987 def f(x, **kwargs):
988 if type(x) not in ctx.types:
--> 989 x = ctx.convert(x)
990 prec, rounding = ctx._prec_rounding
991 if kwargs:
File /usr/local/lib/python3.8/dist-packages/mpmath/ctx_mp_python.py:669, in PythonMPContext.convert(ctx, x, strings)
667 try: return ctx.make_mpf(from_Decimal(x, prec, rounding))
668 except: pass
--> 669 return ctx._convert_fallback(x, strings)
File /usr/local/lib/python3.8/dist-packages/mpmath/ctx_mp.py:634, in MPContext._convert_fallback(ctx, x, strings)
632 else:
633 raise ValueError("can only create mpf from zero-width interval")
--> 634 raise TypeError("cannot create mpf from " + repr(x))
TypeError: cannot create mpf from [1]
Upvotes: 1