ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 20 Oct 2015 16:05:23 -0500How do I define a list of products of matrices from a given list?https://ask.sagemath.org/question/30124/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?Mon, 19 Oct 2015 16:45:15 -0500https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/Answer by tmonteil for <p>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).</p>
<pre><code>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
</code></pre>
<p>The output I get is</p>
<pre><code>[0, 0, 1]
</code></pre>
<p>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.</p>
<p>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?</p>
https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?answer=30140#post-id-30140To 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 3]
[0 1], [1 0], [0 1], [1 1], [0 1], [1 0], [0 1]
]
If you want to make a function that returns a set, you can do, using the `immutabilize` function defined in my answer of [this ask question](http://ask.sagemath.org/question/30102/how-do-i-define-and-work-with-a-set-of-matrices/):
sage: f = lambda l : {immutabilize(prod(i)) for i in itertools.product(M, repeat=l)}
sage: f(1)
{[0 1]
[1 0], [1 0]
[0 1], [1 1]
[0 1]}
sage: f(2)
{[0 1]
[1 0], [0 1]
[1 1], [1 0]
[0 1], [1 1]
[0 1], [1 1]
[1 0], [1 2]
[0 1]}
sage: f(3)
{[0 1]
[1 0], [0 1]
[1 1], [0 1]
[1 2], [1 0]
[0 1], [1 0]
[1 1], [1 1]
[0 1], [1 1]
[1 0], [1 2]
[0 1], [1 2]
[1 1], [1 3]
[0 1], [2 1]
[1 0]}
sage: f(4)
{[0 1]
[1 0], [0 1]
[1 1], [0 1]
[1 2], [0 1]
[1 3], [1 0]
[0 1], [1 0]
[1 1], [1 0]
[2 1], [1 1]
[0 1], [1 1]
[1 0], [1 1]
[1 2], [1 2]
[0 1], [1 2]
[1 1], [1 3]
[0 1], [1 3]
[1 2], [1 4]
[0 1], [2 1]
[1 0], [2 1]
[1 1], [2 3]
[1 1], [3 1]
[1 0]}
As you can see, many matrices in the list are the same, so that making a set seriously reduces the number of product to work with:
sage: [len(f(l)) for l in range(1,10)]
[3, 6, 11, 19, 32, 53, 87, 142, 231]
To be compared with:
sage: [3^l for l in range(1,10)]
[3, 9, 27, 81, 243, 729, 2187, 6561, 19683]
The formatting of sets of matrices is not very nice, but you can make it nicer while printing it as a list (still with the benefits of having removed repetitions, because it comes from a set):
sage: list(f(4))
[
[0 1] [1 1] [1 1] [0 1] [1 0] [0 1] [1 2] [1 2] [0 1] [1 0]
[1 1], [1 0], [0 1], [1 0], [1 1], [1 2], [1 1], [0 1], [1 3], [2 1],
[1 1] [1 0] [1 4] [3 1] [2 1] [1 3] [1 3] [2 3] [2 1]
[1 2], [0 1], [0 1], [1 0], [1 1], [1 2], [0 1], [1 1], [1 0]
]
Tue, 20 Oct 2015 06:39:57 -0500https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?answer=30140#post-id-30140Comment by tmonteil for <div class="snippet"><p>To iterate over the power of a set, you can use <code>itertools</code> as follows:</p>
<pre><code>sage: itertools.product(M, repeat=3)
<itertools.product object at 0x7fa377e38f50>
</code></pre>
<p>Which is a generator that you can expand into al list to check its content is what you need:</p>
<pre><code>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]
)]
</code></pre>
<p>Each element of this list is a triple of matrices, you can make the producst of each such triple as follows:</p>
<pre><code>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 ...</code></pre><span class="expander"> <a>(more)</a></span></div>https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30157#post-id-30157You 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))Tue, 20 Oct 2015 16:05:23 -0500https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30157#post-id-30157Comment by j0equ1nn for <div class="snippet"><p>To iterate over the power of a set, you can use <code>itertools</code> as follows:</p>
<pre><code>sage: itertools.product(M, repeat=3)
<itertools.product object at 0x7fa377e38f50>
</code></pre>
<p>Which is a generator that you can expand into al list to check its content is what you need:</p>
<pre><code>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]
)]
</code></pre>
<p>Each element of this list is a triple of matrices, you can make the producst of each such triple as follows:</p>
<pre><code>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 ...</code></pre><span class="expander"> <a>(more)</a></span></div>https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30151#post-id-30151This is nice, would you know how the runtime of using this method would compare to the that of the solution posted by @calc314?Tue, 20 Oct 2015 12:55:14 -0500https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30151#post-id-30151Answer by calc314 for <p>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).</p>
<pre><code>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
</code></pre>
<p>The output I get is</p>
<pre><code>[0, 0, 1]
</code></pre>
<p>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.</p>
<p>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?</p>
https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?answer=30125#post-id-30125 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
Mon, 19 Oct 2015 17:51:52 -0500https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?answer=30125#post-id-30125Comment by calc314 for <p>Your code is very close. Try a modification to your list comprehension to get what you are looking for.</p>
<pre><code>S=[X*Y for X in M for Y in M]
</code></pre>
<p>For your last question, one approach is:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30130#post-id-30130I see. I will modify my answer.Mon, 19 Oct 2015 20:59:22 -0500https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30130#post-id-30130Comment by j0equ1nn for <p>Your code is very close. Try a modification to your list comprehension to get what you are looking for.</p>
<pre><code>S=[X*Y for X in M for Y in M]
</code></pre>
<p>For your last question, one approach is:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30129#post-id-30129$\langle M\rangle$ is the group generated by $M$.Mon, 19 Oct 2015 19:14:54 -0500https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30129#post-id-30129Comment by j0equ1nn for <p>Your code is very close. Try a modification to your list comprehension to get what you are looking for.</p>
<pre><code>S=[X*Y for X in M for Y in M]
</code></pre>
<p>For your last question, one approach is:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30127#post-id-30127Yes, 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)$.Mon, 19 Oct 2015 19:00:38 -0500https://ask.sagemath.org/question/30124/how-do-i-define-a-list-of-products-of-matrices-from-a-given-list/?comment=30127#post-id-30127