How well print a list of matrices ?

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

Sort by » oldest newest most voted

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.

more

Seems to not be possible anymore (in SageMath 7.2)

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

( 2019-05-20 00:23:51 +0100 )edit