Calculate Hadamard Product using Python

This article was first published on PyShark , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In this article we will discuss the steps and intuition for calculating the Hadamard product with examples using Python.




Table of contents


Introduction

Hadamard product of two matrices calculation is very similar to the process of matrix addition, but the operation itself is multiplication.

To continue following this tutorial we will need the following Python library: numpy.

If you don’t have them installed, please open “Command Prompt” (on Windows) and install them using the following code:

pip install numpy

Hadamard product explained

Hadamard product is also known as the element-wise multiplication or element-wise product.

While Hadamard product performs the multiplication operation, it is different from the matrix multiplication (matrix product) which we usually see in linear algebra.

Hadamard product can be computed only for two matrices of the same dimension (2×2, 3×3, and so on). This operation produces a matrix which is the same dimension as the input matrices:

For matrices \(A\) and \(B\) (of the same dimension), the Hadamard product can be computed as:

$$C = A \circ B$$

where \(\circ\) is the element-wise multiplication, and not the \(\cdot\) multiplication.

Let’s consider two sample matrices \(A\) and \(B\):

$$A = \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{bmatrix}$$

$$B = \begin{bmatrix} b_{11} & b_{12} & b_{13} \\ b_{21} & b_{22} & b_{23} \\ b_{31} & b_{32} & b_{33} \end{bmatrix}$$

Calculating the Hadamard product (element-wise multiplication):

$$C = A \circ B = \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{bmatrix} \circ \begin{bmatrix} b_{11} & b_{12} & b_{13} \\ b_{21} & b_{22} & b_{23} \\ b_{31} & b_{32} & b_{33} \end{bmatrix} = \begin{bmatrix} a_{11} b_{11} & a_{12} b_{12} & a_{13} b_{13} \\ a_{21} b_{21} & a_{22} b_{22} & a_{23} b_{23} \\ a_{31} b_{31} & a_{32} b_{32} & a_{33} b_{33} \end{bmatrix} $$

The process is very similar to matrix addition or matrix subtraction, except here we are multiplying the elements of the matrices.


For an illustrative example, we will use the same matrices \(A\) and \(B\) as we used in the matrix multiplication article:

$$A = \begin{bmatrix} 1 & 6 \\ 2 & 3 \end{bmatrix}$$

$$B = \begin{bmatrix} 5 & 1 \\ 3 & 2 \end{bmatrix}$$

Calculating the Hadamard product:

$$A \circ B = \begin{bmatrix} 1 & 6 \\ 2 & 3 \end{bmatrix} \circ \begin{bmatrix} 5 & 1 \\ 3 & 2 \end{bmatrix} = \begin{bmatrix} 1 \times 5 & 6 \times 1 \\ 2 \times 3 & 3 \times 2 \end{bmatrix} = \begin{bmatrix} 5 & 6 \\ 6 & 6 \end{bmatrix}$$





Hadamard product in Python

In order to calculate the Hadamard product (element-wise matrix multiplication) in Python we will use the numpy library. And the first step will be to import it:

import numpy as np

Numpy has a lot of useful functions, and for this operation we will use the multiply() function which multiplies arrays element-wise.

Recall that in Python matrices are constructed as arrays, and that matrices need to be of the same dimension to be added. And the next step will be to define the input matrices.

We are going to use the same 2×2 matrices as in the previous section:

A = np.array([[1, 6],
              [2, 3]])

B = np.array([[5, 1],
              [3, 2]])

Now that we have the required matrices, we can easily calculate the Hadamard product resulting from the matrix element-wise multiplication:

C = np.multiply(A, B)

print(C)

And you should get:

[[5 6]
 [6 6]]

which is exactly the same output as in our example where we calculated it manually.





Conclusion

In this article we discussed the intuition and steps for Hadamard product, as well as shown complete examples using Python.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Linear Algebra articles.

The post Calculate Hadamard Product using Python appeared first on PyShark.

To leave a comment for the author, please follow the link and comment on their blog: PyShark .

Want to share your content on python-bloggers? click here.