First time here? Check out the FAQ!

Ask Your Question
1

Combining sets with a matrix

asked 3 years ago

phcosta gravatar image

updated 3 years ago

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]
Preview: (hide)

Comments

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.

FabianG gravatar imageFabianG ( 3 years ago )

all tests whether all elements in a sequence evaluate to True

FabianG gravatar imageFabianG ( 3 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 3 years ago

updated 3 years ago

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?

Preview: (hide)
link

Comments

Thank you very much. :)

phcosta gravatar imagephcosta ( 3 years ago )

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.

phcosta gravatar imagephcosta ( 3 years ago )

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?

John Palmieri gravatar imageJohn Palmieri ( 3 years ago )

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.

phcosta gravatar imagephcosta ( 3 years ago )

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.

John Palmieri gravatar imageJohn Palmieri ( 3 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 3 years ago

Seen: 319 times

Last updated: Aug 04 '21