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.Thu, 11 Feb 2016 16:48:26 +0100Define a big matrix in Sagehttps://ask.sagemath.org/question/32473/define-a-big-matrix-in-sage/Hi all,
I would like to define a big matrix E (130$\times$ 30) with entries being a list like E[11,1] = [x, [1,-2]];
E[14,1] = [x, [1,-1,-2,1]];
or 0.
These entries are stored in a separate file which is readable by Pari but not Sage.
How do I define it by Sage?Sat, 06 Feb 2016 20:47:22 +0100https://ask.sagemath.org/question/32473/define-a-big-matrix-in-sage/Comment by kcrisman for <p>Hi all, </p>
<p>I would like to define a big matrix E (130$\times$ 30) with entries being a list like E[11,1] = [x, [1,-2]];</p>
<p>E[14,1] = [x, [1,-1,-2,1]];
or 0.
These entries are stored in a separate file which is readable by Pari but not Sage. </p>
<p>How do I define it by Sage?</p>
https://ask.sagemath.org/question/32473/define-a-big-matrix-in-sage/?comment=32480#post-id-32480Can you make the info a dictionary, which is an easy way to define the matrix? http://doc.sagemath.org/html/en/reference/matrices/sage/matrix/constructor.htmlSun, 07 Feb 2016 00:46:36 +0100https://ask.sagemath.org/question/32473/define-a-big-matrix-in-sage/?comment=32480#post-id-32480Comment by Dianbin Bao for <p>Hi all, </p>
<p>I would like to define a big matrix E (130$\times$ 30) with entries being a list like E[11,1] = [x, [1,-2]];</p>
<p>E[14,1] = [x, [1,-1,-2,1]];
or 0.
These entries are stored in a separate file which is readable by Pari but not Sage. </p>
<p>How do I define it by Sage?</p>
https://ask.sagemath.org/question/32473/define-a-big-matrix-in-sage/?comment=32532#post-id-32532To people who has similar problems: One can unravel multidimensional list to one dimensional list and then input Sage.Thu, 11 Feb 2016 16:48:26 +0100https://ask.sagemath.org/question/32473/define-a-big-matrix-in-sage/?comment=32532#post-id-32532Answer by vdelecroix for <p>Hi all, </p>
<p>I would like to define a big matrix E (130$\times$ 30) with entries being a list like E[11,1] = [x, [1,-2]];</p>
<p>E[14,1] = [x, [1,-1,-2,1]];
or 0.
These entries are stored in a separate file which is readable by Pari but not Sage. </p>
<p>How do I define it by Sage?</p>
https://ask.sagemath.org/question/32473/define-a-big-matrix-in-sage/?answer=32495#post-id-32495A Sage matrix must have its entries being element of some ring. In other words you can not have a matrix of lists. However it is perfectly fine to have a 2d array (aka a "list of lists").
sage: l = [[1, [2,3]], [2, [5,4]]]
sage: print l
[[1, [2, 3]], [2, [5, 4]]]
To execute some code contained in a string you might use the "eval" or "sage_eval" command
sage: my_string = "[[1, [2,3]], [2, [5,4]]]"
sage: sage_eval(my_string) # run the Python code from my_string
[[1, [2, 3]], [2, [5, 4]]]
And to read the content of the file these are the standard Python commands [open/close](https://docs.python.org/2/library/stdtypes.html#bltin-file-objects)
sage: f = open("my_file.data") # open the file
sage: string = f.read() # read what is inside (result is a string)
sage: f.close() # close the fileMon, 08 Feb 2016 15:00:42 +0100https://ask.sagemath.org/question/32473/define-a-big-matrix-in-sage/?answer=32495#post-id-32495