matrix_rank¶
- paddle.linalg. matrix_rank ( x: Tensor, tol: float | Tensor | None = None, hermitian: bool = False, atol: float | Tensor | None = None, rtol: float | Tensor | None = None, name: str | None = None ) Tensor [source]
-
Computes the rank of a matrix.
Notes
Support the use of attribute tol alone or the use of attributes atol and rtol together without tol.
2. When tol is used alone, it will return the rank of a matrix is the number of singular values that are greater than the specified tol threshold when hermitian=False, or the number of eigenvalues in absolute value that are greater than the specified tol threshold when hermitian=True. It is compatible with numpy API.
3. When atol and rtol are used, the tolerance value is computed as max(atol, sigma_1 * rtol), where sigma_1 is largest singular value (or eigenvalues in absolute value).
4. When atol and rtol are used: If rtol is not specified, then it is set to be max(m,n) * eps, where x has dimension(m, n) and eps is the epsilon value for the dtype of x; If rtol is not specified and atol is specified to be greater than 0, then it is set to be 0.
- Parameters
-
x (Tensor) – The input tensor. Its shape should be […, m, n], where … is zero or more batch dimensions. If x is a batch of matrices then the output has the same batch dimensions. The data type of x should be float32 or float64.
tol (float|Tensor, optional) – The tolerance value. If tol is not specified, and sigma is the largest singular value (or eigenvalues in absolute value), and eps is the epsilon value for the dtype of x, then tol is computed with formula tol=sigma * max(m,n) * eps. Note that if x is a batch of matrices, tol is computed this way for every batch. Default: None.
hermitian (bool, optional) – Indicates whether x is Hermitian. Default: False. When hermitian=True, x is assumed to be Hermitian, enabling a more efficient method for finding eigenvalues, but x is not checked inside the function. Instead, We just use the lower triangular of the matrix to compute. Default: False.
atol (float|Tensor, optional) – The absolute tolerance value. When None it is considered to be 0. Default: None.
rtol (float|Tensor, optional) – The relative tolerance value. See above Notes for the value it takes when None. Default: None.
name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
- Returns
-
Rank of tensor x.
- Return type
-
Tensor
Examples
>>> import paddle >>> a = paddle.eye(10) >>> b = paddle.linalg.matrix_rank(a) >>> print(b) Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True, 10) >>> c = paddle.ones(shape=[3, 4, 5, 5]) >>> d = paddle.linalg.matrix_rank(c, tol=0.01, hermitian=True) >>> print(d) Tensor(shape=[3, 4], dtype=int32, place=Place(cpu), stop_gradient=True, [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]])