Ask Your Question
1

How to read and run commands from a text file?

asked 2018-10-26 22:01:18 +0200

Leon gravatar image

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?

edit retag flag offensive close merge delete

Comments

1

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).

rburing gravatar imagerburing ( 2018-10-27 11:00:36 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-10-26 22:13:45 +0200

slelievre gravatar image

The problem is with the file extension.

Use the file extension .sage.

For example, call your file something.sage.

Then type

sage: load('something.sage')

and check that your matrices have been defined correctly.

sage: d0
[1 0 0 0]
[0 1 1 0]
[1 0 0 0]
[0 1 1 0]
sage: d1
[ 0  0  0  0]
[-1  0  1  0]
[-1  0  1  0]
[ 0 -1  0  1]
edit flag offensive delete link more

Comments

Great, it works! Could you also please answer the question about RAM, before I accept?

Leon gravatar imageLeon ( 2018-10-26 22:54:42 +0200 )edit
2

I think Sage can use as much RAM as you have available, no special thing to do.

For some of the software used by Sage, there is a setting to how much memory they are allocated by default when used from Sage, and there are special commands to set that memory amount to some other value than the default.

For GAP and libGAP, use set_gap_memory_pool_size; see this 2018-09 sage-devel thread.

For PARI/GP, there might be something about parisize, parisizemax, primelimit.

For other systems, there might be some indication in the dedicated pages to the interface to each of them... See documentation on interfaces.

Do you have a specific use case in mind?

slelievre gravatar imageslelievre ( 2018-10-27 00:59:06 +0200 )edit

Yes, I'll be computing a lot of homology of chain complexes. Thank you for your answers!!

Leon gravatar imageLeon ( 2018-10-27 10:45:15 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-10-26 20:24:48 +0200

Seen: 1,228 times

Last updated: Oct 26 '18