尝试求解简单的代数方程时,sympy挂起 [英] sympy hangs when trying to solve a simple algebraic equation
问题描述
我最近重新安装了我的python环境,现在可以很好地运行的代码现在可以正常运行了(通常只是挂起,占用了越来越多的内存).
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).
代码的挂起点是:
solve(exp(-alpha * x**2) - 0.01, alpha)
我已经能够通过全新的IPython 0.13.1会话重现此问题:
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)
这适用于整数,但也很慢.在原始代码中,我遍历了这个过程,以查找数百个不同的x值(除了14.7296138519之外)的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.
有什么想法吗?
推荐答案
在这种情况下,引入了Rational = False标志.
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.)
这篇关于尝试求解简单的代数方程时,sympy挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!