无论如何我可以排除一些 sympy 求解器的解决方案吗?

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 for sqrt(l) 并求解 a:

a = phi**2*t**2/(phi**2*t**2 + 4*w**2)

然后反代入sqrt(l)的表达式并平方得到l的表达式;一些简化给出:

l = phi**2*(p - t)**2*(phi**2*t**2 + 4*w**2)/(phi**2*t**2 - 4*w**2)**2