# Submatrix of a given matrix by deleting some rows and columns(For my case 2 rows and columns).

I have a matrix A of order $n\times n$. Now I need another matrix B whose (i,j)th entry is $det A(i,j)$, where $det A(i,j)$ denote the determinant of the sub matrix of A formed by deleting $i^{th}$ and $j^{th}$ rows and columns. I'm unable to generate the submatrices $A(i,j)$ for every element at a time.

edit retag close merge delete

Sort by » oldest newest most voted

Let us fix a matrix $A$, so that the answer can be recovered every time. Since big coefficients should not occur on such a post, let us work over F = GF(5) first. We start with the matrix:

F = GF(5) A = matrix( F, 5, 5, [ 1 + k^2 + n^3 for k in F for n in F ] )

This is:

sage: A
[1 2 0 0 2]
[2 3 1 1 3]
[0 1 4 4 1]
[0 1 4 4 1]
[2 3 1 1 3]


Now, if the (pythonically numbered) rows $1,2$, and the columns $0,4$ have to be deleted, we have to extract (complementarily) the rows $0,3,4$, and the columns $1,2,3$. This is done for instance via:

sage: A[ [0,3,4] , [1,2,3] ]
[2 0 0]
[1 4 4]
[3 1 1]


For our purposes, it is better to use the form:

R = range( 5 )
A[ [ k for k in R if k not in [1,2] ] ,
[ k for k in R if k not in [0,4] ] ]


This is the same.

R = range( 5 )
S = set( R )
F = GF(5)
A = matrix( F, 5, 5, [ 1+k^2+n^3 for k in F for n in F ] )

B = matrix( F,
5, 5,
[ A[ list( S.difference( {j,k}) ) ,
list( S.difference( {j,k}) ) ].det()
for j in R
for k in R ] )


This is:

sage: B
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]

more

Thank you.

( 2017-07-19 12:46:32 +0200 )edit

You can create lists of rows and columns and use them to cut out a submatrix. E.g.

 sage: M=matrix([[1,2,3,4],[3,4,5,6],[6,7,8,9]])
sage: M[[0,2],[0,2]]
[1 3]
[6 8]

more

For large matrix it is not that easy to construct each submatrix one by one. I need $n^2+n$ submatrices for a matrix of order n. I'm looking for a general rule or a program.

( 2017-07-18 10:12:06 +0200 )edit

Well, but this is a straightforward Python loop to write. In the loop, create two lists, say, ro, co, using range(), remove i-th (resp. j-th) elements from it, call A[ro,co].det()/A[ro,ro].det() to compute the corr. determinant ratio, store it in B.

( 2017-07-18 11:30:22 +0200 )edit

Sorry, I could not get your point clearly. Can you please explain it with an example? Consider a random matrix of order 10 and from it find the submatrix by deleting 2nd and 7th rows and columns.

( 2017-07-18 15:30:36 +0200 )edit

A live example with nested for loops: http://sagecell.sagemath.org/?q=dzgfjy

( 2017-07-18 17:55:22 +0200 )edit

Thank you. But the following one simply serves that purpose.

N = random_matrix(ZZ, 10,10) M=N.delete_rows([2,7]) P=M.delete_columns([2,7]). show(N) show(P)

My problem is that I'm unable to generate all such submatrix at a time. What I want is to replace each element of the given matrix by the determinant of such submatrix of that particular element.

( 2017-07-19 06:42:35 +0200 )edit

I can find the submatrix for a particular element in the following way.

N = random_matrix(ZZ, 10,10) M=N.delete_rows([2,7]) P=M.delete_columns([2,7]) show(N) show(P)

My problem is that I'm unable to generate all such submatrix at a time. What I want is to replace each element of the given matrix by the determinant of such submatrix of that particular element. What I tried is the following one. But it shows some error.

L = random_matrix(ZZ, 10,10) R=matrix(QQ, 10) for i in range(10): for j in range(10): if i!=j: B(i,j)=L.delete_rows([i,j]) B(i,j)=B(i,j).delete_columns([i,j]) R[i,j]=B(i,j).det() show(R)

more