# Numpy.kron(): How to Calculate Kronecker Product Using Numpy? Let us say we are given a pair of matrices and would like to transform one matrix at the expense of the other. What this means is to scale one matrix using the elements of the other. The mathematical process of making this happen is called the ‘Kronecker Product’ and the NumPy library in Python has an exclusive function that might serve the purpose – kron( ).

This article will help you to understand how to calculate a Kronecker product using the kron( ) function from the numpy library. Let’s get started in understanding this function by first importing the numpy library using the below code.

```import numpy as np
```

Thereafter, we shall explore further the kron( ) function through each of the following sections.

• Syntax of kron( ) function
• Calculating Kronecker Product for Arrays of Same Dimensions
• Calculating Kronecker Product for Arrays of Different Dimensions

## Syntax of kron( ) function

It is to be noted that while deploying the kron( ) function, one ought to assign the array that is to be scaled as the second input entity, whilst the array that is to be used for the scaling it as the first input entity.

Following are the inputs that are required for the functioning of the kron( ) function.

```numpy.kron(a, b)
```

where,

• a – N-dimensional array containing the elements to be used for scaling
• b – N-dimensional array which is to be scaled

## Calculating Kronecker Product for Arrays of Same Dimensions

In this section let’s find the Kronecker product for a pair of one-dimensional arrays as shown below.

```ar1 = np.array([1, 3, 7, 2, 9, 4])
ar2 = np.array([5, 6, 8, 0, -1, 7])
```

Now let us deploy the kron( ) function for the above two arrays.

```np.kron(ar1, ar2)
```

Once the above code is run, the following computation happens in the back end for calculating the results which shall be returned in the form of a one-dimensional array.

• The first element from ‘ar2’ is multiplied by the first element from ‘ar1’ to deduce the first element of the output array (i.e) 5×1 = 5.
• The second element from ‘ar2’ is multiplied by the first element from ‘ar1’ to deduce the second element of the output array (i.e) 6×1 = 6.
• The same happens with the subsequent elements from ‘ar2’ which gets multiplied by the first element from ‘ar1’ such that 8×1 = 8, 0x1 = 0, -1×1 = -1, 7×1 = 7.
• Now the cycle repeats with the second element from ‘ar1’ such that each element of ‘ar2’ gets multiplied by the second element of ‘ar1’ to obtain further the elements of the output array, such that, 5×3 = 15, 6×3 = 18, …., 7×3 = 21.
• The process iterates again until all the elements in ‘ar2’ are multiplied by those in ‘ar1’.
• The products of these elements are arranged in a sequence starting from the results of the product between elements of ‘ar2’ with the first element from ‘ar1’, followed by the second element from ‘ar2’ & so on.

Putting the above in the lingua of mathematics,

ar1 =      [a11  a12 …..a1n
a21 a22 …..a2n
am1 am2 …..amn]

ar2 =      [b11  b12 …..b1p
b21 b22 …..b2p
bo1 bo2 …..bop]

Kronecker product of ar1 & ar2 =              [a11xb11  a12xb12 …..a1nxb1n
am1xbo1 am2xbo2 …..amn xbop]

## Calculating Kronecker Product for Arrays of Different Dimensions:

The kron( ) function also holds well when arrays of dissimilar dimensions are fed in. But, at times it takes the privilege of prepending the smallest of the inputs with ones (1’s), should the need arise.

```ar3 = np.array([[1, 3, 7],
[2, 9, 4]])
ar4 = np.array([[5, 6],
[8, 0],
[-1, 7]])
np.kron(ar3, ar4)
```