Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
0 votes

How do I save the contents of expression "ex" or "exRule" to a file so I can retrieve it in another Cadabra program without having to recompute it again.

exRule:= ex =bunch of Cadabra stuff

How do I read it back in the other program.

Basic question, sorry

in General questions by (520 points)

1 Answer

0 votes

The key is to use Ex.input_form(). Here's an example. Writing an expression ex1 and ex2 to a file ex.txt (in the directory from which you started Cadabra; use a full path if necessary) is done as in:

ex1:=A_{m n} \sin{x};
ex2:= B_{m n};
with open("ex.txt", "w") as file:
   file.write( ex1.input_form()+"\n" )
   file.write( ex2.input_form()+"\n" )

You can then read them back in later using

with open("ex.txt", "r") as file:
   ex1=Ex( file.readline() )
   ex2=Ex( file.readline() )
by (76.4k points)

The write part of this works. I wrote a file to my Desktop. I could also read it in a Jupyter notebook using

with open("/Users/james2/Desktop/LtotR.txt", "r") as file: LtotR=file.readline()

However when I use this in Cadabra it hangs up

with open("/Users/james2/Desktop/LtotR.txt", "r") as file: LtotR=Ex( file.readline() )

Any ideas?

Weird. Can you send me that LtoR.txt file by email?

...