ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 21 Aug 2022 21:30:52 +0200How to construct matrix space over the set {+1, -1, 0} ?https://ask.sagemath.org/question/63711/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? Sun, 21 Aug 2022 12:15:41 +0200https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/Comment by the1diot for <p>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? </p>
https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/?comment=63714#post-id-63714Actually, 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?Sun, 21 Aug 2022 18:25:10 +0200https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/?comment=63714#post-id-63714Comment by Max Alekseyev for <p>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? </p>
https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/?comment=63713#post-id-63713I do not understand the question. Matrix entries and edge labels can be assigned to any numbers including -1, 0, +1.Sun, 21 Aug 2022 16:04:46 +0200https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/?comment=63713#post-id-63713Answer by Max Alekseyev for <p>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? </p>
https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/?answer=63716#post-id-63716To 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')Sun, 21 Aug 2022 21:30:52 +0200https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/?answer=63716#post-id-63716Answer by John Palmieri for <p>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? </p>
https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/?answer=63715#post-id-63715There 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 ...Sun, 21 Aug 2022 20:53:09 +0200https://ask.sagemath.org/question/63711/how-to-construct-matrix-space-over-the-set-1-1-0/?answer=63715#post-id-63715