How do I extract submatrices from a matrix?

3,144 views
Skip to first unread message

didier deshommes

unread,
Apr 23, 2007, 9:03:49 PM4/23/07
to sage-support
Hi there,
How do I make submatrices (or minors, or block submatrices) in SAGE? I
have a 5x5 matrix
{{{
[ 0 1 0 0 0]
[ 0 0 0 0 1]
[ 0 0 0 1 0]
[ 5/3 -2 -1 2/3 5/3]
[-17/3 1 4 4/3 1/3]
}}}

and I would like to extract from it the first block 2x2 submatrix, ie
[0 1]
[0 0]

Any ideas on how to do this?

didier

David Joyner

unread,
Apr 23, 2007, 9:52:33 PM4/23/07
to sage-s...@googlegroups.com

From the reference manual:


matrix_from_rows_and_columns( )

Return the matrix constructed from self from the given rows and columns.

sage: M = MatrixSpace(Integers(8),3,3)
sage: A = M(range(9)); A
[0 1 2]
[3 4 5]
[6 7 0]
sage: A.matrix_from_rows_and_columns([1], [0,2])
[3 5]
sage: A.matrix_from_rows_and_columns([1,2], [1,2])
[4 5]
[7 0]

Note that row and column indices can be reordered or repeated:

sage: A.matrix_from_rows_and_columns([2,1], [2,1])
[0 7]
[5 4]

For example here we take from row 1 columns 2 then 0 twice, and do
this 3 times.

sage: A.matrix_from_rows_and_columns([1,1,1],[2,0,0])
[5 3 3]
[5 3 3]
[5 3 3]


>
> didier
>
>
> >
>

didier deshommes

unread,
Apr 24, 2007, 12:49:41 AM4/24/07
to sage-s...@googlegroups.com
On 4/23/07, David Joyner <wdjo...@gmail.com> wrote:
>
> On 4/23/07, didier deshommes <dfde...@gmail.com> wrote:

> matrix_from_rows_and_columns( )

Thanks, I knew there was something I was missing. Any chance of
aliasing this function to "submatrix()"? Because
"matrix_from_rows_and_columns()" feels clunky...

didier

William Stein

unread,
Apr 24, 2007, 1:06:53 AM4/24/07
to sage-s...@googlegroups.com
On 4/23/07, didier deshommes <dfde...@gmail.com> wrote:
> On 4/23/07, David Joyner <wdjo...@gmail.com> wrote:
> > On 4/23/07, didier deshommes <dfde...@gmail.com> wrote:
> > matrix_from_rows_and_columns( )
>
> Thanks, I knew there was something I was missing. Any chance of
> aliasing this function to "submatrix()"? Because
> "matrix_from_rows_and_columns()" feels clunky...

No, because it is not a submatrix in general. For example:

sage: a = random_matrix(QQ,3,4); a
[-1/2 1 0 0]
[ -1 -1 2 2]
[-1/2 1/2 -1 0]
sage: a.matrix_from_columns([0,0,0,1,3])
[-1/2 -1/2 -1/2 1 0]
[ -1 -1 -1 -1 2]
[-1/2 -1/2 -1/2 1/2 0]

Let me know if this function is too slow of a base ring that you're
using -- it can be optimized.

William

Kyle Schalm

unread,
Apr 24, 2007, 3:10:34 AM4/24/07
to sage-s...@googlegroups.com

i know William has responded, but i agree with this. some time ago i was
looking for this same feature and my first inclination was to look for a
function called "submatrix". it actually took me a long time to discover
the function i was looking for. perhaps we could add submatrix and
implement it in terms of matrix_from_rows_and_columns. better yet, is it
possible to support multiple slices?

M = matrix(...)
M[1:2, 1:3]

that would be very nice. if that's possible, what is the notation for the
"full slice" -- perhaps something like

M[:,1:3]

?

i also remember thinking it would be useful to have a "minor" function,
but i don't remember what i wanted it for now. this could also be
implemented easily in terms of matrix_from_rows_and_columns (or
submatrix).

-kyle

Reply all
Reply to author
Forward
0 new messages