1

Suppose I have two dense matrices A and B of dimension 252 and 308. C is a diagonal matrix with elements 1s and 0s.

I want to calculate C(A \otimes B)C. If I calculate

C %*% (A %x% B) %*% C

I get cannot allocate vector of size 44.9 Gb. Is there an efficient way to calculate this matrix.

shani
  • 217
  • 1
  • 8
  • Does this answer your question? [Kronecker product for large matrices](https://stackoverflow.com/questions/18850576/kronecker-product-for-large-matrices) – Jan Jul 15 '23 at 08:31
  • @Jan No. Because A and B are dense matrices. – shani Jul 15 '23 at 08:51

2 Answers2

1

I don't think you are able to solve the issue since you will be stuck at (A %x% B) if you have both A and B as dense matrices.

With the given dimensions for A and B, you will have (m*n)^2 = 6024243456 elements in A %*% B, and this number exceeds the upper limit (see the answer there)

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

If C is diagonal with diagonal entries equal to 0 or 1, then

C %*% (A %x% B) %*% C

can be represented compactly as

(A %x% B)[i, i]

where i = which(as.logical(diag(C))). That is, you only need to store a principal submatrix of the Kronecker product. But without knowing anything about the nonzero pattern of i, it is impossible to advise on how to construct that submatrix efficiently ...

Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48