flopscope.numpy.linalg.matrix_rank
fnp.linalg.matrix_rank(A: 'ArrayLike', tol: 'float | None' = None, hermitian: 'bool' = False, *, rtol: 'float | None' = None) -> 'FlopscopeArray | int'[flopscope source][numpy source]
Return matrix rank of array using SVD method
Adapted from NumPy docs np.linalg.matrix_rank
Matrix rank. Cost: m*n*min(m,n) (via SVD).
Rank of the array is the number of singular values of the array that are
greater than tol.
Parameters
- A:{(M,), (..., M, N)} array_like
Input vector or stack of matrices.
- tol:(...) array_like, float, optional
Threshold below which SVD values are considered zero. If
tolis None, andSis an array with singular values forM, andepsis the epsilon value for datatype ofS, thentolis set toS.max() * max(M, N) * eps.- hermitian:bool, optional
If True,
Ais assumed to be Hermitian (symmetric if real-valued), enabling a more efficient method for finding singular values. Defaults to False.- rtol:(...) array_like, float, optional
Parameter for the relative tolerance component. Only
tolorrtolcan be set at a time. Defaults tomax(M, N) * eps.Added in version 2.0.0.
Returns
- rank:(...) array_like
Rank of A.
Notes
The default threshold to detect rank deficiency is a test on the magnitude
of the singular values of A. By default, we identify singular values
less than S.max() * max(M, N) * eps as indicating rank deficiency
(with the symbols defined above). This is the algorithm MATLAB uses [1].
It also appears in Numerical recipes in the discussion of SVD solutions
for linear least squares [2].
This default threshold is designed to detect rank deficiency accounting
for the numerical errors of the SVD computation. Imagine that there
is a column in A that is an exact (in floating point) linear combination
of other columns in A. Computing the SVD on A will not produce
a singular value exactly equal to 0 in general: any difference of
the smallest SVD value from 0 will be caused by numerical imprecision
in the calculation of the SVD. Our threshold for small SVD values takes
this numerical imprecision into account, and the default threshold will
detect such numerical rank deficiency. The threshold may declare a matrix
A rank deficient even if the linear combination of some columns of A
is not exactly equal to another column of A but only numerically very
close to another column of A.
We chose our default threshold because it is in wide use. Other thresholds
are possible. For example, elsewhere in the 2007 edition of Numerical
recipes there is an alternative threshold of S.max() *
flops.finfo(A.dtype).eps / 2. * flops.sqrt(m + n + 1.). The authors describe
this threshold as being based on "expected roundoff error" (p 71).
The thresholds above deal with floating point roundoff error in the
calculation of the SVD. However, you may have more information about
the sources of error in A that would make you consider other tolerance
values to detect effective rank deficiency. The most useful measure
of the tolerance depends on the operations you intend to use on your
matrix. For example, if your data come from uncertain measurements with
uncertainties greater than floating point epsilon, choosing a tolerance
near that uncertainty may be preferable. The tolerance may be absolute
if the uncertainties are absolute rather than relative.
References
1
MATLAB reference documentation, "Rank"
https://www.mathworks.com/help/techdoc/ref/rank.html2
W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery,
"Numerical Recipes (3rd edition)", Cambridge University Press, 2007,
page 795.Examples
>>> import flopscope.numpy as fnp
>>> from flops.linalg import matrix_rank
>>> matrix_rank(flops.eye(4)) # Full rank matrix
4
>>> I=flops.eye(4); I[-1,-1] = 0. # rank deficient matrix
>>> matrix_rank(I)
3
>>> matrix_rank(flops.ones((4,))) # 1 dimension - rank 1 unless all 0
1
>>> matrix_rank(flops.zeros((4,)))
0