Ask Your Question
1

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

asked 2018-02-15 13:24:16 -0600

Redbook1 gravatar image
  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 flag offensive close merge delete

Comments

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.

slelievre gravatar imageslelievre ( 2018-02-15 17:05:03 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
1

answered 2018-02-15 17:45:31 -0600

dan_fulea gravatar image

updated 2018-02-15 17:46:36 -0600

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]
edit flag offensive delete link more

Comments

Perfect! Many thanks.

Redbook1 gravatar imageRedbook1 ( 2018-02-15 22:37:57 -0600 )edit
1

answered 2018-02-15 17:22:05 -0600

slelievre gravatar image

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.

Read their documentation:

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)
....:
edit flag offensive delete link more

Comments

Thank you!

Redbook1 gravatar imageRedbook1 ( 2018-02-15 22:38:19 -0600 )edit
1

answered 2018-02-15 19:02:44 -0600

tmonteil gravatar image

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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-02-15 13:24:16 -0600

Seen: 148 times

Last updated: Feb 15