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, 10 Mar 2017 06:31:48 +0100all final(optimal) values of for loophttps://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/I have written a program to check which matrices in a given class maximizes the determinant. But my program gives me only one such matrix, I need all those matrices which attains the maximum determinant.
def xyz(n):
m=0
A=matrix(QQ,n)
for a in myfunction(n):
if a.det()>m:
m=a.det()
A=a
print AWed, 08 Mar 2017 08:20:07 +0100https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/Answer by Deepak Sarma for <p>I have written a program to check which matrices in a given class maximizes the determinant. But my program gives me only one such matrix, I need all those matrices which attains the maximum determinant. </p>
<pre><code> def xyz(n):
m=0
A=matrix(QQ,n)
for a in myfunction(n):
if a.det()>m:
m=a.det()
A=a
print A
</code></pre>
https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?answer=36861#post-id-36861I have solved the problem with the following modification. But comparatively this takes a lot of time for larger $n$ as the same loop runs twice. Can we do it by running the loop only once?
def xyz(n):
m=max([a.det() for a in myfunction(n)])
for A in myfunction(n):
if A.det()==m:
print AWed, 08 Mar 2017 08:31:19 +0100https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?answer=36861#post-id-36861Answer by tmonteil for <p>I have written a program to check which matrices in a given class maximizes the determinant. But my program gives me only one such matrix, I need all those matrices which attains the maximum determinant. </p>
<pre><code> def xyz(n):
m=0
A=matrix(QQ,n)
for a in myfunction(n):
if a.det()>m:
m=a.det()
A=a
print A
</code></pre>
https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?answer=36869#post-id-36869Instead of storing the best matrix in a variable `a`, you can store all best matrices in a list (or a set), and flush the list when a better matrix is found (and update the value of the best determinant). It will look something like:
def xyz(n):
L = []
max_det = -infinity
for A in myfunction(n):
d = A.det()
if d == max_det:
L.append(A)
elif d > max_det:
L = [A]
max_det = d
return L
Also let me suggest you to read about the difference between `print` and `return`.Wed, 08 Mar 2017 22:00:50 +0100https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?answer=36869#post-id-36869Comment by John Palmieri for <p>Instead of storing the best matrix in a variable <code>a</code>, you can store all best matrices in a list (or a set), and flush the list when a better matrix is found (and update the value of the best determinant). It will look something like:</p>
<pre><code>def xyz(n):
L = []
max_det = -infinity
for A in myfunction(n):
d = A.det()
if d == max_det:
L.append(A)
elif d > max_det:
L = [A]
max_det = d
return L
</code></pre>
<p>Also let me suggest you to read about the difference between <code>print</code> and <code>return</code>.</p>
https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?comment=36871#post-id-36871We must have answered this at the same time.Thu, 09 Mar 2017 01:19:11 +0100https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?comment=36871#post-id-36871Answer by John Palmieri for <p>I have written a program to check which matrices in a given class maximizes the determinant. But my program gives me only one such matrix, I need all those matrices which attains the maximum determinant. </p>
<pre><code> def xyz(n):
m=0
A=matrix(QQ,n)
for a in myfunction(n):
if a.det()>m:
m=a.det()
A=a
print A
</code></pre>
https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?answer=36868#post-id-36868How about something like this:
def xyz(n):
m = -Infinity # or some number that you know is less than the max
matrices = [] # list of matrices with max determinant
for A in myfunction(n):
d = A.det()
if d > m:
matrices = [A]
m = d
elif d == m:
matrices.append(A)
return matrices
Wed, 08 Mar 2017 21:52:55 +0100https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?answer=36868#post-id-36868Comment by Deepak Sarma for <p>How about something like this:</p>
<pre><code>def xyz(n):
m = -Infinity # or some number that you know is less than the max
matrices = [] # list of matrices with max determinant
for A in myfunction(n):
d = A.det()
if d > m:
matrices = [A]
m = d
elif d == m:
matrices.append(A)
return matrices
</code></pre>
https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?comment=36894#post-id-36894Thank you.Fri, 10 Mar 2017 06:31:48 +0100https://ask.sagemath.org/question/36860/all-finaloptimal-values-of-for-loop/?comment=36894#post-id-36894