There are lots of issues with pickling and SymPy unfortunately. Functions in particular have issues because they are dynamically created classes. The best way to save expressions right now is to use srepr/sympify.
Aaron Meurer On Wed, Aug 14, 2019 at 4:33 AM Oscar Benjamin <[email protected]> wrote: > > This seems to be a bug. I think I can reproduce it with: > > In [9]: import pickle > > In [10]: from sympy.physics.mechanics import dynamicsymbols > > In [11]: q1 = dynamicsymbols('q1') > > In [12]: pickle.dumps(q1) > --------------------------------------------------------------------------- > PicklingError Traceback (most recent call last) > <ipython-input-12-6285d23b0ca4> in <module> > ----> 1 pickle.dumps(q1) > > PicklingError: Can't pickle q1: it's not the same object as __main__.q1 > > Although the final error message is slightly different. Normal Symbol > works so it is to do with dynamicsymbol in some way. > > Digging further: > > In [13]: q1 = Function('q1')(Symbol('t')) > > In [14]: pickle.dumps(q1) > --------------------------------------------------------------------------- > PicklingError Traceback (most recent call last) > <ipython-input-14-6285d23b0ca4> in <module> > ----> 1 pickle.dumps(q1) > > PicklingError: Can't pickle q1: it's not the same object as __main__.q1 > > In [15]: q1 = Function('q1') > > In [16]: pickle.dumps(q1) > Out[16]: b'\x80\x03c__main__\nq1\nq\x00.' > > That pickle for the unapplied function is too short. I think something > is wrong with pickling functions. > > > To fix your problem substitute the dynamicsymbols for normal symbols. > > > Oscar > > On Wed, 14 Aug 2019 at 10:54, Ash <[email protected]> wrote: > > > > Hello, > > I have derived the equations of motion of a dynamic system symbolically. To > > evaluate the Mass Matrix and Coriolis Vector which contain several > > derivative terms, the program takes around 14 sec. These two are to be used > > for various other places. I tried saving and loading using 'dill' > > > > import dill > > MassMatrix, Coriolis = get_mc() > > dill.dump(MassMatrix, open("MassMatrix", "wb")) > > # dill.dump(Coriolis, open("Coriolis", "wb")) > > > > The above results in > > > > Traceback (most recent call last): > > File > > "/snap/pycharm-professional/147/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", > > line 3, in Exec > > exec(exp, global_vars, local_vars) > > File "<input>", line 1, in <module> > > File > > "/home/ar0058/Ash/venv3.6/lib/python3.6/site-packages/dill/_dill.py", line > > 287, in dump > > pik.dump(obj) > > File "/usr/lib/python3.6/pickle.py", line 409, in dump > > self.save(obj) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 736, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 751, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 736, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 736, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 751, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 736, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 736, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 736, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 610, in save_reduce > > save(args) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File "/usr/lib/python3.6/pickle.py", line 736, in save_tuple > > save(element) > > File "/usr/lib/python3.6/pickle.py", line 521, in save > > self.save_reduce(obj=obj, *rv) > > File "/usr/lib/python3.6/pickle.py", line 609, in save_reduce > > save(func) > > File "/usr/lib/python3.6/pickle.py", line 476, in save > > f(self, obj) # Call unbound method with explicit self > > File > > "/home/ar0058/Ash/venv3.6/lib/python3.6/site-packages/dill/_dill.py", line > > 1338, in save_type > > StockPickler.save_global(pickler, obj) > > File "/usr/lib/python3.6/pickle.py", line 922, in save_global > > (obj, module_name, name)) > > _pickle.PicklingError: Can't pickle q1: it's not found as __main__.q1 > > > > > > Could someone suggest a way to save properly? > > > > Thanks > > > > -- > > You received this message because you are subscribed to the Google Groups > > "sympy" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to [email protected]. > > To view this discussion on the web visit > > https://groups.google.com/d/msgid/sympy/bc15fc9f-8b3b-42f3-824c-69b475d947ad%40googlegroups.com. > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/CAHVvXxQe%2BYpg-tTO-tpZbjE36AaBPYBdbUGRsXXneVGO7hXrXA%40mail.gmail.com. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6KdCg4LierRFfMFR8Z1pH-ZHuHTuCTmtDjOGdq_LOK4rA%40mail.gmail.com.
