Ask Your Question

Revision history [back]

You have almost everything, just the interface to matrices. I put the following into a file mat.py and within Sage, I evaluated %attach mat.py.

M = FiniteEnumeratedSet({1, 2})
P0 = cartesian_product([M]*2) 
P1 = P0.list()
P2 = cartesian_product([M]*4)
P3 = [p for p in P2 if p[0] <= p[1]]
C = P1 + P3 #len(C) total columns
R = P1*3  #len(R) total rows

d = {}
for (row, pair) in enumerate(R):
    for (col, quad) in enumerate(C):
        if pair == quad:
            d[(row, col)] = -1
        elif len(quad) == 4 and (pair == (quad[0], quad[2]) or pair == (quad[1], quad[3])):
            d[(row, col)] = 1

m = matrix(d)

Try list(enumerate(R)) to see what enumerate does, if it's not familiar. d is a dictionary with entries keyed by pairs (row, col), and matrix(d) produces the corresponding matrix, with zeroes for any unspecified entries.

You have almost everything, just missing the interface to matrices. I put the following into a file mat.py and within Sage, I evaluated %attach mat.py.

M = FiniteEnumeratedSet({1, 2})
P0 = cartesian_product([M]*2) 
P1 = P0.list()
P2 = cartesian_product([M]*4)
P3 = [p for p in P2 if p[0] <= p[1]]
C = P1 + P3 #len(C) total columns
R = P1*3  #len(R) total rows

d = {}
for (row, pair) in enumerate(R):
    for (col, quad) in enumerate(C):
        if pair == quad:
            d[(row, col)] = -1
        elif len(quad) == 4 and (pair == (quad[0], quad[2]) or pair == (quad[1], quad[3])):
            d[(row, col)] = 1

m = matrix(d)

Try list(enumerate(R)) to see what enumerate does, if it's not familiar. d is a dictionary with entries keyed by pairs (row, col), and matrix(d) produces the corresponding matrix, with zeroes for any unspecified entries.

You have almost everything, just missing the interface to matrices. I put the following into a file mat.py and within Sage, I evaluated %attach mat.py.

M = FiniteEnumeratedSet({1, 2})
P0 = cartesian_product([M]*2) 
P1 = P0.list()
P2 = cartesian_product([M]*4)
P3 = [p for p in P2 if p[0] <= p[1]]
C = P1 + P3 #len(C) total columns
R = P1*3  #len(R) total rows

d = {}
for (row, pair) in enumerate(R):
    for (col, quad) in enumerate(C):
        if pair == quad:
            d[(row, col)] = -1
        elif len(quad) == 4 and (pair == (quad[0], quad[2]) or pair == (quad[1], quad[3])):
            d[(row, col)] = 1

m = matrix(d)

Try list(enumerate(R)) to see what enumerate does, if it's not familiar. d is a dictionary with entries keyed by pairs (row, col), and matrix(d) produces the corresponding matrix, with zeroes for any unspecified entries.

Note that with your code, R has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace R with just P1, then here is the transpose of that matrix, with rows and columns labeled:

              (1,1) (1,2) (2,1) (2,2)
(1, 1)        [-1     0     0     0]
(1, 2)        [ 0    -1     0     0]
(2, 1)        [ 0     0    -1     0]
(2, 2)        [ 0     0     0    -1]
(1, 1, 1, 1)  [ 1     0     0     0]
(1, 1, 1, 2)  [ 1     1     0     0]
(1, 1, 2, 1)  [ 1     1     0     0]
(1, 1, 2, 2)  [ 0     1     0     0]
(1, 2, 1, 1)  [ 1     0     1     0]
(1, 2, 1, 2)  [ 1     0     0     1]
(1, 2, 2, 1)  [ 0     1     1     0]
(1, 2, 2, 2)  [ 0     1     0     1]
(2, 2, 1, 1)  [ 0     0     1     0]
(2, 2, 1, 2)  [ 0     0     0     1]

How does this differ from what you're looking for?