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.Tue, 06 Apr 2021 07:29:31 +0200How to convert linear system to matrix formhttps://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/Hi, I am new to SageMath, need help on convert a linear system into matrix form using SageMath, e.g.
> 3 x + 2 y = 16
>
> 7 x + y = 19Mon, 05 Apr 2021 06:09:26 +0200https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/Comment by ortollj for <p>Hi, I am new to SageMath, need help on convert a linear system into matrix form using SageMath, e.g. </p>
<blockquote>
<p>3 x + 2 y = 16</p>
<p>7 x + y = 19</p>
</blockquote>
https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?comment=56503#post-id-56503sorry @slelievre I might not have answered because it is probably a homework assignment, but I couldn't resist when I finally saw a question at my math level ! ;-)Mon, 05 Apr 2021 11:04:58 +0200https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?comment=56503#post-id-56503Comment by tmonteil for <p>Hi, I am new to SageMath, need help on convert a linear system into matrix form using SageMath, e.g. </p>
<blockquote>
<p>3 x + 2 y = 16</p>
<p>7 x + y = 19</p>
</blockquote>
https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?comment=56501#post-id-56501This looks like homework. Could you please tell us, which mathematical equation you want to solve, and what did you try to convert it in Sage ?Mon, 05 Apr 2021 10:52:01 +0200https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?comment=56501#post-id-56501Answer by ortollj for <p>Hi, I am new to SageMath, need help on convert a linear system into matrix form using SageMath, e.g. </p>
<blockquote>
<p>3 x + 2 y = 16</p>
<p>7 x + y = 19</p>
</blockquote>
https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?answer=56502#post-id-56502like that:
eqL=[3*x+2*y==16 ,7*x+y==19]
M=matrix([[3,2],[7,1]])
P=M*matrix([x,y]).transpose()
show(M\matrix([16,19]).transpose())
check :
solve(eqL,x,y)
not sure it is what you askedMon, 05 Apr 2021 10:53:15 +0200https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?answer=56502#post-id-56502Comment by ortollj for <p>like that:</p>
<pre><code>eqL=[3*x+2*y==16 ,7*x+y==19]
M=matrix([[3,2],[7,1]])
P=M*matrix([x,y]).transpose()
show(M\matrix([16,19]).transpose())
</code></pre>
<p>check :</p>
<p>solve(eqL,x,y)</p>
<p>not sure it is what you asked</p>
https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?comment=56505#post-id-56505I forgot to put in the header :var('x','y')Mon, 05 Apr 2021 11:19:59 +0200https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?comment=56505#post-id-56505Answer by slelievre for <p>Hi, I am new to SageMath, need help on convert a linear system into matrix form using SageMath, e.g. </p>
<blockquote>
<p>3 x + 2 y = 16</p>
<p>7 x + y = 19</p>
</blockquote>
https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?answer=56512#post-id-56512To complement @ortollj's answer which explains how to input
the matrix by hand and solve it, here we explain how to enter
the system as equations and get matrix form automatically.
Define the symbolic variables and equations:
sage: x, y = SR.var('x, y')
sage: eqns = [3*x + 2*y == 16,
....: 7*x + y == 19]
Define list of unknowns in desired order, then extract matrix and vector:
sage: unks = [x, y]
sage: m = matrix([[lhs.coefficient(u) for u in unks] for eq in eqns for lhs in [eq.lhs()]])
sage: v = vector([eq.rhs() for eq in eqns])
sage: m
[3 2]
[7 1]
sage: v
(16, 19)
Solve:
sage: sol = m.solve_right(v)
sage: sol
(2, 5)
Check whether there are other solutions:
sage: m.right_kernel()
Vector space of degree 2 and dimension 0 over Symbolic Ring
Basis matrix:
[]
Since the kernel of the matrix has dimension zero, there are no other solutions.
-----
Related:
- [Ask Sage question 54896: Turn solution of linear system into vector](https://ask.sagemath.org/question/54896)
Mon, 05 Apr 2021 23:11:28 +0200https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?answer=56512#post-id-56512Comment by ortollj for <p>To complement <a href="/users/18723/ortollj/">@ortollj</a>'s answer which explains how to input
the matrix by hand and solve it, here we explain how to enter
the system as equations and get matrix form automatically.</p>
<p>Define the symbolic variables and equations:</p>
<pre><code>sage: x, y = SR.var('x, y')
sage: eqns = [3*x + 2*y == 16,
....: 7*x + y == 19]
</code></pre>
<p>Define list of unknowns in desired order, then extract matrix and vector:</p>
<pre><code>sage: unks = [x, y]
sage: m = matrix([[lhs.coefficient(u) for u in unks] for eq in eqns for lhs in [eq.lhs()]])
sage: v = vector([eq.rhs() for eq in eqns])
sage: m
[3 2]
[7 1]
sage: v
(16, 19)
</code></pre>
<p>Solve:</p>
<pre><code>sage: sol = m.solve_right(v)
sage: sol
(2, 5)
</code></pre>
<p>Check whether there are other solutions:</p>
<pre><code>sage: m.right_kernel()
Vector space of degree 2 and dimension 0 over Symbolic Ring
Basis matrix:
[]
</code></pre>
<p>Since the kernel of the matrix has dimension zero, there are no other solutions.</p>
<hr>
<p>Related:</p>
<ul>
<li><a href="https://ask.sagemath.org/question/54896">Ask Sage question 54896: Turn solution of linear system into vector</a></li>
</ul>
https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?comment=56513#post-id-56513**To complement @ortollj's answer which explains how to input the matrix by hand and solve it, here we explain how to enter the system as equations and get matrix form automatically.**
Yes @slelievre after thinking it over, I said to myself that this is probably what the question was asking (it was probably not a simple question of homework). But unfortunately the author of the question does not seem very talkative (no answer for @tmonteil request for precision ). I plus your answer.Tue, 06 Apr 2021 07:29:31 +0200https://ask.sagemath.org/question/56499/how-to-convert-linear-system-to-matrix-form/?comment=56513#post-id-56513