Skip to content

Commit 4bc65f6

Browse files
committed
MVP implementation
1 parent 866deae commit 4bc65f6

7 files changed

Lines changed: 118 additions & 48 deletions

NAMESPACE

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

33
S3method(svds,sparseLRMatrix)
4-
exportClasses(sparseLRMatrix)
4+
export(sparseLRMatrix)
5+
export(svds)
6+
import(Matrix)
7+
importFrom(RSpectra,svds)
8+
importFrom(methods,new)

R/object.R

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#' @slot U Matrix.
1212
#' @slot V Matrix.
1313
#'
14-
#' @return
15-
#' @export
16-
#'
17-
#' @examples
1814
setClass(
1915
Class = "sparseLRMatrix",
2016
# contains = "Matrix",
@@ -30,11 +26,23 @@ setClass(
3026
)
3127
)
3228

33-
# the helper
29+
#' Title
30+
#'
31+
#' @param sparse TODO
32+
#' @param U TODO
33+
#' @param V TODO
34+
#'
35+
#' @return
36+
#' @export
37+
#' @importFrom methods new
38+
#'
39+
#' @examples
40+
#'
41+
#' # TODO
3442
sparseLRMatrix <- function(sparse, U, V) {
35-
new(
43+
methods::new(
3644
Class = "sparseLRMatrix",
37-
sparse = A,
45+
sparse = sparse,
3846
U = Matrix(U),
3947
V = Matrix(V)
4048
)
@@ -55,31 +63,47 @@ setValidity("sparseLRMatrix", function(object) {
5563
setMethod("dim", "sparseLRMatrix", function(x) dim(x@sparse))
5664

5765
Ax <- function(x, A) {
58-
A@sparse %*% x + A@U %*% crossprod(A@V, x)
66+
out <- A@sparse %*% x + A@U %*% Matrix::crossprod(A@V, x)
67+
drop(out)
5968
}
6069

6170
Atx <- function(x, A) {
62-
crossprod(A@sparse, x) + A@V %*% crossprod(A@U, x)
71+
out <- crossprod(A@sparse, x) + A@V %*% Matrix::crossprod(A@U, x)
72+
drop(out)
6373
}
6474

75+
#' @export
76+
#' @importFrom RSpectra svds
77+
svds <- RSpectra::svds
6578

66-
#' @inherit RSpectra::svds title params return
79+
#' Truncated singular value decomposition of a matrix
80+
#'
81+
#' A thin wrapper around [RSpectra::svds()], please see more detailed
82+
#' documentation there. In particular, this function leverages the
83+
#' function interface.
84+
#'
85+
#' @param A Matrix to decompose.
86+
#' @param k Number of singular values to estimate.
87+
#' @param nu Number of left singular vectors to estimate.
88+
#' @param nv Number of right singular vectors to estimate.
89+
#' @param opts Passed to [RSpectra::svds()].
90+
#' @param ... Passed to [RSpectra::svds()].
91+
#'
6792
#' @method svds sparseLRMatrix
6893
#' @export
6994
#'
7095
#' @examples
7196
#'
72-
#' library(RSpectra)
73-
#'
7497
#' set.seed(528491)
7598
#'
7699
#' n <- 50
100+
#' m <- 40
77101
#' k <- 3
78102
#'
79-
#' A <- rsparsematrix(n, n, 0.1)
103+
#' A <- rsparsematrix(n, m, 0.1)
80104
#'
81105
#' U <- Matrix(rnorm(n * k), nrow = n, ncol = k)
82-
#' V <- Matrix(rnorm(n * k), nrow = n, ncol = k)
106+
#' V <- Matrix(rnorm(m * k), nrow = m, ncol = k)
83107
#'
84108
#' X <- sparseLRMatrix(sparse = A, U = U, V = V)
85109
#'
@@ -94,7 +118,8 @@ svds.sparseLRMatrix <- function(A, k, nu = k, nv = k, opts = list(), ...) {
94118
opts = opts,
95119
Atrans = Atx,
96120
dim = dim(A),
97-
args = A
121+
args = A,
122+
...
98123
)
99124
}
100125

R/sparseLRMatrix-package.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#' @keywords internal
2+
#' @import Matrix
3+
"_PACKAGE"
4+
5+
# The following block is used by usethis to automatically manage
6+
# roxygen namespace tags. Modify with care!
7+
## usethis namespace: start
8+
## usethis namespace: end
9+
NULL

man/sparseLRMatrix-class.Rd

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sparseLRMatrix-package.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sparseLRMatrix.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/svds.sparseLRMatrix.Rd

Lines changed: 13 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)