% This is lecture no. 4 of MH1200/: Linear Algebra I.

\chapter{Echelon Form \& Gaussian Elimination}%
\label{cha:echelon_form_&_gaussian_elimination}

\section{Row Echelon Form}%
\label{sec:row_echelon_form}

\begin{definition}
	Row Echelon Form.
	Let $M$ be a matrix or an augmented matrix.

	We say that $M$ is in \textbf{Row Echelon Form} if it satisfies the following two rules:
	\begin{enumerate}
		\item If $M$ has any rows that are entirely zeros then they all appear together right at the bottom of the matrix.
		\item Given any two successive rows of $M$ neither of which is all zeros, the first non-zero  entry in the lower row (which is called the leading entry of the row) appears \textbf{\underline{strictly}} further to the right than the first non-zero entry in the higher row.
	\end{enumerate}
\end{definition}

\begin{exmp}
	Take the following matrix:
	\[
	\begin{bmatrix}
		1 & 1 & 2\\
		0 & 0 & 0\\
		0 & 1 & 0\\
	\end{bmatrix}
	.\]
	It is not in row echelon form, so we must swap the second and third rows:
	\[
	\begin{bmatrix}
		1 & 1 & 2\\
		0 & 0 & 0\\
		0 & 1 & 0\\
	\end{bmatrix}
	\xrightarrow{R_2 \leftrightarrow R_3}
	\begin{bmatrix}
		1 & 1 & 2\\
		0 & 1 & 0\\
		0 & 0 & 0\\
	\end{bmatrix}
	.\]
	 Now the matrix follows both rules to satisfy row echelon form.
\end{exmp}

\section{Back-substitution}%
\label{sec:back_substitution}

Solve the equations one-by-one, starting with the last equation first and then proceeding from there up the list.

\begin{exmp}
	\label{bsub}
	Consider the following system of linear equations:
	\begin{align*}
		x_1 + 2x_2 + 3x_3 & = 0 \\
		4x_2 + 5x_3 & = 1 \\
		2x_3 & = 6
	\end{align*}
	Convert to augmented matrix:
	\[
	\begin{bmatrix}[ccc|c]
			1 & 2 & 3 & 0 \\
			1 & 4 & 5 & 1 \\
			0 & 0 & 2 & 6 \\
	\end{bmatrix}
	.\]
	This augmented matrix is already in row echelon form.
	We start with the last equation.
	This equation is true precisely when $x_3=3$.
	Continuing to solve up the matrix with this method gives the following result:
	\begin{align*}
		x_1 & = -2\\
		x_2 & = -\frac{7}{2}\\
		x_3 & = 3
	\end{align*}
\end{exmp}

\subsection{The General Situation for Back-Substitution}%
\label{sub:the_general_situation_of_back_substitution}

The general situation of a system in echelon form is more complicated than in example \ref{bsub}.

The reason is that as we go up the matrix it is possible that some row introduces several variables which we haven't solved for already:

\begin{exmp}
	Find the solutions for $x_2$:
	\begin{align*}
		x_1 + 2x_2 + x_3 + 2x_4 & = 1\\
		x_2 + x_3 + 3x_4 & = 2\\
		5x_4 & = 10
	\end{align*}
\end{exmp}

Solve for the left-most variable like in example \ref{bsub}, but now make any remaining new variables free parameters.
In this example, the bottom equation tells you $x_4 = 2$.
Then the next equation becomes $x_2 + x_3 = -4$.
This has 2 variables we haven't seen before.
Set $x_3$ to a new free variable $x_3 = s$ and then you solve for $x_2$ in terms of $x_3=s$ to get $x_2 = -4 -s$.

\begin{exmp}
	Consider the following augmented matrix.
	Observe that it is in row echelon form:
	\[
	\begin{bmatrix}[ccccc|c]
		0 & 2 & 2 & 1 & -2 & 2\\
		0 & 0 & 1 & 1 & 1 & 2\\
		0 & 0 & 0 & 0 & 2 & 4\\
	\end{bmatrix}
	.\]
	Each column corresponds with a variable of the associated linear system.
	The corresponding linear system is:
	\begin{align*}
		2x_2 + 2x_3 + x_4 - 2x_5 & = 2 \\
		x_3 + x_4 + x_5 & = 3 \\
		2x_5 & = 4
	\end{align*}
	The third equation gibes us $x_5=2$.
	Substituting into the second equation gives us:
	\[
	x_3 + x_4 = 1
	.\]
	Next, we let $x_4$ be a new free parameter $s$ and solve for the leading term $x_3$ in terms of this new parameter:
	\[
	x_3=1-s
	.\]
	Finally, solve the first equation, substituting the answers we already found, we get:
	\[
		2x_2 + 2(1-s) + s -2\cdot 2 = 2
	.\]
	We can solve this for $x_2$ :
	\[
	x_2 = 2 + \frac{1}{2}s
	.\]
	There are no equations using $x_1$, so $x_1$ can be anything.
	We set it to a new free parameter $x_1=t$.
	The general solution of the system of linear equations is:
	\[
	\begin{aligned}
		x_5 & = 2\\
		x_4 & = s\\
		x_3 & = 1-s\\
		x_2 & = 2 + \frac{1}{2}s\\
		x_1 & = t
	\end{aligned}
	,
	\qquad
	s,t \in \mathbb{R}
	.\]
	There are two free parameters so there are infinitely many solutions.
	To be precise, we will see later that the set of solutions is a 2-dimensional affine subspace of $\mathbb{R}^5$.
\end{exmp}

\section{Gaussian Elimination}%
\label{sec:gaussian_elimination}

\begin{definition}
	Gaussian Elimination.
	An algorithm for transforming an augmented matrix of a linear system into row-echelon form by elementary row operations.
\end{definition}

The algorithm performs elementary row operations in a cycle of four steps which we will repeat until the algorithm terminates when the matrix is in row echelon form.

\bigbreak
Step 1:
Locate the left most column that is not entirely zeros.

\[
\begin{bmatrix}
	0 & 3 & \ldots & \ldots \\
	1 & -2 & \ldots & \ldots \\
	4 & 0 & \ldots & \ldots \\
\end{bmatrix}
\qquad
\begin{bmatrix}
	0 & 3 & 1 & \ldots \\
	0 & 2 & -3 & \ldots \\
	0 & -1 & 6 & \ldots \\
\end{bmatrix}
.\]
In the matrix on the left, the first non-zero column is the first column.
In the matrix on the right, the first non-zero column is the second column.

\bigbreak
Step 2:
If necessary, swap the top row (call it $R_T$) with another row (using the elementary row operation $R_T \leftrightarrow R_B$) so as to ensure that the top entry of the column you identified in Step 1 is not a zero:

\[
\begin{bmatrix}
	1 & -2 & \ldots & \ldots \\
	0 & 3 & \ldots & \ldots \\
	4 & 0 & \ldots & \ldots \\
\end{bmatrix}
\qquad
\begin{bmatrix}
	0 & 3 & 1 & \ldots \\
	0 & 2 & -3 & \ldots \\
	0 & -1 & 6 & \ldots \\
\end{bmatrix}
.\]
In the matrix on the left, we swapped the first and second row.
In the matrix on the right, no action was needed.

\bigbreak
Step 3:
Let the top row be called $R_T$.
For every row below the top row, call it $R_S$, add a suitable multiple of the top row $R_T$ to it (via some choice of elementary row operation $R_S \rightarrow R_S + cR_T$ for some $c\in \mathbb{R}$) so that the entry of $R_S$ immediately below the leading entry of $R_T$ becomes zero.

\[
\begin{bmatrix}
	1 & -2 & \ldots & \ldots \\
	0 & 3 & \ldots & \ldots \\
	0 & 8 & \ldots & \ldots \\
\end{bmatrix}
\qquad
\begin{bmatrix}
	0 & 3 & 1 & \ldots \\
	0 & 0 & -11/3 & \ldots \\
	0 & 0 & 19/3 & \ldots \\
\end{bmatrix}
.\]
In the matrix on the left, we performed $R_3 \rightarrow R_3 -4R_1$.
In the matrix on the right, we performed $R_2 \rightarrow R_2 -\frac{2}{3}R_1$, followed by $R_3 \rightarrow R_3 + \frac{1}{3}R_1$.

\bigbreak
Step 4:
Now, cover the top row in the matrix and begin again with Step 1 applied to the submatrix that remains.
Repeat these steps until you reach the last row.
