Vector Outer Product
In previous lessons, we explored two methods for multiplying vectors: the Dot Product (which sums element products to yield a scalar) and the Hadamard Product (which multiplies corresponding components to yield another vector).
In this lesson, we introduce a third, incredibly powerful method of vector multiplication: the Outer Product. Instead of producing a scalar or a vector, the outer product between two vectors produces a complete matrix.
1. Mathematical Notation and Rules
Recall the differences in mathematical notation for these three vector multiplication methods:
- Dot Product (Inner Product): Written as . We transpose the first vector. The result is always a single scalar number.
- Hadamard Product: Written as . It multiplies corresponding elements and results in another vector of the same size.
- Outer Product: Written as . We transpose the second vector. The result is always a matrix.
Dimensions and Flexibility
Unlike the dot and Hadamard products, the outer product does not require vectors of the same size!
- Let be an -dimensional column vector ().
- Let be an -dimensional column vector ().
- The outer product matrix is defined as:
The resulting matrix has rows (matching the dimension of the left vector ) and columns (matching the dimension of the right vector ).
2. Calculating the Outer Product
Let's compute the outer product of two vectors in :
Transposing turns it into a row vector ():
The resulting matrix is populated by multiplying every element of the left vector by every element of the right vector:
3. Two Conceptual Lenses
There are two complementary ways to visualize how this matrix is constructed. Both yield the exact same result, but they provide different insights that are critical for advanced matrix factorization and machine learning algorithms.
Perspective 1: The Column Perspective
From the Column Perspective, we build the matrix column-by-column.
Each column in the resulting matrix is a vector-scalar multiplication of the entire column vector on the left (), scaled by a different corresponding element of the row vector on the right ():
- Column 1 is scaled by .
- Column 2 is scaled by .
- Column 3 is scaled by .
Because every column is a scaled version of the same vector , all columns of an outer product matrix are linearly dependent. In advanced linear algebra terms, this means an outer product matrix always has a rank of 1.
Perspective 2: The Row Perspective
From the Row Perspective, we build the matrix row-by-row.
Each row in the resulting matrix is a scalar-vector multiplication of the entire transposed row vector on the right (), scaled by a different corresponding element of the column vector on the left ():
- Row 1 is scaled by .
- Row 2 is scaled by .
- Row 3 is scaled by .
Again, because every row is a scaled version of the same vector , all rows are linearly dependent, reinforcing the rank-1 nature of the matrix.
4. Python Implementation
In NumPy, we calculate the outer product using the highly optimized np.outer() function.
To help build intuition, we also show a conceptually illustrative (but computationally inefficient) double-nested loop approach that populates each cell manually:
import numpy as np
# 1. Define two vectors (dimensions do not need to be equal)
v1 = np.array([ 1, 2, 3 ])
v2 = np.array([ -1, 0, 1 ])
# 2. Compute using NumPy's optimized outer product
op_numpy = np.outer(v1, v2)
# 3. Compute manually using loops (conceptual walkthrough)
op_loop = np.zeros((len(v1), len(v2)))
for i in range(0, len(v1)):
for j in range(0, len(v2)):
op_loop[i, j] = v1[i] * v2[j]
print("--- Vector 1 ---")
print(v1)
print("\n--- Vector 2 ---")
print(v2)
print("\n--- NumPy Outer Product Matrix ---")
print(op_numpy)
print("\n--- Loop-Based Outer Product Matrix ---")
print(op_loop)
print(f"\nAre both results identical? {np.array_equal(op_numpy, op_loop)}")--- Vector 1 ---
[1 2 3]
--- Vector 2 ---
[-1 0 1]
--- NumPy Outer Product Matrix ---
[[-1 0 1]
[-2 0 2]
[-3 0 3]]
--- Loop-Based Outer Product Matrix ---
[[-1. 0. 1.]
[-2. 0. 2.]
[-3. 0. 3.]]
Are both results identical? TrueKey Takeaways
- Outer Product: Written as , which multiplies an column vector by a row vector to yield an matrix.
- Flexible Dimensions: The input vectors do not need to have the same number of elements ().
- Column Perspective: The matrix is constructed by columns where each column is the entire vector scaled by an element .
- Row Perspective: The matrix is constructed by rows where each row is the entire vector scaled by an element .
- Rank-1 Property: Because all rows (or columns) are scalar multiples of a single vector, an outer product matrix is always rank 1 (linearly dependent columns/rows).