问题描述
当我运行这个程序时,我最终没有得到解决方案,但应该有一个解决方案(我相信).知道我做错了什么吗?如果你从 e2 方程中去掉 Q,它似乎可以正常工作.
When I run this program, I get no solution at the end, but there should be a solution ( I believe). Any idea what I am doing wrong? If you take away the Q from e2 equation it seems to work correctly.
#!/usr/bin/python
from sympy import *
a,b,w,r = symbols('a b w r',real=True,positive=True)
L,K,Q = symbols('L K Q',real=True,positive=True)
e1=K
e2=(K*Q/2)**(a)
print solve(e1-e2,K)
如果我们执行以下操作,它会起作用:
It works if we do the following:
- 设置 Q=1 或,
- 将 e2 改为 e2=(K*a)(Q/2)**(a)
- Set Q=1 or,
- Change e2 to e2=(K*a)(Q/2)**(a)
不过,我仍然希望它以原始方式工作,因为我的方程比这更复杂.
I would still like it to work in the original way though, as my equations are more complicated than this.
推荐答案
这只是solve
的一个不足.solve
主要基于启发式算法,因此有时当方程以特定形式给出时,它无法弄清楚如何求解.这里的解决方法是在表达式上调用 expand_power_base
,因为 SymPy 能够解决 K - K**a*(Q/2)**a
:>
This is just a deficiency of solve
. solve
is based mostly on heuristics, so sometimes it isn't able to figure out how to solve an equation when it's given in a particular form. The workaround here is to just call expand_power_base
on the expression, since SymPy is able to solve K - K**a*(Q/2)**a
:
In [8]: print(solve(expand_power_base(e1-e2),K))
[(2/Q)**(a/(a - 1))]
还值得指出的是,solve
的 []
的结果 并不 以任何方式意味着没有解决方案,只有solve
无法找到任何.请参阅 http://docs.sympy.org/latest/tutorial/solvers 上的第一条注释.html.
It's also worth pointing out that the result of []
from solve
does not in any way mean that there are no solutions, only that solve
was unable to find any. See the first note at http://docs.sympy.org/latest/tutorial/solvers.html.
这篇关于python sympy中没有指数的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!