NumPy Kronecker Delta | What is NumPy.kron()?

Hello geeks and welcome in this article, we will cover the NumPy Kronecker delta function. Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples.

While writing our program, we represent our function as NumPy.kron(). But at first, let us try to understand what does the Kronecker means in general. We can understand the Kronecker function as the operation on the 2 matrices of arbitrary sizes resulting in a block matrix in mathematical terms.

To denote this operation, the symbol “⊗” is used. So we can conclude that NumPy Kronecker delta or NumPy.kron() helps us by finding the Kronecker product of 2 Input arrays. Next, we will look at the syntax associated with the function.

Numpy Kronecker Delta

Kronecker Delta is a famous matrix representation where the two (mostly nonnegative) variables act upon a rule that determines the matric element value. Dij = 0 if i ≠ j or 1 if i = j. This matrix is useful in many cases in mathematics where we need the output to be in a discrete system.

To create such a matrix, scipy.signal.unit_impulse is used.

Example of NumPy Kronecker delta

Code:

from scipy import signal
x = signal.unit_impulse(6)
print(x)
y = signal.unit_impulse(6, 3) # offset of 3
print(y)

Output:

[1. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]

Explanation:

The above function creates an array with signal impulses similar to Kronecker Delta. Moreover, you can pass 2nd argument as an offset to your output.

The return type is already in the Numpy Ndarray class, there is no need to convert them in a numpy array.

Example 2 of NumPy Kronecker delta

Code:

import numpy as np
n = 3
M = np.einsum('ij,kl->ijkl', np.eye(n,n), np.eye(n,n))
print(M)

Output:

[[[[1. 0. 0.]
   [0. 1. 0.]
   [0. 0. 1.]]

  [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

  [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]]


 [[[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

  [[1. 0. 0.]
   [0. 1. 0.]
   [0. 0. 1.]]

  [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]]


 [[[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

  [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

  [[1. 0. 0.]
   [0. 1. 0.]
   [0. 0. 1.]]]]

Explanation:

The above example creates a matrix of size n*n*n where the inner matrixes are created using the Kronecker delta function.

Numpy.kron() Function

Syntax Of Numpy kron()

numpy.kron(ab)

Here we can see that the function has a straightforward syntax and has only 2 parameters. In the next section, we will cover its parameter as well as the return type.

Parameter Of Numpy kron()

a,b: array_like

This parameter represents the 2 input arrays of which the Kronecker product needs to be calculated.

Return

out: ndarray

The function returns a ndarray representing the Kronecker Product of our input.

Examples

We have covered all the necessary theory associated with our function. Also, we have understood what Kronecker product means in general. In this section, we will look at various examples and understand how the function works. We will start with an elementary level example and gradually move our way to more complicated examples.

#input
import numpy as ppool
a=[1,23,4]
b=[2,45,5]
print(ppool.kron(a,b))

Output:

[   2   45    5   46 1035  115    8  180   20]

Before moving ahead with the explanation of the above example, I would like to make a few things clear. Kronecker multiplication is totally different from when compared to matrix multiplication in general. Now, let’s get back to the example. Here we have at first imported the NumPy module. Then we have defined 2 arrays of which we wish to get the Kronecker product. In the next step, we have used the print statement along with our syntax. Here our output justifies the input properly. We can understand it in this way, the general Kronecker product representation is [a11 b11, a11 b12,……… so on] and we compare it to our input the answer is justified.

From the above example, it is quite evident how it is different from the original matrix product. As for matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. But as in the above example, we can see that we performed operation for two 1*3 matrix. One difference that we can spot is the general matrix multiplication representation is [a11b11+a12b21 + a13b31 + ……… so on].

Now as we are done with this let us look at one more example.

#input
import numpy as ppool
a=[[1,23,4],
  [20,45,9],
  [34,8,6]]
b=[[2,45,5],
   [5,8,7]]
print(ppool.kron(a,b))

Output:

[[   2   45    5   46 1035  115    8  180   20]
 [   5    8    7  115  184  161   20   32   28]
 [  40  900  100   90 2025  225   18  405   45]
 [ 100  160  140  225  360  315   45   72   63]
 [  68 1530  170   16  360   40   12  270   30]
 [ 170  272  238   40   64   56   30   48   42]]

Here we can look at another example. In this particular case, we have followed all the steps similar to that of the first example. But instead of having a 1-d array as that of the 1st example, we went for two different 2-d arrays. The output here justifies our input. Just Imagine doing such humungous calculations by hand.

Also, Read

Conclusion

In this article, we have covered the NumPy Kronecker delta, also know as NumPy.kron(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case.

We also understood the basic difference between the Kronecker product and simple matrix multiplication. In the end, we can conclude that NumPy.Kron() helps us by calculating the Kronecker product of our input arrays.

I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read NumPy median up next.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mike Battaglia
Mike Battaglia
2 years ago

You are mixing up the Kronecker delta function with the Kronecker product, which are two totally different things. Numpy’s “kron” function is the Kronecker product of two matrices. The Kronecker delta function is a special array in which the first entry is 1 and all others are zero, which is found instead in scipy.signal.unit_impulse.

Python Pool
Admin
2 years ago
Reply to  Mike Battaglia

Thank you for informing this. I’ve updated the article with an updated way of calculating Kronecker Delta in Python.

Regards,
Pratik