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. 


BEGIN
 
    PROCEDURE OUTMATRIX(A, W); INTEGER ARRAY A; INTEGER W;
    BEGIN
        INTEGER I, J;
        INTEGER LA1, UA1;
        INTEGER LA2, UA2;
 
        LA1 := LOWERBOUND(A, 1); UA1 := UPPERBOUND(A, 1);
        LA2 := LOWERBOUND(A, 2); UA2 := UPPERBOUND(A, 2);
 
        FOR I := LA1 STEP 1 UNTIL UA1 DO
        BEGIN
            OUTTEXT("[");
            FOR J := LA2 STEP 1 UNTIL UA2 DO
            BEGIN
                IF NOT (J = LA2) THEN OUTCHAR(' ');
                OUTINT(A(I, J), W)
            END;
            OUTTEXT("]");
            OUTIMAGE
        END
    END OUTMATRIX;
 
    PROCEDURE KRONECKERPRODUCT(A, B, C); INTEGER ARRAY A, B, C;
    BEGIN
        INTEGER I, J, K, L, CI, CJ;
        INTEGER LA1, UA1;
        INTEGER LA2, UA2;
        INTEGER LB1, UB1;
        INTEGER LB2, UB2;
 
        LA1 := LOWERBOUND(A, 1); UA1 := UPPERBOUND(A, 1);
        LA2 := LOWERBOUND(A, 2); UA2 := UPPERBOUND(A, 2);
        LB1 := LOWERBOUND(B, 1); UB1 := UPPERBOUND(B, 1);
        LB2 := LOWERBOUND(B, 2); UB2 := UPPERBOUND(B, 2);
 
        CI := 1;
        FOR I := LA1 STEP 1 UNTIL UA1 DO
            FOR K := LB1 STEP 1 UNTIL UB1 DO
            BEGIN
                CJ := 1;
                FOR J := LA2 STEP 1 UNTIL UA2 DO
                    FOR L := LB2 STEP 1 UNTIL UB2 DO
                    BEGIN
                        C(CI, CJ) := A(I, J) * B(K, L);
                        CJ := CJ + 1
                    END;
                CI := CI + 1
            END
    END KRONECKERPRODUCT;
 
    ! --- EXAMPLE 1 --- ;
    BEGIN
        INTEGER ARRAY A(1:2, 1:2);
        INTEGER ARRAY B(1:2, 1:2);
        INTEGER ARRAY C(1:4, 1:4);
 
 
        ! {{1, 2}, {3, 4}} ;
 
        A(1, 1) := 1;
        A(1, 2) := 2;
 
        A(2, 1) := 3;
        A(2, 2) := 4;
 
        ! {{0, 5}, {6, 7}} ;
 
        B(1, 1) := 0;
        B(1, 2) := 5;
 
        B(2, 1) := 6;
        B(2, 2) := 7;
 
        OUTMATRIX(A, 2); OUTTEXT("   *"); OUTIMAGE;
        OUTMATRIX(B, 2); OUTTEXT("   ="); OUTIMAGE;
 
        KRONECKERPRODUCT(A, B, C);
 
        OUTMATRIX(C, 2); OUTIMAGE
 
        ! OUTPUT:
 
        ! [ 0  5  0 10]
        ! [ 6  7 12 14]
        ! [ 0 15  0 20]
        ! [18 21 24 28] ;
 
    END EXAMPLE 1;
 
    ! --- EXAMPLE 2 --- ;
    BEGIN
        INTEGER ARRAY X(1:3, 1:3);
        INTEGER ARRAY Y(1:3, 1:4);
        INTEGER ARRAY C(1:9, 1:12);
 
        ! {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}} ;
 
        X(1,1) := 0;
        X(1,2) := 1;
        X(1,3) := 0;
 
        X(2,1) := 1;
        X(2,2) := 1;
        X(2,3) := 1;
 
        X(3,1) := 0;
        X(3,2) := 1;
        X(3,3) := 0;
 
       ! {{1, 1, 1, 1}, {1, 0, 0, 1}, {1, 1, 1, 1}} ;
 
        Y(1,1) := 1;
        Y(1,2) := 1;
        Y(1,3) := 1;
        Y(1,4) := 1;
 
        Y(2,1) := 1;
        Y(2,2) := 0;
        Y(2,3) := 0;
        Y(2,4) := 1;
 
        Y(3,1) := 1;
        Y(3,2) := 1;
        Y(3,3) := 1;
        Y(3,4) := 1;
 
        OUTIMAGE;
 
        OUTMATRIX(X, 1); OUTTEXT("   *"); OUTIMAGE;
        OUTMATRIX(Y, 1); OUTTEXT("   ="); OUTIMAGE;
 
        KRONECKERPRODUCT(X, Y, C);
 
        OUTMATRIX(C, 1); OUTIMAGE;
 
        ! OUTPUT:
 
        ! [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]
        ! [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] ;
 
    END EXAMPLE 2;
END