为什么我在尝试绘制 mpmath.pcfd 时得到“float() 参数必须是字符串或数字,而不是 'mpc'”?

Why am I getting 'float() argument must be a string or a number, not 'mpc'' when trying to plot mpmath.pcfd?

我正在尝试将 mpmath 用于抛物柱面函数,因为我需要能够使用复杂的变量输入,scipy.special.pbdv 不允许,但我在绘图时不断收到以下错误:

Traceback (most recent call last):

  File "C:\Users\User\Desktop\qwalk\untitled0.py", line 27, in <module>
    plt.plot(p, F)

  File "C:\Users\User\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2796, in plot
    is not None else {}), **kwargs)

  File "C:\Users\User\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1667, in plot
    self.add_line(line)

  File "C:\Users\User\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 1902, in add_line
    self._update_line_limits(line)

  File "C:\Users\User\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 1924, in _update_line_limits
    path = line.get_path()

  File "C:\Users\User\anaconda3\lib\site-packages\matplotlib\lines.py", line 1027, in get_path
    self.recache()

  File "C:\Users\User\anaconda3\lib\site-packages\matplotlib\lines.py", line 675, in recache
    y = _to_unmasked_float_array(yconv).ravel()

  File "C:\Users\User\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 1390, in _to_unmasked_float_array
    return np.asarray(x, float)

  File "C:\Users\User\anaconda3\lib\site-packages\numpy\core\_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)

TypeError: float() argument must be a string or a number, not 'mpc'

在变量资源管理器中,我的 G 显示为“mpmath.ctx_mp_python 模块的 mpc 对象”,F 显示为 [mpc, mpc, mpc, mpc, mpc, ...]。

我的代码如下所示:

import numpy as np
import matplotlib.pyplot as plt
import math
import mpmath as mp


p = np.arange(0.025, 50.000, 0.025)
E = 1
m = 1
phat = -np.sqrt(2/E)*(p/m)*np.exp(- complex(0,math.pi)/4)

v1 = complex(-1, 1/2)
v2 = complex(0, 1/2)
F = []

for val in phat:
    d1 =abs(mp.pcfd(v1, val))**2
    d2 =abs(mp.pcfd(v2, val))**2
    d3 = np.exp(-complex(math.pi)/4) * mp.pcfd(v2, val) * mp.pcfd(v1, np.conj(val)) + np.conj(np.exp(-complex(math.pi)/4) * mp.pcfd(v2, val) * mp.pcfd(v1, np.conj(val)))
    f1 = (1/(2*E))*(1 - (val/(math.sqrt(m**2 + val**2))))
    f2 = (1 + val/(math.sqrt(val**2 + m**2)))
    f3 = -(m/math.sqrt(2*E))*(1/math.sqrt(m**2 + val**2))
    G = (1/2)*np.exp(- complex(0,math.pi/(4*E)))*(f1*d1 + f2*d2 + f3*d3)
    F.append(G)

plt.plot(p, F)
plt.show()

列表 F 基本上包含 mpc 对象,这些对象是具有实数和虚数的复数 part.You 可以通过在循环后打印列表来看到这一点 ends.This 是您通过时导致错误的原因plt.plot 的列表。要了解有关在 python 中绘制复数的更多信息,您可以转到 link Plot Complex Numbers。我取了复数的绝对值,将它们转换为浮点数,然后使用 matplotlib 绘制这些值,以防万一那是你的目标 for.Here 的实现:

import numpy as np
import matplotlib.pyplot as plt
import math
import mpmath as mp


p = np.arange(0.025, 50.000, 0.025)
E = 1
m = 1
phat = -np.sqrt(2/E)*(p/m)*np.exp(- complex(0,math.pi)/4)

v1 = complex(-1, 1/2)
v2 = complex(0, 1/2)
F = []

for val in phat:
    d1 =abs(mp.pcfd(v1, val))**2
    d2 =abs(mp.pcfd(v2, val))**2
    d3 = np.exp(-complex(math.pi)/4) * mp.pcfd(v2, val) * mp.pcfd(v1, np.conj(val)) + np.conj(np.exp(-complex(math.pi)/4) * mp.pcfd(v2, val) * mp.pcfd(v1, np.conj(val)))
    f1 = (1/(2*E))*(1 - (val/(math.sqrt(m**2 + val**2))))
    f2 = (1 + val/(math.sqrt(val**2 + m**2)))
    f3 = -(m/math.sqrt(2*E))*(1/math.sqrt(m**2 + val**2))
    G = (1/2)*np.exp(- complex(0,math.pi/(4*E)))*(f1*d1 + f2*d2 + f3*d3)
    F.append(float(abs(G)))
print(F)

plt.plot(p, F)
plt.show