0

首先,这里是我想要做的工作流程的大纲: 1. 使用 sympy 来“做数学”并开发一些表达式 2. 对相应的表达式进行 Lambdify 3. 将相应的 lambda 函数存储在一个文件中 4. 加载它们在我的代码的独立部分

在第 3 步之前一切都很好。我尝试了不同的方法,特别是在阅读了这篇文章之后,这是一个我想做的简约示例:

import sympy as sp
import dill as pickle

x, y = sp.symbols("x, y") 

f_example = 2*x**2 + 2*x*y
f_lbda= sp.lambdify((x, y),f_example ) 
pickle.settings['recurse'] = True 
fileW = open('file_where_I_dump.dill', 'wb')
# the following line crashes 
pickle.dump([f_lbda, f_lbda], fileW)
fileR = open('file_where_I_dump.dill', 'rb')
f_lbda_loaded = pickle.load(fileR)

我收到此错误(在重要数量之后During handling of the above exception, another exception occurred

ValueError: 'axis' entry is out of bounds

我在这里错过了什么重要的东西吗?

注意:当我转储 sympy 表达式并在 pickle.load 之后对函数进行lambdify 时,一切正常。但这不完全是我需要的工作流程!

谢谢你的帮助 !

4

2 回答 2

1

这是您的代码的略微修改版本(如下):它看起来像一个错误。请注意,腌制 lambda 表达式会破坏原始的符号表达式对象!

我是dill作者。我建议将它作为一个sympygithub 问题发布——如果你把它作为一个dill问题发布,我会深入研究它,然后将它放到sympy.

>>> import sympy as sp
>>> x,y = sp.symbols('x,y')
>>> f_ex = 2*x**2 + 2*x*y
>>> f_la = sp.lambdify((x,y),f_ex)
>>> f_ex
2*x**2 + 2*x*y
>>> import dill
>>> dill.settings['recurse'] = True
>>> la = dill.loads(dill.dumps(f_la))
>>> la
<function <lambda> at 0x10b4ed668>
>>> f_ex
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/basic.py", line 392, in __repr__
    return sstr(self, order=None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/str.py", line 748, in sstr
    s = p.doprint(expr)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 233, in doprint
    return self._str(self._print(expr))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 257, in _print
    return getattr(self, printmethod)(expr, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/str.py", line 51, in _print_Add
    terms = self._as_ordered_terms(expr, order=order)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 271, in _as_ordered_terms
    return expr.as_ordered_terms(order=order)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 888, in as_ordered_terms
    terms, gens = self.as_terms()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 920, in as_terms
    coeff = complex(coeff)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 229, in __complex__
    result = self.evalf()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/evalf.py", line 1377, in evalf
    prec = dps_to_prec(n)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpmath/libmp/libmpf.py", line 67, in dps_to_prec
    return max(1, int(round((int(n)+1)*3.3219280948873626)))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2293, in amax
    out=out, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/_methods.py", line 26, in _amax
    return umr_maximum(a, axis, None, out, keepdims)
ValueError: 'axis' entry is out of bounds
于 2017-01-04T08:41:21.433 回答
0

作为一种解决方法,我提出以下建议:

from sympy import *
from cloudpickle import dump, load

var("x, y") 
f_example = 2*x**2 + 2*x*y
f_lbda= lambdify((x, y),f_example ) 

with open('file_where_I_dump_flbda', 'wb') as f: 
    dump((f_lbda), f)

在我拥有的另一个代码中,我有复杂的 sympy 表达式,我将这些表达式转换为供以后使用,使用dumploadfrom cloudpickle 作品!出色地

于 2017-01-25T12:21:28.287 回答