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.Tue, 21 May 2013 15:24:34 +0200Pulling the index of an entry of a matrixhttps://ask.sagemath.org/question/10144/pulling-the-index-of-an-entry-of-a-matrix/Let $M$ be some matrix, then I am looking for how to find out which entries of $M$ make the statement `M[i,j] == 25` true. Specifically, this is the code I'm using to generate the matrix, called `deg`:
deg = matrix(ZZ, 15)
A = lambda i: WeylCharacterRing("A{0}".format(i), style = "coroots")
for i in range(1,15):
fw = A(i).fundamental_weights()
for j in range(1,len(fw)):
deg[i,j] = A(i)(fw[j]).degree()
print deg
It works great, but now instead of printing deg and finding the entries manually, I would like to be able to store which entries `[i,j]` of `deg` are equal to 25 in some list, and then print that list, which would ideally look something like:
list(tfentry)
{ (2,3), (4,4) }
etc.
Any tips on where to look or help files to examine?
Thanks very much.
Tue, 21 May 2013 12:18:11 +0200https://ask.sagemath.org/question/10144/pulling-the-index-of-an-entry-of-a-matrix/Answer by niles for <p>Let $M$ be some matrix, then I am looking for how to find out which entries of $M$ make the statement <code>M[i,j] == 25</code> true. Specifically, this is the code I'm using to generate the matrix, called <code>deg</code>:</p>
<pre><code>deg = matrix(ZZ, 15)
A = lambda i: WeylCharacterRing("A{0}".format(i), style = "coroots")
for i in range(1,15):
fw = A(i).fundamental_weights()
for j in range(1,len(fw)):
deg[i,j] = A(i)(fw[j]).degree()
print deg
</code></pre>
<p>It works great, but now instead of printing deg and finding the entries manually, I would like to be able to store which entries <code>[i,j]</code> of <code>deg</code> are equal to 25 in some list, and then print that list, which would ideally look something like:</p>
<pre><code>list(tfentry)
{ (2,3), (4,4) }
</code></pre>
<p>etc. </p>
<p>Any tips on where to look or help files to examine? </p>
<p>Thanks very much. </p>
https://ask.sagemath.org/question/10144/pulling-the-index-of-an-entry-of-a-matrix/?answer=14956#post-id-14956One way you could do this is to use a conditional list comprehension; these take the form
[x for x in list if <condition>]
And since you will be iterating over a matrix, a *double* list comprehension is in order; these take the form
[x for b in a for x in b]
where `a` is the "outer list", and `b` is the "inner list". And since you want to know the indices and the entries together, we'll use the [`enumerate`](http://stackoverflow.com/questions/10777271/python-using-enumerate-inside-list-comprehension) function. So here's a conditional double list comprehension which returns `(i,j)` such that `deg(i,j) == 15`:
sage: [(i,j) for (i,deg_i) in enumerate(deg) for (j,deg_ij) in enumerate(deg_i) if deg_ij == 15]
[(5, 2), (5, 4), (14, 1)]
Another Sage-specific data type that might interest you is [sets](http://www.sagemath.org/doc/reference/structure/sage/sets/set.html) -- these are like lists, but automatically condense duplicate entries. For example, the set of values in the matrix `deg` can be given by
sage: deg_values = set([x for row in deg for x in row])
sage: len(deg_values)
52
Tue, 21 May 2013 13:44:30 +0200https://ask.sagemath.org/question/10144/pulling-the-index-of-an-entry-of-a-matrix/?answer=14956#post-id-14956Comment by JoshIzzard for <p>One way you could do this is to use a conditional list comprehension; these take the form</p>
<pre><code>[x for x in list if <condition>]
</code></pre>
<p>And since you will be iterating over a matrix, a <em>double</em> list comprehension is in order; these take the form</p>
<pre><code>[x for b in a for x in b]
</code></pre>
<p>where <code>a</code> is the "outer list", and <code>b</code> is the "inner list". And since you want to know the indices and the entries together, we'll use the <a href="http://stackoverflow.com/questions/10777271/python-using-enumerate-inside-list-comprehension"><code>enumerate</code></a> function. So here's a conditional double list comprehension which returns <code>(i,j)</code> such that <code>deg(i,j) == 15</code>:</p>
<pre><code>sage: [(i,j) for (i,deg_i) in enumerate(deg) for (j,deg_ij) in enumerate(deg_i) if deg_ij == 15]
[(5, 2), (5, 4), (14, 1)]
</code></pre>
<p>Another Sage-specific data type that might interest you is <a href="http://www.sagemath.org/doc/reference/structure/sage/sets/set.html">sets</a> -- these are like lists, but automatically condense duplicate entries. For example, the set of values in the matrix <code>deg</code> can be given by</p>
<pre><code>sage: deg_values = set([x for row in deg for x in row])
sage: len(deg_values)
52
</code></pre>
https://ask.sagemath.org/question/10144/pulling-the-index-of-an-entry-of-a-matrix/?comment=17664#post-id-17664Thanks very much for the help @niles, this should work well. Tue, 21 May 2013 15:24:34 +0200https://ask.sagemath.org/question/10144/pulling-the-index-of-an-entry-of-a-matrix/?comment=17664#post-id-17664