Python memory error in sympy.simplify

。_饼干妹妹 提交于 2021-01-28 18:46:12

问题


Using 64-bit Python 3.3.1 and 32GB RAM and this function to generate target expression 1+1/(2+1/(2+1/...)):

def sqrt2Expansion(limit):
    term = "1+1/2"
    for _ in range(limit):
        i    = term.rfind('2')
        term = term[:i] + '(2+1/2)' + term[i+1:]
    return term

I'm getting MemoryError when calling:

simplify(sqrt2Expansion(100))

Shorter expressions work fine, e.g:

simplify(sqrt2Expansion(50))

Is there a way to configure SymPy to complete this calculation? Below is the error message:

    MemoryError                               Traceback (most recent call last)
<ipython-input-90-07c1e2de29d1> in <module>()
----> 1 simplify(sqrt2Expansion(100))

C:\Python33\lib\site-packages\sympy\simplify\simplify.py in simplify(expr, ratio, measure)
   2878     from sympy.functions.special.bessel import BesselBase
   2879 
-> 2880     original_expr = expr = sympify(expr)
   2881 
   2882     expr = signsimp(expr)

C:\Python33\lib\site-packages\sympy\core\sympify.py in sympify(a, locals, convert_xor, strict, rational)
    176     try:
    177         a = a.replace('\n', '')
--> 178         expr = parse_expr(a, locals or {}, rational, convert_xor)
    179     except (TokenError, SyntaxError):
    180         raise SympifyError('could not parse %r' % a)

C:\Python33\lib\site-packages\sympy\parsing\sympy_parser.py in parse_expr(s, local_dict, rationalize, convert_xor)
    161 
    162     code = _transform(s.strip(), local_dict, global_dict, rationalize, convert_xor)
--> 163     expr = eval(code, global_dict, local_dict) # take local objects in preference
    164 
    165     if not hit:

MemoryError: 

EDIT:

I wrote a version using sympy expressions instead of strings:

def sqrt2Expansion(limit):
    x    = Symbol('x')
    term = 1+1/x
    for _ in range(limit):
        term = term.subs({x: (2+1/x)})
    return term.subs({x: 2})

It runs better: sqrt2Expansion(100) returns valid result, but sqrt2Expansion(200) produces RuntimeError with many pages of traceback and hangs up IPython interpreter with plenty of system memory left unused. I created new question Long expression crashes SymPy with this issue.


回答1:


SymPy is using eval along the path to turn your string into a SymPy object, and eval uses the built-in Python parser, which has a maximum limit. This isn't really a SymPy issue.

For example, for me:

>>> eval("("*100+'3'+")"*100)
s_push: parser stack overflow
Traceback (most recent call last):
  File "<ipython-input-46-1ce3bf24ce9d>", line 1, in <module>
    eval("("*100+'3'+")"*100)
MemoryError

Short of modifying MAXSTACK in Parser.h and recompiling Python with a different limit, probably the best way to get where you're headed is to avoid using strings in the first place. [I should mention that the PyPy interpreter can make it up to ~1100 for me.]



来源:https://stackoverflow.com/questions/16383327/python-memory-error-in-sympy-simplify

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!