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

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



B = mat[[1,2],[1,2]]
[10 12]
[ 5  3]
C = mat[,[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 reopen merge delete