Kronecker for large matrices

I am looking for an efficient way to calculate the Kronecker product of two large matrices. I tried using the kronecker() method as follows:

  I = diag(700) data = replicate(15, rnorm(120)) test = kronecker(I,data) 

However, it takes a long time to complete, and then the following error appears:

  Error: cannot allocate vector of size 6.8 Gb 
+6
source share
2 answers

As long as you use Matrix::Diagonal to build a diagonal matrix, you will automatically get your test object built as a sparse matrix:

 library(Matrix) I=Diagonal(700) data = replicate(15,rnorm(120)) system.time(test <- kronecker(I,data)) ## user system elapsed ## 0.600 0.044 0.671 dim(test) ## [1] 84000 10500 format(object.size(test),"Mb") ## [1] "19.2 Mb" 
+8
source

If you compute kron(I,A)*v , where v is a vector, you can do this using vec(A*V) , where v converts v to a matrix. This uses the more general rule that vec(ABC)=kron(C',A)*vec(B) . This avoids the formation of a Kronecker product and uses far fewer operations to perform calculations.

Note that v may need to be transposed depending on how the matrix storage is handled (columns versus rows).

+3
source

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


All Articles