pickling a lambdified expression causes sympy function to throw ValueError #12023

Open
mmckerns opened this Issue Jan 6, 2017 · 3 comments

Projects

None yet

2 participants

@mmckerns
mmckerns commented Jan 6, 2017 edited

originally reported here by @MarcoMagl

Without looking too deeply into what happens here… there is a pretty weird situation happening here. It looks like the serialization of the lambdafied expression object is destroying a pointer or a weakref on the corresponding sympy expression.

Here's the minimalist example of what happens:

>>> 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

The above is using python 2.7 and:

>>> dill.__version__
'0.2.6.dev0'
>>> sp.__version__
'1.0'
>>>

However, the OP I think demonstrated this behavior in 3.x.

@asmeurer
Member

This must be a caching issue. The lambdified function doesn't actually reference the original expression. It's created from scratch using eval. The only way they could reference each other is through the cache.

@asmeurer
Member

Ah, maybe not. It happens even with the cache off.

@asmeurer
Member

This is perplexing. I don't see how f_la could be referencing f_ex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment