Skip to main content

Linear Algebra with SageMath: Learn math with open-source software

Section 4.1 Gauss-Jordan Elimination

We can also solve a system of linear equations using matrices. First, construct the augmented matrix by extracting the coefficients of the variables and placing the constants from the right-hand side of the equations as the last column. Then, perform elementary row operations to reduce this augmented matrix to reduced row echelon form (RREF). Finally, convert the matrix back into a system of equations to explicitly display the solutions.
In the following sections, we will use Sage to solve the given system of equations:
\begin{gather*} \begin{array}{c} 0x + y - z = 0\\ x + 2y + 0z = 4\\ x + 0y + z = 4 \end{array} \end{gather*}

Section 4.1.1 Augmented Matrix

First, we create the augmented matrix for the system of equations. Each row in the augmented matrix lists the coefficients of the variables in an equation. While we do not directly manipulate the variables themselves, they indicate the position of each coefficient. The last column of the augmented matrix contains the constants from the right-hand side of each equation.
\begin{align*} \left[\begin{array}{ccc|c} 0 \amp 1 \amp -1 \amp 0 \\ 1 \amp 2 \amp 0 \amp 4 \\ 1 \amp 0 \amp 1 \amp 4 \end{array}\right] \end{align*}
Define the coefficient matrix.
Define the constants vector.
Next, create the augmented matrix by passing the constants vector to the augment() method of the coefficient matrix.
Alternatively, we can create the augmented matrix directly by passing a list of lists to the matrix function.

Section 4.1.2 RREF

A matrix is in reduced row echelon form if:
  • The first nonzero number in the row is a leading \(1\text{.}\)
  • In any two consecutive rows that do not consist entirely of zeros, the leading \(1\) in the lower row occurs farther to the right than the leading \(1\) in the higher row.
  • Rows that consist entirely of zeros are at the bottom of the matrix.
  • Each column that contains a leading \(1\) has zeros everywhere else in that column.
The add_multiple_of_row(i, j, s) method preforms an elementary row operation on a matrix by adding \(s\) times row \(j\) to row \(i\text{.}\) The arguments use \(0\) based indexing.
The swap_rows(r1, r2) method also preforms an elementary row operation on a matrix by swapping rows r1 and r2.
The rescale_row(i,s) method preforms an elementary row operation by multiplying row \(i\) by \(s\) in place.
First, we will move the leading \(1\) from row 1 to row 0.
The next step is to multiply the first row by \(-1\) and add it to the third row.
Next, multiple the second row by \(2\) and add it to the third row.
Multiply the third row by \(-1\) and add it to the second row.
Multiply the second row by \(-2\) and add it to the first row.
Multiply the second row in place by \(-1\text{.}\)
Now that we have the matrix in reduced echelon form, we can convert the matrix back into a system of equations:
\begin{gather*} \begin{array}{c} x = 4 \\ y = 0 \\ z = 0 \end{array} \end{gather*}
Sage has a built in method to do all of this for us. The rref() method returns the reduced row echelon Form of a matrix.
In the previous case there was only one solution to the system of equations. The following are examples of other possibilities.
Here is an example of a system with no solutions:
Here is an example of a system with infinitely many solutions: