ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 04 Aug 2021 21:11:44 +0200Combining sets with a matrixhttps://ask.sagemath.org/question/58191/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]Sun, 01 Aug 2021 18:00:04 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/Comment by FabianG for <p>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: </p>
<pre><code>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
</code></pre>
<p>What I would like to do is to mount a matrix where the number of columns represents the sets <code>C</code> (16 columns) and the number of rows represents R. The rule for each entrance it will be: </p>
<pre><code>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
</code></pre>
<p>I would like some output as it follows below:</p>
<pre><code> (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]
</code></pre>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58206#post-id-58206`all` tests whether all elements in a sequence evaluate to `True`Mon, 02 Aug 2021 12:56:58 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58206#post-id-58206Comment by FabianG for <p>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: </p>
<pre><code>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
</code></pre>
<p>What I would like to do is to mount a matrix where the number of columns represents the sets <code>C</code> (16 columns) and the number of rows represents R. The rule for each entrance it will be: </p>
<pre><code>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
</code></pre>
<p>I would like some output as it follows below:</p>
<pre><code> (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]
</code></pre>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58205#post-id-58205With `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.Mon, 02 Aug 2021 12:55:07 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58205#post-id-58205Answer by John Palmieri for <p>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: </p>
<pre><code>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
</code></pre>
<p>What I would like to do is to mount a matrix where the number of columns represents the sets <code>C</code> (16 columns) and the number of rows represents R. The rule for each entrance it will be: </p>
<pre><code>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
</code></pre>
<p>I would like some output as it follows below:</p>
<pre><code> (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]
</code></pre>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?answer=58232#post-id-58232You 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?Tue, 03 Aug 2021 20:50:24 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?answer=58232#post-id-58232Comment by phcosta for <p>You have almost everything, just missing the interface to matrices. I put the following into a file <code>mat.py</code> and within Sage, I evaluated <code>%attach mat.py</code>.</p>
<pre><code>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)
</code></pre>
<p>Try <code>list(enumerate(R))</code> to see what <code>enumerate</code> does, if it's not familiar. <code>d</code> is a dictionary with entries keyed by pairs <code>(row, col)</code>, and <code>matrix(d)</code> produces the corresponding matrix, with zeroes for any unspecified entries.</p>
<p>Note that with your code, <code>R</code> has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace <code>R</code> with just <code>P1</code>, then here is the transpose of that matrix, with rows and columns labeled:</p>
<pre><code> (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]
</code></pre>
<p>How does this differ from what you're looking for?</p>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58271#post-id-58271It works here. Thank you one more time.Wed, 04 Aug 2021 21:11:44 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58271#post-id-58271Comment by John Palmieri for <p>You have almost everything, just missing the interface to matrices. I put the following into a file <code>mat.py</code> and within Sage, I evaluated <code>%attach mat.py</code>.</p>
<pre><code>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)
</code></pre>
<p>Try <code>list(enumerate(R))</code> to see what <code>enumerate</code> does, if it's not familiar. <code>d</code> is a dictionary with entries keyed by pairs <code>(row, col)</code>, and <code>matrix(d)</code> produces the corresponding matrix, with zeroes for any unspecified entries.</p>
<p>Note that with your code, <code>R</code> has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace <code>R</code> with just <code>P1</code>, then here is the transpose of that matrix, with rows and columns labeled:</p>
<pre><code> (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]
</code></pre>
<p>How does this differ from what you're looking for?</p>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58269#post-id-58269I'm not quite sure what you want, but maybe you can change the `elif` to something like: `else` followed by: `if (some condition) then d[(row, col)] = 1` and `elif (some other condition) then d[(row+len(P1), col)] = 1` and `elif (some third condition) then d[(row+2*len(P1), col)] = 1`.Wed, 04 Aug 2021 18:05:54 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58269#post-id-58269Comment by phcosta for <p>You have almost everything, just missing the interface to matrices. I put the following into a file <code>mat.py</code> and within Sage, I evaluated <code>%attach mat.py</code>.</p>
<pre><code>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)
</code></pre>
<p>Try <code>list(enumerate(R))</code> to see what <code>enumerate</code> does, if it's not familiar. <code>d</code> is a dictionary with entries keyed by pairs <code>(row, col)</code>, and <code>matrix(d)</code> produces the corresponding matrix, with zeroes for any unspecified entries.</p>
<p>Note that with your code, <code>R</code> has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace <code>R</code> with just <code>P1</code>, then here is the transpose of that matrix, with rows and columns labeled:</p>
<pre><code> (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]
</code></pre>
<p>How does this differ from what you're looking for?</p>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58268#post-id-58268Thank you very much. I guess, I got the idea. I put an example of the output that I'm looking for in my question. Probably, I need to define 3 matrices using your idea and combine them.Wed, 04 Aug 2021 17:12:19 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58268#post-id-58268Comment by John Palmieri for <p>You have almost everything, just missing the interface to matrices. I put the following into a file <code>mat.py</code> and within Sage, I evaluated <code>%attach mat.py</code>.</p>
<pre><code>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)
</code></pre>
<p>Try <code>list(enumerate(R))</code> to see what <code>enumerate</code> does, if it's not familiar. <code>d</code> is a dictionary with entries keyed by pairs <code>(row, col)</code>, and <code>matrix(d)</code> produces the corresponding matrix, with zeroes for any unspecified entries.</p>
<p>Note that with your code, <code>R</code> has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace <code>R</code> with just <code>P1</code>, then here is the transpose of that matrix, with rows and columns labeled:</p>
<pre><code> (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]
</code></pre>
<p>How does this differ from what you're looking for?</p>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58250#post-id-58250I'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.Wed, 04 Aug 2021 07:28:49 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58250#post-id-58250Comment by phcosta for <p>You have almost everything, just missing the interface to matrices. I put the following into a file <code>mat.py</code> and within Sage, I evaluated <code>%attach mat.py</code>.</p>
<pre><code>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)
</code></pre>
<p>Try <code>list(enumerate(R))</code> to see what <code>enumerate</code> does, if it's not familiar. <code>d</code> is a dictionary with entries keyed by pairs <code>(row, col)</code>, and <code>matrix(d)</code> produces the corresponding matrix, with zeroes for any unspecified entries.</p>
<p>Note that with your code, <code>R</code> has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace <code>R</code> with just <code>P1</code>, then here is the transpose of that matrix, with rows and columns labeled:</p>
<pre><code> (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]
</code></pre>
<p>How does this differ from what you're looking for?</p>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58246#post-id-58246Thank 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.Wed, 04 Aug 2021 04:37:56 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58246#post-id-58246Comment by John Palmieri for <p>You have almost everything, just missing the interface to matrices. I put the following into a file <code>mat.py</code> and within Sage, I evaluated <code>%attach mat.py</code>.</p>
<pre><code>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)
</code></pre>
<p>Try <code>list(enumerate(R))</code> to see what <code>enumerate</code> does, if it's not familiar. <code>d</code> is a dictionary with entries keyed by pairs <code>(row, col)</code>, and <code>matrix(d)</code> produces the corresponding matrix, with zeroes for any unspecified entries.</p>
<p>Note that with your code, <code>R</code> has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace <code>R</code> with just <code>P1</code>, then here is the transpose of that matrix, with rows and columns labeled:</p>
<pre><code> (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]
</code></pre>
<p>How does this differ from what you're looking for?</p>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58240#post-id-58240You 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?Wed, 04 Aug 2021 02:48:38 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58240#post-id-58240Comment by phcosta for <p>You have almost everything, just missing the interface to matrices. I put the following into a file <code>mat.py</code> and within Sage, I evaluated <code>%attach mat.py</code>.</p>
<pre><code>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)
</code></pre>
<p>Try <code>list(enumerate(R))</code> to see what <code>enumerate</code> does, if it's not familiar. <code>d</code> is a dictionary with entries keyed by pairs <code>(row, col)</code>, and <code>matrix(d)</code> produces the corresponding matrix, with zeroes for any unspecified entries.</p>
<p>Note that with your code, <code>R</code> has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace <code>R</code> with just <code>P1</code>, then here is the transpose of that matrix, with rows and columns labeled:</p>
<pre><code> (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]
</code></pre>
<p>How does this differ from what you're looking for?</p>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58239#post-id-58239It 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.Wed, 04 Aug 2021 00:34:48 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58239#post-id-58239Comment by phcosta for <p>You have almost everything, just missing the interface to matrices. I put the following into a file <code>mat.py</code> and within Sage, I evaluated <code>%attach mat.py</code>.</p>
<pre><code>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)
</code></pre>
<p>Try <code>list(enumerate(R))</code> to see what <code>enumerate</code> does, if it's not familiar. <code>d</code> is a dictionary with entries keyed by pairs <code>(row, col)</code>, and <code>matrix(d)</code> produces the corresponding matrix, with zeroes for any unspecified entries.</p>
<p>Note that with your code, <code>R</code> has the same thing repeated three times, so the matrix has the same rows repeated three times over. If I replace <code>R</code> with just <code>P1</code>, then here is the transpose of that matrix, with rows and columns labeled:</p>
<pre><code> (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]
</code></pre>
<p>How does this differ from what you're looking for?</p>
https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58234#post-id-58234Thank you very much. :)Tue, 03 Aug 2021 21:47:22 +0200https://ask.sagemath.org/question/58191/combining-sets-with-a-matrix/?comment=58234#post-id-58234