First time here? Check out the FAQ!

Ask Your Question
1

How to read and run commands from a text file?

asked 6 years ago

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?

Preview: (hide)

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 ( 6 years ago )

1 Answer

Sort by » oldest newest most voted
2

answered 6 years ago

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]
Preview: (hide)
link

Comments

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

Leon gravatar imageLeon ( 6 years ago )
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 ( 6 years ago )

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

Leon gravatar imageLeon ( 6 years ago )

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: 6 years ago

Seen: 1,483 times

Last updated: Oct 26 '18