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.Mon, 25 Feb 2013 13:57:18 +0100How are list of matrices printed by sage?https://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/If I construct a list of matrices and let sage print it, the matrices are (correctly) aligned on the top:
sage: A=matrix([[1,2],[3,4]])
sage: [A,A]
[
[1 2] [1 2]
[3 4], [3 4]
]
Howewer, this is not the output of the __repr__ function of a list: compare:
sage: print [A,A].__repr__()
[[1 2]
[3 4], [1 2]
[3 4]]
I would like to construct an object whose output is on more than one lines (like a matrix), and I would like a list of these objects to be printed aligned on the top (like with the matrices in the first example above). How can I do that?
Sun, 24 Feb 2013 12:03:22 +0100https://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/Answer by Bétréma for <p>If I construct a list of matrices and let sage print it, the matrices are (correctly) aligned on the top:</p>
<pre><code>sage: A=matrix([[1,2],[3,4]])
sage: [A,A]
[
[1 2] [1 2]
[3 4], [3 4]
]
</code></pre>
<p>Howewer, this is not the output of the __repr__ function of a list: compare:</p>
<pre><code>sage: print [A,A].__repr__()
[[1 2]
[3 4], [1 2]
[3 4]]
</code></pre>
<p>I would like to construct an object whose output is on more than one lines (like a matrix), and I would like a list of these objects to be printed aligned on the top (like with the matrices in the first example above). How can I do that?</p>
https://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/?answer=14597#post-id-14597Yes, in interactive Python sessions, output uses `displayhook` instead of `repr`:
sage: A=matrix([[1,2],[3,4]])
sage: u=[A,A]
sage: repr(u)
'[[1 2]\n[3 4], [1 2]\n[3 4]]'
sage: import sys
sage: sys.displayhook(u)
[
[1 2] [1 2]
[3 4], [3 4]
]
Sage defines it's own module `sage.misc.displayhook`. Imho that's a dead end, because math formatting is very complex, and the right tool is LaTeX, very well integrated in Sage and Sage Notebook. I suggest you write a method `_latex_(self)` for your objects, then lists of these objects will be correctly formatted by LaTeX. The *string*
'[' + repr(A) + ',' + repr(B) + ']'
is not a valid representation for [A,B] if there are newlines inside repr(A) and repr(B), while
'\left[' + latex(A) + ',' + latex(B) + '\right]'
is always a valid LaTeX description of [A,B].
Mon, 25 Feb 2013 13:57:18 +0100https://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/?answer=14597#post-id-14597Answer by Jason Grout for <p>If I construct a list of matrices and let sage print it, the matrices are (correctly) aligned on the top:</p>
<pre><code>sage: A=matrix([[1,2],[3,4]])
sage: [A,A]
[
[1 2] [1 2]
[3 4], [3 4]
]
</code></pre>
<p>Howewer, this is not the output of the __repr__ function of a list: compare:</p>
<pre><code>sage: print [A,A].__repr__()
[[1 2]
[3 4], [1 2]
[3 4]]
</code></pre>
<p>I would like to construct an object whose output is on more than one lines (like a matrix), and I would like a list of these objects to be printed aligned on the top (like with the matrices in the first example above). How can I do that?</p>
https://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/?answer=14596#post-id-14596This ticket makes that printing facility more general: http://trac.sagemath.org/sage_trac/ticket/14040
It's not in Sage yet, but I think it would make your objective much easier to obtain.Mon, 25 Feb 2013 06:59:31 +0100https://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/?answer=14596#post-id-14596Comment by ppurka for <p>This ticket makes that printing facility more general: <a href="http://trac.sagemath.org/sage_trac/ticket/14040">http://trac.sagemath.org/sage_trac/ti...</a></p>
<p>It's not in Sage yet, but I think it would make your objective much easier to obtain.</p>
https://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/?comment=18162#post-id-18162More importantly, how does it work currently? If it is not printing the string obtained from `repr()` then what is it printing?Mon, 25 Feb 2013 11:34:55 +0100https://ask.sagemath.org/question/9837/how-are-list-of-matrices-printed-by-sage/?comment=18162#post-id-18162