Ask Your Question

# 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

edit retag close merge delete

## Comments

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 ?

( 2021-04-05 10:52:01 +0200 )edit

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

( 2021-04-05 11:04:58 +0200 )edit

## 2 Answers

Sort by ยป oldest newest most voted

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:

more

## Comments

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.

( 2021-04-06 07:29:31 +0200 )edit

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

more

## Comments

I forgot to put in the header :var('x','y')

( 2021-04-05 11:19:59 +0200 )edit

## Your Answer

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

Add Answer

## Stats

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

Seen: 63 times

Last updated: Apr 05