| rankMatrix {Matrix} | R Documentation |
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.
rankMatrix(x, tol = NULL,
method = c("tolNorm2", "qrLINPACK", "useGrad", "maybeGrad"),
sval = svd(x, 0, 0)$d, warn.t = TRUE)
x |
numeric matrix, of dimension n x m, say. |
tol |
nonnegative number specifying a tolerance for
“practically zero” with specific meaning depending on
|
method |
a character string specifying the computational method, can be abbreviated:
|
sval |
numeric vector of non-increasing singular values of
|
warn.t |
logical indicating if |
positive integer in 1:min(dim(x)), with attributes detailing
the method used.
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.
Martin Maechler; for the "*Grad" methods, building on suggestions by Ravi Varadhan.
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 )