Ask Your Question

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

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 close merge delete

## 2 answers

Sort by ยป oldest newest most voted

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

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)$.

( 2015-10-19 19:00:38 -0600 )edit

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

( 2015-10-19 19:14:54 -0600 )edit

I see. I will modify my answer.

( 2015-10-19 20:59:22 -0600 )edit

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

## 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?

( 2015-10-20 12:55:14 -0600 )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))

( 2015-10-20 16:05:23 -0600 )edit

## Your Answer

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

Add Answer

## Stats

Asked: 2015-10-19 16:45:15 -0600

Seen: 263 times

Last updated: Oct 20 '15