Calculate Hadamard Product using Python — Linear Algebra

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

Misha Sv
Level Up Coding
Published in
3 min readJan 18, 2022

--

Image by Author

Table of contents

  • Introduction
  • Hadamard product explained
  • Hadamard product in Python
  • Conclusion

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:

Source

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

where ∘ is the element-wise multiplication, and not the ⋅ multiplication.

Let’s consider two sample matrices A and B:

Image by Author

Calculating the Hadamard product (element-wise multiplication):

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

--

--