# Combining sets with a matrix

Hello, I hope everyone is doing fine. I try to do some combinations of sets, but I have no clue how. If anyone can point me in any direction, it will be great. Here is the problem:

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


What I would like to do is to mount a matrix where the number of columns represents the sets C (16 columns) and the number of rows represents R. The rule for each entrance it will be:

if (i,j) in R and (i,j) in C:
return -1
elif if (i,j) in R and ((i,u,j,v) in C or (u,i,v,j) in C):
return 1
else:
return 0


I would like some output as it follows below:

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

edit retag close merge delete

With matrix(ZZ, n)you get an n times n matrix over the integers filled with zeros. Then you can set the entries one by one by doing for loops over the column and row index.

( 2021-08-02 12:55:07 +0100 )edit

all tests whether all elements in a sequence evaluate to True

( 2021-08-02 12:56:58 +0100 )edit

Sort by » oldest newest most voted

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):
d[(row, col)] = -1
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?

more

Thank you very much. :)

( 2021-08-03 21:47:22 +0100 )edit

It is possible to force a change of rows? For example keep the elements (1,1,1,1) and (1,1,1,2) at the same row but (1,2,1,1) and (1,2,1,2) at another.

( 2021-08-04 00:34:48 +0100 )edit

You can set up a different dictionary with a key like (1,1,1,1) corresponding to row 3, and then also have (1,1,1,2) also correspond to row 3: d2 = {(1,1,1,1): 3, (1,1,1,2): 3, (1,2,1,1): 4, (1,2,1,2): 5, ...}. Then you can iterate over the list C of tuples t and access the row by d2[t]. (Maybe you mean column rather than row, I'm not sure.) Is that the sort of thing you mean?

( 2021-08-04 02:48:38 +0100 )edit

Thank you for your time. I'll try to explain better: I would like to put ones in such a way that if (i,j,k,l) and (i,j,u,v) in C they have the ones in the same line and zeros in any other places in the corresponding line. That means if they have in common the first two entries. For example:

(1,1,1,1) (1,1,1,2) (1,2,1,1) (1,2,1,2)

*** 1 *** *** 1 *** *** 0 *** *** 0 ****

*** 0 *** *** 0 *** *** 1 *** *** 1 ****

where *** here is just to adjust the space.

( 2021-08-04 04:37:56 +0100 )edit

I've put sample output in my answer (well, the transpose, because it was easier to label the rows and columns that way). Note that the rows for (1,1,1,1) and (1,1,1,2) start the same, because they both have (1,1) as entries 0 and 2, as your original code described. Can you explain what you want done differently? You can certainly modify the elif part of the code to achieve lots of different effects. If you describe the algorithm precisely, you'll probably have the code you want.

( 2021-08-04 07:28:49 +0100 )edit