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 May 2015 03:39:01 +0200How can I get Sage to go over all possible maps between two sets?https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/ What I want to do is this : Say I take a graph $K_{n,n}$ and choose an ordering for each edge arbitrarily - say denote each edge as $(i,j)$ where $i$ is in the left partition and $j$ is in the right partition. I have a set of matrices $A = { A_1, A_2,...,A_k \}$. I want to iterate over all possible ways in which one could have assigned an A matrix to an edge of this graph.
- How does one do that? (..apart from writing a massive sequence of nested loops!..)
Sun, 03 May 2015 00:25:28 +0200https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/Answer by Nathann for <p>What I want to do is this : Say I take a graph $K_{n,n}$ and choose an ordering for each edge arbitrarily - say denote each edge as $(i,j)$ where $i$ is in the left partition and $j$ is in the right partition. I have a set of matrices $A = { A_1, A_2,...,A_k }$. I want to iterate over all possible ways in which one could have assigned an A matrix to an edge of this graph. </p>
<ul>
<li>How does one do that? (..apart from writing a massive sequence of nested loops!..) </li>
</ul>
https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/?answer=26708#post-id-26708 To list all functions from `A` to `B` you must iterate on all points of
`B^|A|`. This can be achieved with itertools' 'product' function:
def all_functions(A,B):
from itertools import product
B_to_the_A = product(B,repeat=len(A))
for p in B_to_the_A:
yield dict(zip(A,p))
sage: for f in all_functions("abc",[0,1]):
....: print f
{'a': 0, 'c': 0, 'b': 0}
{'a': 0, 'c': 1, 'b': 0}
{'a': 0, 'c': 0, 'b': 1}
{'a': 0, 'c': 1, 'b': 1}
{'a': 1, 'c': 0, 'b': 0}
{'a': 1, 'c': 1, 'b': 0}
{'a': 1, 'c': 0, 'b': 1}
{'a': 1, 'c': 1, 'b': 1}
NathannSun, 03 May 2015 11:54:48 +0200https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/?answer=26708#post-id-26708Comment by phoenix for <p>To list all functions from <code>A</code> to <code>B</code> you must iterate on all points of
<code>B^|A|</code>. This can be achieved with itertools' 'product' function:</p>
<pre><code>def all_functions(A,B):
from itertools import product
B_to_the_A = product(B,repeat=len(A))
for p in B_to_the_A:
yield dict(zip(A,p))
sage: for f in all_functions("abc",[0,1]):
....: print f
{'a': 0, 'c': 0, 'b': 0}
{'a': 0, 'c': 1, 'b': 0}
{'a': 0, 'c': 0, 'b': 1}
{'a': 0, 'c': 1, 'b': 1}
{'a': 1, 'c': 0, 'b': 0}
{'a': 1, 'c': 1, 'b': 0}
{'a': 1, 'c': 0, 'b': 1}
{'a': 1, 'c': 1, 'b': 1}
</code></pre>
<p>Nathann</p>
https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/?comment=26757#post-id-26757Is that "repeat" a command?Fri, 08 May 2015 03:36:43 +0200https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/?comment=26757#post-id-26757Comment by phoenix for <p>To list all functions from <code>A</code> to <code>B</code> you must iterate on all points of
<code>B^|A|</code>. This can be achieved with itertools' 'product' function:</p>
<pre><code>def all_functions(A,B):
from itertools import product
B_to_the_A = product(B,repeat=len(A))
for p in B_to_the_A:
yield dict(zip(A,p))
sage: for f in all_functions("abc",[0,1]):
....: print f
{'a': 0, 'c': 0, 'b': 0}
{'a': 0, 'c': 1, 'b': 0}
{'a': 0, 'c': 0, 'b': 1}
{'a': 0, 'c': 1, 'b': 1}
{'a': 1, 'c': 0, 'b': 0}
{'a': 1, 'c': 1, 'b': 0}
{'a': 1, 'c': 0, 'b': 1}
{'a': 1, 'c': 1, 'b': 1}
</code></pre>
<p>Nathann</p>
https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/?comment=26758#post-id-26758Why is this slightly modified thing not working?
def All_Maps(A,B):
from itertools import product
k = len (A)
return product(B,repeat = k)
for f in All_Maps ([a,b],[0,1]):
print fFri, 08 May 2015 03:39:01 +0200https://ask.sagemath.org/question/26705/how-can-i-get-sage-to-go-over-all-possible-maps-between-two-sets/?comment=26758#post-id-26758