Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Kronecker Product🪧
2.1.
Properties of Kronecker Product 🧑‍🏫
3.
Idea 
4.
Implementation in Java🤹 
5.
Implementation in C++🤹 
6.
Complexity Analysis🥷  
7.
Frequently Asked Questions
7.1.
What is the application of Kronecker Product?
7.2.
What is the other name of Kronecker Product?
7.3.
What makes the Kronecker Product different from a normal matrix product?
7.4.
What is the purpose of the Kronecker product?
7.5.
What is the need of taking the auxiliary space?
8.
Conclusion
Last Updated: Mar 27, 2024

Kronecker Product of Two Matrices

Introduction

The Kronecker product is an operation that combines two matrices to create a larger matrix that contains all of the possible products of the two matrices' entries. It possesses a number of properties that are frequently employed in the solution of difficult problems in linear algebra and its applications.

Kronecker product of two matrices

In this article, we are going to look at the Kronecker Product of two matrices along with its implementation. 

So, let us get started: 

teacher

Recommended Topic, Array Implementation of Queue and  Rabin Karp Algorithm

Kronecker Product🪧

Given an n x m matrix A and a p x q matrix B, their Kronecker product: also called their matrix direct product, is an (n p) x (m q) matrix.

Note: The Kronecker product, unlike the ordinary product of two matrices, is defined regardless of the dimensions of the two matrices A and B.
 

Input:

Example test case

arrow
 

Example test case
 

arrow

Example test case

Example test case

Example test case

Now that we have understood how to find the Kronecker Product of two matrices, let us also look at some properties of the Kronecker Product: 

Properties of Kronecker Product 🧑‍🏫

Below are some properties of Kronecker Product:
 

  • Non-Commutative Property
    In general, the Kronecker product is not commutative, i.e., A  B != B  A

     
  • Distributive Property
    The distributive property holds:
    distributive property
    It also holds for the second factor:
    distributive property
  • Zero matrices

Clearly, any Kronecker product involving a zero matrix (i.e., a matrix with all zero entries) yields a zero matrix as a result:


zero matrices

  • Inverse
    The rule for finding the inverse of a Kronecker product is quite simple:

    inverse

Let us now discuss the idea of how we can program the Kronecker Product in any programming language. 

Idea 

The idea is to take an auxiliary space multiple of the size of both the matrices to store the resultant product. Afterward, loop over the elements of matrix A and B, i.e., each element of matrix A is multiplied by the whole matrix B and stored in matrix C. 
 

Now, let us discuss the Implementation of the Kronecker product of two matrices in Java and C++: 

Implementation in Java🤹 

/* 
 Java code to find the Kronecker Product of
 two matrices A and B and stores it as matrix C
*/


import java.io.*;
import java.util.*;


public class KroneckerProduct {

    /* 
       Utility function to compute Kronecker Product of two matrices
    */

    static void Kroneckerproduct(int A[][], int B[][], int row1, int row2, int col1, int col2) {

        int[][] C = new int[row1 * row2][col1 * col2];

        // i loops till row1
        for (int i = 0; i < row1; i++) {

            // k loops till row2
            for (int k = 0; k < row2; k++) {

                // j loops till col1
                for (int j = 0; j < col1; j++) {

                    // l loops till col2
                    for (int l = 0; l < col2; l++) {

                        /* Each element of matrix A is
                          multiplied by whole Matrix B
                          stored in Matrix C
                        */
                        C[i + l + 1][j + k + 1] = A[i][j] * B[k][l];
                        System.out.print(C[i + l + 1][j + k + 1] + " ");
                    }
                }
                System.out.println();
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the row size of the first matrix: ");
        int rowsize1 = sc.nextInt();

        System.out.println("Enter the col size of the first matrix: ");
        int colsize1 = sc.nextInt();

        System.out.println("Enter the row size of the second matrix: ");
        int rowsize2 = sc.nextInt();

        System.out.println("Enter the col size of the second matrix: ");
        int colsize2 = sc.nextInt();

        int[][] A = new int[rowsize1][colsize1];
        int[][] B = new int[rowsize2][colsize2];


        System.out.println("Enter the elements of first matrix ");
        for (int i = 0; i < rowsize1; i++) {
            for (int j = 0; j < colsize1; j++) {
                A[i][j] = sc.nextInt();
            }
        }

        System.out.println("Enter the elements of second matrix ");
        for (int i = 0; i < rowsize2; i++) {
            for (int j = 0; j < colsize2; j++) {
                B[i][j] = sc.nextInt();
            }
        }

        System.out.println("Kronecker Product of matrix A and B is: ");

        Kroneckerproduct(A, B, rowsize1, rowsize2, colsize1, colsize2);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

output

Implementation in C++🤹 

/*
  C++ code to find the Kronecker Product of two
  matrices A and B 
*/
#include <bits/stdc++.h>
using namespace std;

const int colsize1 = 2,
	rowsize1 = 2,
	colsize2 = 2,
	rowsize2 = 2;

/*
   Utility function to compute Kronecker Product of two matrices
*/

void Kroneckerproduct(int A[][colsize1], int B[][colsize2])
{

	int C[rowsize1 *rowsize2][colsize1 *colsize2];

	// i loops till rowsize1
	for (int i = 0; i < rowsize1; i++)
	{

		// j loops till rowsize2
		for (int j = 0; j < rowsize2; j++)
		{

			// k loops till colsize1
			for (int k = 0; k < colsize1; k++)
			{

				// l loops till colsize2
				for (int l = 0; l < colsize2; l++)
				{

					/*Each element of matrix A is
					              multiplied by whole Matrix B
					              stored in Matrix C
					*/
					C[i + l + 1][k + j + 1] = A[i][k] *B[j][l];
					cout << C[i + l + 1][k + j + 1] << " ";
				}
			}
			cout << endl;
		}
	}
}

int main()
{
	int A[2][2] = {
		{ 1, 2 },
			{ 3, 4 }
		},
		B[2][2] = {
		{ 0, 5 },
			{ 6, 7 }
		};

	cout << "Kronecker Product of the matrix A and B: " << endl;

	Kroneckerproduct(A, B);
	return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

output

Complexity Analysis🥷  

Time ComplexityO(rowsize1 * rowsize2 * colsize1 * colsize2), where rowsize1, rowsize2, colsize1 and colsize2 are the dimensions of the matrix A and B. 

Auxiliary SpaceO((rowsize1 * rowsize2) * (colsize1 * colsize2)), where rowsize1, rowsize2, colsize1 and colsize2 are the dimensions of the matrix A and B. 

Frequently Asked Questions

What is the application of Kronecker Product?

Kronecker Product is often used to solve difficult problems in linear algebra and it is also used in Image Processing. 

What is the other name of Kronecker Product?

The Kronecker product is also referred to as the matrix direct product.

What makes the Kronecker Product different from a normal matrix product?

The Kronecker product, unlike the ordinary product of two matrices, is defined regardless of the dimensions of the two matrices A and B.

What is the purpose of the Kronecker product?

The Kronecker product is an operation that combines two matrices to create a larger matrix that contains all of the possible products of the two matrices' entries.

What is the need of taking the auxiliary space?

Auxiliary space is temporary or extra space. This temporary space is created to solve the problem. The total space taken by the algorithm in relation to the input size is referred to as space complexity.

Conclusion

In this article, we have extensively discussed the Kronecker Product of two matrices. 

We hope that this blog has helped you enhance your knowledge regarding the matrices in programming and if you would like to learn more, check out our articles on

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

ninjas

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass