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
from scipy import signal x = signal.unit_impulse(6) print(x) y = signal.unit_impulse(6, 3) # offset of 3 print(y)
[1. 0. 0. 0. 0. 0.] [0. 0. 0. 1. 0. 0.]
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
import numpy as np n = 3 M = np.einsum('ij,kl->ijkl', np.eye(n,n), np.eye(n,n)) print(M)
[[[[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.]]]]
The above example creates a matrix of size n*n*n where the inner matrixes are created using the Kronecker delta function.
Syntax Of Numpy kron()
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()
This parameter represents the 2 input arrays of which the Kronecker product needs to be calculated.
The function returns a ndarray representing the Kronecker Product of our input.
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))
[ 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))
[[ 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.
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.