简体   繁体   English

Python错误索引超出范围

[英]Python error index out of bounds

I have my code written as follow but can not run it because index out of bounds, can someone tell me where I did it wrong? 我的代码编写如下,但由于索引超出范围而无法运行,有人可以告诉我我在哪里做错了吗?

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)

^ is not the exponentiation operator but the binary exclusive or operator; ^不是幂运算符,而是二进制异或运算符; replace ^ with ** to get the exponentiation. ^替换为**以得到幂。 2 ^ 2 == 0 , which causes your matrix to have 0 by 0 elements. 2 ^ 2 == 0 ,这会使matrix元素数为0 x 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM