Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.linalg.matrix_rank

Returns the rank of an array using an SVD-based method. It supports vectors, matrices, and stacks of matrices, with optional tolerance and Hermitian optimization.

Since
2.0.0 (rtol parameter)

How it works

The function determines rank through singular values computed with SVD. It counts values above the selected threshold; by default, that threshold scales with the largest singular value, matrix dimensions, and floating-point precision. Hermitian input can use a faster singular-value route.

Syntax

numpy.linalg.matrix_rank(A, tol=None, hermitian=False, *, rtol=None)

Parameters

NameTypeDescription
Aarray_likeInput vector, matrix, or stack of matrices with shape (M,), (M, N), or (..., M, N).
tolarray_like, float, optionalThreshold below which singular values are treated as zero. If None, NumPy derives it from the largest singular value, matrix dimensions, and dtype precision.
hermitianbool, optionalAssume A is Hermitian, or real-symmetric; defaults to False.
rtolarray_like, float, optionalRelative-tolerance parameter; defaults to max(M, N) × eps. Cannot be supplied with tol.

Returns

rank — array_like: rank of A.

Key points

  • matrix_rank estimates rank from singular values produced by an SVD-based method.
  • A singular value contributes to rank only when it is greater than the effective tolerance.
  • The input may be a vector, a single matrix, or a stack of matrices.
  • The hermitian option can accelerate processing when the input has the required structure.
  • The rtol parameter was added in NumPy 2.0.0.
  • The return value is the rank of A, with array-shaped output for stacked inputs.

Common mistakes

  • Do not provide tol and rtol together.
  • Set hermitian=True only when the input really has the stated Hermitian or real-symmetric structure.
  • The chosen threshold affects whether small singular values contribute to the reported rank.

Example: Rank a matrix or matrix stack

import numpy as np

A = np.array([[1., 2.], [2., 4.]])
print(np.linalg.matrix_rank(A))  # 1
print(np.linalg.matrix_rank([[[1., 0.], [0., 1.]],
                             [[1., 1.], [2., 2.]]]))  # [2 1]

Computes the rank of one matrix and of a stack of matrices.

When to use it

Detect rank deficiency

Check whether a matrix is full rank before solving or estimating parameters.

Analyze matrix stacks

Evaluate several related matrices in one call.

Customize tolerance

Control numerical rank classification for noisy or ill-conditioned data.

Use the Hermitian optimization

Speed up rank computation for known real-symmetric or Hermitian matrices.

Interview questions

QHow does matrix_rank determine rank?

It uses an SVD-based test and counts singular values greater than the threshold. The result is the matrix rank, or one rank value for each matrix in a stack.

Follow-up: How does the default threshold scale? It uses the largest singular value multiplied by the larger matrix dimension and machine epsilon for the singular-value dtype.

QWhat input shapes are supported?

A can be a vector, a matrix, or a stack whose trailing dimensions are M by N. For a stack, the function computes ranks independently across the leading dimensions.

Follow-up: What changed for stacked inputs? The function supports stacks and broadcasts the tolerance against the stack dimensions.

QWhat is the purpose of hermitian?

hermitian=True declares that A is Hermitian, or symmetric when its values are real, and enables a more efficient singular-value computation. The default is False.

Follow-up: When should it remain False? Keep it false unless the declared structure is guaranteed, because the optimization relies on that assumption.

  • numpy.linalg.svd
  • numpy.linalg.norm
  • numpy.linalg.matrix_power
  • numpy.linalg.solve

Source: numpy.linalg.matrix_rank — NumPy v2.5 Manual. Checked 2026-10-15.