Ask Your Question
0

How can I get Sage to go over all possible maps between two sets?

asked 2015-05-02 17:25:28 -0500

phoenix gravatar image

updated 2015-05-02 17:36:27 -0500

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!..)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-05-03 04:54:48 -0500

Nathann gravatar image

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}

Nathann

edit flag offensive delete link more

Comments

Is that "repeat" a command?

phoenix gravatar imagephoenix ( 2015-05-07 20:36:43 -0500 )edit

Why 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 f

phoenix gravatar imagephoenix ( 2015-05-07 20:39:01 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-05-02 17:25:28 -0500

Seen: 113 times

Last updated: May 03 '15