In sagemath, a system of linear equations can be solved with built-in functions matrix(), augment(), solve_right(), solve_left(), rref(), and by row, and column operations.
Gauss Jordan elimination method is to solve \(n\) linear system of equations in \(n\) variables. Gauss Jordon method is similar to the Gauss elimination method, except entries above and below pivot elements are zero.
For simplicity, we take an example and tried to solve it by Gauss Jordon elimination method in sagemath.
Example: Find the solution of the following system of linear equations by Gauss Jordon Elimination method:
\[ 2x-y-z=4\\ 3x+4y-2z=11\\3x-2y+4z=11\]
Matrices Creation
The first step is to make matrices from the system of equations, and then an augmented matrix.
\[A=\left[\begin{array}{rrr}
2 & -1 & -1 \\
3 & 4 & -2 \\
3 & -2 & 4
\end{array}\right]; \quad b=\left[\begin{array}{lll}
4 \\
11 \\
11
\end{array}\right];\quad X=\left[\begin{array}{lll}
x \\
y \\
z
\end{array}\right]\]
For creating matrices in sagemath the following code will be utilized.
Input: A = matrix(QQ, 3,3,[[2,-1, -1],[3,4,-2],[3,-2,4]]);
b = vector([4,11,11]);
Now, making the augmented matrix from \(A\) and \(b\) matrix.
\[A_b=\left[\begin{array}{rrr|l}
2 & -1 & -1 & 4 \\
3 & 4 & -2 & 11 \\
3 & -2 & 4 & 11
\end{array}\right]\]
Input:
Ab = A.augment(b, subdivide=True)
Ab
Output:
[ 2 -1 -1| 4]
[ 3 4 -2|11]
[ 3 -2 4|11]
Reduced Echelon Form
Now, We have to reduce the augmented matrix into a reduced echelon form.
There are two methods one is a direct method in which the built-in function rref() of sagemath can do the whole job.
We can also do it manually step by step with the help of row and column operations.
Direct Method
Now, we have to make the reduced echelon form of the augmented matrix \(A_b\).
Ab.rref() will reduce the matrix into a reduced echelon form.
Input:
Abr = Ab.rref();
Abr
Output:
[1 0 0|3]
[0 1 0|1]
[0 0 1|1]
Abr[:,-1] will extract the last column from the reduced echelon form of the augmented matrix which is the required solution.
Input:
Abr[:,-1]
Output:
[3]
[1]
[1]
\[A_b =\left[\begin{array}{rrr|r}
1 & 0 & 0 & 3 \\
0 & 1 & 0 & 1 \\
0 & 0 & 1 & 1
\end{array}\right]\]
The backward substitution give us \(x=3,\;y=1;\;z=1\).
Manual Step-By-Step Method
In this method, we will use a row or column operation to reduce the augmented matrix into a reduced echelon form.
Here in this example, we are using row operation. Column operation can also be used in the same way.
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|l}
2 & -1 & -1 & 4 \\
3 & 4 & -2 & 11 \\
3 & -2 & 4 & 11
\end{array}\right]\]
In the first step, we are going to swap the first and second row and keep in mind index of rows start from 0.
Input:
Ab.swap_rows(0,1);
Ab
Output:
[ 3 4 -2|11]
[ 2 -1 -1| 4]
[ 3 -2 4|11]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
3 & 4 & -2 & 11 \\
2 & -1 & -1 & 4 \\
3 & -2 & 4 & 11
\end{array}\right] \quad R_{12}\]
Now, we are subtracting \(R_2\) from \(R_1\) by using command add_multiple_row(row1, row2, row2 time).
Input:
Ab.add_multiple_of_row(0,1,-1)
Ab
Output:
[ 1 5 -1| 7]
[ 2 -1 -1| 4]
[ 3 -2 4|11]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 5 & -1 & 7 \\
2 & -1 & -1 & 4 \\
3 & -2 & 4 & 11
\end{array}\right]\quad R_1 – R_2\]
Next, we will subtract two times of \(R_1\) from \(R_2\), and three times \(R_1\) from \(R_3\).
Input:
Ab.add_multiple_of_row(1,0,-2);
Ab.add_multiple_of_row(2,0,-3);
Ab
Output:
[ 1 5 -1| 7]
[ 0 -11 1|-10]
[ 0 -17 7|-10]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 5 & -1 & 7 \\
0 & -11 & 1 & -10 \\
0 & -17 & 7 & -10
\end{array}\right]\quad R_2 – 2R_1\;\text{and} \;R_3 – 3R_1\]
Subtract \(R_2\) from \(R_3\).
Input:
Ab.add_multiple_of_row(2,1,-1);
Ab
Output:
[ 1 5 -1| 7]
[ 0 -11 1|-10]
[ 0 -6 6| 0]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 5 & -1 & 7 \\
0 & -11 & 1 & -10 \\
0 & -6 & 6 & 0
\end{array}\right]\quad R_3 – R_2\]
Multiply \(R_3\) by \(\frac{1}{6}\).
Input:
Ab.rescale_row(2,1/6)
Ab
Output:
[ 1 5 -1| 7]
[ 0 -11 1|-10]
[ 0 -1 1| 0]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 5 & -1 & 7 \\
0 & -11 & 1 & -10 \\
0 & -1 & 1 & 0
\end{array}\right]\quad \frac{1}{6}R_3 \]
Subtract \(R_3\) from \(R_2\).
Input:
Ab.add_multiple_of_row(1,2,-1);
Ab
Output:
[ 1 5 -1| 7]
[ 0 -10 0|-10]
[ 0 -1 1| 0]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 5 & -1 & 7 \\
0 & -10 & 0 & -10 \\
0 & -1 & 1 & 0
\end{array}\right]\quad R_2-R_3 \]
Multiply \(R_2\) by \(-\frac{1}{10}\).
Input:
Ab.rescale_row(1,-1/10)
Ab
Output:
[ 1 5 -1| 7]
[ 0 1 0| 1]
[ 0 -1 1| 0]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 5 & -1 & 7 \\
0 & 1 & 0 & 1 \\
0 & -1 & 1 & 0
\end{array}\right]\quad -\frac{1}{10}R_2 \]
Add \(R_2\) and \(R3\).
Input:
Ab.add_multiple_of_row(2,1,1);
Ab
Output:
[ 1 5 -1| 7]
[ 0 1 0| 1]
[ 0 0 1| 1]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 5 & -1 & 7 \\
0 & 1 & 0 & 1 \\
0 & 0 & 1 & 1
\end{array}\right]\quad R_2+R_3 \]
Add \(R_1\) and \(R3\).
Input:
Ab.add_multiple_of_row(0,2,1);
Ab
Output:
[1 5 0|8]
[0 1 0|1]
[0 0 1|1]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 5 & 0 & 8 \\
0 & 1 & 0 & 1 \\
0 & 0 & 1 & 1
\end{array}\right]\quad R_1+R_3 \]
Add \(R_1\) to \(R_2\) after multiplying \(R_2\) by \(5\).
Input:
Ab.add_multiple_of_row(0,1,-5);
Ab
Output:
[1 0 0|3]
[0 1 0|1]
[0 0 1|1]
\[\underset{\sim}{R} \;\left[\begin{array}{rrr|r}
1 & 0 & 0 & 3 \\
0 & 1 & 0 & 1 \\
0 & 0 & 1 & 1
\end{array}\right]\quad R_1+5R_3 \]
So, \(x=3,\;y=1;\;z=1\).
References
Andrilli, S., & Hecker, D. (2010). Systems of Linear Equations. In Elsevier eBooks (pp. 79–142). https://doi.org/10.1016/b978-0-12-374751-8.00018-4
Mathematical Methods. (n.d.). Google Books. https://books.google.com.pk/books/about/Mathematical_Methods.html?id=s5ACkgEACAAJ
Lipschutz, S., & Lipson, M. (2008). Schaum’s Outline of Linear Algebra Fourth Edition. McGraw Hill Professional.
Lay, D. C. (2012). Linear Algebra and Its Applications. Addison Wesley Longman.