2.42 Generate sparse matrix with n by n matrix repeated on its diagonal

Given matrix \(\begin {pmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end {pmatrix} \), generate the following sparse matrix with this matrix on the diagonal

\[\begin {pmatrix} 1 & 2 & 3 & 0 & 0 & 0 & 0 & 0 & 0\\ 4 & 5 & 6 & 0 & 0 & 0 & 0 & 0 & 0\\ 7 & 8 & 9 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 2 & 3 & 0 & 0 & 0\\ 0 & 0 & 0 & 4 & 5 & 6 & 0 & 0 & 0\\ 0 & 0 & 0 & 7 & 8 & 9 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 3\\ 0 & 0 & 0 & 0 & 0 & 0 & 4 & 5 & 6\\ 0 & 0 & 0 & 0 & 0 & 0 & 7 & 8 & 9 \end {pmatrix} \]

Mathematica

mat = N[{{1,2,3},{4,5,6},{7,8,9}}]; 
sp  = SparseArray[Band[{1,1}]-> 
        ConstantArray[mat,{3}]] 
MatrixForm[sp]
 

\[ \left ( {\begin {array}{ccccccccc} 1. & 2. & 3. & 0 & 0 & 0 & 0 & 0 & 0 \\ 4. & 5. & 6. & 0 & 0 & 0 & 0 & 0 & 0 \\ 7. & 8. & 9. & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1. & 2. & 3. & 0 & 0 & 0 \\ 0 & 0 & 0 & 4. & 5. & 6. & 0 & 0 & 0 \\ 0 & 0 & 0 & 7. & 8. & 9. & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1. & 2. & 3. \\ 0 & 0 & 0 & 0 & 0 & 0 & 4. & 5. & 6. \\ 0 & 0 & 0 & 0 & 0 & 0 & 7. & 8. & 9. \\ \end {array}} \right ) \]

 

Matlab

A  = [1 2 3; 
      4 5 6; 
      7 8 9]; 
Iy = speye(3); 
full(Iy)
 

ans = 
     1     0     0 
     0     1     0 
     0     0     1
 

sp = kron(Iy,A); 
full(sp)
 

ans = 
 1  2  3  0  0  0  0  0  0 
 4  5  6  0  0  0  0  0  0 
 7  8  9  0  0  0  0  0  0 
 0  0  0  1  2  3  0  0  0 
 0  0  0  4  5  6  0  0  0 
 0  0  0  7  8  9  0  0  0 
 0  0  0  0  0  0  1  2  3 
 0  0  0  0  0  0  4  5  6 
 0  0  0  0  0  0  7  8  9
 

 

Maple

A:=Matrix([[1,2,3],[4,5,6],[7,8,9]]); 
Iy:=LinearAlgebra:-IdentityMatrix(3); 
LinearAlgebra:-KroneckerProduct(Iy,A)
 

Matrix(9, 9, [[1, 2, 3, 0, 0, 0, 0, 0, 0], 
              [4, 5, 6, 0, 0, 0, 0, 0, 0], 
              [7, 8, 9, 0, 0, 0, 0, 0, 0], 
              [0, 0, 0, 1, 2, 3, 0, 0, 0], 
              [0, 0, 0, 4, 5, 6, 0, 0, 0], 
              [0, 0, 0, 7, 8, 9, 0, 0, 0], 
              [0, 0, 0, 0, 0, 0, 1, 2, 3], 
              [0, 0, 0, 0, 0, 0, 4, 5, 6], 
              [0, 0, 0, 0, 0, 0, 7, 8, 9]])