为什么 sympy.solve 不输出任何东西?
Why does sympy.solve not output anything?
我正在尝试求解方程组。下例中的方程是拉格朗日函数(LDash 1 到 7)的一阶导数,是我用笔和纸获得的。系统的解决方案应该给出候选最大值和最小值。
选择变量是 X1, X2, LC, LA, s, u
,而所有其他符号都是正实数的占位符。
脚本运行但随后没有在我的控制台中显示任何内容。在整个示例中 SymPy documentation solve
输出解决方案。你能帮我找出为什么在我下面的例子中它没有这样做吗?
我是 Python 的新手,所以请告诉我如何改进。谢谢!
# import stuff
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import Function
from sympy.solvers import solve
import sympy as sp
X1, X2, LC, LA, s, u = sp.var('X_1, X_2, L_C, L_A, s, u', positive=True);
# import exogenous variables
a1, a2, a3, a4, S1, S2, S3, S4, p1, p2, p3, p4, fdash, vdash,f, v = sp.symbols('alpha_1, alpha_2, alpha_3, alpha_4, S_1, S_2, S_3, S_4, p_1, p_2, p_3, p_4, \hat{F}, \hat{V}, f, v', positive=True)
v0 = sp.symbols('v_0') # raw skill level
i = sp.symbols('\hat{\imath}') # children in the household - time endowment
I = sp.symbols('I') # household size and total time endowment
climate = sp.symbols('theta') # climate variable
capital = sp.symbols('K') # quasi fixed land and (non-human) capital
lam = sp.symbols('\lambda') # shadow prices (lagrange multiplier)
# pen-and-paper: derivatives of the Lagrange function w.r.t choice variables
LDash1 = a1/(X1-S1)-lam*p1 # L' w.r.t X1
LDash2 = a2/(X2-S2)-lam*p2 # L' w.r.t X2
LDash3 = -a3/(I-i-LA-S3) + 2*lam*p3 + lam*p1*fdash # L' w.r.t LA
LDash4 = -1/(i-LC) - a4/(i-LC-S4) + 2*lam*p4 + lam*p1*fdash*(-s*u + s*v +(1-s)*v0) # L' w.r.t LC
LDash5 = lam*p1*fdash *(-u*LC + v*LC - v0*LC) # L' w.r.t. s
LDash6 = lam*p1*fdash *(-s*LC + s*vdash*LC) # L' w.r.t u
LDash7 = p1*X1 + p2*X2 -2*p3*I + 2*p3*i + 2*p3*LA -2*p4*i + 2*p4*LC + p1*f # L' w.r.t lambda
solve((LDash1,LDash2, LDash3, LDash4, LDash5, LDash6, LDash7), (X1,X2,LA,LC,u,s))
您有两个问题,首先,解决调用将花费很长时间,并且可能 return 根本不会成功。其次,即使它 return,假设您用 solve(LDash1, X1)
替换调用,solve
的 return 值也永远不会被打印出来。尝试用这个替换最后一行,例如能够调试你的方程式
solve(LDash1, X1)
答案打印在文档中的原因是因为他们使用 interactive mode,这通常由 Python 交互式提示 >>>
指示
我正在尝试求解方程组。下例中的方程是拉格朗日函数(LDash 1 到 7)的一阶导数,是我用笔和纸获得的。系统的解决方案应该给出候选最大值和最小值。
选择变量是 X1, X2, LC, LA, s, u
,而所有其他符号都是正实数的占位符。
脚本运行但随后没有在我的控制台中显示任何内容。在整个示例中 SymPy documentation solve
输出解决方案。你能帮我找出为什么在我下面的例子中它没有这样做吗?
我是 Python 的新手,所以请告诉我如何改进。谢谢!
# import stuff
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import Function
from sympy.solvers import solve
import sympy as sp
X1, X2, LC, LA, s, u = sp.var('X_1, X_2, L_C, L_A, s, u', positive=True);
# import exogenous variables
a1, a2, a3, a4, S1, S2, S3, S4, p1, p2, p3, p4, fdash, vdash,f, v = sp.symbols('alpha_1, alpha_2, alpha_3, alpha_4, S_1, S_2, S_3, S_4, p_1, p_2, p_3, p_4, \hat{F}, \hat{V}, f, v', positive=True)
v0 = sp.symbols('v_0') # raw skill level
i = sp.symbols('\hat{\imath}') # children in the household - time endowment
I = sp.symbols('I') # household size and total time endowment
climate = sp.symbols('theta') # climate variable
capital = sp.symbols('K') # quasi fixed land and (non-human) capital
lam = sp.symbols('\lambda') # shadow prices (lagrange multiplier)
# pen-and-paper: derivatives of the Lagrange function w.r.t choice variables
LDash1 = a1/(X1-S1)-lam*p1 # L' w.r.t X1
LDash2 = a2/(X2-S2)-lam*p2 # L' w.r.t X2
LDash3 = -a3/(I-i-LA-S3) + 2*lam*p3 + lam*p1*fdash # L' w.r.t LA
LDash4 = -1/(i-LC) - a4/(i-LC-S4) + 2*lam*p4 + lam*p1*fdash*(-s*u + s*v +(1-s)*v0) # L' w.r.t LC
LDash5 = lam*p1*fdash *(-u*LC + v*LC - v0*LC) # L' w.r.t. s
LDash6 = lam*p1*fdash *(-s*LC + s*vdash*LC) # L' w.r.t u
LDash7 = p1*X1 + p2*X2 -2*p3*I + 2*p3*i + 2*p3*LA -2*p4*i + 2*p4*LC + p1*f # L' w.r.t lambda
solve((LDash1,LDash2, LDash3, LDash4, LDash5, LDash6, LDash7), (X1,X2,LA,LC,u,s))
您有两个问题,首先,解决调用将花费很长时间,并且可能 return 根本不会成功。其次,即使它 return,假设您用 solve(LDash1, X1)
替换调用,solve
的 return 值也永远不会被打印出来。尝试用这个替换最后一行,例如能够调试你的方程式
solve(LDash1, X1)
答案打印在文档中的原因是因为他们使用 interactive mode,这通常由 Python 交互式提示 >>>