Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 生成方程组_Python - Fatal编程技术网

Python 生成方程组

Python 生成方程组,python,Python,我想做一个函数,它可以生成一个方程组,在单独的程序中求解。方程式由同位素衰变树生成,但为简单起见,我有以下树: 所以这可以被分成两个可能的衰变链: [(A,0,1,5), (B,1,.4,4), (C,0,.4,0)] [(A,0,1,5), (B,1,.6,6), (C,0,.6,0)] 其中格式为物种、数量、衰变概率、半衰期。我正在尝试制作一个函数,它会自动生成一个衰变树的方程组,这可能比这更复杂。但对于任何树,规则都是相同的: 对于某些物种X,其父母为Y_1,Y_2,是: X_fina

我想做一个函数,它可以生成一个方程组,在单独的程序中求解。方程式由同位素衰变树生成,但为简单起见,我有以下树:

所以这可以被分成两个可能的衰变链:

[(A,0,1,5), (B,1,.4,4), (C,0,.4,0)]

[(A,0,1,5), (B,1,.6,6), (C,0,.6,0)]
其中格式为物种、数量、衰变概率、半衰期。我正在尝试制作一个函数,它会自动生成一个衰变树的方程组,这可能比这更复杂。但对于任何树,规则都是相同的:

对于某些物种X,其父母为Y_1,Y_2,是:

X_final=每个母体物种的总和Y_n衰变概率->X*Y_n数量/Y_n半衰期-X数量/X半衰期,可表示为:

链中的每一个物种都会有自己的方程,稍后再求解。对于这个,我想要下面的方程组:

A_f = - A_i/5
B1_f = .4 * A_i/5 - B1_i / 4
B2_f = .6 * A_i/5 - Β2_i / 6
C = B1_i / 4 + B2_i / 6
另外,如果半衰期为0,则表示它是稳定的。目前,我正在通过制作字符串字典来生成方程组,但我认为有更好的方法。在我用字符串创建系统之后,我计划稍后将字符串转换为变量。这是我的密码:

A = 'A'
B = 'B'
C = 'C'
D = 'D'

chain1 = [(A,0,1,5),(B,1,.4,4),(C,0,.4,0),(D,0,.4,0)]
chain2 = [(A,0,1,5),(B,2,.6,6),(C,0,.6,0),(D,0,.6,0)]
master_chain = [chain1, chain2]

def equation_builder(master_chain):
    master_equations = {}
    m = 0
    for chain in master_chain:
        n = 0
        for item in chain:
            if item == chain[0]:
                equation = {str(item[0]) + str(item[1]) + 'f' :\
                '-' +  str(item[0]) + str(item[1])  + 'i/' +  str(item[3])}
                master_equations.update(equation)
            elif str(item[0])+str(item[1])+'f' not in master_equations:
                equation = {str(item[0]) + str(item[1]) + 'f' :\
                str(item[2]/chain[n-1][2])+str(chain[n-1][0]) + 
                str(chain[n-1][1])+'i/' + str(chain[n-1][3])+\
                '-'+str(item[0])+str(item[1])+'i/'+str(item[3])}
                master_equations.update(equation)
            elif str(item[0])+str(item[1])+'f' in master_equations \
            and master_chain[m-1][n-1] != master_chain[m][n-1]:
                old_equation = master_equations[str(item[0])+str(item[1])+'f']
                new_equation = old_equation + '+' +\
                str(item[2]/chain[n-1][2])+str(chain[n-1][0]) +\
                str(chain[n-1][1])+'i/' + str(chain[n-1][3])
                equation = {str(item[0])+str(item[1])+'f' : new_equation}
                master_equations.update(equation)
            n += 1
        m += 1

    return master_equations

if __name__ == '__main__':
    print equation_builder(master_chain)
使用。Symphy是一个符号计算工具箱,非常适合此用例。可以使用a=symphy.SymbolA创建符号,然后在表达式中使用a,就像使用任何变量一样。例如,如果A和B是符号,那么如果写入C=A*expB,打印C将输出A*expB。使用表达式的args属性,还可以访问任何表达式的语法树表示形式,如果要进一步处理表达式,这可能很有用

下面是一个使用您的图表的示例,我不太明白您是如何得出结果的,因此这可能需要进行一些调整,但应该足以得出以下想法:

import sympy as sp

A, B1, B2, C = sp.symbols("A, B1, B2, C")

chain1 = [(A,0,1,5),(B1,1,.4,4),(C,0,0.4,0)]
chain2 = [(A,0,1,5),(B2,2,.6,6),(C,0,0.6,0)]
master_chain = [chain1, chain2]

finals = {}
for subchain in master_chain:
    for i, (species, number, decay_prob, half_life) in enumerate(subchain):
        input_species = sp.Symbol(str(species) + "_i")
        if species not in finals:
            finals[species] = -input_species / half_life if half_life else 0
        if i < len(subchain) - 1:
            (other_species, other_number, other_decay_prob, other_half_life) = subchain[i+1]
            if other_species not in finals:
                finals[other_species] = -sp.Symbol(str(other_species) + "_i") / other_half_life if other_half_life else 0
            finals[other_species] += input_species * decay_prob / half_life

print finals
请注意,Symbolx==Symbolx,例如,符号由其字符串表示形式标识,因此您可以在每次需要时安全地重新创建符号。

使用。Symphy是一个符号计算工具箱,非常适合此用例。可以使用a=symphy.SymbolA创建符号,然后在表达式中使用a,就像使用任何变量一样。例如,如果A和B是符号,那么如果写入C=A*expB,打印C将输出A*expB。使用表达式的args属性,还可以访问任何表达式的语法树表示形式,如果要进一步处理表达式,这可能很有用

下面是一个使用您的图表的示例,我不太明白您是如何得出结果的,因此这可能需要进行一些调整,但应该足以得出以下想法:

import sympy as sp

A, B1, B2, C = sp.symbols("A, B1, B2, C")

chain1 = [(A,0,1,5),(B1,1,.4,4),(C,0,0.4,0)]
chain2 = [(A,0,1,5),(B2,2,.6,6),(C,0,0.6,0)]
master_chain = [chain1, chain2]

finals = {}
for subchain in master_chain:
    for i, (species, number, decay_prob, half_life) in enumerate(subchain):
        input_species = sp.Symbol(str(species) + "_i")
        if species not in finals:
            finals[species] = -input_species / half_life if half_life else 0
        if i < len(subchain) - 1:
            (other_species, other_number, other_decay_prob, other_half_life) = subchain[i+1]
            if other_species not in finals:
                finals[other_species] = -sp.Symbol(str(other_species) + "_i") / other_half_life if other_half_life else 0
            finals[other_species] += input_species * decay_prob / half_life

print finals

请注意,Symbolx==Symbolx,例如,符号由其字符串表示形式标识,因此您可以在每次需要时安全地重新创建符号。

单独的程序如何接受要格式化的方程式?单独的程序如何接受要格式化的方程式?对于任意大的链,这是否易于修改,那可能包括几十个我事先不知道的符号?我的程序正在自动计算并生成可能很长的链。这也意味着我在运行程序时只知道第一个符号。当然。我曾经用这种方法在一个反应机制中找到稳态物种的显式表达式,这个反应机制有大约150个物种和几个自动的准稳态假设,所以在一个与你非常相似的设置中。工作起来很有魅力,不过如果在最终系统上运行sympy的solve来求解某些变量,那么对于非常大的系统来说,这可能会变得有点慢。只要你只想生成表达式,我怀疑有一个相关的限制。在事先不知道物种的情况下,我怎么能做到这一点?就像有一个自动创建sympy符号并返回所有符号列表的函数一样。例如,如果我有一个字符串变量列表str_var=['a'、'b'、'c'、…'n'],并且希望有一个函数能够自动为每个变量创建一个sympy符号?sym=mapsp.symbol,str_var对于任意大的链,这是否容易修改,其中可能包括几十个我事先不知道的符号?我的程序正在自动计算并生成可能很长的链。这也意味着我在运行程序时只知道第一个符号。当然。我曾经用这种方法在一个反应机制中找到稳态物种的显式表达式,这个反应机制有大约150个物种和几个自动的准稳态假设,所以在一个与你非常相似的设置中。工作起来很有魅力,不过如果在最终系统上运行sympy的solve来求解某些变量,那么对于非常大的系统来说,这可能会变得有点慢。只要你只想生成表达式,我怀疑有一个相关的限制。在事先不知道物种的情况下,我怎么能做到这一点?就像有一个函数可以自动创建sympy符号并返回 所有这些的清单。例如,如果我有一个字符串变量str_var=['a'、'b'、'c'、…'n']的列表,并且想要一个函数自动为这些变量中的每一个创建一个sympy符号?sym=mapsp.symbol,str_var