Ask Your Question
1

How to input from file?

asked 2015-03-17 11:17:22 -0500

Feckous gravatar image

updated 2015-03-17 11:22:56 -0500

I have a file with set of matrices in a file. How to put them into array of matrices in sage? Example of file:

[1,0,0]
[0,1,0]
[1,1,1]

[0,1,0]
[1,0,0]
[0,0,1]

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-03-17 12:35:22 -0500

calc314 gravatar image

updated 2015-03-18 19:28:47 -0500

For small number of small-sized matrices like you list above, you could do the following.

Create a file named mat.sage that contains:

a=matrix([[1,0,0], 
[0,1,0],
[1,1,1]])

b=matrix([[0,1,0],
[1,0,0],
[0,0,1]])

Then, in your Sage worksheet, use the command load('mat.sage') to read the matrices into memory. You can then begin computing with these matrices. For example, your worksheet might include:

load('mat.sage')
a*b

which gives a result of

[0 1 0]
[1 0 0]
[1 1 1]

For a very large number of matrices, there are a number of ways you might approach this.

1) You could treat it like one huge matrix and read just that one matrix in. Then, you could break it out into smaller matrices. I don't know if this will result in a memory issue or not.

2) You could read the data in from a csv file and then convert the appropriate parts to matrices. (This is what I would do.)

edit flag offensive delete link more

Comments

You say "create a file", but where do I put it? If I put it in the Sage directory, it doesn't seem to see it.

JackM gravatar imageJackM ( 2016-11-01 07:17:50 -0500 )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: 2015-03-17 11:17:22 -0500

Seen: 671 times

Last updated: Mar 18 '15