Examples for 'Matrix::rcond'


Estimate the Reciprocal Condition Number

Aliases: rcond rcond,ANY,missing-method rcond,denseMatrix,character-method rcond,dgeMatrix,character-method rcond,dpoMatrix,character-method rcond,dppMatrix,character-method rcond,dspMatrix,character-method rcond,dsyMatrix,character-method rcond,dtpMatrix,character-method rcond,dtrMatrix,character-method rcond,sparseMatrix,character-method

Keywords: array algebra

### ** Examples

x <- Matrix(rnorm(9), 3, 3)
rcond(x)
[1] 0.05210746
## typically "the same" (with more computational effort):
1 / (norm(x) * norm(solve(x)))
[1] 0.05210746
rcond(Hilbert(9))  # should be about 9.1e-13
[1] 9.093827e-13
## For non-square matrices:
rcond(x1 <- cbind(1,1:10))# 0.05278
[1] 0.05278005
rcond(x2 <- cbind(x1, 2:11))# practically 0, since x2 does not have full rank
[1] 1.144414e-17
## sparse
(S1 <- Matrix(rbind(0:1,0, diag(3:-2))))
8 x 6 sparse Matrix of class "dgCMatrix"
                  
[1,] . 1 . 1  .  1
[2,] . . . .  .  .
[3,] 3 . . .  .  .
[4,] . 2 . .  .  .
[5,] . . 1 .  .  .
[6,] . . . .  .  .
[7,] . . . . -1  .
[8,] . . . .  . -2
rcond(S1)
Warning in .local(qr, complete, ...): qr.R(<sparse>) may differ from
qr.R(<dense>) because of permutations.  Possibly use our qrR() instead
Warning in rcond(if (d[1L] == d[2L]) {: rcond(.) via sparse -> dense coercion
[1] 0.2992542
m1 <- as(S1, "denseMatrix")
all.equal(rcond(S1), rcond(m1))
Warning in .local(qr, complete, ...): qr.R(<sparse>) may differ from
qr.R(<dense>) because of permutations.  Possibly use our qrR() instead

Warning in .local(qr, complete, ...): rcond(.) via sparse -> dense coercion
[1] "Mean relative difference: 0.254644"
## wide and sparse
rcond(Matrix(cbind(0, diag(2:-1))))
Warning in .local(qr, complete, ...): qr.R(<sparse>) may differ from
qr.R(<dense>) because of permutations.  Possibly use our qrR() instead

Warning in .local(qr, complete, ...): rcond(.) via sparse -> dense coercion
[1] 0
## Large sparse example ----------
m <- Matrix(c(3,0:2), 2,2)
M <- bdiag(kronecker(Diagonal(2), m), kronecker(m,m))
36*(iM <- solve(M)) # still sparse
8 x 8 sparse Matrix of class "dtCMatrix"
                           
[1,] 12 -6  .  . .  .  .  .
[2,]  . 18  .  . .  .  .  .
[3,]  .  . 12 -6 .  .  .  .
[4,]  .  .  . 18 .  .  .  .
[5,]  .  .  .  . 4 -2 -2  1
[6,]  .  .  .  . .  6  . -3
[7,]  .  .  .  . .  .  6 -3
[8,]  .  .  .  . .  .  .  9
MM <- kronecker(Diagonal(10), kronecker(Diagonal(5),kronecker(m,M)))
dim(M3 <- kronecker(bdiag(M,M),MM)) # 12'800 ^ 2
[1] 12800 12800
if(interactive()) ## takes about 2 seconds if you have >= 8 GB RAM
  system.time(r <- rcond(M3))
Warning in rcond(if (d[1L] == d[2L]) {: rcond(.) via sparse -> dense coercion
Warning in asMethod(object): sparse->dense coercion: allocating vector of size
1.2 GiB
   user  system elapsed 
  0.969   2.104   4.247 
## whereas this is *fast* even though it computes  solve(M3)
system.time(r. <- rcond(M3, useInv=TRUE))
       user      system     elapsed 
0.006000000 0.000000000 0.007000003 
if(interactive()) ## the values are not the same
  c(r, r.)  # 0.05555 0.013888
[1] 0.05555556 0.01388889
## for all 4 norms available for sparseMatrix :
cbind(rr <- sapply(c("1","I","F","M"),
             function(N) rcond(M3, norm=N, useInv=TRUE)))
          [,1]
1 1.388889e-02
I 7.812500e-03
F 2.059462e-05
M 3.292181e-02
## Don't show: 
stopifnot(all.equal(r., 1/72, tolerance=1e-12))
## End(Don't show)

[Package Matrix version 1.5-3 Index]