Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solve running forever #22248

Open
AntonioJCosta opened this issue Oct 10, 2021 · 4 comments
Open

solve running forever #22248

AntonioJCosta opened this issue Oct 10, 2021 · 4 comments

Comments

@AntonioJCosta
Copy link

from fractions import Fraction
from pprint import pprint
from sympy import *
import numpy as np

x = symbols('x')

g = 9.81; y0 =100; v0 = 55; m = 80; c = 15
y = y0 + (m / c) * (v0 + ((m * g) / c)) * (1 - exp(- (c / m) * x)) - ((m * g) / c) * x
print('Equation:')
print(y)

Output:

Equation:
-52.32x + 672.373333333333 - 572.373333333333exp(-0.1875*x)

When I try to solve this using solve, the program runs forever and don't stop, without any error message.

solve(y)

@praneethratna
Copy link
Contributor

praneethratna commented Oct 10, 2021

solve() is used to algebraically solve equations and systems of equations , that is solving for a particular symbol in the equation where the first argument is an equation equaled to 0 and second argument is the symbol that we wanted to solve for.

>>> solve(x**2 - 1, x)
[-1, 1]

Here you wanted to substitute values in y if I am right for which you can use .subs() with list of tuples of values to be substituted in the equation as argument for the required output.

>>> g = 9.81; y0 =100; v0 = 55; m = 80; c = 15
>>> y = y0 + (m / c) * (v0 + ((m * g) / c)) * (1 - exp(- (c / m) * x)) - ((m * g) / c) * x
>>> y.subs([(g,9.81),(y0,100),(v0,55),(m,80),(c,15)])
-52.32*x + 672.373333333333 - 572.373333333333*exp(-0.1875*x)

skirpichev added a commit to skirpichev/diofant that referenced this issue Oct 10, 2021
@ThePauliPrinciple
Copy link
Contributor

solve() is used to algebraically solve equations and systems of equations , that is solving for a particular symbol in the equation where the first argument is an equation equaled to 0 and second argument is the symbol that we wanted to solve for.

>>> solve(x**2 - 1, x)
[-1, 1]

Here you wanted to substitute values in y if I am right for which you can use .subs() with list of tuples of values to be substituted in the equation as argument for the required output.

>>> g = 9.81; y0 =100; v0 = 55; m = 80; c = 15
>>> y = y0 + (m / c) * (v0 + ((m * g) / c)) * (1 - exp(- (c / m) * x)) - ((m * g) / c) * x
>>> y.subs([(g,9.81),(y0,100),(v0,55),(m,80),(c,15)])
-52.32*x + 672.373333333333 - 572.373333333333*exp(-0.1875*x)

subs here does nothing, since g, y0,v0,m and c are not symbols, just python objects of ints and floats.
It would be nice if the OP could describe in words what is attempted.

I would agree with the OP though that a solution should be provided, the question asked to sympy was:
solve -52.32x + 672.373333333333 - 572.373333333333exp(-0.1875*x)=0, which, given that one equation and one unknown was given should be solvable.

Even when specifying to solve for x explicitly:
solve(y,x) there seems to be no solution found, nor the algorithm exiting and telling that no solution was found, so I would think this is a bug of some sort.

When we take the exponential term to the rhs and plot both sides of the equation we can find that a solution should exist:
afbeelding

@praneethratna
Copy link
Contributor

Even when specifying to solve for x explicitly: solve(y,x) there seems to be no solution found, nor the algorithm exiting and telling that no solution was found, so I would think this is a bug of some sort.

I agree with you , I have understood the issue in a wrong way ,so there seems to be a bug in solve.

@oscarbenjamin
Copy link
Contributor

If you pass rational=False then it returns quickly:

In [7]: solve(y, rational=False)
Out[7]: [11.610838471061, 5.33333333333333W(-0.184305885067861, -1) + 12.8511722731906]

The problem is that the floats nsimplify to complicated rationals:

In [8]: nsimplify(y)
Out[8]: 
                                              -3x 
                                              ─────
                                                16 
  1308x   672373333333333   572373333333333     
- ────── + ─────────────── - ──────────────────────
    25      1000000000000        1000000000000  

Internally the slow part in solve is attempting to compute 572373333333333**279040000000000 which is a stupendously large integer. It would be better if that Pow would remain unevaluated.

There are a number of related issues but see also #6835

skirpichev added a commit to skirpichev/diofant that referenced this issue Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants