AttributeError:“ Mul”对象没有属性“ eval”

时间:2018-09-30 20:37:51

标签: python-2.7 object attributes eval sympy

在下面的代码中,该代码计算了量子谐波振荡器的本征态,而mr和gaussq的乘积等于基态,我试图获得本征态,并绘制出包含所有每个子图。

但是,运行代码将返回:

*for i in xx: rePsi.eval(i)
AttributeError: 'Mul' object has no attribute 'eval'*

代码示例:

import numpy as np
import matplotlib.pylab as plt
import sympy as sym
from math import *

w=1
m=1
hbar=1
x = sym.symbols ('x')
a=-1
b=1
phi_n=0
psi=0
plt.ylim(-1,1 )
plt.xlim(-3,3)

print('give me the number of states')
nn = int(input())
if (nn<0):
    print 'must be >=0\n'
if (nn>=0):
    print('time to evaluate')
    t = float(input())
    if (t<0):
        print 'Time must be >=0'

print '   '

mr = float((m*w/(hbar*pi))**0.25)
gaussq = sym.exp(-m*w*x**2/(2*hbar))

fig = [0 for i in range(nn)]
fig1 = plt.figure(1)

xx = np.arange(-3., 3.,0.1)
for n in range (0,nn,2):
    H_root = sym.sqrt(1/(2**n*float(sym.factorial(n))))
    Hermite = (-1)**n*sym.exp(m*w*x**2/hbar)*sym.diff(sym.exp(-m*w*x**2/hbar),x,n)
    Energy = hbar*w*(n+0.5)
    phi_n = sym.simplify(H_root*mr*gaussq*Hermite)
    arg_int = sym.simplify(phi_n*gaussq*mr)
    c_n = sym.simplify(sym.integrate(arg_int, (x,a,b)))
    psi_n = c_n*phi_n*sym.exp(-1j*w*t*(n+0.5))
    psi_n_re = sym.re(psi_n)
    psi_n_im = sym.im(psi_n)
    psi += sym.simplify(psi_n)
    rePsi = sym.re(psi) 
    imPsi = sym.im(psi)
    print type (rePsi)
    print type (imPsi)
    for i in xx: rePsi.eval(i)
    for i in xx: imPsi.eval(i)
    list_rePsi= rePsi
    list_imPsi= imPsi
    plt.subplot(nn, 1, (n+2)/2)
    plt.plot(xx,list_repsi)
    plt.plot(xx,list_impsi)

plt.show()
print '\n'+str(psi)

1 个答案:

答案 0 :(得分:-1)

也许您打算创建一个列表,用符号xx中的值替换符号x?您可以尝试使用list_rePsi = [rePsi.subs(x, i) for i in xx],但Python区分大小写,因此请确保绘制list_rePsi而不是list_repsi