Nullity and Rank of a Matrix in C++

Hello there! In this article, we will learn to find the Nullity and Rank of a matrix in C++.

Nullspace, Nullity, and Rank of a Matrix

If M is a p x q matrix, then the Nullspace of the matrix is the set of all n-dimensional column vectors N such that MN=0.

The dimension of the null space is called the nullity of the matrix.
The dimension of the row space is called the rank of a matrix.
Rank + Nullity = Number of columns of a Matrix.

Example :

The Rank of the above matrix is 2 as there are two independent rows in the matrix i.e, row 2 and row 3. Therefore, the Nullity of the matrix will be the number of columns in the matrix-Rank which will be 1 for the above matrix.

Finding Rank and Nullity of Matrix in C++

1. Taking Input

First, Take the number of rows and columns as input and store them into the variables n and m respectively.

Next, take the matrix values from the input and store them in the declared matrix called mat[n][m].

2. Making the matrix ready for row transformations

Now we have to make our matrix ready for the row reducing transformations.

If mat[0][0] is zero, we use the Z variable initialized with -1 and two for loops iterating over columns and rows to find the respective row number to be exchanged with the first row.

Z value doesn’t change if there is no such row else it stores the value of the row to be exchanged. In case Z is not -1 we exchange the rows.

If all the values in the first column are 0 then we go for the second column values in the respective rows and repeat the process.

3. Performing row transformations to achieve echelon form

Next, we convert the matrix to its row echelon form by performing row transformations.

It requires 3 for loops to perform the row transformation. First for loop will iterate k from 0 th row to n-2 th row
and second for loop will iterate j from k+1 th row to n-1 th row.

Store mat[k][k] into p variableĀ and mat[i][k] into q variable to use in the formula for performing row transformations.

Next, iterate the variable j from 0 to m-1 and perform row transformation using the formula.
mat[i][j]=p*mat[i][j] – q*mat[k][j]

Now,we shall find the rank of the matrix.

4. Finding rank of the matrix

Initialize the variable Z to 0(We use Z here to find the number of independent rows in the matrix.)

Use two for loops, the first one iterates over the rows and the second one over the columns. After the first for loop, we use a variable c to count the number of zeros in each row. if c equals m we increment the z.

Now, the Rank of the matrix can be found by using the formula n-Z. So store the value of n-Z into R which is the rank of the matrix.

Now store the value of m-R into the N variable which represents the Nullity value.

Finally, we print the values of Rank and Nullity.

C++ Program to find Nullity and Rank of a Matrix

using namespace std;
int main()
  int mat[10][10],i,j,n,k,m,r,N,z,R,p,q,v;
        for( v=0;v<m;v++){
          int t=mat[0][v];
        mat[i][j]=p*mat[i][j] - q*mat[k][j];
    int c=0;
  cout<<"Rank = "<<R<<endl;
  cout<<"Nullity = "<<N<<endl;


4 4
0 0 -1 0
0 0 -1 0
1 0 0 0
1 0 1 0


Rank = 2
Nullity = 2

Hurrah! we have successfully learned how to find the rank and nullity of a matrix using C++.

Thank you for reading the article, I hope you have found it useful.

Also do check out our other related articles below :

Shuffle a matrix elements in C++

Matrix chain multiplication in C++

Leave a Reply

Your email address will not be published.