我已经设置了一个带有double for循环的函数来计算不同数组的总和。为什么我的函数总是返回0?

What I want/What I get

我得到了一个双重求和公式以python编码。基本上,我得到了30个不同的数组,它们分别具有e和k值的不同排列,分别对应于负电荷或正电荷。这些电荷的总和为qm和qn,因此qm = -1和qn =1。e的相对位置表示为m,而k的相对位置表示为n。这些加起来就是m和n。数组的名称一直为s1,s2,s3,s4等,一直到s30。我将在此处为s3和s4包括一个示例数组,所以您知道我在说什么。

s3由s3 = np.array( ['k','e','k','e'])

给出

s4由s4 = np.array( ['k','e'])

给出

因此,现在我定义将位置定位为'k'的n_value和'e'的m_value。这些功能如下

m_value

def m_value(position_num,array_name):
"""
same as for the n_value,except for 'e' instead of 'k'
"""
    cnt = 0
    for i in range(len(array_name)):
        if array_name[i]=='e':
            cnt+=1
        if cnt==position_num:
            return i+1
    return 0

对于n_value

def n_value(position_num,array_name):
"""
this is a function to get all of the positions of 
'k' in the array entered.
    position_num = what number 'k' value you want (1st,2nd,etc...)
    array_name = what scd row (s1,s2,etc...)
define the specific element of the array 
returns value of where the 'k' is in the given sequence
"""
    cnt = 0
    for i in range(len(array_name)):
        if array_name[i]=='k':
            cnt+=1
        if cnt==position_num:
            return i+1
    return 0

然后,我分别定义了e和k的电荷

charge_values = {'e' : -1,'k' : 1}

e_charge = charge_values ['e'] k_charge = charge_values ['k']

并定义了要计数的N值

N = 50

最后,我为函数编写了一个定义,以根据给定的求和公式将其总结为“ SCD”。在这里

def SCD(array_name):
"""
this should sum all of the elements in accordance with the
SCD summation formula. see notes to see full layout of 
formula and elements
"""
    summ = 0.0
    nposition_num=0
    mposition_num=0
    for m in range(2,N):
        summ = 0.0
        mposition_num = m_value(m,array_name)
        qm = mposition_num*e_charge
        for n in range (0,m-1):
            nposition_num= n_value(n,array_name)
            qn = nposition_num*k_charge
            summ += qm*qn*np.sqrt(m-n)
        summ += summ
    summ = (summ/float(N))     
    return summ

现在,这里出现了一个主要问题。无论它是什么数组,为SCD返回的值始终为0.0。我知道这是不准确的,所以我想知道我在此期间发生了什么逻辑或编码错误,导致输出始终为零。我是python的新手,所以任何提示或想法都将不胜感激。

hwzzwc 回答:我已经设置了一个带有double for循环的函数来计算不同数组的总和。为什么我的函数总是返回0?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3127931.html

大家都在问