ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 21 Jan 2013 20:41:59 -0600Selecting matrices with only certain entrieshttp://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/Hi all -
a slightly more general but less ambitious version of my previous (as yet unanswered) question:
http://ask.sagemath.org/question/2114/unitary-matrices-over-finite-fields
I set up a matrix space M (for simplicity say over a finite field F) and I have a (finite) subset S of F. I need to do a search through the elements of M which satisfy certain algebraic constraints; however I only wish to study matrices whose entries ALL lie in S.
How please do I restrict to such matrices?
Many thanks in advanceMon, 21 Jan 2013 09:00:53 -0600http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/Answer by fidbc for <p>Hi all - </p>
<p>a slightly more general but less ambitious version of my previous (as yet unanswered) question:
<a href="http://ask.sagemath.org/question/2114/unitary-matrices-over-finite-fields">http://ask.sagemath.org/question/2114...</a></p>
<p>I set up a matrix space M (for simplicity say over a finite field F) and I have a (finite) subset S of F. I need to do a search through the elements of M which satisfy certain algebraic constraints; however I only wish to study matrices whose entries ALL lie in S.</p>
<p>How please do I restrict to such matrices?</p>
<p>Many thanks in advance</p>
http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/?answer=14468#post-id-14468Python's `itertools` module might be useful.
Assuming `M` is the matrix space (`m` by `n` matrices over `F`) and `S` is the subset of `F`. The following loop might do the trick:
from itertools import product
for entries in product(S,repeat=m*n):
m = M( entries )Mon, 21 Jan 2013 10:48:17 -0600http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/?answer=14468#post-id-14468Comment by GaryMak for <p>Python's <code>itertools</code> module might be useful.</p>
<p>Assuming <code>M</code> is the matrix space (<code>m</code> by <code>n</code> matrices over <code>F</code>) and <code>S</code> is the subset of <code>F</code>. The following loop might do the trick:</p>
<pre><code>from itertools import product
for entries in product(S,repeat=m*n):
m = M( entries )
</code></pre>
http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/?comment=18357#post-id-18357Thank you - I didn't realise the product construction would work in this context!!Mon, 21 Jan 2013 20:41:31 -0600http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/?comment=18357#post-id-18357Answer by John Palmieri for <p>Hi all - </p>
<p>a slightly more general but less ambitious version of my previous (as yet unanswered) question:
<a href="http://ask.sagemath.org/question/2114/unitary-matrices-over-finite-fields">http://ask.sagemath.org/question/2114...</a></p>
<p>I set up a matrix space M (for simplicity say over a finite field F) and I have a (finite) subset S of F. I need to do a search through the elements of M which satisfy certain algebraic constraints; however I only wish to study matrices whose entries ALL lie in S.</p>
<p>How please do I restrict to such matrices?</p>
<p>Many thanks in advance</p>
http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/?answer=14483#post-id-14483fidelbc's answer is better than this one (because it's much faster), but here's more of a brute force technique, since it iterates through all of the elements of the matrix space `M`:
sage: M = MatrixSpace(GF(5), 3, 2)
sage: S = [GF(5)(_) for _ in 2,4] # S = {2,4}
sage: [a for a in M if set(a.list()).issubset(S)]
Note the speed difference:
sage: timeit('[a for a in M if set(a.list()).issubset(S)]')
5 loops, best of 3: 5.43 s per loop
versus
sage: from itertools import product
sage: m = []
sage: timeit('for entries in product(S, repeat=6): m.append(M(entries))')
125 loops, best of 3: 1.91 ms per loopMon, 21 Jan 2013 15:11:41 -0600http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/?answer=14483#post-id-14483Comment by GaryMak for <p>fidelbc's answer is better than this one (because it's much faster), but here's more of a brute force technique, since it iterates through all of the elements of the matrix space <code>M</code>:</p>
<pre><code>sage: M = MatrixSpace(GF(5), 3, 2)
sage: S = [GF(5)(_) for _ in 2,4] # S = {2,4}
sage: [a for a in M if set(a.list()).issubset(S)]
</code></pre>
<p>Note the speed difference:</p>
<pre><code>sage: timeit('[a for a in M if set(a.list()).issubset(S)]')
5 loops, best of 3: 5.43 s per loop
</code></pre>
<p>versus</p>
<pre><code>sage: from itertools import product
sage: m = []
sage: timeit('for entries in product(S, repeat=6): m.append(M(entries))')
125 loops, best of 3: 1.91 ms per loop
</code></pre>
http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/?comment=18356#post-id-18356Thanks - a useful alternative perspectiveMon, 21 Jan 2013 20:41:59 -0600http://ask.sagemath.org/question/9727/selecting-matrices-with-only-certain-entries/?comment=18356#post-id-18356