Converting Matrix into Row Echelon Form in Python
Last Updated :
24 Apr, 2025
In this article, we will see how we can convert the matrix into a Row Echelon Form. We will see how we can do this with the help of a Python Program for converting the matrix to Row Echelon Form.
What is the Row Echelon Form?
A matrix is in Row Echelon form if it has the following properties:
- Any row consisting entirely of zeros occurs at the bottom of the matrix.
- For each row that does not contain entirely zeros, the first non-zero entry is 1 (called a leading 1)
- For two successive (non-zero) rows, the leading 1 in the higher row is further left than the leading one in the lower row.
Any matrix can be transformed into a row echelon and reduced row echelon form, using a technique called Gaussian elimination. This is particularly useful for solving systems of linear equations.
Gaussian Elimination
Gaussian Elimination is a way of converting a matrix into the row echelon and reduced row echelon form. It can also be used as a way of finding a solution to a solution to the system of linear equations. The idea behind this is that we perform some mathematical operations on the row and continue until only one variable is left.
Below are some operations that we can perform:
- Interchange any two rows
- Add two rows together.
- Multiply one row by a non-zero constant (i.e. 1/3, -1/5, 2).
Reduce a Matrix to Row Echelon Form
Below are the steps by which we can convert the matrix into Row Echelon Form in Python:
Step 1: Check for Non-Zero Rows
Traverse through first column of the matrix to search for non-zero entries. Below is the function in which we are checking for non-zero rows.
Python3
def find_nonzero_row(matrix, pivot_row, col):
nrows = matrix.shape[0]
for row in range(pivot_row, nrows):
if matrix[row, col] != 0:
return row
return None