Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kronecker product for large matrices

Tags:

r

matrix

I am looking for an effficient way of computing the Kronecker product of two large matrices. I have tried using the method kronecker() as follows:

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

However, it takes a long time to execute and then gives the following error:

 Error: cannot allocate vector of size 6.8 Gb
like image 598
Mayou Avatar asked Sep 17 '13 12:09

Mayou


2 Answers

If you are computing kron(I,A)*v where v is a vector you can do this using vec(A*V) where V reshapes v into a matrix. This uses the more general rule that vec(ABC)=kron(C',A)*vec(B). This avoids forming the Kronecker product and uses far less operations to perform the computation.

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

like image 148
Paul Fackler Avatar answered Sep 28 '22 04:09

Paul Fackler


As long as you use Matrix::Diagonal to construct your diagonal matrix, you'll automatically get your test object constructed 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"
like image 22
Ben Bolker Avatar answered Sep 28 '22 04:09

Ben Bolker