# kronecker delta in numpy

• Last Update :
• Techknowledgy :

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

```from scipy
import signal
x = signal.unit_impulse(6)
print(x)
y = signal.unit_impulse(6, 3) # offset of 3
print(y)```
1._
```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.]```
3._
```import numpy as np
n = 3
M = np.einsum('ij,kl->ijkl', np.eye(n, n), np.eye(n, n))
print(M)```
6._
```#input
import numpy as ppool
a = [1, 23, 4]
b = [2, 45, 5]
print(ppool.kron(a, b))```
8._
```#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))```
```import numpy as np
n = 3
M = np.einsum('ij,kl->ijkl', np.eye(n, n), np.eye(n, n))
print(M)```
```#input
import numpy as ppool
a = [1, 23, 4]
b = [2, 45, 5]
print(ppool.kron(a, b))```
```#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))```

Suggestion : 2

Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first.,Kronecker product of two arrays.,The function assumes that the number of dimensions of a and b are the same, if necessary prepending the smallest with ones. If a.shape = (r0,r1,..,rN) and b.shape = (s0,s1,...,sN), the Kronecker product has shape (r0*s0, r1*s1, ..., rN*SN). The elements are products of elements from a and b, organized explicitly by:,In the common 2-D case (N=1), the block structure can be visualized:

`kron(a, b)[k0, k1, ..., kN] = a[i0, i1, ..., iN] * b[j0, j1, ..., jN]`
`kt = it * st + jt, t = 0, ..., N`
```[
[a[0, 0] * b, a[0, 1] * b, ..., a[0, -1] * b],
[......],
[a[-1, 0] * b, a[-1, 1] * b, ..., a[-1, -1] * b]
]```
```>>> np.kron([1, 10, 100], [5, 6, 7])
array([5, 6, 7, ..., 500, 600, 700]) >>>
np.kron([5, 6, 7], [1, 10, 100])
array([5, 50, 500, ..., 7, 70, 700])```
```>>> np.kron(np.eye(2), np.ones((2, 2)))
array([
[1., 1., 0., 0.],
[1., 1., 0., 0.],
[0., 0., 1., 1.],
[0., 0., 1., 1.]
])```
```>>> a = np.arange(100).reshape((2, 5, 2, 5)) >>>
b = np.arange(24).reshape((2, 3, 4)) >>>
c = np.kron(a, b) >>>
c.shape(2, 10, 6, 20) >>>
I = (1, 3, 0, 2) >>>
J = (0, 2, 1) >>>
J1 = (0, ) + J # extend to ndim = 4 >>>
S1 = (1, ) + b.shape >>>
K = tuple(np.array(I) * np.array(S1) + np.array(J1)) >>>
c[K] == a[I] * b[J]
True```

Suggestion : 3

Alternative solution using numpy's einsum:

```n = 5
M = np.einsum('ij,kl->ijkl', np.eye(n, n), np.eye(n, n))```

Your particular example can be done like so

```import numpy as np
n = 3
i, k = np.ogrid[: n,: n]
res = np.zeros((n, n, n, n), int)
res[i, i, k, k] = 1
res
array([
[
[
[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]
]
]
])```

And the followup:

```res3 = np.zeros((3, 3, 3), int)
i = np.arange(n)
res3[i, i, i] = 1
res3
array([
[
[1, 0, 0],
[0, 0, 0],
[0, 0, 0]
],

[
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
],

[
[0, 0, 0],
[0, 0, 0],
[0, 0, 1]
]
])```

Suggestion : 4

numpy.kron(a, b) [source] ¶ Kronecker product of two arrays. Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first. , 1 day ago Sep 03, 2021  · The Kronecker product of two given multi-dimensional arrays can be computed using the kron() method in the NumPy module. The kron() method takes two arrays as an argument and returns the Kronecker product of those … , This is documentation for an old release of NumPy (version 1.10.1). Read this page in the documentation of the latest stable release (version > 1.17). Kronecker product of two arrays. Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first. , 5 days ago numpy.kron# numpy. kron ... Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first. Parameters a, b array_like Returns out ndarray. See also. outer. The outer product. Notes. The function assumes that the number of dimensions of a and b are the same, if necessary prepending the smallest with ones.

```import numpy as np n = 3 i, k = np.ogrid[: n,: n] res = np.zeros((n, n, n, n), int) res[i, i, k, k] = 1 res array([
[
[
[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]
]
]
])```
1._
```from scipy
import signal x = signal.unit_impulse(6) print(x) y = signal.unit_impulse(6, 3) # offset of 3 print(y)```
`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.]`
``[1. 0. 0. 0. 0. 0.]  [0. 0. 0. 1. 0. 0.]``
`import numpy as np n = 3 M = np.einsum('ij,kl->ijkl', np.eye(n, n), np.eye(n, n)) print(M)`
`numpy.kron(a, b)`
``numpy.kron`(a, b)`
```#input
import numpy as ppool a = [1, 23, 4] b = [2, 45, 5] print(ppool.kron(a, b))```

Suggestion : 5

Unit impulse signal (discrete delta function) or unit basis vector., Signal processing ( scipy.signal ) ,2-dimensional impulse, centered:, Random Number Generators ( scipy.stats.sampling )

```>>> from scipy
import signal
>>>
signal.unit_impulse(8)
array([1., 0., 0., 0., 0., 0., 0., 0.])```
```>>> signal.unit_impulse(7, 2)
array([0., 0., 1., 0., 0., 0., 0.])```
```>>> signal.unit_impulse((3, 3), 'mid')
array([
[0., 0., 0.],
[0., 1., 0.],
[0., 0., 0.]
])```
```>>> signal.unit_impulse((4, 4), 2)
array([
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 0.]
])```
```>>> imp = signal.unit_impulse(100, 'mid') >>>
b, a = signal.butter(4, 0.2) >>>
response = signal.lfilter(b, a, imp)```
```>>>
import matplotlib.pyplot as plt >>>
plt.plot(np.arange(-50, 50), imp) >>>
plt.plot(np.arange(-50, 50), response) >>>
plt.margins(0.1, 0.1) >>>
plt.xlabel('Time [samples]') >>>
plt.ylabel('Amplitude') >>>
plt.grid(True) >>>
plt.show()```

Suggestion : 6

Last Updated : 03 Sep, 2021,GATE CS 2021 Syllabus

1._
```A = | (a00)(a01) |
|
(a10)(a11) |

B = | (b00)(b01) |
|
(b10)(b11) |

A⊗ B = | (a00) * (b00)(a00) * (b01)(a01) * (b00)(a01) * (b00) |
|
(a00) * (b01)(a00) * (b11)(a01) * (b01)(a01) * (b11) |
|
(a10) * (b00)(a10) * (b01)(a11) * (b00)(a11) * (b01) |
|
(a10) * (b10)(a10) * (b11)(a11) * (b10)(a11) * (b11) |```

Syntax:

`numpy.kron(array1, array2)`

Output:

```Array1: [
[1 2]
[3 4]
]

Array2: [
[5 6]
[7 8]
]

Array1⊗ Array2: [
[5 6 10 12]
[7 8 14 16]
[15 18 20 24]
[21 24 28 32]
]```