Python program to find the Reduced Row Echelon Form of a matrix


Gaming Laptop Series

We call a matrix is in echelon or row echelon form if it meets the following conditions –

  • The leading entry in each non-zero row of the given matrix is a 1 (also known as the leading coefficient).
  • Every leading 1 is to the right of the row above it.
  • Any non-zero rows are always above the row with all zeros.

The following are some examples of the row echelon form of a matrix.

Example 1:

The given 3×5 matrix is in its row echelon form.

echelon form

Example 2:

echelon form

A matrix is in Reduced Row Echelon Form (RREF) if it satisfies the given conditions –

  • The matrix is in row echelon form
  • The leading entry in each nonzero row is a 1
  • Each column containing a leading 1 has zeros in all its other entries

Example 1:

The given matrix is in its reduced row echelon form.

reduced row echelon form

Example 2:

reduce echelon form

Any matrix can be transformed into row echelon or reduced row echelon form by using the Gaussian elimination method which includes a number of elementary row operations.

There are three types of elementary row operations –

  • Swapping two rows
  • Multiplying a row by a nonzero number
  • Adding or subtracting a multiple of one row to another row

Python program to find Reduced Row Echelon Form of a Matrix

SymPy is an open-source python library for symbolic computation. We will use rref() method of this module to find the reduced row echelon form of a matrix.

But before you use it in your program first you need to download the SymPy package on your system, for that use –

sudo pip3 install sympy

Once it gets installed you can import this library into your Python program.

# Python program to transform a matrix in RREF

# import sympy
from sympy import *

# Enter a matrix
myMatrix = Matrix([[1, 0, 7, 3, 2], [0, 6, 4, 7, 3], [7,-1, -3, -5, -4]])
print("Matrix : {} ".format(myMatrix))

# Using sympy.rref() method to find rref
matrix_rref = myMatrix.rref()

print("The Row echelon form of given matrix and the pivot column is: {}".format(matrix_rref))

When you execute this program it will display the given output.

matrix rref output

Similarly, you enter a matrix with mxn dimension to transform it into reduced row echelon form.

Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.