# Minimal determinant of a matrix with varied entries

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 close merge delete

Sort by » oldest newest most voted 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:
min_params.append((i,j,k))
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


Output:

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)]

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!)

1

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:.

1

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