AppDividend
Latest Code Tutorials

Numpy kron: How to Use np kron() Function

0

Numpy kron() method is used to get the Kronecker product of two given lists. But wait, what is the Kronecker product? Suppose we have two lists: A [a0,a1,a2] and B [ b0, b1, b2] . If we want to calculate the Kronecker product of these two lists, the answer will be:

[a0*b1, a1*b0, a2*b0, a0*b1, a1*b1, a2*b1, a0*b2, a1*b2, a2*b2]

Numpy kron()

To calculate the Kronecker product in Python, use the np kron() function. The kron() function computes the Kronecker product, a composite array made of blocks of the second array scaled by the first.

Syntax

np.kron(list1, list2)  

Parameters

The kron() function takes two main parameters:

list1: This is the first list.

list2: This is the second list with which we want to calculate the Kronecker product.

Return Value

The kron() function returns a vector of size ( shape|list1| * shape |list2| ) which contains the Kronecker product.

Programming Example

Finding Kronecker product when two lists are 1D

# Finding Kronecker product when two lists are 1D
import numpy as np

# Declaring the first array
arr1 = np.array([-2, -1, 0, 1, 2, 3])
arr2 = np.array([0, 1, 2, 3, 4, 5])

print("First array is :\n", arr1)
print("Second array is :\n", arr2)

# Calculating the Kronecker product
ans = np.kron(arr1, arr2)

print("Kronecker product of these arrays are:\n", ans)

Output

First array is :
 [-2 -1  0  1  2  3]
Second array is :
 [0 1 2 3 4 5]
Kronecker product of these arrays are:
 [  0  -2  -4  -6  -8 -10   0  -1  -2  -3  -4  -5   0   0   0   0   0   0
   0   1   2   3   4   5   0   2   4   6   8  10   0   3   6   9  12  15]

Explanation

First, we have created two arrays of one dimensional. Then we have printed those two arrays. Then we have called numpy.kron() to get the Kronecker vector product.

Finding a Kronecker product when one of the lists is 2D and the other is 1D.

See the following code.

# Finding Kronecker product when two lists are 2D
import numpy as np

# Declaring the first array
arr1 = np.array([[-2, -1, 0], [1, 2, 3]])
arr2 = np.array([[0, 1, 2], [3, 4, 5]])

print("First array is :\n", arr1)
print("Second array is :\n", arr2)

# Calculating the Kronecker product
ans = np.kron(arr1, arr2)

print("Kronecker product of these arrays are:\n", ans)

Output

First array is :
 [[-2 -1  0]
 [ 1  2  3]]
Second array is :
 [[0 1 2]
 [3 4 5]]
Kronecker product of these arrays are:
 [[  0  -2  -4   0  -1  -2   0   0   0]
 [ -6  -8 -10  -3  -4  -5   0   0   0]
 [  0   1   2   0   2   4   0   3   6]
 [  3   4   5   6   8  10   9  12  15]]

Explanation

First, we have created two arrays of two-dimensional. Then we have printed those two arrays. Then we have called numpy.kron() to get the Kronecker vector product.

That is it for the numpy kron() function.

See also

Numpy matrix_power()

Numpy outer()

Numpy linalg matrix_rank()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.