.matrix_rank()
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): IfTrue
,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 matrixA = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])# Compute the matrix rankrank = 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:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours