ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 31 Jul 2020 06:34:24 +0200Creating a matrix from blockhttps://ask.sagemath.org/question/52763/creating-a-matrix-from-block/ Suppose I have the following elements
A=matrix([[1, 2, 3, 4],[4, 3, 2, 1],[2, 4, 3, 1]])
b=vector([10, 20, 30])
c=vector([1, -2, 3, -1])
II=identity_matrix(3)
z=zero_vector(4)
Now I would like to construct a block matrix
A I | b
c z | 0
0 being a scalar
the | being facultative. I have tried many combination with block_matrix but with any success
Thu, 30 Jul 2020 23:08:50 +0200https://ask.sagemath.org/question/52763/creating-a-matrix-from-block/Answer by John Palmieri for <p>Suppose I have the following elements</p>
<pre><code>A=matrix([[1, 2, 3, 4],[4, 3, 2, 1],[2, 4, 3, 1]])
b=vector([10, 20, 30])
c=vector([1, -2, 3, -1])
II=identity_matrix(3)
z=zero_vector(4)
</code></pre>
<p>Now I would like to construct a block matrix</p>
<p>A I | b</p>
<p>c z | 0</p>
<p>0 being a scalar</p>
<p>the | being facultative. I have tried many combination with block_matrix but with any success</p>
https://ask.sagemath.org/question/52763/creating-a-matrix-from-block/?answer=52765#post-id-52765I'm not sure what you've tried, but you need to convert the vectors to matrices:
sage: A = matrix([[1, 2, 3, 4],[4, 3, 2, 1],[2, 4, 3, 1]])
sage: II = identity_matrix(3)
sage: b = vector([10, 20, 30])
sage: c = vector([1, -2, 3, -1])
sage: z = zero_vector(3) # use a vector with 3 entries, for consistency with the size of II
sage: m = block_matrix(2, [[A, II, matrix(b).transpose()], [matrix(c), matrix(z), zero_matrix(1, 1)]])
sage: m
[ 1 2 3 4| 1 0 0|10]
[ 4 3 2 1| 0 1 0|20]
[ 2 4 3 1| 0 0 1|30]
[-----------+--------+--]
[ 1 -2 3 -1| 0 0 0| 0]
Now you can modify the subdivisions, if you don't like the current ones. For example:
sage: m.subdivisions() # current subdivision
([3], [4, 7])
sage: m.subdivide([3], [7])
sage: m
[ 1 2 3 4 1 0 0|10]
[ 4 3 2 1 0 1 0|20]
[ 2 4 3 1 0 0 1|30]
[--------------------+--]
[ 1 -2 3 -1 0 0 0| 0]
Thu, 30 Jul 2020 23:58:30 +0200https://ask.sagemath.org/question/52763/creating-a-matrix-from-block/?answer=52765#post-id-52765Comment by Cyrille for <p>I'm not sure what you've tried, but you need to convert the vectors to matrices:</p>
<pre><code>sage: A = matrix([[1, 2, 3, 4],[4, 3, 2, 1],[2, 4, 3, 1]])
sage: II = identity_matrix(3)
sage: b = vector([10, 20, 30])
sage: c = vector([1, -2, 3, -1])
sage: z = zero_vector(3) # use a vector with 3 entries, for consistency with the size of II
sage: m = block_matrix(2, [[A, II, matrix(b).transpose()], [matrix(c), matrix(z), zero_matrix(1, 1)]])
sage: m
[ 1 2 3 4| 1 0 0|10]
[ 4 3 2 1| 0 1 0|20]
[ 2 4 3 1| 0 0 1|30]
[-----------+--------+--]
[ 1 -2 3 -1| 0 0 0| 0]
</code></pre>
<p>Now you can modify the subdivisions, if you don't like the current ones. For example:</p>
<pre><code>sage: m.subdivisions() # current subdivision
([3], [4, 7])
sage: m.subdivide([3], [7])
sage: m
[ 1 2 3 4 1 0 0|10]
[ 4 3 2 1 0 1 0|20]
[ 2 4 3 1 0 0 1|30]
[--------------------+--]
[ 1 -2 3 -1 0 0 0| 0]
</code></pre>
https://ask.sagemath.org/question/52763/creating-a-matrix-from-block/?comment=52768#post-id-52768Thanks JohnFri, 31 Jul 2020 06:34:24 +0200https://ask.sagemath.org/question/52763/creating-a-matrix-from-block/?comment=52768#post-id-52768