简体   繁体   中英

Is there anyway I can exclude some of the solutions of sympy solver?

It is taking hours to solve. Here is the code: If anyone has any idea how I can get a solution:

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)

Try this:

solve((dcda,dcdl),(a,l), check=False, simplify=False)

That gives an answer after a few seconds. The answer is complicated though (I think it comes from the quartic formula).

Notice that dcda factors nicely into two factors containing 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))

So solve for sqrt(l) :

>>> solve(dcda, sqrt(l))
[0, sqrt(a)*(-p + t)/(t*(2*a - 1))]

Only the 2nd solution is valid since l > 0 . Substitute this into dcdl for sqrt(l) and solve for a :

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

Then backsubstitute into the expression for sqrt(l) and square to get an expression for l ; some simplification gives:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM