Python - write symbolic expression (sympy) to txt file
Asked Answered
S

3

11

What is the best way to write a symbolic expression to a txt file (so that I can use the symbolic expression after reading the txt file)?

Is there a sympy equivalent of numpy's savetxt() function for example?

for example:

from sympy import Symbol
a=Symbol('a')
b=Symbol('b')

y=a+3*b

How can I properly write and read y?

Schleswig answered 8/7, 2013 at 0:16 Comment(0)
F
5

You could use the pickle library to store it to a file (as you can save most Python objects):

import pickle
with open("y.txt", "w") as outf:
    pickle.dump(y, outf)

You would later load it with

with open("y.txt") as inf:
    y = pickle.load(inf)
Fredrika answered 8/7, 2013 at 0:19 Comment(1)
Note that pickle chokes on many SymPy expressions. dill does a better job.Welltimed
W
13

str(expr) in SymPy should save most of the information about your expression. The advantage is that it is human readable. The best way to load from it is to use sympify(), as opposed to just copy-paste or eval, because for example, str(Rational(1, 2)) will give 1/2, which will evaluate to 0.5 directly.

There is also a function srepr() (SymPy's version of repr) that gives a more verbose output.

One thing to be aware of is that neither saves information about assumptions on Symbols at the moment (Symbol('x', real=True)), so if you need that, you should use something like pickle or dill.

Welltimed answered 8/7, 2013 at 18:55 Comment(5)
srepr does store assumption (at least by now)...Lewislewisite
how do you load in something that you have saved with srepr?Vinia
@Vinia you can parse a string back to a sympy expression with sympify or parse_expr.Welltimed
@Welltimed I think the problem is rather: how do you get back the symbols and use them again. Also what can you do when you have two different expressions, containing the same symbol? You can retrieve the symbols from the expression with free_symbol but how do you make sure that the same symbol can be used in both expressions again?Vinia
@Vinia SymPy treats two symbols that have the same name and assumptions as the same, so you can just recreate the symbol with Symbol. Also, it's better to ask a new question for things like this.Welltimed
F
5

You could use the pickle library to store it to a file (as you can save most Python objects):

import pickle
with open("y.txt", "w") as outf:
    pickle.dump(y, outf)

You would later load it with

with open("y.txt") as inf:
    y = pickle.load(inf)
Fredrika answered 8/7, 2013 at 0:19 Comment(1)
Note that pickle chokes on many SymPy expressions. dill does a better job.Welltimed
I
0

With pickle, you are likely to run into errors like

TypeError: write() argument must be str, not bytes

I would recommended using the basic Python 3 print function:

y=a+3*b # this is my linear form
f = open('out.txt', 'w')
print("equation = ", y, file=f)  
f.close()

I use it successfully (also to feed SymPy output to other programs)

In some of the more difficult cases, you might need 'wb', for writing in binary mode

Insult answered 5/5, 2024 at 22:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.