Hadamard Product
The hadamard product is how some people might first think matrix multiplication works, which is wrong.
The hadamard product is denoted using and is also commonly known as the element-wise matrix multiplication which perfectly describes how it works. It is defined for two matrices and of the same dimensions and results in another matrix that has the same dimension as the two input matrices so . So each element at in the resulting matrix is the product of the elements at of the two input matrices.
Beginners in numpy often write the following lines of code to multiply two matrices
import numpy as np
A = np.array([[1,4,7],[2,3,8],[9,5,6]])
B = np.array([[6,5,8],[3,2,9],[7,4,1]])
A * B
array([[ 6, 20, 56],
[ 6, 6, 72],
[63, 20, 6]])
But as might notice this is the hadamard product/element-wise multiplication. You would get the same result if you used the np.multiply(A,B)
(opens in a new tab) function as this is the function that is called under the hood when using the *
operator. If you really did want to multiply with you would need to use the @
operator or np.matmul(A,B)
(opens in a new tab) function.
If you try to calculate a hadamard product in numpy where the matrices do not have the same shape you will probably get the following error:
ValueError: operands could not be broadcast together with shapes (2,2) (3,3)
So you can see that the hadamard product only works if A and B are of the same dimension/shape. However, from the error you can also see that there is an exception to this definition "if the matrices can be broadcast together". To find out more about broadcasting and how it works check out this page.