在 Julia 中使用 SymPy 进行多重替换

Multiple substitution with SymPy in Julia

我在 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

有没有办法像我尝试的前两种方法一样一次进行多次替换?

您可以将第一个构造与 splatting 一起使用:

X = subs(X,zip(toin,toout)...)