0

I'm trying to use mpmath for parabolic cylinder functions as I need to be able to use complex variable inputs, which scipy.special.pbdv doesn't allow, but I keep getting the following error when plotting:

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'

In the variable explorer, my G shows up as just "mpc object of mpmath.ctx_mp_python module" and F shows up as [mpc, mpc, mpc, mpc, mpc, ...].

My code looks like this:

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()
PewtDmD
  • 3
  • 1

1 Answers1

0

The list F basically contains the mpc objects which are complex numbers having a real and imaginary part.You can see this by printing the list after the loop ends.This is what causes the error when you pass the the list to plt.plot. To learn more about plotting complex numbers in python you can go to the link Plot Complex Numbers. I took the absolute value of the complex numbers, converted them to float and then used matplotlib to plot the values, in case if that was what you were aiming for.Here's the implementation for that:

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
Ahsan Goheer
  • 669
  • 6
  • 9