ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 16 Apr 2018 02:46:37 -0500Map a matrix to a block matrixhttp://ask.sagemath.org/question/41974/map-a-matrix-to-a-block-matrix/ Hello,
I have a function that maps an element $x$ into a $1\times n$ matrix, say for example $[x,x,x]$. I would like to map this function to a matrix and consider the resulting matrix as a "block matrix". E.g: change matrix([[1,2],[3,4]]) into matrix([[1,1,1,2,2,2],[3,3,3,4,4,4]]).
For now, I use a trick that basically converts back the matrix to a a list using something like:
block_matrix([[f(elt) for elt in row] for row in M.rows()])
but it looks quite dirty so I would like to know if there are some better way to proceed.
Thank you!Tue, 10 Apr 2018 17:15:48 -0500http://ask.sagemath.org/question/41974/map-a-matrix-to-a-block-matrix/Comment by slelievre for <p>Hello,</p>
<p>I have a function that maps an element $x$ into a $1\times n$ matrix, say for example $[x,x,x]$. I would like to map this function to a matrix and consider the resulting matrix as a "block matrix". E.g: change matrix([[1,2],[3,4]]) into matrix([[1,1,1,2,2,2],[3,3,3,4,4,4]]).</p>
<p>For now, I use a trick that basically converts back the matrix to a a list using something like:</p>
<pre><code>block_matrix([[f(elt) for elt in row] for row in M.rows()])
</code></pre>
<p>but it looks quite dirty so I would like to know if there are some better way to proceed.</p>
<p>Thank you!</p>
http://ask.sagemath.org/question/41974/map-a-matrix-to-a-block-matrix/?comment=41980#post-id-41980I think your solution is not dirty at all. This is very much what I would do.Wed, 11 Apr 2018 04:25:30 -0500http://ask.sagemath.org/question/41974/map-a-matrix-to-a-block-matrix/?comment=41980#post-id-41980Answer by j.c. for <p>Hello,</p>
<p>I have a function that maps an element $x$ into a $1\times n$ matrix, say for example $[x,x,x]$. I would like to map this function to a matrix and consider the resulting matrix as a "block matrix". E.g: change matrix([[1,2],[3,4]]) into matrix([[1,1,1,2,2,2],[3,3,3,4,4,4]]).</p>
<p>For now, I use a trick that basically converts back the matrix to a a list using something like:</p>
<pre><code>block_matrix([[f(elt) for elt in row] for row in M.rows()])
</code></pre>
<p>but it looks quite dirty so I would like to know if there are some better way to proceed.</p>
<p>Thank you!</p>
http://ask.sagemath.org/question/41974/map-a-matrix-to-a-block-matrix/?answer=41983#post-id-41983Here's another way to do it:
def f(elt):
return matrix([elt for j in range(3)])
block_matrix([map(f,row) for row in M])
However, from testing with `%%timeit` on random 3x3 matrices (`random_matrix(RDF,3,3)`) it seems that this is always very slightly slower than your list constructor approach:
your approach:
1000 loops, best of 3: 932 µs per loop
my above approach:
1000 loops, best of 3: 958 µs per loop
I also tried using `M.apply_map(f)` but it seems that that function assumes that f returns a scalar element and I couldn't get it to work.Wed, 11 Apr 2018 04:36:45 -0500http://ask.sagemath.org/question/41974/map-a-matrix-to-a-block-matrix/?answer=41983#post-id-41983Comment by tobiasBora for <p>Here's another way to do it:</p>
<pre><code>def f(elt):
return matrix([elt for j in range(3)])
block_matrix([map(f,row) for row in M])
</code></pre>
<p>However, from testing with <code>%%timeit</code> on random 3x3 matrices (<code>random_matrix(RDF,3,3)</code>) it seems that this is always very slightly slower than your list constructor approach:</p>
<p>your approach:</p>
<pre><code>1000 loops, best of 3: 932 µs per loop
</code></pre>
<p>my above approach:</p>
<pre><code>1000 loops, best of 3: 958 µs per loop
</code></pre>
<p>I also tried using <code>M.apply_map(f)</code> but it seems that that function assumes that f returns a scalar element and I couldn't get it to work.</p>
http://ask.sagemath.org/question/41974/map-a-matrix-to-a-block-matrix/?comment=42041#post-id-42041Ok, thank you for your help !Mon, 16 Apr 2018 02:46:37 -0500http://ask.sagemath.org/question/41974/map-a-matrix-to-a-block-matrix/?comment=42041#post-id-42041