![](/img/trans.png)
[英]Sympy: possible to make the solver return as soon as a solution is found (before all solutions are found)?
[英]Is there anyway I can exclude some of the solutions of sympy solver?
需要几个小时才能解决。 这是代码:如果有人知道如何获得解决方案:
from sympy import symbols
from sympy import sqrt
from sympy import solve
from sympy import diff
from sympy import Rational
from sympy import solveset, Eq, S
from sympy import init_printing
init_printing()
a, l, w, t, x, phi, y, p, q = symbols('a l w t x phi y p q', positive=True,real=True)
x = a**Rational(1,2)*l**Rational(1,2)
q = phi*l**Rational(1,2)*(1-a)**Rational(1,2)
y = q*(1-x)
cost = w*l+ t*y
objective_function = q*p -cost
from sympy import diff
#partial derivatives with respect to a and l
dcda = diff(objective_function, a)
dcdl = diff(objective_function, l)
solve((dcda,dcdl),(a,l),real=True)
尝试这个:
solve((dcda,dcdl),(a,l), check=False, simplify=False)
几秒钟后给出答案。 答案很复杂(我认为它来自四次公式)。
请注意, dcda
很好地分解为包含sqrt(l)
的两个因子
>>> var('sl', positive=True)
sl
>>> dcda.subs(sqrt(l),sl).factor()
-phi*sl*(sqrt(a)*p - sqrt(a)*t + 2*a*sl*t - sl*t)/(2*sqrt(a)*sqrt(1 - a))
所以解决sqrt(l)
:
>>> solve(dcda, sqrt(l))
[0, sqrt(a)*(-p + t)/(t*(2*a - 1))]
由于l > 0
,只有第二个解决方案有效。 将其代入dcdl
中的sqrt(l)
并求解a
:
a = phi**2*t**2/(phi**2*t**2 + 4*w**2)
然后反向代入sqrt(l)
和 square 的表达式以获得l
的表达式; 一些简化给出:
l = phi**2*(p - t)**2*(phi**2*t**2 + 4*w**2)/(phi**2*t**2 - 4*w**2)**2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.