Gaussian elimination

2. Gaussian elimination#

Important!

If you’re completely new to Python, you may want to go through the exercises in the Python fundamentals notebook first!

To solve matrix equations of the form \(A \vec{x} = \vec{b}\), one of the most popular algorithms is Gaussian elimination. It is also known as row reduction, as one applies successive elementary row operations until the lower left-hand corner of the matrix is all zeros. Since you have already practiced this manually, let’s see how we can do the same in Python.

Summary of commands#

In this exercise, we will demonstrate the following:

  • np.linalg.matrix_rank(A) - Returns the rank of a matrix A.

  • np.linalg.inv(A) - Returns the inverse of a matrix A.

  • sympy_matrix.rref()[0] - Returns the reduced row echelon form of a Matrix object in SymPy.

    • This method is from the SymPy library, which is a popular option for symbolic computation. We don’t want to throw too many libraries at you, but this one is the most straightforward way of computing the reduced row-echelon form.

Demo#

Find the rank and solution (if it exists) to the following system of equations:

\[\begin{split} \begin{align*} 7y + 3z &= -12 \\ 2x + 8y + z &= 0 \\ -5x + 2y - 9z &= 26 \end{align*} \end{split}\]

using the reduced row echelon method, inverse method, and Gaussian elimination method.

# initialize the matrix
import numpy as np
A = np.array([[0, 7, 3, -12], [2, 8, 1, 0], [-5, 2, -9, 26]])

# calculate and display its rank
Arank = np.linalg.matrix_rank(A)
print("Rank of A:")
print(Arank)
print()

# calculate and display reduced row echelon form
from sympy import Matrix
A_sympy = Matrix(A)
print("Reduced row echelon form of A:")
display(A_sympy.rref()[0])
print()

# calculate and display inverse method
A = np.array([[0, 7, 3], [2, 8, 1], [-5, 2, -9]])
b = np.array([[-12], [0], [26]])
x = np.linalg.inv(A) @ b
print("Solution of A*x = b:")
print(x)
print()

# solve using Gaussian elimination
x = np.linalg.solve(A, b)
print("Solution of A*x = b:")
print(x)
Rank of A:
3
Reduced row echelon form of A:
\[\begin{split}\displaystyle \left[\begin{matrix}1 & 0 & 0 & 2\\0 & 1 & 0 & 0\\0 & 0 & 1 & -4\end{matrix}\right]\end{split}\]
Solution of A*x = b:
[[ 2.]
 [ 0.]
 [-4.]]

Solution of A*x = b:
[[ 2.]
 [ 0.]
 [-4.]]

Note

The function rref() needs a matrix input in the form of \( A = \begin{bmatrix} x_1 & y_1 & z_1 & \cdots & c_1 \\ x_2 & y_2 & z_2 & \cdots & c_2 \\ \cdots & \cdots & \cdots & \cdots & \cdots \end{bmatrix}\).
For the matrix that rref() produces, the last column contains the solutions for the matrix. The first element is the solution for \(x\), the second for \(y\), and the last for \(z\).