sympy solve slow (or broken)

302 views
Skip to first unread message

pdknsk

unread,
Apr 16, 2017, 5:01:16 PM4/16/17
to sympy
I've been trying to get the solution for the following equations.

4.5*b = a*b^0.45-a+1
4.5 = 0.45*a*b^(0.45-1)

I think this should do it.

ab = sympy.solve([a * b**0.45 - a + 1 - 4.5 * b, 0.45 * a * b**(0.45 - 1) - 4.5])

After waiting 100 minutes I cancelled it. It's not clear to me if it will eventually find a solution, or if it has reached a broken internal state which will make it compute indefinitely.

Aaron Meurer

unread,
Apr 16, 2017, 5:10:32 PM4/16/17
to sy...@googlegroups.com
solve() has issues with equations like these currently because it
wants to treat them as polynomials. But the degree of the polynomial
is related to the numerator and denominator of the exponent:

In [14]: Rational(0.45)
Out[14]:
8106479329266893
─────────────────
18014398509481984

If you use rational numbers, and solve for a manually in the second
equation, you get a degree 20 polynomial (after multiplying by b**20):

In [24]: expr = a * b**Rational(45, 100) - a + 1 - 4.5 * b

In [25]: expr.subs(solve([0.45 * a * b**(Rational(45, 100) - 1) - 4.5], [a]))
Out[25]:
11
──
20
- 10.0⋅b + 5.5⋅b + 1

I was able to get solutions with solve(expr.subs(solve([0.45 * a *
b**(Rational(45, 100) - 1) - 4.5], [a]))*b**20, rational=True), but
none are expressible exactly.

I recommend trying nsolve, which can give you numeric solutions to
this system, and quite fast:

In [30]: nsolve([a * b**0.45 - a + 1 - 4.5 * b, 0.45 * a * b**(0.45 -
1) - 4.5], [a, b], [1, 1])
Out[30]:
⎡ 1.09929682680944 ⎤
⎢ ⎥
⎣0.0180539685108078⎦

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/857b5528-22eb-496c-b0a1-5e449c0c2075%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

pdknsk

unread,
Apr 16, 2017, 11:55:21 PM4/16/17
to sympy
nsolve works well. Thanks!

Akshitha Reddy

unread,
Jan 16, 2020, 2:23:30 PM1/16/20
to sympy
What is the approach I can take when I need to find the value of x from an equation which is in the form of eqn=y*(x*a)*0.3014?
Solve() is taking over a minute and Solveset is raising a ConditionSet. I don't think nsolve works as well. 

Akshitha Reddy

unread,
Jan 16, 2020, 2:36:26 PM1/16/20
to sympy
**0.3014
It's (x*a)^
Sorry

Oscar Benjamin

unread,
Jan 16, 2020, 4:58:13 PM1/16/20
to sympy
I'm not sure what you're asking. These all calculate instantly:

In [11]: x, y, a = symbols('x, y, a')

In [12]: eqn=y*(x*a)*0.3014

In [13]: eqn
Out[13]: 0.3014⋅a⋅x⋅y

In [14]: solve(eqn, x)
Out[14]: [0.0]

In [15]: eqn=y*(x*a)**0.3014

In [16]: eqn
Out[16]:
0.3014
y⋅(a⋅x)

In [17]: solve(eqn, x)
Out[17]: [0.0]

--
Oscar

On Thu, 16 Jan 2020 at 19:36, Akshitha Reddy <ksht...@gmail.com> wrote:
>
> **0.3014
> It's (x*a)^
> Sorry
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/fe102631-6aac-4c77-a038-dd3a6a2b9e54%40googlegroups.com.

Akshitha Reddy

unread,
Jan 16, 2020, 10:11:56 PM1/16/20
to sympy
I apologize for the miscommunication. I just need to solve for x in the equation (y*(x*a)^^0.3041)-b=0. The rest of the values are known but when you try to use solve(), It's taking a lot of time to return the value of x.'x' is the only variable.

Akshitha Reddy

unread,
Jan 16, 2020, 11:17:14 PM1/16/20
to sympy
Take the value of y to be 0.027881006415611188
a to be 14.4e+15 and b to be 5.742694e-17
Please solve this without rearrangement.
Reply all
Reply to author
Forward
0 new messages