How to read and run commands from a text file?
I have all my commands in a text file, and I would just like to run them in Sage. What is the easiest way to do this?
For example, in sage.txt, I have written:
d0=matrix(ZZ,4,4,{(0,0):1,(2,0):1,(1,1):1,(3,1):1,(1,2):1,(3,2):1});
d1=matrix(ZZ,4,4,{(1,0):-1,(2,0):-1,(3,1):-1,(1,2):1,(2,2):1,(3,3):1});
The command with open('/home/sage.txt','r') as ll: bdrs=[sage_eval(l.strip()) for l in ll];
returns
File "<string>", line 1
d0=matrix(ZZ,Integer(4),Integer(4),{(Integer(0),Integer(0)):Integer(1),(Integer(2),Integer(0)):Integer(1),(Integer(1),Integer(1)):Integer(1),(Integer(3),Integer(1)):Integer(1),(Integer(1),Integer(2)):Integer(1),(Integer(3),Integer(2)):Integer(1)});
^
SyntaxError: invalid syntax
Also, does Sage have a limit on how much RAM it can use? If yes, how can I increase the available memory?
The error in your code is that
sage_eval
evaluates expressions and not arbitrary statements such as assignments (the^
is pointing at the equals sign).