rankMatrix {Matrix}R Documentation

Rank of a Matrix

Description

Compute ‘the’ matrix rank, a well-defined functional in theory, somewhat ambigous in practice. We provide several methods, the default corresponding to Matlab's definition.

Usage

rankMatrix(x, tol = NULL,
           method = c("tolNorm2", "qrLINPACK", "useGrad", "maybeGrad"),
           sval = svd(x, 0, 0)$d, warn.t = TRUE)

Arguments

x

numeric matrix, of dimension n x m, say.

tol

nonnegative number specifying a tolerance for “practically zero” with specific meaning depending on method; by default, max(dim(x)) * .Machine$double.eps * abs(max(sval)) is according to Matlab's default (for its only method "tolNorm2").

method

a character string specifying the computational method, can be abbreviated:

tolNorm2

the number of singular values >= tol;

qrLINPACK

this is the rank of qr(x, tol, LAPACK=FALSE), which is qr(...)$rank for a dense matrix, and the rank of R for sparse x (where qr uses a "sparseQR" method, see qr-methods, and not LINPACK).

This used to be the recommended way to compute a matrix rank for a while in the past. For this method, sval are not used (nor computed), which may be crucially important for a large sparse matrix x.

useGrad

considering the “gradient” of the (decreasing) singular values, the index of the smallest gap.

maybeGrad

choosing method "useGrad" only when that seems reasonable; otherwise using "tolNorm2".

sval

numeric vector of non-increasing singular values of x; typically unspecified and computed from x when needed, i.e., unless method = "qrLINPACK".

warn.t

logical indicating if rankMatrix() should warn when it needs t(x) instead of x. Currently, for method = "qrLINPACK" only.

Value

positive integer in 1:min(dim(x)), with attributes detailing the method used.

Note

For large sparse matrices x, unless you can specify sval yourself, currently method = "qrLINPACK" may be the only feasible one, as the others need sval and call svd() which currently coerces x to a denseMatrix which may be very slow or impossible, depending on the matrix dimensions.

Author(s)

Martin Maechler; for the "*Grad" methods, building on suggestions by Ravi Varadhan.

See Also

qr, svd.

Examples

rankMatrix(cbind(1, 0, 1:3)) # 2

(meths <- eval(formals(rankMatrix)$method))

## a "border" case:
H12 <- Hilbert(12)
rankMatrix(H12, tol = 1e-20) # 12;  but  11  with default method & tol.
sapply(meths, function(.m.) rankMatrix(H12, method = .m.))
## tolNorm2 qrLINPACK   useGrad maybeGrad
##       11        12        11        11

## "sparse" case:
M15 <- kronecker(diag(x=c(100,1,10)), Hilbert(5))
sapply(meths, function(.m.) rankMatrix(M15, method = .m.))
#--> all 15, but 'useGrad' has 14.

## "large" sparse
n <- 200000; p <- 33; nnz <- 10000
L <- sparseMatrix(i = sample.int(n, nnz, replace=TRUE),
                  j = sample.int(p, nnz, replace=TRUE), x = rnorm(nnz))
(st1 <- system.time(r1 <- rankMatrix(L)))                # almost 1 sec
(st2 <- system.time(r2 <- rankMatrix(L, method = "qr"))) # considerably faster!
r1[[1]] == print(r2[[1]]) ## -->  ( 33  TRUE )

[Package Matrix version 1.0-14 Index]