fn

matrix_rank

Tensor
matrix_rank(A: Tensor, tol: float | None = None, hermitian: bool = False)
source

Compute the numerical rank of a matrix.

The numerical rank is the number of singular values of AA that exceed a tolerance:

rank(A)={i:σi>τ}.\mathrm{rank}(A) \,=\, |\{\,i : \sigma_i > \tau\,\}|.

Parameters

ATensor
Input matrix of shape (*, m, n).
tolfloat or None= None
Threshold below which singular values are treated as zero. None (default) uses max(m,n)εσmax\max(m, n) \cdot \varepsilon \cdot \sigma_{\max} with ε\varepsilon the float32 machine epsilon (1.19×107\approx 1.19 \times 10^{-7}).
hermitianbool= False
If True exploit Hermitian structure for a cheaper eigen-based computation. Currently unused (SVD path is always taken).

Returns

Tensor

Integer scalar (or batched scalars) holding the rank.

Notes

Computed via svd so cost is O(min(m,n)2max(m,n))O(\min(m,n)^2 \cdot \max(m,n)). Choosing the tolerance is application-specific — consider scaling by A\|A\| when the singular values span many orders of magnitude.

Examples

>>> import lucid
>>> from lucid.linalg import matrix_rank
>>> matrix_rank(lucid.tensor([[1.0, 2.0], [2.0, 4.0]]))
Tensor(1)