matrix_rank

Contents

matrix_rank#

class saiunit.linalg.matrix_rank(M, rtol=None, *, tol=None, **kwargs)#

Compute the rank of a matrix.

SaiUnit implementation of numpy.linalg.matrix_rank().

The rank is calculated via the Singular Value Decomposition (SVD) and determined by the number of singular values greater than the specified tolerance. The unit is stripped before computation and the result is always a dimensionless integer array.

Parameters:
  • M (Array | ndarray | bool | number | bool | int | float | complex | saiunit.Quantity) – Input of shape (..., N, K) whose rank is to be computed. If M carries a unit, the unit is removed before computation.

  • rtol (Array | ndarray | bool | number | bool | int | float | complex | saiunit.Quantity | None) – Relative tolerance. Singular values smaller than rtol * largest_singular_value are considered to be zero. If None (the default), a reasonable default is chosen based on the floating-point precision of the input.

  • tol (Array | ndarray | bool | number | bool | int | float | complex | None) – Deprecated alias for rtol. Will result in a DeprecationWarning if used.

Returns:

out – Matrix rank of shape M.shape[:-2], as a dimensionless integer array.

Return type:

Array

See also

saiunit.linalg.cond

Condition number of a matrix.

saiunit.linalg.slogdet

Sign and log-determinant of a matrix.

Notes

The rank calculation may be inaccurate for matrices with very small singular values or those that are numerically ill-conditioned. Consider adjusting the rtol parameter or using a more specialised rank-computation method in such cases.

Examples

Full-rank matrix:

>>> import saiunit as u
>>> import jax.numpy as jnp
>>> a = jnp.array([[1., 2.],
...                [3., 4.]]) * u.meter
>>> u.linalg.matrix_rank(a)
Array(2, dtype=int32)

Rank-deficient matrix:

>>> import saiunit as u
>>> import jax.numpy as jnp
>>> b = jnp.array([[1., 0.],
...                [0., 0.]]) * u.meter
>>> u.linalg.matrix_rank(b)
Array(1, dtype=int32)