2017-01-03 2 views
0

Zu Beginn, hier ist der Umriss des Arbeitsablaufs, den ich machen möchte: 1. Verwenden Sie sympy zu "Mathe" und entwickeln Sie einige Ausdrücke 2. Lambdify die entsprechenden Ausdrücke 3. Bewahren sie die entsprechenden Lambda-Funktionen in einer Datei 4. Legen sie sie in einem unabhängigen Teil meines Codesdump eine Lambda-Funktion in einer Datei und laden Sie es in einer anderen Funktion

Alles geht gut, bis Schritt 3 ich verschiedene Dinge ausprobiert, und insbesondere nach dem lesen this Dies ist ein minimalistisches Beispiel, das ich gerne arbeiten würde:

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) 

ich diesen Fehler (nach einer wichtigen Anzahl von During handling of the above exception, another exception occurred:

ValueError: 'axis' entry is out of bounds 

Fehle ich etwas Wichtiges hier?

Hinweis: Wenn ich die Sympy-Ausdrücke stattdessen dump und Lambdify die Funktion nach einem Pickle.load, alles geht gut. Aber das ist nicht genau der Workflow, den ich brauche!

Danke für Ihre Hilfe!

+0

https://docs.python.org/2/library/json.html –

+0

Ich fand dieses ähnlich aussehende Problem. Funktioniert die Lösung für Sie? https://github.com/uqfoundation/dill/issues/104 –

Antwort

0

Hier ist eine leicht modifizierte Version Ihres Codes (unten): Es sieht wie ein Fehler aus. Beachten Sie, dass das Beizen des Lambda-Ausdrucks das ursprüngliche symbolische Ausdrucksobjekt zerstört!

Ich bin der dill Autor. Ich würde vorschlagen, es als sympy github Problem zu veröffentlichen - f Sie es als dill Problem, ich werde es graben dann stochern es auf 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 
0

Als Abhilfe schlage ich folgendes:

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) 

In einem anderen Code, den ich habe, wo ich kompliziert sympy Ausdrücke, die ich für die spätere Verwendung lambdify, dump und load von cloudpickle Arbeiten mit! gut

Verwandte Themen