Mapping 2-dimensional matrix index to a list [closed]

asked 2019-02-12 18:54:13 -0500

imnvsh gravatar image

updated 2019-02-12 18:54:49 -0500

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]]
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?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by imnvsh
close date 2019-02-13 05:46:02.994039


Are the individual parts always blocks without gaps in them? Or can a single part also be e.g. the first and the third row?

rburing gravatar imagerburing ( 2019-02-13 01:22:47 -0500 )edit

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.

imnvsh gravatar imageimnvsh ( 2019-02-13 01:44:34 -0500 )edit

My question was whether an individualpart 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.

rburing gravatar imagerburing ( 2019-02-13 02:09:27 -0500 )edit