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. 


EnableExplicit
DataSection
  Matrix_A_B_Dimension_Bsp1:
  Data.i 2,2,?MatrixA_Werte_Bsp1,2,2,?MatrixB_Werte_Bsp1
 
  Matrix_A_B_Dimension_Bsp2:
  Data.i 3,3,?MatrixA_Werte_Bsp2,3,4,?MatrixB_Werte_Bsp2
 
  MatrixA_Werte_Bsp1:
  Data.i 1,2,3,4
 
  MatrixA_Werte_Bsp2:
  Data.i 0,1,0,1,1,1,0,1,0
 
  MatrixB_Werte_Bsp1:
  Data.i 0,5,6,7
 
  MatrixB_Werte_Bsp2:
  Data.i 1,1,1,1,1,0,0,1,1,1,1,1
EndDataSection
 
Define.i ma, na, mb, nb, adr1, adr2, i, j, k, l
Define mk$
 
Gosub Bsp1_Matrix_A_B : Gosub LoadMatrix : Gosub Bsp2_Matrix_A_B : Gosub LoadMatrix : End
 
LoadMatrix:
Read.i ma
Read.i na
Read.i adr1
Read.i mb
Read.i nb
Read.i adr2
 
Dim mxa.i(ma,na)
Dim mxb.i(mb,nb)
NewMap mxc.i()
 
For i=1 To ma
  For j=1 To na
    mxa(i,j)=PeekI(adr1)
    adr1+SizeOf(Integer)
  Next
Next
 
For i=1 To mb
  For j=1 To nb
    mxb(i,j)=PeekI(adr2)
    adr2+SizeOf(Integer)
  Next
Next
 
OpenConsole("Kronecker product")
PrintN("Matrix A:")
For i=1 To ma ; Zeile
  Print("|")
  For j=1 To na ; Spalte
    Print(RSet(Str(mxa(i,j)),2," ")+" ")
  Next
  PrintN("|")
Next
PrintN("")
 
PrintN("Matrix B:")
For i=1 To mb ; Zeile
  Print("|")
  For j=1 To nb ; Spalte
    Print(RSet(Str(mxb(i,j)),2," ")+" ")
  Next
  PrintN("|")
Next
PrintN("")
 
PrintN("Matrix C=AxB")
For i=1 To ma ; Zeile MA
  For j=1 To na ; Spalte MA
    For k=1 To mb ; Zeile MB
      For l=1 To nb ; Spalte MB        
        mxc(Str(i)+","+Str(j)+","+Str(k)+","+Str(l))=mxa(i,j)*mxb(k,l)
      Next
    Next
  Next
Next
 
For i=1 To ma ; Zeile MA
  For k=1 To mb; Zeile MB
    Print("|")
    For j=1 To na ; Spalte MA
      For l=1 To nb ; Spalte MB
        mk$=Str(i)+","+Str(j)+","+Str(k)+","+Str(l)
        If FindMapElement(mxc(),mk$)        
          Print(RSet(Str(mxc()),2," ")+" ")
        EndIf        
      Next
    Next
    PrintN("|")
  Next    
Next
PrintN("Press return") : Input()
Return
 
Bsp1_Matrix_A_B:
  Restore Matrix_A_B_Dimension_Bsp1
Return
 
Bsp2_Matrix_A_B:
  Restore Matrix_A_B_Dimension_Bsp2
Return