tests/testthat/test-svds-matches.R

test_that("singular values match", {

  set.seed(528491)

  n <- 50
  m <- 40
  k <- 3

  A <- rsparsematrix(n, m, 0.1)

  U <- Matrix(rnorm(n * k), nrow = n, ncol = k)
  V <- Matrix(rnorm(m * k), nrow = m, ncol = k)

  # construct the matrix, which represents A + U %*% t(V)
  X <- sparseLRMatrix(sparse = A, U = U, V = V)

  s <- svds(X, 5)  # efficient

  Y <- A + tcrossprod(U, V)
  s2 <- svds(Y, 5)  # inefficient, but same calculation

  expect_equal(s, s2)
})

Try the sparseLRMatrix package in your browser

Any scripts or data that you put into this service are public.

sparseLRMatrix documentation built on March 2, 2021, 5:08 p.m.