我在 Julia 中使用 SymPy 。我有以下代码:
using SymPy
# Define variables and function
alpha_m, alpha_r, cm, cr phi = Sym("alpha_m", "alpha_r", "cm", "cr", "phi");
X = 1+(alpha_r/alpha_m)^(1/(1-phi))*(cr/cm)^(phi/(1-phi));
现在,我想用其他变量替换几个表达式:
# New definitions
m, r = Sym("m","r");
我尝试了三种方法,但只有最后一种方法有效,即为每次重新定义编写子方法:
# Multiple replacement
toin = (alpha_m, alpha_r);
toout = (m/(m+r),r/(m+r));
X = subs(X,zip(toin,toout)) # NOT WORKING
X = subs(X,[alpha_m,m/(m+r),alpha_r,r/(m+r)]); # NOT WORKING
X = subs(X,alpha_m,m/(m+r))
X = subs(X,alpha_r,r/(m+r)) # WORKING
有没有办法一次进行多次替换,就像我尝试前两种方法一样?
答案 0 :(得分:3)
您可以使用第一个带splatting的构造:
X = subs(X,zip(toin,toout)...)