ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 08 Feb 2019 14:24:26 +0100Minimal determinant of a matrix with varied entrieshttps://ask.sagemath.org/question/45367/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.Fri, 08 Feb 2019 11:05:15 +0100https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/Answer by rburing for <p>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.</p>
<p>For example, say with the 4x4 matrix A (sorry for weird layout, I can't get the Latex code to work properly?):</p>
<p>0 1 i j</p>
<p>1 0 1 k</p>
<p>0 1 0 1</p>
<p>0 0 1 0</p>
<p>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.</p>
<p>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?</p>
<p>Side note: I know I could of course write </p>
<pre><code>sage A.determinant()
</code></pre>
<p>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.</p>
<p>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.</p>
https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?answer=45368#post-id-45368Do 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)]Fri, 08 Feb 2019 11:40:14 +0100https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?answer=45368#post-id-45368Comment by LukeC93 for <p>Do you mean something like this?</p>
<pre><code>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
</code></pre>
<p>Output:</p>
<pre><code>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)]
</code></pre>
https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?comment=45369#post-id-45369Yes, 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!)Fri, 08 Feb 2019 12:02:35 +0100https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?comment=45369#post-id-45369Comment by rburing for <p>Do you mean something like this?</p>
<pre><code>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
</code></pre>
<p>Output:</p>
<pre><code>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)]
</code></pre>
https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?comment=45370#post-id-45370The 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:`.Fri, 08 Feb 2019 13:21:05 +0100https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?comment=45370#post-id-45370Comment by LukeC93 for <p>Do you mean something like this?</p>
<pre><code>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
</code></pre>
<p>Output:</p>
<pre><code>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)]
</code></pre>
https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?comment=45371#post-id-45371Amazing; that makes sense and works perfectly for what I'm after. Thank you again!Fri, 08 Feb 2019 14:19:20 +0100https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?comment=45371#post-id-45371Comment by rburing for <p>Do you mean something like this?</p>
<pre><code>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
</code></pre>
<p>Output:</p>
<pre><code>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)]
</code></pre>
https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?comment=45372#post-id-45372You're welcome!Fri, 08 Feb 2019 14:24:26 +0100https://ask.sagemath.org/question/45367/minimal-determinant-of-a-matrix-with-varied-entries/?comment=45372#post-id-45372