Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sympy hangs when trying to solve a simple algebraic equation

I recently reinstalled my python environment and a code that used to work very quickly now creeps at best (usually just hangs taking up more and more memory).

The point at which the code hangs is:

solve(exp(-alpha * x**2) - 0.01, alpha)

I've been able to reproduce this problem with a fresh IPython 0.13.1 session:

In [1]: from sympy import solve, Symbol, exp
In [2]: x = 14.7296138519
In [3]: alpha = Symbol('alpha', real=True)
In [4]: solve(exp(-alpha * x**2) - 0.01, alpha)

this works for integers but also quite slow. In the original code I looped over this looking for hundreds of different alpha's for different values of x (other than 14.7296138519) and it didn't take more than a second.

any thoughts?

like image 598
Shahar Avatar asked Oct 15 '25 10:10

Shahar


1 Answers

The rational=False flag was introduced for such cases as this.

>>> q=14.7296138519
>>> solve(exp(-alpha * q**2) - 0.01, alpha, rational=False)
[0.0212257459123917]

(The explanation is given in the issue cited above.)

like image 152
smichr Avatar answered Oct 16 '25 22:10

smichr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!