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

*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,

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

**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 = [a_{11 }a_{12 }…..a_{1n}

a_{21 }a_{22 }…..a_{2n}

a_{m1 }a_{m2 }…..a_{mn}]

ar2 = [b_{11 }b_{12 }…..b_{1p}

b_{21 }b_{22 }…..b_{2p}

b_{o1 }b_{o2 }…..b_{op}]

Kronecker product of ar1 & ar2 = [a_{11}xb_{11 }a_{12}xb_{12 }…..a_{1n}xb_{1n}_{ }a_{m1}xb_{o1 }a_{m2}xb_{o2 }…..a_{mn} xb_{op}]

**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)
```

**Conclusion:**

Now that we have reached the end of this article, hope it has elaborated on how to use the *kron( ) *function from the *numpy *library to calculate the Kronecker product for a given pair of tensors. Here’s another article that explains the *tensordot*( ) function from *numpy *in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, *adios*!