Ask Your Question

How can I transform a "solution" of linear sytem in a vector?

asked 2020-12-25 18:25:26 +0200

phcosta gravatar image

updated 2020-12-26 02:45:08 +0200

slelievre gravatar image

For example: solve a linear system (in sagemath) with infinitely many solutions and get:

sage: var('x y z w')
sage: eqq = [x + 2*y + 2*z + 2*w, 2*x + 4*y + 6*z + 8*w, 3*x + 6*y + 8*z + 10*w]
sage: solve(eqq, x, y, z, w)
[[x == 2*r1 - 2*r2, y == r2, z == -2*r1, w == r1]]

How can I extract the vector

V = [2*r1 - 2*r2, r2, -2*r1, r1]

describing these solutions?

Any suggestion to find the dimension of the solution space for a system with infinitely many solutions as above using Sage?

PS. I'm sorry if the question is too trivial, I'm new a user...

edit retag flag offensive close merge delete


Welcome to Ask Sage! Thank you for your question!

slelievre gravatar imageslelievre ( 2020-12-26 01:40:21 +0200 )edit

Ideally, provide the code for entering the system into Sage, so other can copy-paste and get started.

This makes it easier for others to study your question.

Thus it increases the likelihood and speed of getting an answer.

Can you edit your question to do that?

slelievre gravatar imageslelievre ( 2020-12-26 01:41:26 +0200 )edit

Thank you, I just added.

phcosta gravatar imagephcosta ( 2020-12-26 02:15:50 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2020-12-26 02:59:51 +0200

slelievre gravatar image

updated 2020-12-26 12:42:23 +0200

Some useful resources:

But back to answering your question...

The variables:

sage: x, y, z, w = SR.var('x, y, z, w')

The equations (writing == 0 is not needed in order to solve):

sage: eqq = [x + 2*y + 2*z + 2*w, 2*x + 4*y + 6*z + 8*w, 3*x + 6*y + 8*z + 10*w]

The solutions (tip: naming things you compute makes them easier to reuse):

sage: sols = solve(eqq, x, y, z, w)
sage: sols
[[x == 2*r1 - 2*r2, y == r2, z == -2*r1, w == r1]]

Get the desired V with:

sage: sol = sols[0]
sage: sol
[x == 2*r1 - 2*r2, y == r2, z == -2*r1, w == r1]
sage: V = [s.rhs() for s in sol]
sage: V
[2*r1 - 2*r2, r2, -2*r1, r1]

or directly:

sage: V = [s.rhs() for s in sols[0]]; V
[2*r1 - 2*r2, r2, -2*r1, r1]

The dimension of the space of solutions is the dimension of the kernel of the corresponding matrix.

Compute the matrix:

sage: vv = x, y, z, w
sage: a = matrix([[eq.coefficient(v) for v in vv] for eq in eqq])
sage: a
[ 1  2  2  2]
[ 2  4  6  8]
[ 3  6  8 10]

The kernel:

sage: E = a.right_kernel()
sage: E
Vector space of degree 4 and dimension 2 over Symbolic Ring
Basis matrix:
[  1   0  -1 1/2]
[  0   1  -2   1]

Dimension of the (right) kernel:

sage: k = E.dimension()
sage: k

Basis of the kernel:

sage: B = E.basis()
sage: B
(1, 0, -1, 1/2),
(0, 1, -2, 1)

Linear combination of kernel basis vectors, similar to the one given by solve:

sage: s, t = SR.var('s t')
sage: E.linear_combination_of_basis((2*(s - t), t))
(2*s - 2*t, t, -2*s, s)
edit flag offensive delete link more


Thank you very much. You gave me a very nice and complete answer. All the best

phcosta gravatar imagephcosta ( 2020-12-26 13:46:22 +0200 )edit

Glad this was helpful. You can accept the answer to mark the question as solved. This is done by clicking the "✓" button, to the left of the top of the answer, below the "upvote" and "downvote" buttons and the answer's score.

slelievre gravatar imageslelievre ( 2020-12-26 14:01:58 +0200 )edit

It is not easy to understand how to validate an answer on ask sagemath, (at least for me!), it took me a few months of presence on the site before understanding how to do it!

ortollj gravatar imageortollj ( 2020-12-26 16:40:31 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2020-12-25 18:25:26 +0200

Seen: 661 times

Last updated: Dec 26 '20