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, 26 Mar 2021 07:17:31 +0100Splitting a matrixhttps://ask.sagemath.org/question/56377/splitting-a-matrix/Dear SAGE community,
I am looking for a way that can help me split this matrix, A
A = matrix([[x], [x - 2*y]]), which is a linear combination of variables x, and y
into a Coefficient matrix, C = matrix([[1, 0], [1, -2]]) times the vector containing independent variables b = matrix([[x], [y]]).
Basically, A = C*b.
If there's a way to obtain the matrix C, would be really helpful.
-TIAThu, 25 Mar 2021 20:18:08 +0100https://ask.sagemath.org/question/56377/splitting-a-matrix/Answer by slelievre for <p>Dear SAGE community,
I am looking for a way that can help me split this matrix, A
A = matrix([[x], [x - 2*y]]), which is a linear combination of variables x, and y</p>
<p>into a Coefficient matrix, C = matrix([[1, 0], [1, -2]]) times the vector containing independent variables b = matrix([[x], [y]]).
Basically, A = C*b.</p>
<p>If there's a way to obtain the matrix C, would be really helpful.</p>
<p>-TIA</p>
https://ask.sagemath.org/question/56377/splitting-a-matrix/?answer=56380#post-id-56380Since you know that the variables are `x` and `y`,
grab their coefficients in each entry of `A`.
Use list comprehension to build a list of lists out of that.
Then create a matrix from this list of list.
The variables:
sage: x, y = SR.var('x, y')
The matrix `A`:
sage: A = matrix([[x], [x - 2*y]])
sage: A
[ x]
[x - 2*y]
The vector of variables:
sage: b = matrix([[x], [y]])
The matrix of coefficients:
sage: C = matrix([[e.coefficient(x), e.coefficient(y)] for row in A for e in row])
Check:
sage: C, b, C*b, A
(
[ 1 0] [x] [ x] [ x]
[ 1 -2], [y], [x - 2*y], [x - 2*y]
)
Thu, 25 Mar 2021 23:51:14 +0100https://ask.sagemath.org/question/56377/splitting-a-matrix/?answer=56380#post-id-56380Comment by Apoorv for <p>Since you know that the variables are <code>x</code> and <code>y</code>,
grab their coefficients in each entry of <code>A</code>.</p>
<p>Use list comprehension to build a list of lists out of that.</p>
<p>Then create a matrix from this list of list.</p>
<p>The variables:</p>
<pre><code>sage: x, y = SR.var('x, y')
</code></pre>
<p>The matrix <code>A</code>:</p>
<pre><code>sage: A = matrix([[x], [x - 2*y]])
sage: A
[ x]
[x - 2*y]
</code></pre>
<p>The vector of variables:</p>
<pre><code>sage: b = matrix([[x], [y]])
</code></pre>
<p>The matrix of coefficients:</p>
<pre><code>sage: C = matrix([[e.coefficient(x), e.coefficient(y)] for row in A for e in row])
</code></pre>
<p>Check:</p>
<pre><code>sage: C, b, C*b, A
(
[ 1 0] [x] [ x] [ x]
[ 1 -2], [y], [x - 2*y], [x - 2*y]
)
</code></pre>
https://ask.sagemath.org/question/56377/splitting-a-matrix/?comment=56381#post-id-56381@Slelievre, Thank you very much for suggesting!Fri, 26 Mar 2021 07:17:31 +0100https://ask.sagemath.org/question/56377/splitting-a-matrix/?comment=56381#post-id-56381