Gaussian Elimination also known as row reduction process that uses these operations:
Swapping two rows,
Multiplying a row by a nonzero scalar,
Adding a multiple of one row to another.
Using these operations, the goal is to transformed the matrix into echelon form.
Echelon form is more less an upper triangular matrix which has zeros below the main diagonal (from top left to botton right) and it is also unique. Here is an example.
[ 2 1 − 1 − 3 − 1 2 − 2 1 2 ] [ 8 − 11 3 ] ⟶ [ 2 1 − 1 0 1 2 1 2 0 0 − 1 ] [ 8 1 1 ] \begin{bmatrix}
2 & 1 & -1\\
-3 & -1 & 2\\
-2 & 1 & 2\\
\end{bmatrix}
\begin{bmatrix}
8\\
-11\\
3\\
\end{bmatrix}
\longrightarrow
\begin{bmatrix}
2 & 1 & -1\\
0 & \frac{1}{2} & \frac{1}{2}\\
0 & 0 & -1\\
\end{bmatrix}
\begin{bmatrix}
8\\
1\\
1\\
\end{bmatrix} 2 − 3 − 2 1 − 1 1 − 1 2 2 8 − 11 3 ⟶ 2 0 0 1 2 1 0 − 1 2 1 − 1 8 1 1
How to get the echelon form programatically?
Remember we can do operation mentioned above, from that we can make a generic like:
L i = L i − ϕ L i + 1 L_i = L_i - \phi L_{i+1} L i = L i − ϕ L i + 1
where L L L is the row and i i i is the row index and ϕ \phi ϕ is a scaling factor obtained from
ϕ = e ( j , i ) e ( i , i ) \phi = \frac{e_{(j,i)}}{e_{(i,i)}} ϕ = e ( i , i ) e ( j , i )
where j j j is the column index.
Step 1:
for example reducing the L 2 L_2 L 2 from the matrix above
the ϕ \phi ϕ will be the element at 0,0 and 1,0:
ϕ = e 0 , 0 e 1 , 0 = − 3 1 \phi = \frac{e_{0,0}}{e_{1,0}} = \frac{-3}{1} ϕ = e 1 , 0 e 0 , 0 = 1 − 3
then do operation
L 2 = L 2 − − 3 1 L 1 L_2 = L_2 - \frac{-3}{1} L_1 L 2 = L 2 − 1 − 3 L 1
so the L 2 L_2 L 2 becomes
[ 2 1 − 1 0 1 2 1 2 − 2 1 2 ] [ 8 1 3 ] \begin{bmatrix}
2 & 1 & -1\\
0 & \frac{1}{2} & \frac{1}{2}\\
-2 & 1 & 2\\
\end{bmatrix}
\begin{bmatrix}
8\\
1\\
3\\
\end{bmatrix} 2 0 − 2 1 2 1 1 − 1 2 1 2 8 1 3
After that do same operation for the L 3 L_3 L 3 ,
calculate the ϕ \phi ϕ
ϕ = e 0 , 0 e 2 , 0 = 2 − 2 \phi = \frac{e_{0,0}}{e_{2,0}} = \frac{2}{-2} ϕ = e 2 , 0 e 0 , 0 = − 2 2
then L 3 L_3 L 3 becomes,
[ 2 1 − 1 0 1 2 1 2 0 2 1 ] [ 8 1 5 ] \begin{bmatrix}
2 & 1 & -1\\
0 & \frac{1}{2} & \frac{1}{2}\\
0 & 2 & 1\\
\end{bmatrix}
\begin{bmatrix}
8\\
1\\
5\\
\end{bmatrix} 2 0 0 1 2 1 2 − 1 2 1 1 8 1 5
Why not do L 3 = L 3 + L 1 L_3=L_3+L_1 L 3 = L 3 + L 1 directly?
It's because we need to do the same operation for all rows, otherwise it will be hard to code
Step 2
Now, it's done for the second column, move to the third columns.
This time what is the ϕ \phi ϕ will be ?
ϕ = e 1 , 1 e 2 , 1 = 2 1 2 \phi = \frac{e_{1,1}}{e_{2,1}} = \frac{2}{\frac{1}{2}} ϕ = e 2 , 1 e 1 , 1 = 2 1 2
Do we need to operate the first row?
NO, because we only need to make zeros below the main diagonal.
now the L 3 L_3 L 3 will be
[ 2 1 − 1 0 1 2 1 2 0 0 − 1 ] [ 8 1 1 ] \begin{bmatrix}
2 & 1 & -1\\
0 & \frac{1}{2} & \frac{1}{2}\\
0 & 0 & -1\\
\end{bmatrix}
\begin{bmatrix}
8\\
1\\
1\\
\end{bmatrix} 2 0 0 1 2 1 0 − 1 2 1 − 1 8 1 1
Is it finished?
Yes, because it's all zeros below the main diagonal
Step 3
Now, we can get the value of Z and from that we do back substitution for Y and X