# How to construct matrix space over the set {+1, -1, 0} ?

I want to construct a set of matrices with entry either +1, -1 or 0. How to do this either matrix way or using digraph way, that is, if I consider set of digraphs over n vertices, then how do I make the edges have weights either +1, -1 or 0?

edit retag close merge delete

I do not understand the question. Matrix entries and edge labels can be assigned to any numbers including -1, 0, +1.

Actually, what I want to do is construct the set of all matrices of given order n whose entries are either +1, -1 or 0. I know that the following: MS = MatrixSpace(GF(2),n,n) given me a space of all matrices with entries 1 or 0, but here 1+1=0, whereas I want the addition to be done over the integer field. In terms of digraphs, is there a way to restrict the weights to only +1, -1 or 0?

Sort by » oldest newest most voted There is no built-in way to construct such a set of matrices, but you can construct the set of integer lists of length n^2 with entries 0, 1, 2. Then you can subtract 1 from each entry to get entries -1, 0, 1, and then you can form a matrix from them:

sage: n = 3
sage: L = IntegerListsLex(length=n**2, max_part=2)
sage: len(L)
19683
sage: L[:6]
[[2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 0],
[2, 2, 2, 2, 2, 2, 2, 1, 2],
[2, 2, 2, 2, 2, 2, 2, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 1, 0]]
sage: [matrix(n, n, [b-1 for b in a]) for a in L[:6]]
[
[1 1 1]  [1 1 1]  [ 1  1  1]  [1 1 1]  [1 1 1]  [ 1  1  1]
[1 1 1]  [1 1 1]  [ 1  1  1]  [1 1 1]  [1 1 1]  [ 1  1  1]
[1 1 1], [1 1 0], [ 1  1 -1], [1 0 1], [1 0 0], [ 1  0 -1]
]

sage: M = [matrix(3, 3, [b-1 for b in a]) for a in L]
sage: len(M)
19683


Using M = (matrix(...)) with parentheses instead of square brackets is faster and more memory efficient, because it doesn't create the whole list at once. In particular, if you want to loop over the set, don't construct the whole list, but do something like this:

sage: for a in L:
....:    mat = matrix(3, 3, [b-1 for b in a])
....:    ... do stuff with mat ...

more To get all matrices over $\{ -1, 0, +1\}$ one can construct matrix space over GF(3) and then change ring of each matrix to ZZ and subtract 1 from each element:

for T in MatrixSpace(GF(3),2,2):
M = T.change_ring(ZZ).apply_map(lambda x: x-1)
print(M,'\n')

more