t(m5 <- as.matrix.csr(c(-1:1,0,0)))
t(M4 <- as.matrix.csc(c(0:2,0), 4))
(S3 <- as.matrix.ssr(diag(x = 0:2))) # *symmetric*
stopifnot(identical(dim(m5), c(5L, 1L)),
identical(dim(M4), c(4L, 1L)),
identical(dim(S3), c(3L, 3L)))
n1 <- 10
p <- 5
a <- round(rnorm(n1*p), 2)
a[abs(a) < 0.7] <- 0
A <- matrix(a,n1,p)
B <- t(A) %*% A
A.csr <- as.matrix.csr(A)
A.csc <- as.matrix.csc(A)
B.ssr <- as.matrix.ssr(B)
B.ssc <- as.matrix.ssc(B)
stopifnot(exprs = {
is.matrix.csr(A.csr) # -> TRUE
is.matrix.csc(A.csc) # -> TRUE
is.matrix.ssr(B.ssr) # -> TRUE
is.matrix.ssc(B.ssc) # -> TRUE
})
as.matrix(A.csr)
as.matrix(A.csc)
as.matrix(B.ssr)
as.matrix(B.ssc)
as.matrix.csr(0, 2,3) # sparse matrix of all zeros
## Diagonal (sparse) :
as(4, "matrix.diag.csr") # identity matrix of dimension 4
as(2:0, "matrix.diag.csr") # diagonal 3x3 matrix
Run the code above in your browser using DataLab