Ask Your Question

Minimal determinant of a matrix with varied entries

asked 2019-02-08 11:05:15 +0200

LukeC93 gravatar image

updated 2019-02-08 11:17:00 +0200

I would like Sage to tell me to minimal value of the determinant of a matrix when a vary some entries over a set range.

For example, say with the 4x4 matrix A (sorry for weird layout, I can't get the Latex code to work properly?):

0 1 i j

1 0 1 k

0 1 0 1

0 0 1 0

I'd like to know, for ${i,j,k} \in {0, \pm1}$, what the smallest non-trivial determinant is, and for which combination(s) of $i,j,k$ this is for.

I'm still pretty new to Sage, so I'm a bit unsure of how to do this effectively, if it is at all possible to do this?

Side note: I know I could of course write

sage A.determinant()

and this would give me the value of the determinant in terms of $i,j,k$. And in this example that would likely be much easier.

The idea is that I'd like to do this with larger matrices (that aren't as nice as this), more variables, etc. It would obviously be nicer to have have Sage simply compute the smallest determinant and give the corresponding values for the variables than me have to plug it in.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-02-08 11:40:14 +0200

rburing gravatar image

updated 2019-02-08 11:46:15 +0200

Do you mean something like this?

min_det = infinity
min_params = []
import itertools
for (i,j,k) in itertools.product([0,-1,1], repeat=3):
    M = Matrix(ZZ, [[0,1,i,j], [1,0,1,k], [0,1,0,1], [0,0,1,0]])
    M_det = M.determinant()
    if M_det == min_det:
    elif M_det != 0 and M_det < min_det:
        min_det = M_det
        min_params = [(i,j,k)]
print 'Minimal nontrivial determinant:', min_det
print 'Sets of parameters with this determinant:', min_params


Minimal nontrivial determinant: 1
Sets of parameters with this determinant: [(0, 0, 0), (0, 0, -1), (0, 0, 1), (-1, 0, 0), (-1, 0, -1), (-1, 0, 1), (1, 0, 0), (1, 0, -1), (1, 0, 1)]
edit flag offensive delete link more


Yes, that's very much what I'm after - thank you! Couple of questions so I can check I understand the code:

Why do we set min_det = infinity What does this do / mean exactly?

Say I wanted to see the minimal value which was larger than a set number (say 1); presumably I would change the zero in this part of the code to a 1:

elif M_det != 0 and M_det < min_det:

(I know the matrix I've used isn't the best example for checking that!)

LukeC93 gravatar imageLukeC93 ( 2019-02-08 12:02:35 +0200 )edit

The variable min_det contains the minimal determinant found so far. It should be initialized to something large enough so that it will be overwritten immediately. To find the minimal determinant larger than $x$ you should include this condition in the conditional, i.e. elif M_det != 0 and M_det > x and M_det < min_det:. If $x \geqslant 0$ then the first condition can be omitted: elif M_det > x and M_det < min_det:.

rburing gravatar imagerburing ( 2019-02-08 13:21:05 +0200 )edit

Amazing; that makes sense and works perfectly for what I'm after. Thank you again!

LukeC93 gravatar imageLukeC93 ( 2019-02-08 14:19:20 +0200 )edit

You're welcome!

rburing gravatar imagerburing ( 2019-02-08 14:24:26 +0200 )edit

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


Asked: 2019-02-08 11:05:15 +0200

Seen: 259 times

Last updated: Feb 08 '19