How to find all the sub-square matrices of an 4x4 matrix and all their determinants

1. I need to find all the sub-square matrices of a $4 \times 4$ matrix.
2. Then find the determinant of all $70$ of them.

I was directed to Sage but I know little about it. A simple program would be much appreciated.

For example, the matrix below:

$$2\quad 3\quad 1\quad 1$$ $$1\quad 2\quad 3\quad 1$$ $$1\quad 1\quad 2\quad 3$$ $$3\quad 1\quad 1\quad 2$$

Thanks to anyone who can help.

edit retag close merge delete

Note: if you already figured out how to input a matrix, you would save potential answerers some time by putting that code in your question, ready to copy-paste.

( 2018-02-16 00:05:03 +0200 )edit

Sort by ยป oldest newest most voted

A possible solution illustrating slicing for submatrices of a given matrix is as follows:

A = matrix( ZZ, 4, 4, [ 2,3,1,1, 1,2,3,1, 1,1,2,3, 3,1,1,2 ] )
print "The matrix A is:\n%s" % A

parts = sorted( [ tuple(S) for S in Set( [0,1,2,3] ).subsets() if S ]
, key=lambda S: (len(S), S) )
for S in parts:
for T in parts:
if len(S) != len(T):
continue
print ( "A[ %s , %s ].det() = %s"
% ( S, T, A.matrix_from_rows_and_columns(S, T).det() ) )


The results are:

The matrix A is:
[2 3 1 1]
[1 2 3 1]
[1 1 2 3]
[3 1 1 2]
A[ (0,) , (0,) ].det() = 2
A[ (0,) , (1,) ].det() = 3
A[ (0,) , (2,) ].det() = 1
A[ (0,) , (3,) ].det() = 1
A[ (1,) , (0,) ].det() = 1
A[ (1,) , (1,) ].det() = 2
A[ (1,) , (2,) ].det() = 3
A[ (1,) , (3,) ].det() = 1
A[ (2,) , (0,) ].det() = 1
A[ (2,) , (1,) ].det() = 1
A[ (2,) , (2,) ].det() = 2
A[ (2,) , (3,) ].det() = 3
A[ (3,) , (0,) ].det() = 3
A[ (3,) , (1,) ].det() = 1
A[ (3,) , (2,) ].det() = 1
A[ (3,) , (3,) ].det() = 2
A[ (0, 1) , (0, 1) ].det() = 1
A[ (0, 1) , (0, 2) ].det() = 5
A[ (0, 1) , (0, 3) ].det() = 1
A[ (0, 1) , (1, 2) ].det() = 7
A[ (0, 1) , (1, 3) ].det() = 1
A[ (0, 1) , (2, 3) ].det() = -2
A[ (0, 2) , (0, 1) ].det() = -1
A[ (0, 2) , (0, 2) ].det() = 3
A[ (0, 2) , (0, 3) ].det() = 5
A[ (0, 2) , (1, 2) ].det() = 5
A[ (0, 2) , (1, 3) ].det() = 8
A[ (0, 2) , (2, 3) ].det() = 1
A[ (0, 3) , (0, 1) ].det() = -7
A[ (0, 3) , (0, 2) ].det() = -1
A[ (0, 3) , (0, 3) ].det() = 1
A[ (0, 3) , (1, 2) ].det() = 2
A[ (0, 3) , (1, 3) ].det() = 5
A[ (0, 3) , (2, 3) ].det() = 1
A[ (1, 2) , (0, 1) ].det() = -1
A[ (1, 2) , (0, 2) ].det() = -1
A[ (1, 2) , (0, 3) ].det() = 2
A[ (1, 2) , (1, 2) ].det() = 1
A[ (1, 2) , (1, 3) ].det() = 5
A[ (1, 2) , (2, 3) ].det() = 7
A[ (1, 3) , (0, 1) ].det() = -5
A[ (1, 3) , (0, 2) ].det() = -8
A[ (1, 3) , (0, 3) ].det() = -1
A[ (1, 3) , (1, 2) ].det() = -1
A[ (1, 3) , (1, 3) ].det() = 3
A[ (1, 3) , (2, 3) ].det() = 5
A[ (2, 3) , (0, 1) ].det() = -2
A[ (2, 3) , (0, 2) ].det() = -5
A[ (2, 3) , (0, 3) ].det() = -7
A[ (2, 3) , (1, 2) ].det() = -1
A[ (2, 3) , (1, 3) ].det() = -1
A[ (2, 3) , (2, 3) ].det() = 1
A[ (0, 1, 2) , (0, 1, 2) ].det() = 4
A[ (0, 1, 2) , (0, 1, 3) ].det() = 3
A[ (0, 1, 2) , (0, 2, 3) ].det() = 11
A[ (0, 1, 2) , (1, 2, 3) ].det() = 17
A[ (0, 1, 3) , (0, 1, 2) ].det() = 17
A[ (0, 1, 3) , (0, 1, 3) ].det() = 4
A[ (0, 1, 3) , (0, 2, 3) ].det() = 3
A[ (0, 1, 3) , (1, 2, 3) ].det() = 11
A[ (0, 2, 3) , (0, 1, 2) ].det() = 11
A[ (0, 2, 3) , (0, 1, 3) ].det() = 17
A[ (0, 2, 3) , (0, 2, 3) ].det() = 4
A[ (0, 2, 3) , (1, 2, 3) ].det() = 3
A[ (1, 2, 3) , (0, 1, 2) ].det() = 3
A[ (1, 2, 3) , (0, 1, 3) ].det() = 11
A[ (1, 2, 3) , (0, 2, 3) ].det() = 17
A[ (1, 2, 3) , (1, 2, 3) ].det() = 4
A[ (0, 1, 2, 3) , (0, 1, 2, 3) ].det() = -35


And we can copy+paste from each of the above lines the determinant computation to convince ourselves that the corresponding relation is true, for instance:

sage: A[ (0, 1, 3) , (0, 1, 2) ].det()
17


where the submatrix is...

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

more

Perfect! Many thanks.

( 2018-02-16 05:37:57 +0200 )edit

To get a square sub-matrix from a square matrix, you just need to delete an equal number of rows and columns.

Define the matrix. For example (starting from a copy-paste of the entries of the matrix...):

sage: m = matrix(ZZ, 4, [ZZ(a) for a in '2311123111233112'])
sage: m
[2 3 1 1]
[1 2 3 1]
[1 1 2 3]
[3 1 1 2]


Explore the available methods using dot and the tab key:

sage: m.<TAB>


Notice the methods delete_rows and delete_columns.

sage: m.delete_rows?
sage: m.delete_columns?


To enumerate subsets of size 2 of a set S, we can use Subsets(S, k=2).

Combining all this, write a loop to iterate over all possible numbers of rows and columns to delete.

sage: r = range(4)
sage: for k in [0 .. 4]:
....:     for i in Subsets(r, k=k):
....:         for j in Subsets(r, k=k):
....:             a = m.delete_rows(i).delete_columns(j)
....:             print a
....:             print det(a)
....:

more

Thank you!

( 2018-02-16 05:38:19 +0200 )edit

To complement previous answers in a "don't reinvent the wheel" way, let me point to the following two methods:

• matrix_from_rows_and_columns if you want to extract submatrices, see this documentation page
• minors if you are only interesed in the determinants (you will have to iterate over all sizes), see this documentation page
more