# 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 = 19

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 = 19

1

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.

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:

**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.

what's the difference between SR.var and just using var ? @slelievre

0

like 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 asked

Please start posting anonymously - your entry will be published after you log in or create a new account.

Asked: ** 2021-04-05 06:09:26 +0200 **

Seen: **493 times**

Last updated: **Apr 05 '21**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.

This 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 ?

sorry @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 ! ;-)