<- matrix(1:16, nrow = 4, ncol = 4))
(A1 #> [,1] [,2] [,3] [,4]
#> [1,] 1 5 9 13
#> [2,] 2 6 10 14
#> [3,] 3 7 11 15
#> [4,] 4 8 12 16
<- matrix(1:20, nrow = 4, ncol = 5))
(B1 #> [,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
<- matrix(1:40, nrow = 5, ncol = 8))
(A2 #> [,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
<- matrix(1:50, nrow = 10, ncol = 5))
(B2 #> [,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
Lab 5
- Please ensure you review the document STAT 385 Lab 5 Help Document – Kronecker Product Illustration on Canvas or visit the weblink https://ycjiaweijia.github.io/F24STAT385/Lab5HelpDoc/Lab5HelpDoc.html for helpful guidance on this lab.
- Please complete the following questions and submit your Lab 5 to Gradescope.
Mathemetical Definition of Kronecker Product
In mathematics, the Kronecker product, sometimes denoted by \(\bigotimes\), 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.
- You can find more information about Kronecker Product on Wikipedia https://en.wikipedia.org/wiki/Kronecker_product.
If \(\mathbf{A}\) is an \(m \times n\) matrix and \(\mathbf{B}\) is a \(p \times q\) matrix, then the Kronecker product \(\mathbf{A}\bigotimes \mathbf{B}\) is the \(pm \times qn\) block matrix:
\[\mathbf{A}\bigotimes \mathbf{B} = \begin{bmatrix} a_{11}\mathbf{B}\quad \cdots \quad a_{1n}\mathbf{B}\\ \vdots\quad \ddots\quad \vdots\\ a_{m1}\mathbf{B}\quad \cdots \quad a_{mn}\mathbf{B} \end{bmatrix},\]
more explicitly,
\[\mathbf{A}\bigotimes \mathbf{B} =\left[\begin{array}{cccccccccc} a_{11}b_{11}& a_{11}b_{12} & \cdots & a_{11}b_{1q}& \cdots& \cdots& a_{1n}b_{11}& a_{1n}b_{12} & \cdots & a_{1n}b_{1q} \\ a_{11}b_{21}& a_{11}b_{22} & \cdots & a_{11}b_{2q}& \cdots& \cdots& a_{1n}b_{21}& a_{1n}b_{22} & \cdots & a_{1n}b_{2q} \\ \vdots& \vdots& \ddots& \vdots & & & \vdots& \vdots& \ddots& \vdots \\ a_{11}b_{p1}& a_{11}b_{p2} & \cdots & a_{11}b_{pq}& \cdots& \cdots& a_{1n}b_{p1}& a_{1n}b_{p2} & \cdots & a_{1n}b_{pq} \\ \vdots& \vdots& & \vdots & \ddots& & \vdots & \vdots& & \vdots \\ \vdots& \vdots& & \vdots & &\ddots & \vdots & \vdots& & \vdots \\ a_{m1}b_{11}& a_{m1}b_{12} & \cdots & a_{m1}b_{1q}& \cdots& \cdots& a_{mn}b_{11}& a_{mn}b_{12} & \cdots & a_{mn}b_{1q} \\ a_{m1}b_{21}& a_{m1}b_{22} & \cdots & a_{m1}b_{2q}& \cdots& \cdots& a_{mn}b_{21}& a_{mn}b_{22} & \cdots & a_{mn}b_{2q} \\ \vdots& \vdots& \ddots& \vdots & & & \vdots& \vdots& \ddots& \vdots \\ a_{m1}b_{p1}& a_{m1}b_{p2} & \cdots & a_{m1}b_{pq}& \cdots& \cdots& a_{mn}b_{p1}& a_{mn}b_{p2} & \cdots & a_{mn}b_{pq} \end{array}\right],\]
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()