我的代码编写如下,但由于索引越界,无法运行它,有人可以告诉我哪里做错了吗?
def potential(a,b,c,d):
energylist = []
for x in np.arange(-5,10,0.1):
for y in np.arange(-5,10,0.1):
expa = np.exp(-(x-a)**2)
expb = np.exp(-(y-b)**2)
expc = np.exp(-(x-c)**2)
expd = np.exp(-(y-d)**2)
if x-y != 0:
integ = expa*expb*(1/np.abs(x-y))*expc*expd
energylist.append(integ)
val = sum(energylist)
return val
def htable(*myions):
k = len(myions)
matrix = np.zeros((k^2,k^2),float)
for i in np.arange(0,k^2-1,1):
for j in np.arange(0,k^2-1,1):
m = myions[i/k]
n = myions[i%k]
o = myions[i/k]
p = myions[i%k]
matrix[i,j] = potential(m,n,o,p)
print matrix
return matrix
htable(1,3)
答案 0 :(得分:2)
^
不是指数运算符,而是二进制异或运算符;将^
替换为**
以获取取幂。 2 ^ 2 == 0
,这会导致您的matrix
有0到0个元素。