Ask Your Question
0

Convert solution set to vector notation?

asked 2024-04-30 21:42:25 +0100

snowch gravatar image

I have the following matrix and RREF:

[
[ 2  1  0 -1  0| 4]    [   1    0    0   -1 -1/2|   0]
[ 0  1  0  1  1| 4]    [   0    1    0    1    1|   4]
[ 1  0 -1  2  0| 0],   [   0    0    1   -3 -1/2|   0]
]

Is it possible to convert the set of solutions:

  x == 1/2*u + w
  y == -u - w + 4
  z == 1/2*u + 3*w

into vector form:

[ x ]   [ 0 ]   [  1 ]      [ 1/2 ]   |   
[ y ]   [ 4 ]   [ -1 ]      [  -1 ]   |
[ z ] = [ 0 ] + [  3 ] w +  [ 1/2 ]u  |   w, u ∈ ℝ
[ w ]   [ 0 ]   [  1 ]      [   0 ]   |
[ u ]   [ 0 ]   [  0 ]      [   1 ]   |

Is this possible with Sagemath?

The solutions are created using:

def my_solve(augmented_matrix, vars=None):

    A = augmented_matrix[:, :-1]
    Y = augmented_matrix[:, -1]

    m, n = A.dimensions()
    p, q = Y.dimensions()

    if m!=p:
        raise RuntimeError("The matrices have different numbers of rows")

    if vars and len(vars) != n:
        raise RuntimeError(f"Provided variables '{vars}' != number of columns '{n}'")

    if vars:
        # don't include the free variables in solve
        X = vector([var(vars[i]) for i in [0..n-1]])
        X_pivots = vector([var(X[i]) for i in [0..n-1] if i in A.pivots()])
        X_free = vector([var(X[i]) for i in [0..n-1] if i not in A.pivots()])
    else:
        X = vector([var(f"x_{i}") for i in [0..n-1]])
        X_pivots = vector([var(f"x_{i}") for i in [0..n-1] if i in A.pivots()])
        X_free = vector([var(f"x_{i}") for i in [0..n-1] if i not in A.pivots()])

    sols = []
    for j in range(q):
        system = [A[i]*X==Y[i,j] for i in range(m)]
        sols += solve(system, *X_pivots)

    return sols, X, X_pivots, X_free

M = matrix(QQ, 3, [2,1,0,-1,0,   0,1,0,1,1,   1,0,-1,2,0])
v = vector(QQ, [4,4,0])
Maug = M.augment(v, subdivide=True)
my_solve(Maug, var('x y z w u'))[0]
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2024-05-01 01:01:50 +0100

Max Alekseyev gravatar image

updated 2024-05-01 04:04:53 +0100

Since you have linear equations, it'd be more straightforward and efficient to employ linear algebra rather than symbolic solve() function.

If I got your manipulations correctly, then instead of my_solve() you can simply use

M = matrix(QQ, 3, [2,1,0,-1,0,   0,1,0,1,1,   1,0,-1,2,0])
v = vector(QQ, [4,4,0])
s = M.solve_right(v)
K = M.right_kernel().basis()
print(s)
print(K)

which prints

(0, 4, 0, 0, 0)
[
(1, 0, 5, 2, -2),
(0, 1, 2, 1, -2)
]

These are exactly the "free term" and the vectors you multiply by real coefficients w and u to obtain the solution you search for.

edit flag offensive delete link more

Comments

apologies for the basic question ... why don't the w and u vector values match mine (e.g 1,-1,3,1,0 for w)?

snowch gravatar imagesnowch ( 2024-05-01 03:36:38 +0100 )edit

There exist multiple different bases for the same kernel. Your vectors give one basis, those in my answer give another. But they both generate/span the same solutions.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-05-01 04:01:37 +0100 )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

1 follower

Stats

Asked: 2024-04-30 21:42:25 +0100

Seen: 160 times

Last updated: May 01