Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I suppose you are aware that, in fact, you don't need to use solve: once defined $A$ and $Y$, the solution of the linear system $AX=Y$ is given by A\Y. For example,

sage: A = matrix([[1,2,3], [4,5,6], [-7,8,9]])
sage: Y = vector([3,9,1])
sage: print "Solution:", A\Y
Solution: (1, 1, 0)

Anyway, if you really need to use solve and write the system in the form $AX=Y$, you can continue the above example as follows:

sage: X = vector([var("x_{}".format(i)) for i in [1..3]])
sage: system = [A[i]*X==Y[i] for i in range(len(Y))]
sage: solve(system, *X)
[[x_1 == 1, x_2 == 1, x_3 == 0]]

I suppose you are aware that, in fact, you don't need to use solve: once defined $A$ and $Y$, the solution of the linear system $AX=Y$ is given by A\Y. For example,

sage: A = matrix([[1,2,3], [4,5,6], [-7,8,9]])
sage: Y = vector([3,9,1])
sage: print "Solution:", A\Y
Solution: (1, 1, 0)

Anyway, if you really need to use solve and write the system in the form $AX=Y$, you can continue the above example as follows:

sage: X = vector([var("x_{}".format(i)) for i in [1..3]])
sage: system = [A[i]*X==Y[i] for i in range(len(Y))]
sage: solve(system, *X)
[[x_1 == 1, x_2 == 1, x_3 == 0]]

Post-scriptum. The following function covers the cases considered in the comments below:

def my_solve(A, Y):
    m, n = Y.dimensions()
    X = vector([var("x_{}".format(i)) for i in [1..m]])
    sols = []
    for j in range(n):
        system = [A[i]*X==Y[i,j] for i in range(m)]
        sols += solve(system, *X)
    return sols

Of course, A and Y should be two matrices with the same number of rows. Let us check the examples:

sage: A = matrix(QQ, [[1, 2, 2, 2], [2, 4, 6, 8], [3, 6, 8, 10], [0, 0, 0, 0]])
sage: Y = matrix(QQ, 4, 1, [0, 0, 0, 0])
sage: my_solve(A, Y)
[[x_1 == 2*r1 - 2*r2, x_2 == r2, x_3 == -2*r1, x_4 == r1]]

Please, note the way Y is given.

sage: A = matrix(QQ, [[2, 4, -3], [-3, -2, 4], [2, -5, -2]])
sage: Y = matrix(QQ, [[-5, -2], [3, 1], [0, 3]])
sage: my_solve(A, Y)
[[x_1 == 51, x_2 == 4, x_3 == 41], [x_1 == -5, x_2 == -1, x_3 == -4]]

You can see these examples in this Sagemath Cell

I suppose you are aware that, in fact, you don't need to use solve: once defined $A$ and $Y$, the solution of the linear system $AX=Y$ is given by A\Y. For example,

sage: A = matrix([[1,2,3], [4,5,6], [-7,8,9]])
sage: Y = vector([3,9,1])
sage: print "Solution:", A\Y
Solution: (1, 1, 0)

Anyway, if you really need to use solve and write the system in the form $AX=Y$, you can continue the above example as follows:

sage: X = vector([var("x_{}".format(i)) for i in [1..3]])
sage: system = [A[i]*X==Y[i] for i in range(len(Y))]
sage: solve(system, *X)
[[x_1 == 1, x_2 == 1, x_3 == 0]]

Post-scriptum. The following function covers the cases considered in the comments below:

def my_solve(A, Y):
 m, n = A.dimensions()
p, q = Y.dimensions()
 if m!=p:
    raise RuntimeError("The matrices have different numbers of rows")
X = vector([var("x_{}".format(i)) for i in [1..m]])
    [1..n]])
sols = []
 for j in range(n):
    range(q):
    system = [A[i]*X==Y[i,j] for i in range(m)]
     sols += solve(system, *X)
 return sols

Of course, A and Y should be two matrices with the same number of rows. Let us check the examples:

sage: A = matrix(QQ, [[1, 2, 2, 2], [2, 4, 6, 8], [3, 6, 8, 10], [0, 0, 0, 0]])
sage: Y = matrix(QQ, 4, 1, [0, 0, 0, 0])
sage: my_solve(A, Y)
[[x_1 == 2*r1 - 2*r2, x_2 == r2, x_3 == -2*r1, x_4 == r1]]

Please, note the way Y is given.

sage: A = matrix(QQ, [[2, 4, -3], [-3, -2, 4], [2, -5, -2]])
sage: Y = matrix(QQ, [[-5, -2], [3, 1], [0, 3]])
sage: my_solve(A, Y)
[[x_1 == 51, x_2 == 4, x_3 == 41], [x_1 == -5, x_2 == -1, x_3 == -4]]

You can see these examples in this Sagemath Cell

.

Update. The code had an error in the way the number of unknowns was determined. I have modified the code to correct this bug. Let us check it with this new example:

sage: A = matrix([[1, 2, 3], [5, 7, 11]])
sage: Y = matrix([[13 ,17], [19, 23]])
sage: my_solve(A, Y)
[[x_1 == -1/3*r11 - 53/3, x_2 == -4/3*r11 + 46/3, x_3 == r11],
 [x_1 == -1/3*r12 - 73/3, x_2 == -4/3*r12 + 62/3, x_3 == r12]]

I suppose you are aware that, in fact, you don't need to use solve: once defined $A$ and $Y$, the solution of the linear system $AX=Y$ is given by A\Y. For example,

sage: A = matrix([[1,2,3], [4,5,6], [-7,8,9]])
sage: Y = vector([3,9,1])
sage: print "Solution:", A\Y
Solution: (1, 1, 0)

Anyway, if you really need to use solve and write the system in the form $AX=Y$, you can continue the above example as follows:

sage: X = vector([var("x_{}".format(i)) for i in [1..3]])
sage: system = [A[i]*X==Y[i] for i in range(len(Y))]
sage: solve(system, *X)
[[x_1 == 1, x_2 == 1, x_3 == 0]]

Post-scriptum. The following function covers the cases considered in the comments below:

def my_solve(A, Y):
 m, n = A.dimensions()
 p, q = Y.dimensions()
 if m!=p:
     raise RuntimeError("The matrices have different numbers of rows")
 X = vector([var("x_{}".format(i)) for i in [1..n]])
 sols = []
 for j in range(q):
     system = [A[i]*X==Y[i,j] for i in range(m)]
     sols += solve(system, *X)
 return sols

Of course, A and Y should be two matrices with the same number of rows. Let us check the examples:

sage: A = matrix(QQ, [[1, 2, 2, 2], [2, 4, 6, 8], [3, 6, 8, 10], [0, 0, 0, 0]])
sage: Y = matrix(QQ, 4, 1, [0, 0, 0, 0])
sage: my_solve(A, Y)
[[x_1 == 2*r1 - 2*r2, x_2 == r2, x_3 == -2*r1, x_4 == r1]]

Please, note the way Y is given.

sage: A = matrix(QQ, [[2, 4, -3], [-3, -2, 4], [2, -5, -2]])
sage: Y = matrix(QQ, [[-5, -2], [3, 1], [0, 3]])
sage: my_solve(A, Y)
[[x_1 == 51, x_2 == 4, x_3 == 41], [x_1 == -5, x_2 == -1, x_3 == -4]]

You can see these examples in this Sagemath Cell.

Update. The code had an error in the way the number of unknowns was determined. I have modified the code to correct this bug. Let us check it with this new example:

sage: A = matrix([[1, 2, 3], [5, 7, 11]])
sage: Y = matrix([[13 ,17], [19, 23]])
sage: my_solve(A, Y)
[[x_1 == -1/3*r11 - 53/3, x_2 == -4/3*r11 + 46/3, x_3 == r11],
 [x_1 == -1/3*r12 - 73/3, x_2 == -4/3*r12 + 62/3, x_3 == r12]]

I suppose you are aware that, in fact, you don't need to use solve: once defined $A$ and $Y$, the solution of the linear system $AX=Y$ is given by A\Y. For example,

sage: A = matrix([[1,2,3], [4,5,6], [-7,8,9]])
sage: Y = vector([3,9,1])
sage: print "Solution:", A\Y
Solution: (1, 1, 0)

Anyway, if you really need to use solve and write the system in the form $AX=Y$, you can continue the above example as follows:

sage: X = vector([var("x_{}".format(i)) for i in [1..3]])
sage: system = [A[i]*X==Y[i] for i in range(len(Y))]
sage: solve(system, *X)
[[x_1 == 1, x_2 == 1, x_3 == 0]]

Post-scriptum. The following function covers the cases considered in the comments below:

def my_solve(A, Y):
    m, n = A.dimensions()
    p, q = Y.dimensions()
    if m!=p:
        raise RuntimeError("The matrices have different numbers of rows")
    X = vector([var("x_{}".format(i)) for i in [1..n]])
    sols = []
    for j in range(q):
        system = [A[i]*X==Y[i,j] for i in range(m)]
        sols += solve(system, *X)
    return sols

Of course, A and Y should be two matrices with the same number of rows. Let us check the examples:

sage: A = matrix(QQ, [[1, 2, 2, 2], [2, 4, 6, 8], [3, 6, 8, 10], [0, 0, 0, 0]])
sage: Y = matrix(QQ, 4, 1, [0, 0, 0, 0])
sage: my_solve(A, Y)
[[x_1 == 2*r1 - 2*r2, x_2 == r2, x_3 == -2*r1, x_4 == r1]]

Please, note the way Y is given.

sage: A = matrix(QQ, [[2, 4, -3], [-3, -2, 4], [2, -5, -2]])
sage: Y = matrix(QQ, [[-5, -2], [3, 1], [0, 3]])
sage: my_solve(A, Y)
[[x_1 == 51, x_2 == 4, x_3 == 41], [x_1 == -5, x_2 == -1, x_3 == -4]]

You can see these examples in this Sagemath Cell.

Update. The code had an error in the way the number of unknowns was determined. I have modified the code to correct this bug. Let us check it with this new example:

sage: A = matrix([[1, 2, 3], [5, 7, 11]])
sage: Y = matrix([[13 ,17], [19, 23]])
sage: my_solve(A, Y)
[[x_1 == -1/3*r11 - 53/3, x_2 == -4/3*r11 + 46/3, x_3 == r11],
 [x_1 == -1/3*r12 - 73/3, x_2 == -4/3*r12 + 62/3, x_3 == r12]]

You can see these examples in this Sagemath Cell.