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. 


function kronecker(sequence a, b)
    integer ar = length(a),
            ac = length(a[1]),
            br = length(b),
            bc = length(b[1])
    sequence res = repeat(repeat(0,ac*bc),ar*br)
    for ia=1 to ar do
        integer i0 = (ia-1)*br
        for ja=1 to ac do
            integer j0 = (ja-1)*bc
            for ib=1 to br do
                integer i = i0+ib
                for jb=1 to bc do
                    integer j = j0+jb
                    res[i,j] = a[ia,ja]*b[ib,jb]
                end for
            end for
        end for
    end for
    return res
end function
 
constant a = {{1,2},
              {3,4}},
         b = {{0,5},
              {6,7}},
         c = {{0,1,0},
              {1,1,1},
              {0,1,0}},
         d = {{1,1,1,1},
              {1,0,0,1},
              {1,1,1,1}}
 
pp(kronecker(a,b),{pp_Nest,1,pp_IntFmt,"%2d"})
pp(kronecker(c,d),{pp_Nest,1})
