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]
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 doingfor
loops over the column and row index.all
tests whether all elements in a sequence evaluate toTrue