将速率方程转换为python代码的问题

问题描述

我正在尝试将这些速率方程式转换为python代码,我进行了很多研究,但似乎并没有明确的途径来实现这一目标,请提供任何帮助

Image of Equations

这是新近更新的代码。...我是用Tom10的quid写的.....请您怎么看?

    import numpy as np
# import numpy as sum  # not necessary,just for convenience,and replaces the builtin

# set N_core value
N_CORE = 0

# set the initial conditions appropriately (you need to set these correctly)
N = np.ones(8)
r = np.ones((8,8))
dN = np.zeros(8) # the value here is not important for your equations

# set constant for equation 1
R_P1abs37 = 20
F_P1 = 20
R_P1abs47 = 40
W_3317 = 1.0

# set constant for equation 2
W_6142 = 90
W_5362 = 80

# Set you constants appropriately for equation 3
R_P2abs35 = 30
F_P2 = 40
R_L2se34 = 50
F_L2 = 90

# equation 4 constants
W_2214 = 20

#equation 5 constants
R_P1abs13 = 30
R_L2se32 = 20
F_L1 = 10

# equation 1 formular
dN[7] =sum(r[7,:]*N[7]) + (R_P1abs37*F_P1) + (R_P1abs47*F_P1) + (W_3317*N[3]**2)

# equation 2 formular
dN[6] = (r[7,6]*N[7]) - sum(r[6,:]*N[6]) - (W_6142*N[6]*N[1]) + (W_5362*N[5]*N[3])

#equation 3 formular
dN[5] = sum(r[:,5]*N) - sum(r[5,:]*N[5]) + R_P2abs35*F_P2 - R_L2se34*F_L2 - W_5362*N[5]*N[3]

# equation 4 formular
dN[4] = sum(r[:,4]*N) - sum(r[4,:]*N[4]) - (R_P1abs47*F_P1) + (R_L2se34*F_L2) + (W_2214*N[2]**2)+ (W_6142*N[6]*N[1])

#equation 5 formular
dN[3] = sum(r[:,3]*N) - sum(r[3,:]*N[3]) + (R_P1abs13*F_P1) - (R_P1abs37*F_P1) - (R_P2abs35*F_P2)
-(R_L2se32*F_L1) - ((2*W_3317)*N[3]**2) - (W_5362*N[5]*N[3])

#equation 6 formular
dN[2] = sum(r[:,2]*N) - (r[2,1]*N[2]) + (R_L2se32*F_L1) - ((2*W_2214)*N[2]**2) + (W_6142*N[6]*N[1])+(W_5362*N[5]*N[3])


#equation 7 formular
dN[1] = sum(r[:,1] * N) - (R_P1abs13*F_P1) + (W_2214*N[2]**2) + (W_3317+N[3]**2) - (W_6142+N[6]*N[1])

#equation for N CORE
N_CORE = sum(dN)


print(N_CORE)

解决方法

以下是根据您的问题和评论列出的相关问题:

  • 通常,如果求和超过 i ,那么没有 i 下标的所有内容对于该和都是恒定的。 (从数学上讲这些常数项只能从和中得出;因此第一个等式有点奇怪,其中N_7可以从和中移出,但我认为他们将其保留以显示与其他等式的对称性都具有r * N项。

  • 国会大厦的sigma符号(Σ)表示您需要做一个和,可以循环执行,但是Python list和numpy都有一个sum函数。 Numpy的另一个优点是乘法被解释为单个元素的乘法,从而使表达更容易。因此,对于a[0]*[b0] + a[1]*b[1] + a[2]*b[2]和numpy数组来说,它们只是sum(a*b);对于Python列表,它是sum([a[i]*b[i] for in range(len(a))]

因此,使用numpy时,设置和第三个方程将如下所示:

import numpy as np
import numpy.sum as sum  # not necessary,just for convenience,and replaces the builtin

# set the initial conditions appropriately (you need to set these correctly)
N = np.ones(7,dtype=np.float)
# r seems to be a coupling matrix,and should be set according to your system
r = np.ones((7,7),dtype = np.float) 
# the values for dN are not important for your equations because dN only appears on the left side of the equations,so we just make a place to store the results
dN = np.zeros(7,dtype=np.float) 

# Set you constants appropriate.y
R_P2abs35 = 1.0
F_P2 = 1.0
R_L2se34 = 1.0
F_L2 = 1.0
W_5362 = 1.0

dN[5] = sum(r[:,5]*N) - sum(r[5,:]*N[5]) + R_P2abs35*F_P2 - R_L2se34*F_L2 - W_5362*N[5]*N[3]

请注意,尽管总和中的表达式看起来相似,但是第一个本质上是两个向量之间的点积,第二个是标量乘以向量,因此{{ 1}}可以从总和中取出(但我将其留在此处以匹配等式)。

最后说明:我看到您是S.O的新手。所以我认为如果我为您回答这个问题会有所帮助。将来,请尝试一下该代码-确实有很大帮助。