# Numpy Linalg.matrix_rank – Explained

When you want to try your luck with solving a system of linear equations, your odds are better if you have the information on the rank of the matrices beforehand. The possibility of deducing a solution is concrete when the rank of the matrix and the number of variables is alike.

A rank is like a barcode for a matrix from which many valuable details could be retrieved. In case of a square matrix, calculating its determinant would help us deduce its rank. But, as the dimensions become larger, it is better to tap the computational capabilities of Python.

Also read: NumPy linalg.matrix_power: Computing the power of a square matrix

In this article, we shall explore the functionality of a function within the NumPy library of Python that helps us in determining the rank of the matrix. A handful of use case demonstrations would also be done along the way to understand its nuances. Behold! The linalg.matrix_rank( ) function which shall be covered in the following sections.

• Syntax of the linalg.matrix_rank( ) function
• Use cases for the linalg.matrix_rank( ) function

## Syntax of the linalg.matrix_rank( ) function

The linalg.matrix_rank( ) function uses Singular Value Decomposition (SVD) technique to return the rank of the input matrix. Following is its syntax detailing the mandatory and optional constituents required for its functioning.

```linalg.matrix_rank( A, tol=None, hermitian=False)
```

where,

• Input matrix or stack of matrices for which the matrix rank is to be determined
• tol – Set to ‘None’ by default, it is used to specify a threshold value below which the SVD values are considered to be zero
• hermitian – Set to ‘False’ by default, it is used to specify whether the input matrix is Hermitian so as to deploy a more efficient technique for rank deduction

## Use cases for the linalg.matrix_rank( ) function

The rank of any matrix must at least be ‘1’, except for the null matrix for which it is zero. Also, any matrix’s rank cannot be higher than the smallest dimension in that matrix. For instance, if m<n in a matrix of dimensions ‘m x n’ then, the rank of the matrix can never be higher than ‘m’.

Let us start things off by importing the numpy library using the code given below.

```import numpy as np
```

Once done, let us now construct an input matrix whose rank is to be determined.

```A = np.array([[-1, 0, -4, 5],
[2, 9, -8, 6],
[3, -10, 12, 2]])
print (A)
```

The above matrix is of the order 3×4 & now the rank of the matrix shall be calculated using the linalg.matrix_rank( ) function as shown below.

```R = np.linalg.matrix_rank(A)
print ("Rank of matrix:", R)
```

Here the tol is not specified, leaving it to take its default value ‘None’. When this happens the singular values for the input matrix ‘S’ and the epsilon value, ‘eps’ for the datatype of ‘S’ is used for setting tol value. Refer to this numpy guide for more information on this.

The following output is obtained when the aforementioned code is run.

For the same matrix, let us now apply a threshold limit of ‘10’ & calculate its rank. This can be done by using the tol option within the syntax of the linalg.matrix_rank( ) function as shown below.

```R1 = np.linalg.matrix_rank(A, tol=10)
print ("Rank of matrix:", R1)
```

Following is the result for the above code.

Let us now use an identity matrix in order to determine its rank.

```I = np.eye(6)
print (I)
R2 = np.linalg.matrix_rank(I)
print ("Rank of Identity Matrix:", R2)
```

Running the above code yields the following result.

## Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to find the matrix rank using the linalg.matrix_rank( ) function from the numpy library. Here’s another article that details the computation of the pseudo inverse of a matrix using the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est facere!