Linear system solving: linsolve

### 5.56.6 Linear system solving: linsolve

The linsolve command solves systems of linear equations. It can take its arguments as a list of equations or as a matrix of coefficients followed by a vector of the right hand side. It can also take the matrix of coefficients after an LU factorization (see Section 5.49.5), which can be useful when you have several systems of equations which only differ on their right hand side. If the Step by step box is is checked in the general configuration (see Section 2.5.9), linsolve will show you the steps in getting a solution.

Given a system of equations:

- linsolve takes two arguments:
  - eqns, a list of linear equations or expressions (where each expression is assumed to be equal to zero).
  - vars, a list of variable names.
- linsolve(eqns,vars) returns the solution of the equations as a list.

#### Examples

- Input:
  linsolve([2*x+y+z=1,x+y+2*z=1,x+2*y+z=4],[x,y,z])
  Output:

  ⎡ ⎢ ⎢ ⎣ | − |
  1
  2
  | , |
  5
  2
  | ,− |
  1
  2
  | ⎤ ⎥ ⎥ ⎦

  Which means that

  x=− |
  1
  2
  | , y= |
  5
  2
  | , z=− |
  1
  2

  is the solution of the system:

  ⎧ ⎪ ⎨ ⎪ ⎩ |
  2x+y+z | =1
  x+y+2z | =1
  x+2y+z | =4

- Input:
  linsolve([x+2*y+3*z=1,3*x+2*y+z=2],[x,y,z])
  Output:

  ⎡ ⎢ ⎢ ⎣ | z+ |
  1
  2
  | ,−2 z+ |
  1
  4
  | ,z | ⎤ ⎥ ⎥ ⎦

Given the matrix of coefficients:

- linsolve takes two arguments:
  - A, the matrix of coefficients of a linear system.
  - b, a list of the values of the right hand side of the system.
- linsolve(A,b) returns the solution of the corresponding equations as a list.

#### Example

Input:

linsolve ([[2,1,1], [1,1,2], [1,2,1]], [1,1,4])

Output:

⎡ ⎢ ⎢ ⎣ | − |
1
2
| , |
5
2
| ,− |
1
2
| ⎤ ⎥ ⎥ ⎦

If the Step by step option is checked in the general configuration, a window will also pop up showing:

⎡ ⎢ ⎢ ⎣ |
2 | 1 | 1 | −1
1 | 1 | 2 | −1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
| Reduce column 1 with pivot 1 at row 2 |  |  |  |  |  |  |  |  |
| Exchange row 1 and row 2 |  |  |  |  |  |  |  |  |
L2 <−(1)*L2 −(2)*L1 on | ⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
2 | 1 | 1 | −1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
L3 <− 1*L3 −(1)*L1 on | ⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | −1 | −3 | 1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | −1 | −3 | 1
0 | 1 | −1 | −3
| ⎤ ⎥ ⎥ ⎦
| Reduce column 2 with pivot 1 at row 3 |  |  |  |  |  |  |  |  |
| Exchange row 2 and row 3 |  |  |  |  |  |  |  |  |
L1 <− (1)*L1−(1)*L2 on | ⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | 1 | −1 | −3
0 | −1 | −3 | 1
| ⎤ ⎥ ⎥ ⎦
L3 <− (1)*L3−(−1)*L2 on | ⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | −1 | −3 | 1
| ⎤ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎦
| Reduce column 3 with pivot -4 at row 3 |  |  |  |  |  |  |  |  |
L1 <− (1)*L1−(−3/4)*L3 on | ⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎦
L2 <− (1)*L2−(1/4)*L3 on | ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 |
1
2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦
End reduction | ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 |
1
2
0 | 1 | 0 |
− |
5
2
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
2 | 1 | 1 | −1
1 | 1 | 2 | −1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
| Reduce column 1 with pivot 1 at row 2 |  |  |  |  |  |  |  |  |
| Exchange row 1 and row 2 |  |  |  |  |  |  |  |  |
L2 <− (1)*L2 −(2)*L1 on | ⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
2 | 1 | 1 | −1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
L3 <− (1)*L3−(1)*L1 on | ⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | −1 | −3 | 1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | −1 | −3 | 1
0 | 1 | −1 | −3
| ⎤ ⎥ ⎥ ⎦
| Reduce column 2 with pivot 1 at row 3 |  |  |  |  |  |  |  |  |
| Exchange row 2 and row 3 |  |  |  |  |  |  |  |  |
L1 <− (1)*L1 −(1)*L2 on | ⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | 1 | −1 | −3
0 | −1 | −3 | 1
| ⎤ ⎥ ⎥ ⎦
L3 <− (1)*L3−(−1)*L2 on | ⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | −1 | −3 | 1
| ⎤ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎦
| Reduce column 3 with pivot -4 at row 3 |  |  |  |  |  |  |  |  |
L1 <− (1)*L1−(−3/4)*L3 on | ⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎦
L2 <− (1)*L2−(1/4)*L3 on | ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 |
1
2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦
End reduction | ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 |
1
2
0 | 1 | 0 |
− |
5
2
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

Given the matrix of coefficients in factored form:

- linsolve takes four arguments:
  - P, L, U, the LU decomposition of the matrix of coefficients.
  - b, a list of the values of the right hand side of the system.
- linsolve(P,L,U,b) returns the solution of the corresponding equations as a list.

#### Example

Input:

p,l,u:=lu([[2,1,1],[1,1,2],[1,2,1]])
linsolve(p,l,u,[1,1,4])

Output:

⎡ ⎢ ⎢ ⎣ | − |
1
2
| , |
5
2
| ,− |
1
2
| ⎤ ⎥ ⎥ ⎦

The linsolve command also solves systems with coefficients in ℤ/nℤ.

#### Example

Input:

linsolve([2*x+y+z-1,x+y+2*z-1,x+2*y+z-4]%3,[x,y,z])

Output:

⎡ ⎣ | 1%3,1%3,1%3 | ⎤ ⎦