Ask Your Question
0

How do I define a list of products of matrices from a given list?

asked 2015-10-19 23:45:15 +0100

j0equ1nn gravatar image

updated 2015-10-20 00:28:57 +0100

I'm trying to define a list of products of matrices, of a set length, from another list of matrices. This how I've tried to do it (using simplified data and length 2).

a = matrix(ZZ,[[1,0],[0,1]])
b = matrix(ZZ,[[0,1],[1,0]])
c = matrix(ZZ,[[1,1],[0,1]])
M=[a,b,c]
S=[X*Y for X, Y in M]
S

The output I get is

[0, 0, 1]

Obviously this is not the list of desired products, moreover it is not a list of matrices, and the list has 3 items rather than the 6 possible things that should occur. Beyond this simple example, I'd also like to be able to do this with longer products and more complicated initial lists.

This probably has a simple answer (I am a beginner). My next question though would be, how could I define a function that inputs some $l\in\mathbb{N}$ and outputs the list of products of that length?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-10-20 00:51:52 +0100

calc314 gravatar image

updated 2015-10-20 03:59:54 +0100

Your code is very close. Try a modification to your list comprehension to get what you are looking for.

S=[X*Y for X in M for Y in M]

For your last question, one approach is:

s=range(4)
q=Permutations(s,3)
print len(q)
print q
a = matrix(ZZ,[[1,0],[0,1]])
b = matrix(ZZ,[[0,1],[1,0]])
c = matrix(ZZ,[[1,1],[0,1]])
d = matrix(ZZ,[[0,1],[0,1]])
M=[a,b,c,d]
S=[prod([M[i] for i in list(p)]) for p in q]
print S
edit flag offensive delete link more

Comments

Yes, that worked! My last question is about making a function like

$f:\mathbb{N}\rightarrow\langle M\rangle , l\mapsto$ { $x_1 x_2 \dots x_l\mid x_i\in M$ }.

In the code given, with your correction, $S=f(2)$.

j0equ1nn gravatar imagej0equ1nn ( 2015-10-20 02:00:38 +0100 )edit

$\langle M\rangle$ is the group generated by $M$.

j0equ1nn gravatar imagej0equ1nn ( 2015-10-20 02:14:54 +0100 )edit

I see. I will modify my answer.

calc314 gravatar imagecalc314 ( 2015-10-20 03:59:22 +0100 )edit
1

answered 2015-10-20 13:39:57 +0100

tmonteil gravatar image

updated 2015-10-20 13:41:40 +0100

To iterate over the power of a set, you can use itertools as follows:

sage: itertools.product(M, repeat=3)
<itertools.product object at 0x7fa377e38f50>

Which is a generator that you can expand into al list to check its content is what you need:

sage: list(itertools.product(M, repeat=3))
[(
[1 0]  [1 0]  [1 0]
[0 1], [0 1], [0 1]
),
 (
[1 0]  [1 0]  [0 1]
[0 1], [0 1], [1 0]
),
 (
[1 0]  [1 0]  [1 1]
[0 1], [0 1], [0 1]
),
 (
[1 0]  [0 1]  [1 0]
[0 1], [1 0], [0 1]
),
 (
[1 0]  [0 1]  [0 1]
[0 1], [1 0], [1 0]
),
 (
[1 0]  [0 1]  [1 1]
[0 1], [1 0], [0 1]
),
 (
[1 0]  [1 1]  [1 0]
[0 1], [0 1], [0 1]
),
 (
[1 0]  [1 1]  [0 1]
[0 1], [0 1], [1 0]
),
 (
[1 0]  [1 1]  [1 1]
[0 1], [0 1], [0 1]
),
 (
[0 1]  [1 0]  [1 0]
[1 0], [0 1], [0 1]
),
 (
[0 1]  [1 0]  [0 1]
[1 0], [0 1], [1 0]
),
 (
[0 1]  [1 0]  [1 1]
[1 0], [0 1], [0 1]
),
 (
[0 1]  [0 1]  [1 0]
[1 0], [1 0], [0 1]
),
 (
[0 1]  [0 1]  [0 1]
[1 0], [1 0], [1 0]
),
 (
[0 1]  [0 1]  [1 1]
[1 0], [1 0], [0 1]
),
 (
[0 1]  [1 1]  [1 0]
[1 0], [0 1], [0 1]
),
 (
[0 1]  [1 1]  [0 1]
[1 0], [0 1], [1 0]
),
 (
[0 1]  [1 1]  [1 1]
[1 0], [0 1], [0 1]
),
 (
[1 1]  [1 0]  [1 0]
[0 1], [0 1], [0 1]
),
 (
[1 1]  [1 0]  [0 1]
[0 1], [0 1], [1 0]
),
 (
[1 1]  [1 0]  [1 1]
[0 1], [0 1], [0 1]
),
 (
[1 1]  [0 1]  [1 0]
[0 1], [1 0], [0 1]
),
 (
[1 1]  [0 1]  [0 1]
[0 1], [1 0], [1 0]
),
 (
[1 1]  [0 1]  [1 1]
[0 1], [1 0], [0 1]
),
 (
[1 1]  [1 1]  [1 0]
[0 1], [0 1], [0 1]
),
 (
[1 1]  [1 1]  [0 1]
[0 1], [0 1], [1 0]
),
 (
[1 1]  [1 1]  [1 1]
[0 1], [0 1], [0 1]
)]

Each element of this list is a triple of matrices, you can make the producst of each such triple as follows:

sage: [prod(i) for i in itertools.product(M, repeat=3)]
[
[1 0]  [0 1]  [1 1]  [0 1]  [1 0]  [0 1]  [1 1]  [1 1]  [1 2]  [0 1]
[0 1], [1 0], [0 1], [1 0], [0 1], [1 1], [0 1], [1 0], [0 1], [1 0],

[1 0]  [0 1]  [1 0]  [0 1]  [1 1]  [0 1]  [1 0]  [0 1]  [1 1]  [1 1]
[0 1], [1 1], [0 1], [1 0], [0 1], [1 1], [1 1], [1 2], [0 1], [1 0],

[1 2]  [1 1]  [1 1]  [1 2]  [1 2]  [2 1]  [1 ...
(more)
edit flag offensive delete link more

Comments

This is nice, would you know how the runtime of using this method would compare to the that of the solution posted by @calc314?

j0equ1nn gravatar imagej0equ1nn ( 2015-10-20 19:55:14 +0100 )edit

You can make timings by using the %timeit and %time functions.

That said, it seems that @calc314 answer will not produce all possible products of a given length since it uses Permutations which do not allow a matrix to be repeated in the product, while itertools.product does exactly that. To be convinced, you should compare the results of

sage: list(Permutations(range(4),3))

vs

sage: list(itertools.product(range(4),repeat=3))
tmonteil gravatar imagetmonteil ( 2015-10-20 23:05:23 +0100 )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-10-19 23:45:15 +0100

Seen: 774 times

Last updated: Oct 20 '15