R Under development (unstable) (2025-08-17 r88631 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ## any sparse matrix format that inherits from dMatrix should work > library(sparsesvd) > library(Matrix) > > 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)) > > M1 <- Matrix(M, sparse=TRUE) # standard format (column-compressed) > M1 <- as(M1, "CsparseMatrix") > stopifnot(is(M1, "dgCMatrix")) > res1 <- sparsesvd(M1) > > M2 <- Matrix(M, sparse=FALSE) # dense matrix > stopifnot(is(M2, "dgeMatrix")) > res2 <- sparsesvd(M2) > > M3 <- Matrix(M, sparse=TRUE) # triplet format > M3 <- as(M3, "TsparseMatrix") > stopifnot(is(M3, "dgTMatrix")) > res3 <- sparsesvd(M3) > > M4 <- Matrix(M, sparse=TRUE) # row-compressed > M4 <- as(M4, "RsparseMatrix") > stopifnot(is(M4, "dgRMatrix")) > res4 <- sparsesvd(M4) > ## TODO -- row-compressed form cannot be converted to dgCMatrix > > ## check that eigenvalues are the same > stopifnot(all.equal(res1$d, res2$d, tolerance=1e-12)) > stopifnot(all.equal(res1$d, res3$d, tolerance=1e-12)) > stopifnot(all.equal(res1$d, res4$d, tolerance=1e-12)) > > ## special classes for symmetric matrices > A1 <- crossprod(M1) > A1 <- as(A1, "CsparseMatrix") > stopifnot(is(A1, "dsCMatrix")) > res1a <- sparsesvd(A1) > > A2 <- crossprod(M2) > stopifnot(is(A2, "dpoMatrix")) # dense positive-semidefinite symmetric matrix > res2a <- sparsesvd(A2) > ## -- fails > > A3 <- as(A1, "TsparseMatrix") > stopifnot(is(A3, "dsTMatrix")) > res3a <- sparsesvd(A3) > ## -- symmetric matrix can only be converted if already in row-compressed form > > A4 <- as(A1, "RsparseMatrix") > stopifnot(is(A4, "dsRMatrix")) > res4a <- sparsesvd(A4) > ## -- fails > > ## check that eigenvalues are the same and consistent with M > stopifnot(all.equal(res1a$d, (res1$d)^2, tolerance=1e-12)) > stopifnot(all.equal(res1a$d, res2a$d, tolerance=1e-12)) > stopifnot(all.equal(res1a$d, res3a$d, tolerance=1e-12)) > stopifnot(all.equal(res1a$d, res4a$d, tolerance=1e-12)) > > proc.time() user system elapsed 0.96 0.10 1.06