ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 09 Nov 2021 10:19:45 +0100How to use the output matrix as an input in Sagehttps://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/ Suppose
[1 2 3]
[4 5 6]
[5 6 7]
is a matrix I get as an output in a file. How can I use the matrix as an input to a different code? It seems the matrix output doesn't have the same input structure in Sagemath. Tue, 09 Nov 2021 00:08:06 +0100https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/Answer by John Palmieri for <p>Suppose
[1 2 3]
[4 5 6]
[5 6 7]
is a matrix I get as an output in a file. How can I use the matrix as an input to a different code? It seems the matrix output doesn't have the same input structure in Sagemath. </p>
https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?answer=59650#post-id-59650You can try the `sage_input` function:
sage: m = matrix(QQ, [[1,2,3], [4,5,6], [7,8,9]])
sage: m
[1 2 3]
[4 5 6]
[7 8 9]
sage: sage_input(m)
matrix(QQ, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Save the last line to a file instead of just
[1 2 3]
[4 5 6]
[7 8 9]
The `sage_input` function is documented [here](https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_input.html)
Tue, 09 Nov 2021 01:08:56 +0100https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?answer=59650#post-id-59650Answer by Max Alekseyev for <p>Suppose
[1 2 3]
[4 5 6]
[5 6 7]
is a matrix I get as an output in a file. How can I use the matrix as an input to a different code? It seems the matrix output doesn't have the same input structure in Sagemath. </p>
https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?answer=59647#post-id-59647For example, you can parse such matrix printout as
s = '[1 2 3] [4 5 6] [5 6 7]'
M = Matrix(QQ, eval('['+s.replace(' ',',')+']') )
print(M)Tue, 09 Nov 2021 00:32:36 +0100https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?answer=59647#post-id-59647Comment by dasdipayan9038 for <p>For example, you can parse such matrix printout as</p>
<pre><code>s = '[1 2 3] [4 5 6] [5 6 7]'
M = Matrix(QQ, eval('['+s.replace(' ',',')+']') )
print(M)
</code></pre>
https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59655#post-id-59655Thanks. I could able to do the uniformity of spacing using notepad.Tue, 09 Nov 2021 10:19:45 +0100https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59655#post-id-59655Comment by Max Alekseyev for <p>For example, you can parse such matrix printout as</p>
<pre><code>s = '[1 2 3] [4 5 6] [5 6 7]'
M = Matrix(QQ, eval('['+s.replace(' ',',')+']') )
print(M)
</code></pre>
https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59652#post-id-59652It's easy to make spacing uniform by replacing two spaces with one until all such paired spaces are gone.Tue, 09 Nov 2021 03:31:48 +0100https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59652#post-id-59652Comment by dasdipayan9038 for <p>For example, you can parse such matrix printout as</p>
<pre><code>s = '[1 2 3] [4 5 6] [5 6 7]'
M = Matrix(QQ, eval('['+s.replace(' ',',')+']') )
print(M)
</code></pre>
https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59651#post-id-59651For a large matrix (say of order 1000 and elements in range(2^15)), this space can be tricky. For instance, there are non-uniform spaces. Thus it might be a little difficult just to use replace space with a comma to define the matrix.
Is there any way [2 4 6] can be treated as a vector [2,4,6] in sage?Tue, 09 Nov 2021 01:48:26 +0100https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59651#post-id-59651Comment by Max Alekseyev for <p>For example, you can parse such matrix printout as</p>
<pre><code>s = '[1 2 3] [4 5 6] [5 6 7]'
M = Matrix(QQ, eval('['+s.replace(' ',',')+']') )
print(M)
</code></pre>
https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59649#post-id-59649load file's content to a string and proceed as above.Tue, 09 Nov 2021 00:36:42 +0100https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59649#post-id-59649Comment by dasdipayan9038 for <p>For example, you can parse such matrix printout as</p>
<pre><code>s = '[1 2 3] [4 5 6] [5 6 7]'
M = Matrix(QQ, eval('['+s.replace(' ',',')+']') )
print(M)
</code></pre>
https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59648#post-id-59648Thank you very much. But how should I use it from a file?Tue, 09 Nov 2021 00:35:29 +0100https://ask.sagemath.org/question/59646/how-to-use-the-output-matrix-as-an-input-in-sage/?comment=59648#post-id-59648