sparseLRMatrix
provides a single matrix S4 class called
sparseLRMatrix
which represents matrices that can be
expressed as the sum of sparse matrix and a low rank matrix. We also
provide an efficient SVD method for these matrices by wrapping the
RSpectra
SVD implementation.
Eventually, we will fully subclass Matrix::Matrix
objects, but the current implementation is extremely minimal.
You can install the released version of sparseLRMatrix from CRAN with:
# install.packages("remotes")
::install_github("RoheLab/sparseLRMatrix") remotes
library(sparseLRMatrix)
#> Loading required package: Matrix
library(RSpectra)
set.seed(528491)
<- 50
n <- 40
m <- 3
k
<- rsparsematrix(n, m, 0.1)
A
<- Matrix(rnorm(n * k), nrow = n, ncol = k)
U <- Matrix(rnorm(m * k), nrow = m, ncol = k)
V
# construct the matrix, which represents A + U %*% t(V)
<- sparseLRMatrix(sparse = A, U = U, V = V)
X
<- svds(X, 5) # efficient s
And a quick sanity check
<- A + tcrossprod(U, V)
Y <- svds(Y, 5) # inefficient, but same calculation
s2
# singular values match up, you can check for yourself
# that the singular vectors do as well!
all.equal(s$d, s2$d)
#> [1] TRUE