ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 01 Nov 2016 07:17:50 -0500How to input from file?https://ask.sagemath.org/question/26221/how-to-input-from-file/ 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]
Tue, 17 Mar 2015 11:17:22 -0500https://ask.sagemath.org/question/26221/how-to-input-from-file/Answer by calc314 for <p>I have a file with set of matrices in a file.
How to put them into array of matrices in sage?
Example of file:</p>
<p>[1,0,0] <br/>
[0,1,0] <br/>
[1,1,1]</p>
<p>[0,1,0] <br/>
[1,0,0] <br/>
[0,0,1]</p>
https://ask.sagemath.org/question/26221/how-to-input-from-file/?answer=26225#post-id-26225For 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.)
Tue, 17 Mar 2015 12:35:22 -0500https://ask.sagemath.org/question/26221/how-to-input-from-file/?answer=26225#post-id-26225Comment by JackM for <p>For small number of small-sized matrices like you list above, you could do the following.</p>
<p>Create a file named <code>mat.sage</code> that contains:</p>
<pre><code>a=matrix([[1,0,0],
[0,1,0],
[1,1,1]])
b=matrix([[0,1,0],
[1,0,0],
[0,0,1]])
</code></pre>
<p>Then, in your Sage worksheet, use the command <code>load('mat.sage')</code> to read the matrices into memory. You can then begin computing with these matrices. For example, your worksheet might include:</p>
<pre><code>load('mat.sage')
a*b
</code></pre>
<p>which gives a result of </p>
<pre><code>[0 1 0]
[1 0 0]
[1 1 1]
</code></pre>
<p>For a very large number of matrices, there are a number of ways you might approach this.</p>
<p>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.</p>
<p>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.)</p>
https://ask.sagemath.org/question/26221/how-to-input-from-file/?comment=35327#post-id-35327You 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.Tue, 01 Nov 2016 07:17:50 -0500https://ask.sagemath.org/question/26221/how-to-input-from-file/?comment=35327#post-id-35327