numpy.kron() method in Python

In this article, we will learn how to use the numpy.kron method in Python with the help of a few examples.

Using numpy.kron() in Python

In Python, we use numpy.kron() method to find the  Kronecker product of two given arrays. Let us see how with the help of an example.

Firstly, we have to import the library numpy in our Python program. After importing numpy, we will declare two lists or arrays.

import numpy as np

# Declaring the arrays
A = [1,2,3]
B = [4,5,6]

Here, we have declared the two lists of which we want to find the Kronecker product. Now, use the syntax np.kron() to find the product.

# Using np.kron() method

C = np.kron(A,B)
print(C)

Output:

[ 4 5 6 8 10 12 12 15 18]

This is the required product. Let us take another example having three lists this time.

import numpy as np
A = [1,2,3]
B = [4,5,6]
D = [7,8,9]
c = np.kron([A,B],D)
print(c)

Output:

[[ 7  8  9 14 16 18 21 24 27]
 [28 32 36 35 40 45 42 48 54]]

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *