sparsesvd: Singular Value Decomposition of a Sparse Matrix.

Description Usage Arguments Value References See Also Examples

View source: R/sparsesvd.R

Description

Compute the (usually truncated) singular value decomposition (SVD) of a sparse real matrix. This function is a shallow wrapper around the SVDLIBC implementation of Berry's (1992) single Lanczos algorithm.

Usage

1
  sparsesvd(M, rank=0L, tol=1e-15, kappa=1e-6)

Arguments

M

a sparse real matrix in Matrix package format. The preferred format is a dgCMatrix and other storage formats will automatically be converted if possible.

rank

an integer specifying the desired number of singular components, i.e. the rank of the truncated SVD. Specify 0 to return all singular values of magnitude larger than tol (default).

tol

exclude singular values whose magnitude is smaller than tol

kappa

accuracy parameter κ of the SVD algorithm (with SVDLIBC default)

Value

The truncated SVD decomposition

M_r = U_r D V_r^T

where M_r is the optimal rank r approximation of M. Note that r may be smaller than the requested number rank of singular components.

The returned value is a list with components

d

a vector containing the first r singular values of M

u

a column matrix of the first r left singular vectors of M

v

a column matrix of the first r right singular vectors of M

References

http://tedlab.mit.edu/~dr/SVDLIBC/

Berry, Michael~W. (1992). Large scale sparse singular value computations. International Journal of Supercomputer Applications, 6, 13–49.

See Also

svd, sparseMatrix

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
M <- rbind(
  c(20, 10, 15,  0,  2),
  c(10,  5,  8,  1,  0),
  c( 0,  1,  2,  6,  3),
  c( 1,  0,  0, 10,  5))
M <- Matrix::Matrix(M, sparse=TRUE)
print(M)

res <- sparsesvd(M, rank=2L) # compute first 2 singular components
print(res, digits=3)

M2 <- res$u %*% diag(res$d) %*% t(res$v) # rank-2 approximation
print(M2, digits=1)

print(as.matrix(M) - M2, digits=2) # approximation error

Example output

4 x 5 sparse Matrix of class "dgCMatrix"
                  
[1,] 20 10 15  . 2
[2,] 10  5  8  1 .
[3,]  .  1  2  6 3
[4,]  1  .  . 10 5
$d
[1] 30.4 13.0

$u
       [,1]    [,2]
[1,] 0.8880 -0.0831
[2,] 0.4523 -0.0068
[3,] 0.0652  0.5110
[4,] 0.0509  0.8555

$v
       [,1]    [,2]
[1,] 0.7353 -0.0676
[2,] 0.3690 -0.0274
[3,] 0.5620 -0.0216
[4,] 0.0445  0.8968
[5,] 0.0733  0.4358

     [,1] [,2] [,3] [,4] [,5]
[1,] 19.9 10.0 15.2  0.2    2
[2,] 10.1  5.1  7.7  0.5    1
[3,]  1.0  0.5  1.0  6.0    3
[4,]  0.4  0.3  0.6 10.0    5
       [,1]   [,2]  [,3]    [,4]   [,5]
[1,]  0.094  0.019 -0.18 -0.2359  0.492
[2,] -0.108 -0.071  0.28  0.4671 -0.969
[3,] -1.009  0.450  1.03 -0.0232 -0.029
[4,]  0.611 -0.268 -0.63 -0.0054  0.058

sparsesvd documentation built on Feb. 15, 2018, 3:01 p.m.