No solutions with exponents in python sympy

女生的网名这么多〃 提交于 2019-12-23 00:25:35

问题


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:

  • 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.


回答1:


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))]

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.



来源:https://stackoverflow.com/questions/19167958/no-solutions-with-exponents-in-python-sympy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!