Saving sympy expressions to disk

2,347 views
Skip to first unread message

Mani Chandra

unread,
Jul 1, 2011, 8:17:09 AM7/1/11
to sy...@googlegroups.com
Hi,

Is it possible to save sympy expressions to disk and then load them later? For ex:

filename = "equation"
save(filename, expression)
datafile = load(filename)
expression = datafile.expression

or something like that?

Thanks,
Mani Chandra

Tom Bachmann

unread,
Jul 1, 2011, 9:35:30 AM7/1/11
to sy...@googlegroups.com
Hi,

sympy should in theory support pickling (the standard python way to save
objects on disk), but I'm not sure how good the support is.

Regards,
Tom

> --
> You received this message because you are subscribed to the Google
> Groups "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/S05rOKsC6JwJ.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

Chris Smith

unread,
Jul 1, 2011, 11:27:25 AM7/1/11
to sy...@googlegroups.com
Just like that (almost):

    >>> eq
    x + y**2
    >>> pickle.dump(eq, file('eq.dat','w'))
    >>> pickle.load(file('eq.dat'))
    x + y**2

The nice thing about this is that assumptions on your variables are preserved, too, so if x and y were designated as real, the x and y in the equation you load will be real, too. Note, however, that if you start a new session and load a saved expression this does not automatically recreate python variables having the same name as the Symbols in the equation. To save on the tedium of redefining those you could do something like this if you last issued `eq = pickle.load(file('eq.dat'))` :

    >>> [var(str(i), **i.assumptions0) for i in eq.atoms(Symbol)]

/Chris

Mani Chandra

unread,
Jul 1, 2011, 11:53:14 AM7/1/11
to sy...@googlegroups.com
There is a problem with that procedure when I'm trying to save a function.

In [70]: a = Function('a')

In [71]: pickle.dump(a, file('a.dat', 'w'))
---------------------------------------------------------------------------
PicklingError                             Traceback (most recent call last)

/home/mc/test.py in <module>()
----> 1 
      2 
      3 
      4 
      5 

/usr/lib/python2.7/pickle.pyc in dump(obj, file, protocol)
   1368 
   1369 def dump(obj, file, protocol=None):
-> 1370     Pickler(file, protocol).dump(obj)
   1371 
   1372 def dumps(obj, protocol=None):

/usr/lib/python2.7/pickle.pyc in dump(self, obj)
    222         if self.proto >= 2:
    223             self.write(PROTO + chr(self.proto))
--> 224         self.save(obj)
    225         self.write(STOP)
    226 

/usr/lib/python2.7/pickle.pyc in save(self, obj)
    293             issc = 0
    294         if issc:
--> 295             self.save_global(obj)
    296             return
    297 

/usr/lib/python2.7/pickle.pyc in save_global(self, obj, name, pack)
    746             raise PicklingError(
    747                 "Can't pickle %r: it's not found as %s.%s" %
--> 748                 (obj, module, name))
    749         else:
    750             if klass is not obj:

PicklingError: Can't pickle a: it's not found as sympy.core.function.a

Aaron Meurer

unread,
Jul 1, 2011, 11:07:17 PM7/1/11
to sy...@googlegroups.com
This is a known issue. Unfortunately, it seems the pickle does not
work for Function. See
http://code.google.com/p/sympy/issues/detail?id=1198.

Any ideas on how to fix this?

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/sympy/-/TUayQHfdoGYJ.

Christophe BAL

unread,
Jul 2, 2011, 5:29:59 AM7/2/11
to sy...@googlegroups.com
Hello.

I don't know if this will hepl you but I've been in trouble when I've tried to pickle class using lambda functions in one attribut.

Best regards.
Christophe.

Ondrej Certik

unread,
Jul 15, 2011, 6:38:01 PM7/15/11
to sy...@googlegroups.com
On Fri, Jul 1, 2011 at 8:07 PM, Aaron Meurer <asme...@gmail.com> wrote:
> This is a known issue.  Unfortunately, it seems the pickle does not
> work for Function.  See
> http://code.google.com/p/sympy/issues/detail?id=1198.
>
> Any ideas on how to fix this?

I wonder how this works at live.sympy.org, because there the
expressions are somehow stored in a database. And it works for a
Function.

Ondrej

Mateusz Paprocki

unread,
Jul 15, 2011, 7:42:00 PM7/15/11
to sy...@googlegroups.com
Hi,

SymPy Live uses a hack to overcome this. If an expression results in unpicklables, it stores the expression in the database and reevaluates it before every future evaluation.
 

Ondrej

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.


Mateusz
Reply all
Reply to author
Forward
0 new messages