将速率方程转换为 python 代码的问题
issue with converting rate equation to python code
我正在尝试将这些速率方程转换为 python 代码,我已经进行了大量研究,但似乎无法找到任何明确的途径来实现这一点,请提供任何帮助,我们将不胜感激
这是一个新更新的代码....我使用 Tom10 的 quide 编写的......请问您觉得如何?
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 项)。
capitol sigma 符号 (Σ) 意味着你需要做一个求和,你可以在一个循环中做,但是 Python list 和 numpy 都有求和函数。 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]
请注意,尽管求和中的表达式看起来相似,但第一个本质上是两个向量之间的 点积 ,第二个是向量的 标量乘积 所以 N[5]
可以从总和中取出(但我把它留在那里以匹配等式)。
最后说明:我看到你是 S.O 的新手。所以我认为如果我为您回答这个问题会有所帮助。以后请展示一下代码的尝试——真的很有帮助。
我正在尝试将这些速率方程转换为 python 代码,我已经进行了大量研究,但似乎无法找到任何明确的途径来实现这一点,请提供任何帮助,我们将不胜感激
这是一个新更新的代码....我使用 Tom10 的 quide 编写的......请问您觉得如何?
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 项)。
capitol sigma 符号 (Σ) 意味着你需要做一个求和,你可以在一个循环中做,但是 Python list 和 numpy 都有求和函数。 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]
请注意,尽管求和中的表达式看起来相似,但第一个本质上是两个向量之间的 点积 ,第二个是向量的 标量乘积 所以 N[5]
可以从总和中取出(但我把它留在那里以匹配等式)。
最后说明:我看到你是 S.O 的新手。所以我认为如果我为您回答这个问题会有所帮助。以后请展示一下代码的尝试——真的很有帮助。