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.Mon, 03 Feb 2014 02:07:12 -0600Nice display of a list of "matrices"https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/In the context of graphs I have seen that it is possible to display in a very nice picture of all graphs in a certain list using for instance the following code:
sage: G = GraphQuery(display_cols=['graph6'], num_vertices=7, diameter=5)
sage: L = G.get_graphs_list()
sage: graphs_list.show_graphs(L)
The output one gets is the picture
![image description](http://www.iiia.csic.es/~fbou/picturelistgraphs.png)
I would like to get the same output display but this time using a diferent kind of objects (instead of graphs), namely matrices for the sake of this question. So, here is the question: suppose you have a list L of matrices, for example the one defined by
sage: L=[ MatrixSpace(ZZ,3).random_element() for i in [0..8]]
Is there some way to obtain a picture of the same kind than before except for replacing the graphs with the display of each one of the matrices in L?Thu, 30 Jan 2014 01:04:58 -0600https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/Answer by slelievre for <p>In the context of graphs I have seen that it is possible to display in a very nice picture of all graphs in a certain list using for instance the following code:</p>
<pre><code>sage: G = GraphQuery(display_cols=['graph6'], num_vertices=7, diameter=5)
sage: L = G.get_graphs_list()
sage: graphs_list.show_graphs(L)
</code></pre>
<p>The output one gets is the picture
<img alt="image description" src="http://www.iiia.csic.es/~fbou/picturelistgraphs.png"/></p>
<p>I would like to get the same output display but this time using a diferent kind of objects (instead of graphs), namely matrices for the sake of this question. So, here is the question: suppose you have a list L of matrices, for example the one defined by </p>
<pre><code>sage: L=[ MatrixSpace(ZZ,3).random_element() for i in [0..8]]
</code></pre>
<p>Is there some way to obtain a picture of the same kind than before except for replacing the graphs with the display of each one of the matrices in L?</p>
https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?answer=15990#post-id-15990You could define a function to produce a latex array (or tabular):
def matrix_array(L,ncols):
nl = '\n'
s = nl + r'\begin{array}{' + ('c' * ncols) + r'}' + nl
for k in xrange(len(L)):
if k%ncols != 0: s += '&' + nl
s += str(latex(L[k])) + nl
if ((k+1)%ncols == 0) or (k == len(L)):
s += r'\\' + nl
s += r'\end{array}' + nl
return LatexExpr(s)
and then view it:
sage: L = [MatrixSpace(ZZ,3).random_element() for i in [0..8]]
sage: view(matrix_array(L,ncols=3))
Thu, 30 Jan 2014 05:02:06 -0600https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?answer=15990#post-id-15990Comment by boumol for <p>You could define a function to produce a latex array (or tabular):</p>
<pre><code>def matrix_array(L,ncols):
nl = '\n'
s = nl + r'\begin{array}{' + ('c' * ncols) + r'}' + nl
for k in xrange(len(L)):
if k%ncols != 0: s += '&' + nl
s += str(latex(L[k])) + nl
if ((k+1)%ncols == 0) or (k == len(L)):
s += r'\\' + nl
s += r'\end{array}' + nl
return LatexExpr(s)
</code></pre>
<p>and then view it:</p>
<pre><code>sage: L = [MatrixSpace(ZZ,3).random_element() for i in [0..8]]
sage: view(matrix_array(L,ncols=3))
</code></pre>
https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?comment=16362#post-id-16362Thanks. Your answer has really helped me.Fri, 31 Jan 2014 04:20:28 -0600https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?comment=16362#post-id-16362Answer by ndomes for <p>In the context of graphs I have seen that it is possible to display in a very nice picture of all graphs in a certain list using for instance the following code:</p>
<pre><code>sage: G = GraphQuery(display_cols=['graph6'], num_vertices=7, diameter=5)
sage: L = G.get_graphs_list()
sage: graphs_list.show_graphs(L)
</code></pre>
<p>The output one gets is the picture
<img alt="image description" src="http://www.iiia.csic.es/~fbou/picturelistgraphs.png"/></p>
<p>I would like to get the same output display but this time using a diferent kind of objects (instead of graphs), namely matrices for the sake of this question. So, here is the question: suppose you have a list L of matrices, for example the one defined by </p>
<pre><code>sage: L=[ MatrixSpace(ZZ,3).random_element() for i in [0..8]]
</code></pre>
<p>Is there some way to obtain a picture of the same kind than before except for replacing the graphs with the display of each one of the matrices in L?</p>
https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?answer=15994#post-id-15994Just another suggestion:
L = [random_matrix(ZZ,2,2) for k in range(20)]
def list_of_lists(L,ncol):
return [L[k:k+ncol] for k in range(0,len(L),ncol)]
html.table(list_of_lists(L,5))Fri, 31 Jan 2014 21:47:07 -0600https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?answer=15994#post-id-15994Comment by ndomes for <p>Just another suggestion:</p>
<pre><code>L = [random_matrix(ZZ,2,2) for k in range(20)]
def list_of_lists(L,ncol):
return [L[k:k+ncol] for k in range(0,len(L),ncol)]
html.table(list_of_lists(L,5))
</code></pre>
https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?comment=16350#post-id-16350https://bitbucket.org/whuss/sws2tex/Mon, 03 Feb 2014 02:07:12 -0600https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?comment=16350#post-id-16350Comment by boumol for <p>Just another suggestion:</p>
<pre><code>L = [random_matrix(ZZ,2,2) for k in range(20)]
def list_of_lists(L,ncol):
return [L[k:k+ncol] for k in range(0,len(L),ncol)]
html.table(list_of_lists(L,5))
</code></pre>
https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?comment=16352#post-id-16352Thanks. Is there some easy way to get the output in pdf format (or in png, jpg)? I ask this because later I want to use this output inside a paper I am writing using LaTeX.Sun, 02 Feb 2014 21:07:02 -0600https://ask.sagemath.org/question/10960/nice-display-of-a-list-of-matrices/?comment=16352#post-id-16352