ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 12 Feb 2019 18:54:13 -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?imnvshTue, 12 Feb 2019 18:54:13 -0600http://ask.sagemath.org/question/45436/create matrix as having a subset of columns from another matrixhttp://ask.sagemath.org/question/24035/create-matrix-as-having-a-subset-of-columns-from-another-matrix/ I need to get a new matrix generated by selecting a subset of columns from another matrix, given a list (or tuple) of column indices.
The following is the code I am working on (there is a bit more than just the attempt to create a new matrix, but might be interesting for you to have some context).
A = matrix(QQ,[
[2,1,4,-1,2],
[1,-1,5,1,1],
[-1,2,-7,0,1],
[2,-1,8,-1,2]
])
print "A\n",A
print "A rref\n",A.rref()
p = A.pivots()
print "A pivots",p
with the following output:
A
[ 2 1 4 -1 2]
[ 1 -1 5 1 1]
[-1 2 -7 0 1]
[ 2 -1 8 -1 2]
A rref
[ 1 0 3 0 0]
[ 0 1 -2 0 0]
[ 0 0 0 1 0]
[ 0 0 0 0 1]
A pivots (0, 1, 3, 4)
Now I expected to find easily a method from `matrix` objects which allowed to construct a new matrix with a subset of columns by just giving the tuple `p` as parameter, but could not find anything like that.
Any ideas on how to solve this *elegantly* in a sage-friendly way? (avoiding `for` loops and excess code)
thanks!stablumFri, 05 Sep 2014 14:52:40 -0500http://ask.sagemath.org/question/24035/