Non-sequential substitution in SymPy

后端 未结 5 888
醉梦人生
醉梦人生 2021-02-13 14:11

I\'m trying to use [SymPy][1] to substitute multiple terms in an expression at the same time. I tried the [subs function][2] with a dictionary as parameter, but found out that i

5条回答
  •  情书的邮戳
    2021-02-13 15:04

    Answering the edited question.

    In your example you can use some temporary variables which will not be over-written be subsequent substitutions. Then, once all of the potentially overlapping substitutions have been made, you can replace the temporary variables with the real ones.

    This example works for the question, if your full problem contains more complex substitutions, I think you should still be able to create temporary variables to avoid overlapping substitutions.

    from sympy import Symbol, sin, cos, pi
    
    I_x, I_y, I_z = Symbol("I_x"), Symbol("I_y"), Symbol("I_z")
    S_x, S_y, S_z = Symbol("S_x"), Symbol("S_y"), Symbol("S_z")
    J_is = Symbol("J_IS")
    t = Symbol("t")
    I_x_temp, I_y_temp, I_z_temp = Symbol("I_x_temp"), Symbol("I_y_temp"), Symbol("I_z_temp")
    
    f = 2*I_x*S_z
    answer = I_y*sin(2*pi*J_is*t) + 2*I_x*S_z*cos(2*pi*J_is*t)
    
    subs1a = [
        (2*I_x*S_z, 2*I_x_temp*S_z*cos(2*pi*J_is*t) + I_y_temp*sin(2*pi*J_is*t)),
        (I_x,  I_x_temp*cos(2* pi*J_is*t) + 2*I_x_temp*S_z*sin(2*pi*J_is*t)),
        (I_y,  I_y_temp*cos(2*pi*J_is*t) - 2*I_x_temp*S_z* sin(2*pi*J_is*t))
    ]
    
    subs_temp = [(I_x_temp, I_x), (I_y_temp, I_y), (I_z_temp, I_z)]
    
    print f
    f = f.subs(subs1a)
    print f
    f = f.subs(subs_temp)
    print f
    print f == answer # True
    

    Note, you can also perform two substitutions back to back:

    f.subs(subs1a).subs(subs_temp) == answer
    

提交回复
热议问题