Processing math: 100%
Ask Your Question
0

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

asked 9 years ago

j0equ1nn gravatar image

updated 9 years ago

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 lN and outputs the list of products of that length?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 9 years ago

calc314 gravatar image

updated 9 years ago

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
Preview: (hide)
link

Comments

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

f:NM,l { x1x2xlxiM }.

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

j0equ1nn gravatar imagej0equ1nn ( 9 years ago )

M is the group generated by M.

j0equ1nn gravatar imagej0equ1nn ( 9 years ago )

I see. I will modify my answer.

calc314 gravatar imagecalc314 ( 9 years ago )
1

answered 9 years ago

tmonteil gravatar image

updated 9 years ago

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)
Preview: (hide)
link

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 ( 9 years ago )

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 ( 9 years ago )

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: 9 years ago

Seen: 804 times

Last updated: Oct 20 '15