Ask Your Question

# Revision history [back]

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


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 ...