Ask Your Question
2

How well print a list of matrices ?

asked 2013-09-10 08:18:10 +0100

updated 2013-09-10 08:20:09 +0100

In a program, I use the function "print" for printing lists of matrices.
Unfortunately, the displaying is not as well as it could, see the following example :

sage: MM=[identity_matrix(QQ,3) for i in range(3)]
sage: MM
[
[1 0 0]  [1 0 0]  [1 0 0]
[0 1 0]  [0 1 0]  [0 1 0]
[0 0 1], [0 0 1], [0 0 1]
]
sage: print(MM)
[[1 0 0]
[0 1 0]
[0 0 1], [1 0 0]
[0 1 0]
[0 0 1], [1 0 0]
[0 1 0]
[0 0 1]]

Is there a way to print a list of matrices as the first above ?

(Perhaps by saving it on an external file, what would be the line code for that ?)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2013-09-10 08:33:02 +0100

tmonteil gravatar image

You can do:

sage: print sage.misc.displayhook.format_obj(MM)
[
[1 0 0]  [1 0 0]  [1 0 0]
[0 1 0]  [0 1 0]  [0 1 0]
[0 0 1], [0 0 1], [0 0 1]
]

You can also have a look at this question.

edit flag offensive delete link more

Comments

Seems to not be possible anymore (in SageMath 7.2)

Daniel Krenn gravatar imageDaniel Krenn ( 2016-08-06 16:57:47 +0100 )edit

@Daniel Krenn - try this instead

sage: l = [identity_matrix(QQ, 2) for _ in range(3)]
sage: from sage.repl.display.formatter import SagePlainTextFormatter
sage: f = SagePlainTextFormatter()
sage: f(l)
'[\n[1 0]  [1 0]  [1 0]\n[0 1], [0 1], [0 1]\n]'
sage: print(f(l))
[
[1 0]  [1 0]  [1 0]
[0 1], [0 1], [0 1]
]
slelievre gravatar imageslelievre ( 2019-05-20 00:23:51 +0100 )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

Stats

Asked: 2013-09-10 08:18:10 +0100

Seen: 766 times

Last updated: Sep 10 '13