Ask Your Question

How are list of matrices printed by sage?

asked 2013-02-24 05:03:22 -0500

anto gravatar image

updated 2013-02-24 05:10:28 -0500

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 flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2013-02-25 06:57:18 -0500

updated 2013-02-25 07:21:04 -0500

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

edit flag offensive delete link more

answered 2013-02-24 23:59:31 -0500

Jason Grout gravatar image

This ticket makes that printing facility more general:

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

edit flag offensive delete link more


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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-02-24 05:03:22 -0500

Seen: 332 times

Last updated: Feb 25 '13