使用 64 位 Python 3.3.1 和 32GB RAM 和此函数生成目标表达式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
我打电话MemoryError
时得到:
simplify(sqrt2Expansion(100))
较短的表达式可以正常工作,例如:
simplify(sqrt2Expansion(50))
有没有办法配置 SymPy 来完成这个计算?以下是错误消息:
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:
编辑:
我使用 sympy 表达式而不是字符串编写了一个版本:
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})
它运行得更好:sqrt2Expansion(100)
返回有效结果,但sqrt2Expansion(200)
产生RuntimeError
许多回溯页面并挂起 IPython 解释器,并且有大量未使用的系统内存。我创建了新问题Long expression使 SymPy因这个问题而崩溃。