Ask Your Question

Revision history [back]

There might be a better way, but you can use Python's list comprehensions:

sage: M = Matrix([[1,2,3],[0,4,5],[0,0,6]])
sage: Z = zero_matrix(ZZ, 3)
sage: M.list()
[1, 2, 3, 0, 4, 5, 0, 0, 6]
sage: zip(M.list(), Z.list())
[(1, 0), (2, 0), (3, 0), (0, 0), (4, 0), (5, 0), (0, 0), (0, 0), (6, 0)]

So here is a one-liner to get something like what you want:

sage: Matrix(3, 3, [0 if x==y else 1 for (x,y) in zip(M.list(), Z.list())])
[1 1 1]
[0 1 1]
[0 0 1]

It's harder to get a matrix with entries True or False.