ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 19 Jul 2017 12:46:32 +0200Submatrix of a given matrix by deleting some rows and columns(For my case 2 rows and columns).https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-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.Tue, 18 Jul 2017 06:58:26 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/Answer by dan_fulea for <p>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.</p>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?answer=38322#post-id-38322Let 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.
And now the answer to your question:
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]
Wed, 19 Jul 2017 12:12:48 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?answer=38322#post-id-38322Comment by Deepak Sarma for <p>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
<code>F = GF(5)</code> first. We start with the matrix:</p>
<p>F = GF(5)
A = matrix( F, 5, 5, [ 1 + k^2 + n^3 for k in F for n in F ] )</p>
<p>This is:</p>
<pre><code>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]
</code></pre>
<p>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:</p>
<pre><code>sage: A[ [0,3,4] , [1,2,3] ]
[2 0 0]
[1 4 4]
[3 1 1]
</code></pre>
<p>For our purposes, it is better to use the form:</p>
<pre><code>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] ] ]
</code></pre>
<p>This is the same.</p>
<p>And now the answer to your question:</p>
<pre><code>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 ] )
</code></pre>
<p>This is:</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38323#post-id-38323Thank you.Wed, 19 Jul 2017 12:46:32 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38323#post-id-38323Answer by Dima for <p>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.</p>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?answer=38293#post-id-38293You 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]
Tue, 18 Jul 2017 09:15:30 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?answer=38293#post-id-38293Comment by Deepak Sarma for <p>You can create lists of rows and columns and use them to cut out a submatrix. E.g.</p>
<pre><code> 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]
</code></pre>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38304#post-id-38304Sorry, 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.Tue, 18 Jul 2017 15:30:36 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38304#post-id-38304Comment by Deepak Sarma for <p>You can create lists of rows and columns and use them to cut out a submatrix. E.g.</p>
<pre><code> 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]
</code></pre>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38300#post-id-38300For 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.Tue, 18 Jul 2017 10:12:06 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38300#post-id-38300Comment by Dima for <p>You can create lists of rows and columns and use them to cut out a submatrix. E.g.</p>
<pre><code> 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]
</code></pre>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38302#post-id-38302Well, 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.Tue, 18 Jul 2017 11:30:22 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38302#post-id-38302Comment by ndomes for <p>You can create lists of rows and columns and use them to cut out a submatrix. E.g.</p>
<pre><code> 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]
</code></pre>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38310#post-id-38310A live example with nested for loops: [http://sagecell.sagemath.org/?q=dzgfjy](http://sagecell.sagemath.org/?q=dzgfjy)Tue, 18 Jul 2017 17:55:22 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38310#post-id-38310Comment by Deepak Sarma for <p>You can create lists of rows and columns and use them to cut out a submatrix. E.g.</p>
<pre><code> 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]
</code></pre>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38319#post-id-38319Thank 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.Wed, 19 Jul 2017 06:42:35 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?comment=38319#post-id-38319Answer by Deepak Sarma for <p>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.</p>
https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?answer=38320#post-id-38320I 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)Wed, 19 Jul 2017 06:53:57 +0200https://ask.sagemath.org/question/38292/submatrix-of-a-given-matrix-by-deleting-some-rows-and-columnsfor-my-case-2-rows-and-columns/?answer=38320#post-id-38320