# 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?

edit retag close merge delete

Sort by » oldest newest most voted

Yes, 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].

more

This ticket makes that printing facility more general: http://trac.sagemath.org/sage_trac/ti...

It's not in Sage yet, but I think it would make your objective much easier to obtain.

more

More importantly, how does it work currently? If it is not printing the string obtained from repr() then what is it printing?

( 2013-02-25 04:34:55 -0500 )edit