3. Gaussian elimination algorithm
Gaussian elimination algorithm allows us to solve systems of linear equations.
Let us have a system of linear equations:
$$ \begin{pmatrix} a_{11}&a_{12}&\dots &a_{1n}\\ a_{21}&a_{22}&\dots &a_{2n}\\ \vdots & \vdots &\ddots&\vdots\\ a_{m1}&a_{m2}&\dots &a_{mn} \end{pmatrix} \begin{pmatrix} x_1\\ x_2\\ \vdots\\ x_m \end{pmatrix}= \begin{pmatrix} b_1\\ b_2\\ \vdots\\ b_m \end{pmatrix} $$
and its augmented matrix:
$$ \left(\begin{array}{rrrr|r} a_{11}&a_{12}&\dots &a_{1n}&b_1\\ a_{21}&a_{22}&\dots &a_{2n}&b_2\\ \vdots & \vdots &\ddots&\vdots&\vdots\\ a_{m1}&a_{m2}&\dots &a_{mn}&b_m \end{array}\right). $$
- Switching two rows.
- Multiplying a row by a nonzero constant.
- Adding to some row a nonzero constant times another row.
The matrix $\begin{pmatrix}-2g & -2h & -2i\\a+d & b+e & c+f\\a & b & c\end{pmatrix}$
has been obtained from the matrix $\begin{pmatrix}a & b & c\\d & e & f\\g & h & i\end{pmatrix}$
by elementary row operations.
Name the elementary row operations used.
Write an IPython function for (a) adding to a constant $l$ times row $i$ a constant $k$
times row $j$ in a given matrix, return the new matrix; (b) switching rows $i$ and $j$ in a given matrix,
return the new matrix.
In terms of the augmented matrix, the first elementary row operation means that we can reorder
equations within the system.
The second elementary row operation means that we can multiply an equation with some constant, i.e.
$ \begin{align*} a_{i1}x_1+\cdots+a_{in}x_n=\beta\qquad\Longleftrightarrow\qquad \gamma a_{i1}x_1+\cdots+\gamma a_{in}x_n=\gamma\beta_i\enspace. \end{align*} $
The third elementary row operation means adding two equations:
$ \begin{align*} \begin{aligned} a_{k1}x_1+\cdots+a_{kn}x_n=\beta_k\\ a_{l1}x_1+\cdots+a_{ln}x_n=\beta_l\\ \end{aligned} \qquad\Longleftrightarrow\qquad (a_{k1}+\gamma a_{l1})x_1+\cdots+(a_{kn}+\gamma a_{ln})x_n=\beta_k+\beta_l.\\ \end{align*} $
For example, let
$ \begin{align*} \left(\begin{array}{rrr|r} 1 & 3 & 6 & 25\\ 2 & 7 & 14 & 58\\ 0 & 2 & 5 & 19\\ \end{array}\right) \quad\overset{R_2-2\cdot R_1}\longrightarrow\quad \left(\begin{array}{rrr|r} 1 & 3 & 6 & 25\\ 0 & 1 & 2 & 8\\ 0 & 2 & 5 & 19\\ \end{array}\right)\enspace, \end{align*} $
then
$ \begin{align*} \left\{ \begin{aligned} 1x + 3y + 6z &= 25\\ 2x + 7y + 14z &= 58\\ 2y + 5z &= 19 \end{aligned} \right. \end{align*}\quad $ and $ \quad\begin{align*} \left\{ \begin{aligned} 1x + 3y + 6z &= 25\\ 1y + 2z &= 8\\ 2y + 5z &= 19 \end{aligned} \right. \end{align*} $
have one and the same solution ($x=1,\ y=6,\ z=3$).
3.1 Simple Gaussian elimination algorithm
Let $m=n$ (i.e. the number of equations equals to the number of unknown) and let the system of linear equations have precisely one solution, that is, there is only one set of values $x_1=c_1$, $x_2=c_2$, …, $x_m=c_m$ for which all the equation in the system are true.
$$ \left(\begin{array}{rrrr|r} a_{11}&a_{12}&\dots &a_{1m}&b_1\\ a_{21}&a_{22}&\dots &a_{2m}&b_2\\ \vdots & \vdots &\ddots&\vdots&\vdots\\ a_{m1}&a_{m2}&\dots &a_{mm}&b_m \end{array}\right)\qquad\overset{\text{elementary row operations}}{\longrightarrow \cdots\longrightarrow}\qquad \left(\begin{array}{rrrr|r} 1&0&\dots &0&c_1\\ 0&1&\dots &0&c_2\\ \vdots & \vdots &\ddots&\vdots&\vdots\\ 0&0&\dots &1&c_m \end{array}\right), $$
i.e. the augmented matrix of a system of linear equations is converted into such diagonal form by using the elementary row operations.
Note that the diagonal form corresponds to the following system of linear equations
$ \begin{align*} \left\{ \begin{aligned} x_1&=c_1\\ x_2&=c_1\\ \cdots&\\ x_n&=c_m \end{aligned}\right. \end{align*} $
and thus, the last column in the diagonal form obtained from the initial augmented matrix by elementary row operations, i.e. $\begin{pmatrix}c_1&\cdots &c_n\end{pmatrix}^T$, is the solution of the original system (see Theorem).
Let us now use Gaussian elimination algorithm for solving the system
$ \begin{align*} \left\{ \begin{aligned} x_2-10x_3&=0\\ 3x_1-x_2-5x_3&=9\\ -2x_1+x_2+3x_3&=-6 \end{aligned}\right. \end{align*}. $
The augmented matrix of this system is
$$ \left(\begin{array}{rrr|r} 0&1&-10&0\\ 3&-1&-5&9\\ -2&1&3&-6 \end{array}\right). $$
STEP 1. (a) Switch the first row of the augmented matrix with rows below till the first element in the first column is nonzero:
$$ \left(\begin{array}{rrr|r} 0&1&-10&0\\ 3&-1&-5&9\\ -2&1&3&-6 \end{array}\right)\quad \overset{\text{switch}\ R_1\ \text{and}\ R_2}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 3&-1&-5&9\\ 0&1&-10&0\\ -2&1&3&-6 \end{array}\right). $$
(b) By using the first element of the first column we zero out the rest of the entries in the first column:
$$ \left(\begin{array}{rrr|r} \color{blue}3&-1&-5&9\\ \color{red}0&1&-10&0\\ \color{red}{-2}&1&3&-6 \end{array}\right) \quad\overset{3R_3+2R_1}{\longrightarrow}\quad \left(\begin{array}{rrr|r} \color{blue}3&-1&-5&9\\ \color{red}0&1&-10&0\\ \color{red}0&1&-1&0 \end{array}\right) $$
Verify the step in IPython by using the functions you created for elementary row operations
(e.g., swap_rows and
add_row_multiples).
Solve the task in IPython for $\mathbb{Z}_{11}$.
STEP 2. (a) Now switch the second row with rows below till the second element in the second column is nonzero, i.e. apply STEP 1 (a) for the submatrix obtained by ignoring the first row and the first column:
$$ \left(\begin{array}{rrr|r} \color{lightgray}3&\color{lightgray}{-1}&\color{lightgray}{-5}&\color{lightgray}9\\ \color{lightgray}0&1&-10&0\\ \color{lightgray}0&1&-1&0 \end{array}\right). $$
In our case the second element of the second row is already nonzero, thus no switching is needed.
(b) By using the second element of the second column we zero out the rest of the entries in the second column:
$$ \left(\begin{array}{rrr|r} 3&\color{red}{-1}&-5&9\\ 0&\color{blue}1&-10&0\\ 0&\color{red}1&-1&0 \end{array}\right) \quad\overset{R_1+R_2}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 3&\color{red}{0}&-15&9\\ 0&\color{blue}1&-10&0\\ 0&\color{red}1&-1&0 \end{array}\right) \quad\overset{R_3-R_2}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 3&\color{red}0&-15&9\\ 0&\color{blue}1&-10&0\\ 0&\color{red}0&9&0 \end{array}\right). $$
Verify the step in IPython (like in step 1).
Solve the task in IPython for $\mathbb{Z}_{11}$.
STEP 3. The third element in the third row is nonzero (if it were zero, the system would have more then one solution, see General Gaussian elimination algorithm) and can be used to zero out the rest of the elements in the third column:
$$ \left(\begin{array}{rrr|r} 3&0&\color{red}{-15}&9\\ 0&1&\color{red}{-10}&0\\ 0&0&\color{blue}{9}&0 \end{array}\right) \quad\overset{9R_1+15R_3}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 27&0&\color{red}{0}&81\\ 0&1&\color{red}{-10}&0\\ 0&0&\color{blue}{9}&0 \end{array}\right) \quad\overset{9R_2+10R_3}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 27&0&\color{red}0&81\\ 0&9&\color{red}0&0\\ 0&0&\color{blue}{9}&0 \end{array}\right). $$
Verify the step in IPython (like in step 2).
Solve the task in IPython for $\mathbb{Z}_{11}$.
THE RESULT. It remains to multiply each row by the inverse element of the value of the leading entry:
$$ \left(\begin{array}{rrr|r} \color{red}{27}&0&0&81\\ 0&\color{red}{9}&0&0\\ 0&0&\color{red}{9}&0 \end{array}\right) \quad\overset{\frac{1}{27}R_1,\ \frac{1}{9}R_2,\ \frac{1}{9}R_3}{\longrightarrow}\quad \left(\begin{array}{rrr|r} \color{blue}{1}&0&0&3\\ 0&\color{blue}{1}&0&0\\ 0&0&\color{blue}{1}&0 \end{array}\right), $$
and thus, $x_1=3,\ x_2=0,\ x_3=0$.
(a) if $a_{ii}=0$ and $i>m$ switch the $i^{th}$ row with a row $j$, where $j>i$ till $a_{ii}$ is nonzero;
(b) use $a_{ii}$ to zero out all of the other elements in the $i^{th}$ column.
Write a function in IPython for performing the $i^{th}$ step of the algorithm.
Write a function in IPython for solving a system of linear equation with the simple Gaussian elimination
algorithm (mind the assumptions for the algorithm; use the function created in the previous exercise
(e.g., gaussian_step)).
Find a solution to the equation $A\mathbf{x}=\mathbf{b}$ when
$$\begin{align*}A=\begin{pmatrix}1 & 1 & 1\\1 & 2 & 4\\1 & 3 & 9\end{pmatrix}\end{align*}$$ and $\mathbf{b}=(1\ 1\ 1)^T$ over $\mathbb{Z}_2$, $\mathbb{Z}_3$ and $\mathbb{Z}_5$.
3.2 General Gaussian elimination algorithm
Let us now use the simple Gaussian elimination algorithm for the system
$ \begin{align*} \left\{ \begin{aligned} 2x_1-3x_2+5x_3&=1\\ 4x_1-6x_2+2x_3&=2\\ 2x_1-3x_2-11x_3&=1\\ \end{aligned}\right.\enspace. \end{align*} $
The first step is
$$ \left(\begin{array}{rrr|r} 2&-3&5&1\\ 4&-6&2&2\\ 2&-3&-11&1 \end{array}\right) \quad\overset{R_2-2R_1}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 2&-3&5&1\\ 0&0&-8&0\\ 2&-3&-11&1 \end{array}\right) \quad\overset{R_3-R_2}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 2&-3&5&1\\ 0&0&-8&0\\ 0&0&-16&0 \end{array}\right). $$
The second step can not be performed, since $a_{22}$ is zero and it can not be changed by switching second row with any row below:
$$ \left(\begin{array}{rrr|r} 2&-3&5&1\\ 0&\color{red}0&-8&0\\ 0&\color{red}0&-16&0 \end{array}\right). $$
So lets skip the second step.
The third step is again standard:
$$ \left(\begin{array}{rrr|r} 2&-3&5&1\\ 0&0&-8&0\\ 0&0&-16&0 \end{array}\right) \quad\overset{16R_1+5R_3}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 32&-48&0&16\\ 0&0&-8&0\\ 0&0&-16&0 \end{array}\right) \quad\overset{2R_2-R_3}{\longrightarrow}\quad \left(\begin{array}{rrr|r} 32&-48&0&16\\ 0&0&0&0\\ 0&0&-16&0 \end{array}\right). $$
Note, that the equation $0x_1+0x_2+0x_3=0$ holds always and hence, does not change the solution of the system. Thus, we may disregard the row and thus, have obtain the system
$ \begin{align*} \left\{ \begin{aligned} 32x_1-48x_2&=16\\ -16x_3&=0\\ \end{aligned}\right.\enspace, \end{align*} $
which is equivalent to
$ \begin{align*} \left\{ \begin{aligned} x_1&=\frac{16}{32}+\frac{48}{32}x_2\\ x_3&=0\\ \end{aligned}\right.\enspace. \end{align*} $
Note, that $x_3$ has a concrete value, $x_1$ has a criteria for evaluating it, and $x_2$ has no boundaries for valuations ($x_2$ is a free variable in this system). Clearly, there isn't just one set of values $x_1=c_1,\ x_2=c_2,\ x_3=c_3$ which satisfy the system of equations, that is, the system has no unique solution. In such a case, the solution is described as a set
$$\left\{\left(\frac{16}{32}+\frac{48}{32}c_2,\ c_2,\ 0\right): c_2\in\mathbb{R}\right\}.$$
By giving $c_2$ a concrete value, for example, let us take $c_2=0$, we obtain one solution of the system: $(\frac{16}{32},0,0)$, but it is not the only solution. Actually, the system has infinitely many solutions, since we can choose infinitely many different values for $c_2$.
Use the IPython function you created for simple Gaussian elimination algoritm (e.g., simple_gaussian) for solving the system
$ \begin{align*} \left\{ \begin{aligned} 2x_1-3x_2+5x_3&=1\\ 4x_1-6x_2+2x_3&=2\\ 2x_1-3x_2-11x_3&=1\\ \end{aligned}\right.\enspace. \end{align*} $
Analyse the result: what should be changed in order to obtain the final augmented matrix from above.
Now apply the changed simple Gaussian elimination to the system
$ \begin{align*} \left\{ \begin{aligned} 2x_1-3x_2-x_3+5x_4&=1\\ 4x_1-6x_2-2x_3+2x_4&=2\\ 2x_1-3x_2-x_3-11x_4&=1\\ \end{aligned}\right.\enspace. \end{align*} $
Do you get the correct answer? Answer
As you can see, the simple Gaussian elimination algorithm does not work in such a case. Next we will generalize the simple Gaussian elimination algorithm in order to be able to solve any (solvable) system.
So, let us have a system of linear equation without any restrictions to $m$ or $n$ or number of solutions
$$ \begin{pmatrix} a_{11}&a_{12}&\dots &a_{1n}\\ a_{21}&a_{22}&\dots &a_{2n}\\ \vdots & \vdots &\ddots&\vdots\\ a_{m1}&a_{m2}&\dots &a_{mn} \end{pmatrix} \begin{pmatrix} x_1\\ x_2\\ \vdots\\ x_m \end{pmatrix}= \begin{pmatrix} b_1\\ b_2\\ \vdots\\ b_m \end{pmatrix}. $$
In the obtained matrix, if $r=n$ and $a_{ii}=1$ for every $i=1,\dots,n$, then the system has a unique solution. The solution is the last column of the augmented matrix, see simple Gaussian elimination algorithm.
If $r<n$, then the system has more than one solution. In the case of solving such a system over $\mathbb{R}$
(i.e. the solution of the system is searched among real numbers), there are usually infinitely many solutions
(see the example) which can not all be listed. There exists theory for
giving the general formula for the solution, however, we will not go into it and just give one
particular solution of the system.
In the case of solving such a system over a finite field, e.g., $\mathbb{Z}_n$, where $n$ is a prime number,
all the solution can be listed ($\mathbb{Z}_n$ has finite number of elements).
If for any row $i$ the only nonzero element is in the last column of the augmented matrix, i.e. in row $i$ only $c_i\neq 0$, then the system has no solution at all. Because then the $i^{th}$ row re-presence the equation
$0x_{1}+0x_{2}+\dots+0x_{n}=c_{i}\quad\Longleftrightarrow\quad 0=c_{i}$
which is never true ($c_{i}$ is nonzero).
Which of the following augmented matrices are the results of general Gaussian elimination algorithm. If some matrix is the result of general Gaussian elimination algorithm, then decide weather it has unique solution or not.
$\left(\begin{array}{rrrrr|r}1&0&0&5&8&0\\0&0&1&2&7&0\\0&0&0&0&0&1\end{array}\right)$ | $\left(\begin{array}{rrrrr|r}1&0&6&5&8&2\\0&0&2&2&7&3\end{array}\right)$ |
Answer | Answer |
$\left(\begin{array}{rrr|r}1&0&0&3\\0&1&0&-2\\0&0&1&10\\0&0&0&0\end{array}\right)$ | $\left(\begin{array}{rrrrr|r}1&0&0&0&0&3\\0&0&9&1&0&-3\\0&1&-2&0&0&2\\0&0&0&0&1&5\end{array}\right)$ |
Answer | Answer |
Note that this time we do not move within the augmented matrix along the diagonal elements a_ii. However, the simple Gaussian elimination is a special case of the general Gaussian elimination: just assume, that all the blocks of arbitrary numbers are with 0 width.
row $i$ and column $j$
if there exists row $k$, $k\leq j$ such that $a_{kj}\neq 0$ then switch rows till $a_{ij}$ is nonzero and by using the new $a_{ij}$ zero out all the rest of the elements in column $j$; next step row $i+1$ and column $j+1$
else next step row $i$ and column $j+1$.
Write a function in IPython for the general Gaussian elimination step.
Write a function in IPython for solving a system of linear equations (no restriction to the system here) with the Gaussian elimination algorithm.
Find all solutions of the system $A\mathbf{x}=\mathbf{b}$ where $$A=\begin{pmatrix}1&1&0\\0&0&1\\0&0&2\end{pmatrix}$$ and $\mathbf{b}=\begin{pmatrix}1&2&4\end{pmatrix}^T$ over $\mathbb{Z}_3$.