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.Tue, 21 Nov 2017 18:30:58 +0100extraction of principal submatriceshttps://ask.sagemath.org/question/39645/extraction-of-principal-submatrices/ How to extract all principal submatrices of fixed order say k from a given square matrix of order n by n. please explain with one exampleSat, 18 Nov 2017 13:15:01 +0100https://ask.sagemath.org/question/39645/extraction-of-principal-submatrices/Answer by slelievre for <p>How to extract all principal submatrices of fixed order say k from a given square matrix of order n by n. please explain with one example</p>
https://ask.sagemath.org/question/39645/extraction-of-principal-submatrices/?answer=39646#post-id-39646The [submatrix section of Wikipedia's matrix
page](https://en.wikipedia.org/wiki/Matrix_(mathematics)#Submatrix)
mentions several possible definitions of principal submatrix of order `k`.
Given your question, you likely adopt the definition by which a principal
submatrix of order `k` is a `k` by `k` submatrix in which the set of row indices
that remain is the same as the set of column indices that remain.
So let us define `principal_submatrix` as a function that takes a matrix
and a set of indices to keep.
def principal_submatrix(m, s, sort=False):
"""
Return the submatrix of m corresponding to indices in s
INPUT:
- ``m`` -- a matrix
- ``s`` -- an iterable for the indices of rows and columns to keep
OUTPUT:
The submatrix of ``m`` using the indices in ``s``.
An optional argument specifies whether to sort the indices.
Note that duplicate indices will be kept.
"""
if sort:
s = sorted(s)
return m[s, s]
From there define `principal_submatrices` as a function that takes
a matrix and the number of indices to keep.
def principal_submatrices(m, k):
"""
Return the list of principal submatrices of m of order k
These are the submatrices of m (assumed to be a square matrix)
obtained by keeping k rows and columns, with the same indices.
"""
S = Subsets(range(m.ncols()), k)
return [principal_submatrix(m, s, sort=True) for s in S]
To illustrate these functions, let us use the following 4 by 4 matrix.
sage: m = matrix(ZZ, 4, range(4^2))
sage: m
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
Extract a specified principal submatrix:
sage: principal_submatrix(m, [0, 1, 3])
[ 0 1 3]
[ 4 5 7]
[12 13 15]
List all principal submatrices of each order:
sage: principal_submatrices(m, 0)
[[]]
sage: principal_submatrices(m, 1)
[[0], [5], [10], [15]]
sage: principal_submatrices(m, 2)
[
[0 1] [ 0 2] [ 0 3] [ 5 6] [ 5 7] [10 11]
[4 5], [ 8 10], [12 15], [ 9 10], [13 15], [14 15]
]
sage: principal_submatrices(m, 3)
[
[ 0 1 2] [ 0 1 3] [ 0 2 3] [ 5 6 7]
[ 4 5 6] [ 4 5 7] [ 8 10 11] [ 9 10 11]
[ 8 9 10], [12 13 15], [12 14 15], [13 14 15]
]
sage: principal_submatrices(m, 4)
[
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
]
Sat, 18 Nov 2017 17:20:29 +0100https://ask.sagemath.org/question/39645/extraction-of-principal-submatrices/?answer=39646#post-id-39646Comment by rewi for <p>The <a href="https://en.wikipedia.org/wiki/Matrix_(mathematics)#Submatrix">submatrix section of Wikipedia's matrix
page</a>
mentions several possible definitions of principal submatrix of order <code>k</code>.</p>
<p>Given your question, you likely adopt the definition by which a principal
submatrix of order <code>k</code> is a <code>k</code> by <code>k</code> submatrix in which the set of row indices
that remain is the same as the set of column indices that remain.</p>
<p>So let us define <code>principal_submatrix</code> as a function that takes a matrix
and a set of indices to keep.</p>
<pre><code>def principal_submatrix(m, s, sort=False):
"""
Return the submatrix of m corresponding to indices in s
INPUT:
- ``m`` -- a matrix
- ``s`` -- an iterable for the indices of rows and columns to keep
OUTPUT:
The submatrix of ``m`` using the indices in ``s``.
An optional argument specifies whether to sort the indices.
Note that duplicate indices will be kept.
"""
if sort:
s = sorted(s)
return m[s, s]
</code></pre>
<p>From there define <code>principal_submatrices</code> as a function that takes
a matrix and the number of indices to keep.</p>
<pre><code>def principal_submatrices(m, k):
"""
Return the list of principal submatrices of m of order k
These are the submatrices of m (assumed to be a square matrix)
obtained by keeping k rows and columns, with the same indices.
"""
S = Subsets(range(m.ncols()), k)
return [principal_submatrix(m, s, sort=True) for s in S]
</code></pre>
<p>To illustrate these functions, let us use the following 4 by 4 matrix.</p>
<pre><code>sage: m = matrix(ZZ, 4, range(4^2))
sage: m
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
</code></pre>
<p>Extract a specified principal submatrix:</p>
<pre><code>sage: principal_submatrix(m, [0, 1, 3])
[ 0 1 3]
[ 4 5 7]
[12 13 15]
</code></pre>
<p>List all principal submatrices of each order:</p>
<pre><code>sage: principal_submatrices(m, 0)
[[]]
sage: principal_submatrices(m, 1)
[[0], [5], [10], [15]]
sage: principal_submatrices(m, 2)
[
[0 1] [ 0 2] [ 0 3] [ 5 6] [ 5 7] [10 11]
[4 5], [ 8 10], [12 15], [ 9 10], [13 15], [14 15]
]
sage: principal_submatrices(m, 3)
[
[ 0 1 2] [ 0 1 3] [ 0 2 3] [ 5 6 7]
[ 4 5 6] [ 4 5 7] [ 8 10 11] [ 9 10 11]
[ 8 9 10], [12 13 15], [12 14 15], [13 14 15]
]
sage: principal_submatrices(m, 4)
[
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
]
</code></pre>
https://ask.sagemath.org/question/39645/extraction-of-principal-submatrices/?comment=39656#post-id-39656Thank you.Sun, 19 Nov 2017 06:41:20 +0100https://ask.sagemath.org/question/39645/extraction-of-principal-submatrices/?comment=39656#post-id-39656Comment by slelievre for <p>The <a href="https://en.wikipedia.org/wiki/Matrix_(mathematics)#Submatrix">submatrix section of Wikipedia's matrix
page</a>
mentions several possible definitions of principal submatrix of order <code>k</code>.</p>
<p>Given your question, you likely adopt the definition by which a principal
submatrix of order <code>k</code> is a <code>k</code> by <code>k</code> submatrix in which the set of row indices
that remain is the same as the set of column indices that remain.</p>
<p>So let us define <code>principal_submatrix</code> as a function that takes a matrix
and a set of indices to keep.</p>
<pre><code>def principal_submatrix(m, s, sort=False):
"""
Return the submatrix of m corresponding to indices in s
INPUT:
- ``m`` -- a matrix
- ``s`` -- an iterable for the indices of rows and columns to keep
OUTPUT:
The submatrix of ``m`` using the indices in ``s``.
An optional argument specifies whether to sort the indices.
Note that duplicate indices will be kept.
"""
if sort:
s = sorted(s)
return m[s, s]
</code></pre>
<p>From there define <code>principal_submatrices</code> as a function that takes
a matrix and the number of indices to keep.</p>
<pre><code>def principal_submatrices(m, k):
"""
Return the list of principal submatrices of m of order k
These are the submatrices of m (assumed to be a square matrix)
obtained by keeping k rows and columns, with the same indices.
"""
S = Subsets(range(m.ncols()), k)
return [principal_submatrix(m, s, sort=True) for s in S]
</code></pre>
<p>To illustrate these functions, let us use the following 4 by 4 matrix.</p>
<pre><code>sage: m = matrix(ZZ, 4, range(4^2))
sage: m
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
</code></pre>
<p>Extract a specified principal submatrix:</p>
<pre><code>sage: principal_submatrix(m, [0, 1, 3])
[ 0 1 3]
[ 4 5 7]
[12 13 15]
</code></pre>
<p>List all principal submatrices of each order:</p>
<pre><code>sage: principal_submatrices(m, 0)
[[]]
sage: principal_submatrices(m, 1)
[[0], [5], [10], [15]]
sage: principal_submatrices(m, 2)
[
[0 1] [ 0 2] [ 0 3] [ 5 6] [ 5 7] [10 11]
[4 5], [ 8 10], [12 15], [ 9 10], [13 15], [14 15]
]
sage: principal_submatrices(m, 3)
[
[ 0 1 2] [ 0 1 3] [ 0 2 3] [ 5 6 7]
[ 4 5 6] [ 4 5 7] [ 8 10 11] [ 9 10 11]
[ 8 9 10], [12 13 15], [12 14 15], [13 14 15]
]
sage: principal_submatrices(m, 4)
[
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
]
</code></pre>
https://ask.sagemath.org/question/39645/extraction-of-principal-submatrices/?comment=39708#post-id-39708If this answers your question, you can click the tick sign next to the answer; this will mark this answer as solving your question, and mark your question as solved.Tue, 21 Nov 2017 18:30:58 +0100https://ask.sagemath.org/question/39645/extraction-of-principal-submatrices/?comment=39708#post-id-39708