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

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 close merge delete

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

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

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

Sort by » oldest newest most voted

Some useful resources:

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
2


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)

more

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

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

( 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!

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