ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 06 Apr 2013 21:05:58 -0500Convert a text file into a matrixhttp://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/I have a file with a list of elements inside the square of a Quadratic field, i.e. `QQ(sqrt[13])^2`. The file looks like `[[2 + sqrt[13],4 - 2*sqrt[13]],[2,sqrt[13]],...]`. I want to import this file into a single matrix over the field, in the previous example `F.<a> = QuadraticField(13)`. Currently, I read everything in as a string then convert to the matrix, but there must be an easier way to do this. I am somewhat new to python programming in general.Tue, 02 Apr 2013 05:42:35 -0500http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/Answer by vdelecroix for <p>I have a file with a list of elements inside the square of a Quadratic field, i.e. <code>QQ(sqrt[13])^2</code>. The file looks like <code>[[2 + sqrt[13],4 - 2*sqrt[13]],[2,sqrt[13]],...]</code>. I want to import this file into a single matrix over the field, in the previous example <code>F.<a> = QuadraticField(13)</code>. Currently, I read everything in as a string then convert to the matrix, but there must be an easier way to do this. I am somewhat new to python programming in general.</p>
http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/?answer=14731#post-id-14731There is no direct way to do that. Nevertheless, you may use the command sage_eval to convert your string to a list of lists.
sage: my_string = '[[sqrt(2),sqrt(3)], [sqrt(5),sqrt(7)]]'
sage: matrix(sage_eval(my_string))
[sqrt(2) sqrt(3)]
[sqrt(5) sqrt(7)]Thu, 04 Apr 2013 01:21:51 -0500http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/?answer=14731#post-id-14731Comment by jmcshan1 for <p>There is no direct way to do that. Nevertheless, you may use the command sage_eval to convert your string to a list of lists.</p>
<pre><code>sage: my_string = '[[sqrt(2),sqrt(3)], [sqrt(5),sqrt(7)]]'
sage: matrix(sage_eval(my_string))
[sqrt(2) sqrt(3)]
[sqrt(5) sqrt(7)]
</code></pre>
http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/?comment=17942#post-id-17942This does not seem to work with variable names, such as
sage: my_string = '[[x, 3 - x],[x, 3+x]]'
Is there a way to make sage_eval work with these variables?Thu, 04 Apr 2013 05:33:10 -0500http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/?comment=17942#post-id-17942Comment by jmcshan1 for <p>There is no direct way to do that. Nevertheless, you may use the command sage_eval to convert your string to a list of lists.</p>
<pre><code>sage: my_string = '[[sqrt(2),sqrt(3)], [sqrt(5),sqrt(7)]]'
sage: matrix(sage_eval(my_string))
[sqrt(2) sqrt(3)]
[sqrt(5) sqrt(7)]
</code></pre>
http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/?comment=17941#post-id-17941Nevermind, I got it:
sage_eval(my_string, locals={'x':x})Thu, 04 Apr 2013 05:42:36 -0500http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/?comment=17941#post-id-17941Answer by Eviatar Bach for <p>I have a file with a list of elements inside the square of a Quadratic field, i.e. <code>QQ(sqrt[13])^2</code>. The file looks like <code>[[2 + sqrt[13],4 - 2*sqrt[13]],[2,sqrt[13]],...]</code>. I want to import this file into a single matrix over the field, in the previous example <code>F.<a> = QuadraticField(13)</code>. Currently, I read everything in as a string then convert to the matrix, but there must be an easier way to do this. I am somewhat new to python programming in general.</p>
http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/?answer=14751#post-id-14751Alternatively, an uglier but safer method (preventing possible evaluation of malicious code),
my_string = '[[sqrt(2),sqrt(3)], [sqrt(5),sqrt(7)]]'
matrix(filter(None, [map(SR, filter(lambda s:not s.strip() == '', l)) for l in map(lambda x:x.split(','), my_string.replace('[', '').split(']'))]))
Replace `SR` with whatever ring/field you want to coerce the entries into.Sat, 06 Apr 2013 21:05:58 -0500http://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/?answer=14751#post-id-14751