First time here? Check out the FAQ!

Ask Your Question
4

How are list of matrices printed by sage?

asked 12 years ago

anto gravatar image

updated 12 years ago

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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 12 years ago

updated 12 years ago

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

Preview: (hide)
link
2

answered 12 years ago

Jason Grout gravatar image

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.

Preview: (hide)
link

Comments

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 ( 12 years ago )

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 12 years ago

Seen: 1,293 times

Last updated: Feb 25 '13