.matrix_rank()

Anonymous contributor's avatar
Anonymous contributor
Published Apr 5, 2025
Contribute to Docs

In NumPy, the .matrix_rank() function under the linalg module computes the rank of a matrix, which is a foundational concept in linear algebra. The matrix rank is the dimension of the vector space generated by its columns (column rank) or rows (row rank), which are always equal. It represents the number of linearly independent rows or columns in the matrix.

The matrix rank is crucial for understanding:

  • The solutions to systems of linear equations
  • The invertibility of matrices (a matrix is invertible if its rank equals its dimension)
  • The dimensionality of the image space of a linear transformation

Syntax

numpy.linalg.matrix_rank(input, tol=None, hermitian=False)

Parameters:

  • input: The input array (matrix) whose rank is to be computed.
  • tol (Optional): A threshold value used to determine which singular values are considered zero.
  • hermitian (Optional): If True, input is assumed to be Hermitian (symmetric if real-valued).

Return value:

Returns an integer representing the rank of the input matrix.

Example

The following example demonstrates the usage of the .matrix_rank() function:

import numpy as np
# Create a matrix
A = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Compute the matrix rank
rank = np.linalg.matrix_rank(A)
print("Rank of matrix A:", rank)

The above code produces the following output:

Rank of matrix A: 2

Since the third row is a linear combination of the first two rows, the output is 2.

Codebyte Example

The following codebyte example demonstrates the usage of the .matrix_rank() function:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy