Vectorized Elemental Division on Sparse Matrices in R

A / B in R performs elemental matrix decomposition.

However, if I create a sparse matrix from the Matrix package and try to split A / B, I get this error:

> class(N) [1] "dgCMatrix" attr(,"package") [1] "Matrix" > N/N Error in asMethod(object) : Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 105 > 

Interesting. When the sparse matrix is ​​small in total size, I get the following:

 > m <- sparseMatrix(i=c(1,2,1,3), j=c(1,1,3,3), x=c(1,2,1,4)) > m/m 3 x 3 Matrix of class "dgeMatrix" [,1] [,2] [,3] [1,] 1 NaN 1 [2,] 1 NaN NaN [3,] NaN NaN 1 > 

But when it is moderately sized (~ 20,000 items), I get a Cholmod error.

Is there a workaround or a more correct way to perform elementary division on sparse matrices in R?

+4
source share
1 answer

The problem with elementary division is that if your matrices are sparse, then as a result you will get a lot of Inf and NaN , and this will make it dense. This is why you get errors from memory.

If you want to replace Inf and NaN zeros as a result, then the solution will be relatively simple, you just get summary() both matrices and work directly with indexes and values.

You will need to limit the index indices A and B their intersection and divide by this. To get the intersection of index pairs, you can use merge() .

Here is a quick and dirty implementation:

 # Some example data A <- sparseMatrix(i=c(1,1,2,3), j=c(1,3,1,3), x=c(1,1,2,3)) B <- sparseMatrix(i=c(3,2,1), j=c(3,2,1), x=c(3,2,1)) sdiv <- function(X, Y, names=dimnames(X)) { sX <- summary(X) sY <- summary(Y) sRes <- merge(sX, sY, by=c("i", "j")) sparseMatrix(i=sRes[,1], j=sRes[,2], x=sRes[,3]/sRes[,4], dimnames=names) } sdiv(A, B) # 3 x 3 sparse Matrix of class "dgCMatrix" # # [1,] 1 . . # [2,] . . . # [3,] . . 1 

Thanks to flodel for suggesting using summary and merge .

+6
source

Source: https://habr.com/ru/post/1436917/


All Articles