Lab 5

Mathemetical Definition of Kronecker Product

In mathematics, the Kronecker product, sometimes denoted by , is an operation on two matrices of arbitrary size resulting a block matrix. It is a specialization of the tensor product (which is denoted by the same symbol) from vectors to matrices and gives the matrix of the tensor product linear map with respect to a standard choice of basis. The Kronecker product is to be distinguished from the usual matrix multiplication, which is an entirely different operation. The Kronecker product is also sometimes called matrix direct product.

If A is an m×n matrix and B is a p×q matrix, then the Kronecker product AB is the pm×qn block matrix:

AB=[a11Ba1nBam1BamnB],

more explicitly,

AB=[a11b11a11b12a11b1qa1nb11a1nb12a1nb1qa11b21a11b22a11b2qa1nb21a1nb22a1nb2qa11bp1a11bp2a11bpqa1nbp1a1nbp2a1nbpqam1b11am1b12am1b1qamnb11amnb12amnb1qam1b21am1b22am1b2qamnb21amnb22amnb2qam1bp1am1bp2am1bpqamnbp1amnbp2amnbpq],

Question 1

[20 pts] Please define the function forloop_kronecker(), using for loop, to calculate the Kronecker product of matrices A and B with arbitrary dimensions.

Question 2

[20 pts] Please define the function vectorization_kronecker(), using vectorization, to calculate the Kronecker product of matrices A and B with arbitrary dimensions.

Question 3

[20 pts] Compare the CPU times for three Kronecker product calculation methods below for matrices A1 and B1, A2 and B2, and other pairs of matrices you choose to test. Use times = 10000L for each calculation. Discuss the computational efficiency of the three methods based on your findings.

  • vectorization_kronecker()
  • kronecker()
  • forloop_kronecker()
(A1 <- matrix(1:16, nrow = 4, ncol = 4))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    5    9   13
#> [2,]    2    6   10   14
#> [3,]    3    7   11   15
#> [4,]    4    8   12   16
(B1 <- matrix(1:20, nrow = 4, ncol = 5))
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    1    5    9   13   17
#> [2,]    2    6   10   14   18
#> [3,]    3    7   11   15   19
#> [4,]    4    8   12   16   20

(A2 <- matrix(1:40, nrow = 5, ncol = 8))
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#> [1,]    1    6   11   16   21   26   31   36
#> [2,]    2    7   12   17   22   27   32   37
#> [3,]    3    8   13   18   23   28   33   38
#> [4,]    4    9   14   19   24   29   34   39
#> [5,]    5   10   15   20   25   30   35   40
(B2 <- matrix(1:50, nrow = 10, ncol = 5))
#>       [,1] [,2] [,3] [,4] [,5]
#>  [1,]    1   11   21   31   41
#>  [2,]    2   12   22   32   42
#>  [3,]    3   13   23   33   43
#>  [4,]    4   14   24   34   44
#>  [5,]    5   15   25   35   45
#>  [6,]    6   16   26   36   46
#>  [7,]    7   17   27   37   47
#>  [8,]    8   18   28   38   48
#>  [9,]    9   19   29   39   49
#> [10,]   10   20   30   40   50