MCPcopy Create free account
hub / github.com/numpy/numpy / matrix_rank

Function matrix_rank

numpy/array_api/linalg.py:199–218  ·  view source on GitHub ↗

Array API compatible wrapper for :py:func:`np.matrix_rank `. See its docstring for more information.

(x: Array, /, *, rtol: Optional[Union[float, Array]] = None)

Source from the content-addressed store, hash-verified

197
198# Note: the keyword argument name rtol is different from np.linalg.matrix_rank
199def matrix_rank(x: Array, /, *, rtol: Optional[Union[float, Array]] = None) -> Array:
200 """
201 Array API compatible wrapper for :py:func:`np.matrix_rank <numpy.matrix_rank>`.
202
203 See its docstring for more information.
204 """
205 # Note: this is different from np.linalg.matrix_rank, which supports 1
206 # dimensional arrays.
207 if x.ndim < 2:
208 raise np.linalg.LinAlgError("1-dimensional array given. Array must be at least two-dimensional")
209 S = np.linalg.svd(x._array, compute_uv=False)
210 if rtol is None:
211 tol = S.max(axis=-1, keepdims=True) * max(x.shape[-2:]) * np.finfo(S.dtype).eps
212 else:
213 if isinstance(rtol, Array):
214 rtol = rtol._array
215 # Note: this is different from np.linalg.matrix_rank, which does not multiply
216 # the tolerance by the largest singular value.
217 tol = S.max(axis=-1, keepdims=True)*np.asarray(rtol)[..., np.newaxis]
218 return Array._new(np.count_nonzero(S > tol, axis=-1))
219
220
221# Note: this function is new in the array API spec. Unlike transpose, it only

Callers

nothing calls this directly

Calls 3

_newMethod · 0.80
maxFunction · 0.70
maxMethod · 0.45

Tested by

no test coverage detected