Kronecker product

You are encouraged to solve this task according to the task description, using any language you may know.


Task

Implement the   Kronecker product   of two matrices (arbitrary sized) resulting in a block matrix. 




Test cases

Show results for each of the following two samples:




Sample 1 (from Wikipedia):



          ┌   ┐     ┌   ┐     ┌           ┐
          │1 2│     │0 5│     │ 0  5  0 10│
          │3 4│  x  │6 7│  =  │ 6  7 12 14│
	  └   ┘     └   ┘     │ 0 15  0 20│
	                      │18 21 24 28│
                              └           ┘ 

Sample 2:

          ┌     ┐     ┌       ┐     ┌                       ┐       
          │0 1 0│     │1 1 1 1│     │0 0 0 0 1 1 1 1 0 0 0 0│
          │1 1 1│  x  │1 0 0 1│  =  │0 0 0 0 1 0 0 1 0 0 0 0│
          │0 1 0│     │1 1 1 1│     │0 0 0 0 1 1 1 1 0 0 0 0│
	  └     ┘     └       ┘     │1 1 1 1 1 1 1 1 1 1 1 1│
                                    │1 0 0 1 1 0 0 1 1 0 0 1│
                                    │1 1 1 1 1 1 1 1 1 1 1 1│
                                    │0 0 0 0 1 1 1 1 0 0 0 0│
                                    │0 0 0 0 1 0 0 1 0 0 0 0│
                                    │0 0 0 0 1 1 1 1 0 0 0 0│
                                    └                       ┘



See implementations and results below in JavaScript and PARI/GP languages.



Related task

  Kronecker product based fractals. 


/*REXX program calculates the   Kronecker product   of   two arbitrary size   matrices. */
w= 0                                             /*W:  max width of any matrix element. */
     aMat= 2x2  1 2 3 4                          /*define  A  matrix size  and elements.*/
     bMat= 2x2  0 5 6 7                          /*   "    B     "     "    "     "     */
call makeMat 'A', aMat                           /*construct   A   matrix from elements.*/
call makeMat 'B', bMat                           /*    "       B      "     "     "     */
call KronMat 'Kronecker product'                 /*calculate the  Kronecker  product.   */
w= 0;      say;     say copies('░', 55);   say   /*display a fence between the 2 outputs*/
     aMat= 3x3  0 1 0 1 1 1 0 1 0                /*define  A  matrix size  and elements.*/
     bMat= 3x4  1 1 1 1 1 0 0 1 1 1 1 1          /*   "    B     "     "    "     "     */
call makeMat 'A', aMat                           /*construct   A   matrix from elements.*/
call makeMat 'B', bMat                           /*    "       B      "     "     "     */
call KronMat 'Kronecker product'                 /*calculate the  Kronecker  product.   */
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
KronMat: parse arg what;                  parse var  @.a.shape   aRows aCols
         #= 0;                            parse var  @.b.shape   bRows bCols
               do       rA=1  for aRows
                 do     rB=1  for bRows;  #= #+1;          ##= 0;      _=
                   do   cA=1  for aCols;  x= @.a.rA.cA
                     do cB=1  for bCols;  y= @.b.rB.cB;    ##= ##+1;   xy= x*y;    _= _ xy
                     @.what.#.##=xy;      w= max(w, length(xy) )
                     end   /*cB*/
                   end     /*cA*/
                 end       /*rB*/
               end         /*rA*/
         call showMat what, aRows*bRows || 'X' || aRows*bCols;         return
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeMat: parse arg what, size elements;   arg , row 'X' col .;       @.what.shape=row  col
         #=0;    do   r=1  for row               /* [↓]  bump item#; get item; max width*/
                   do c=1  for col;  #= #+1;  _= word(elements, #);   w= max(w, length(_))
                   @.what.r.c=_
                   end   /*c*/                   /* [↑] define an element of WHAT matrix*/
                 end     /*r*/
         call showMat what, size;         return
/*──────────────────────────────────────────────────────────────────────────────────────*/
showMat: parse arg what, size .;  z= '┌';  parse var  size  row  "X"  col;   $=left('', 6)
         say;                             say $ copies('═',7) "matrix" what  copies('═',7)
              do   r=1  for row;  _= '│'                  /*start with long vertical bar*/
                do c=1  for col;  _=_  right(@.what.r.c, w);  if r==1  then z=z left('',w)
                end   /*c*/
              if r==1  then do; z=z '┐';  say $ $ z;  end /*show the top part of matrix.*/
              say $ $ _ '│'                               /*append a long vertical bar. */
              end     /*r*/
         say $ $ translate(z, '└┘', "┌┐");   return       /*show the bot part of matrix.*/