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