ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 13 Feb 2019 02:09:27 -0600Mapping 2-dimensional matrix index to a listhttp://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/Hi all,
I have a following matrix:
mat = matrix([[1,5,7],[3,10,12],[0,5,3]])
[ 1 5 7]
[ 3 10 12]
[ 0 5 3]
I got 3 parts from the matrix:
A = mat[[0,1,2],[0]]
[1]
[3]
[0]
B = mat[[1,2],[1,2]]
[10 12]
[ 5 3]
C = mat[[0],[1,2]]
[5 7]
To know that these 3 parts are not overlapping to each other, I have an idea to map the original matrix to 1-dimension array as IDs for each cell:
tt = copy(mat)
row = mat.nrows()
col = mat.ncols()
for x in range(row):
for y in range(col):
tt[x,y] = x*col+y
sage: tt
[0 1 2]
[3 4 5]
[6 7 8]
Then again I go over all coordinate that I took for A, B and C to collect these mapped ID.
Regarding to A,
cells_A = []
for i in range(0, 3):
for j in range(0, 1):
cells_A.append(tt[i, j])
sage: cells_A
[0, 3, 6]
Regarding to B:
cells_B = []
for i in range(1, 3):
for j in range(1, 3):
cells_B.append(tt[i, j])
sage: cells_B
[4, 5, 7, 8]
Regarding to C, similarly we have:
sage: cells_C = [1,2]
If size of union from these 3 cells is equal to the total size of A, B, and C, then I conclude no overlapping among them.
area = sum([A.nrows()*A.ncols(), B.nrows()*B.ncols(), C.nrows()*C.ncols()])
if len(set.union(*[set(cells_A), set(cells_B), set(cells_C)])) == area:
print("No overlapping parts")
However, this way requires lots of work and **SLOW**. Is there any already SageMath's Matrix feature supporting some steps above, especially a way to improve/avoid mapping index?Tue, 12 Feb 2019 18:54:13 -0600http://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/Comment by rburing for <p>Hi all,</p>
<p>I have a following matrix:</p>
<pre><code>mat = matrix([[1,5,7],[3,10,12],[0,5,3]])
[ 1 5 7]
[ 3 10 12]
[ 0 5 3]
</code></pre>
<p>I got 3 parts from the matrix:</p>
<pre><code>A = mat[[0,1,2],[0]]
[1]
[3]
[0]
B = mat[[1,2],[1,2]]
[10 12]
[ 5 3]
C = mat[[0],[1,2]]
[5 7]
</code></pre>
<p>To know that these 3 parts are not overlapping to each other, I have an idea to map the original matrix to 1-dimension array as IDs for each cell: </p>
<pre><code>tt = copy(mat)
row = mat.nrows()
col = mat.ncols()
for x in range(row):
for y in range(col):
tt[x,y] = x*col+y
sage: tt
[0 1 2]
[3 4 5]
[6 7 8]
</code></pre>
<p>Then again I go over all coordinate that I took for A, B and C to collect these mapped ID.
Regarding to A,</p>
<pre><code>cells_A = []
for i in range(0, 3):
for j in range(0, 1):
cells_A.append(tt[i, j])
sage: cells_A
[0, 3, 6]
</code></pre>
<p>Regarding to B:</p>
<pre><code>cells_B = []
for i in range(1, 3):
for j in range(1, 3):
cells_B.append(tt[i, j])
sage: cells_B
[4, 5, 7, 8]
</code></pre>
<p>Regarding to C, similarly we have:</p>
<pre><code>sage: cells_C = [1,2]
</code></pre>
<p>If size of union from these 3 cells is equal to the total size of A, B, and C, then I conclude no overlapping among them.</p>
<pre><code>area = sum([A.nrows()*A.ncols(), B.nrows()*B.ncols(), C.nrows()*C.ncols()])
if len(set.union(*[set(cells_A), set(cells_B), set(cells_C)])) == area:
print("No overlapping parts")
</code></pre>
<p>However, this way requires lots of work and <strong>SLOW</strong>. Is there any already SageMath's Matrix feature supporting some steps above, especially a way to improve/avoid mapping index?</p>
http://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/?comment=45439#post-id-45439My question was whether an *individual* part is always "connected", so what you said about shapes answers it in the positive. So you want to know whether a bunch of rectangles overlap. https://stackoverflow.com/questions/30307168/given-a-set-of-rectangles-do-any-overlap https://stackoverflow.com/questions/40622016/efficient-way-to-find-overlapping-of-n-rectangles/40673354Wed, 13 Feb 2019 02:09:27 -0600http://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/?comment=45439#post-id-45439Comment by imnvsh for <p>Hi all,</p>
<p>I have a following matrix:</p>
<pre><code>mat = matrix([[1,5,7],[3,10,12],[0,5,3]])
[ 1 5 7]
[ 3 10 12]
[ 0 5 3]
</code></pre>
<p>I got 3 parts from the matrix:</p>
<pre><code>A = mat[[0,1,2],[0]]
[1]
[3]
[0]
B = mat[[1,2],[1,2]]
[10 12]
[ 5 3]
C = mat[[0],[1,2]]
[5 7]
</code></pre>
<p>To know that these 3 parts are not overlapping to each other, I have an idea to map the original matrix to 1-dimension array as IDs for each cell: </p>
<pre><code>tt = copy(mat)
row = mat.nrows()
col = mat.ncols()
for x in range(row):
for y in range(col):
tt[x,y] = x*col+y
sage: tt
[0 1 2]
[3 4 5]
[6 7 8]
</code></pre>
<p>Then again I go over all coordinate that I took for A, B and C to collect these mapped ID.
Regarding to A,</p>
<pre><code>cells_A = []
for i in range(0, 3):
for j in range(0, 1):
cells_A.append(tt[i, j])
sage: cells_A
[0, 3, 6]
</code></pre>
<p>Regarding to B:</p>
<pre><code>cells_B = []
for i in range(1, 3):
for j in range(1, 3):
cells_B.append(tt[i, j])
sage: cells_B
[4, 5, 7, 8]
</code></pre>
<p>Regarding to C, similarly we have:</p>
<pre><code>sage: cells_C = [1,2]
</code></pre>
<p>If size of union from these 3 cells is equal to the total size of A, B, and C, then I conclude no overlapping among them.</p>
<pre><code>area = sum([A.nrows()*A.ncols(), B.nrows()*B.ncols(), C.nrows()*C.ncols()])
if len(set.union(*[set(cells_A), set(cells_B), set(cells_C)])) == area:
print("No overlapping parts")
</code></pre>
<p>However, this way requires lots of work and <strong>SLOW</strong>. Is there any already SageMath's Matrix feature supporting some steps above, especially a way to improve/avoid mapping index?</p>
http://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/?comment=45438#post-id-45438**Are the individual parts always blocks without gaps in them?**
No, the parts could be either blocks with gaps or block without gaps.
For more example:
(E1) The 1st and 3rd rows could be chosen as parts.
(E2) 3 parts: the 1st column, the top-right cell, and the bottom-right cell. The rest is not chosen.
Note:
a) Parts could be the choice that does not the whole matrix as Example (E2)
b) Parts' shapes are only square or rectangle, e.g no L-shape, /-shape, \-shape, #-shape, or +-shape and so on.Wed, 13 Feb 2019 01:44:34 -0600http://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/?comment=45438#post-id-45438Comment by rburing for <p>Hi all,</p>
<p>I have a following matrix:</p>
<pre><code>mat = matrix([[1,5,7],[3,10,12],[0,5,3]])
[ 1 5 7]
[ 3 10 12]
[ 0 5 3]
</code></pre>
<p>I got 3 parts from the matrix:</p>
<pre><code>A = mat[[0,1,2],[0]]
[1]
[3]
[0]
B = mat[[1,2],[1,2]]
[10 12]
[ 5 3]
C = mat[[0],[1,2]]
[5 7]
</code></pre>
<p>To know that these 3 parts are not overlapping to each other, I have an idea to map the original matrix to 1-dimension array as IDs for each cell: </p>
<pre><code>tt = copy(mat)
row = mat.nrows()
col = mat.ncols()
for x in range(row):
for y in range(col):
tt[x,y] = x*col+y
sage: tt
[0 1 2]
[3 4 5]
[6 7 8]
</code></pre>
<p>Then again I go over all coordinate that I took for A, B and C to collect these mapped ID.
Regarding to A,</p>
<pre><code>cells_A = []
for i in range(0, 3):
for j in range(0, 1):
cells_A.append(tt[i, j])
sage: cells_A
[0, 3, 6]
</code></pre>
<p>Regarding to B:</p>
<pre><code>cells_B = []
for i in range(1, 3):
for j in range(1, 3):
cells_B.append(tt[i, j])
sage: cells_B
[4, 5, 7, 8]
</code></pre>
<p>Regarding to C, similarly we have:</p>
<pre><code>sage: cells_C = [1,2]
</code></pre>
<p>If size of union from these 3 cells is equal to the total size of A, B, and C, then I conclude no overlapping among them.</p>
<pre><code>area = sum([A.nrows()*A.ncols(), B.nrows()*B.ncols(), C.nrows()*C.ncols()])
if len(set.union(*[set(cells_A), set(cells_B), set(cells_C)])) == area:
print("No overlapping parts")
</code></pre>
<p>However, this way requires lots of work and <strong>SLOW</strong>. Is there any already SageMath's Matrix feature supporting some steps above, especially a way to improve/avoid mapping index?</p>
http://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/?comment=45437#post-id-45437Are the individual parts always blocks without gaps in them? Or can a single part also be e.g. the first and the third row?Wed, 13 Feb 2019 01:22:47 -0600http://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/?comment=45437#post-id-45437