Vector Hadamard Multiplication (Element-Wise)
If you were to ask someone on the street who had never studied linear algebra how to multiply two vectors together, they would likely describe Hadamard Multiplication. Also commonly known as element-wise multiplication, this operation is the most intuitive form of vector multiplication.
In this lesson, we define the Hadamard product, explore its geometric constraint, contrast it with the dot product, and implement it in Python.
1. Mathematical Definition
The Hadamard Product is defined as the multiplication of corresponding elements from two vectors of the same size.
Mathematically, we denote Hadamard multiplication using the circle-dot operator :
For two vectors , the resulting vector is computed element-by-element:
Writing it out in vector form:
2. The Dimensionality Constraint
Hadamard multiplication has a strict geometric constraint: it is defined only for vectors of the exact same size.
- Valid: Multiplying two 3-dimensional vectors.
- Invalid: Multiplying a 3-dimensional vector and a 5-dimensional vector.
If the vectors have different shapes, their element-wise corresponding parts do not exist, and the operation is mathematically undefined.
3. Step-by-Step Example
Let's compute the Hadamard product of two vectors and in :
Multiply the corresponding elements:
4. Hadamard Product vs. Dot Product
It is crucial not to confuse Hadamard multiplication with the Vector Dot Product:
| Feature | Hadamard Product () | Dot Product () |
|---|---|---|
| Output Type | Vector (same size as inputs) | Scalar (a single number) |
| Summation | No summation | Sums the products of elements |
| Notation | Circle-dot () | Transpose matrix multiplication or dot () |
| Core Concept | Component scaling / Masking | Spatial projection / Similarity |
If we took the dot product of the example vectors above, we would sum the Hadamard results:
5. Python Implementation
In Python, NumPy provides two primary ways to compute the Hadamard product:
Method 1: Using np.multiply()
The np.multiply() function accepts standard Python lists or arrays and computes their element-wise product:
import numpy as np
# Define lists
w1 = [ 1, 3, 5 ]
w2 = [ 3, 4, 2 ]
# Compute Hadamard multiplication
w3 = np.multiply(w1, w2)
print(w3)[ 3 12 10]Method 2: Using the Overloaded * Operator
When you convert lists to NumPy arrays, the standard multiplication asterisk operator * is overloaded to perform element-wise (Hadamard) multiplication automatically:
import numpy as np
# Convert to NumPy arrays
arr1 = np.array([ 1, 3, 5 ])
arr2 = np.array([ 3, 4, 2 ])
# Compute using overloaded * operator
w3 = arr1 * arr2
print(w3)[ 3 12 10]Avoid the*Operator on Lists: Do not use the standard*operator on raw Python lists (e.g.w1 * w2). Python will throw aTypeErrorbecause it interprets list multiplication as duplication, which is invalid between two lists. Always convert to NumPy arrays first or usenp.multiply().
Key Takeaways
- Hadamard Product: Element-wise multiplication, yielding a vector of the same size.
- Symbol: Denoted by the circle-dot operator .
- Constraint: Valid only for vectors of identical dimensions.
- Python Syntax: Use
np.multiply(a, b)or convert to arrays and multiply witha * b.