# How to Calculate the Outer Product of Vectors in Python

In linear algebra, the outer product of two vectors produces a matrix. Each element in this matrix is the product of elements from the two vectors at corresponding positions.

Let’s begin by examining two column vectors:

The outer product of these vectors is calculated as follows:

Let’s demonstrate this operation using Python:

import numpy as np

# Vector 1: 3x1 column vector
vector1 = np.array([[1], [2], [3]])

# Vector 2: 3x1 column vector
vector2 = np.array([[4], [5], [6]])

# Calculate the outer product of Vector 1 and Vector 2
outer_product_12 = np.outer(vector1, vector2)

# Print the outer product
print("Outer Product of Vector 1 and Vector 2:")
print(outer_product_12)


NumPy provides the np.outer() function specifically for computing the outer product of two vectors. Using this function, we get the following result:

Outer Product of Vector 1 and Vector 2:

[[ 4  5  6]
[ 8 10 12]
[12 15 18]]


Unlike Hadamard multiplication, which requires both vectors to have the same dimension, the outer product does not impose such restrictions. Let’s consider an example where the 2 vectors have different dimensions:

import numpy as np

# Vector 3: 1x3 row vector
vector3 = np.array([7, 8, 9])

# Vector 4: 1x2 row vector
vector4 = np.array([2, 4])

# Calculate the outer product of Vector 3 and Vector 4
outer_product_34 = np.outer(vector3, vector4)

# Print the outer product
print("Outer Product of a 3-dimensional Vector and 2-dimensional Vector:")
print(outer_product_34)


This output shows a 3 matrix as a result of the outer product between a 3-dimensional vector and a 2-dimensional vector:

Outer Product of a 3-dimensional Vector and 2-dimensional Vector:

[[14 28]
[16 32]
[18 36]]


The dimensions of the resulting outer product matrix are solely determined by the lengths of the two input vectors. Specifically, if the first vector has ‘m’ elements and second vector as ‘n’ elements, the resulting matrix will be an ‘m x n’ matrix. Let’s check if this the case for our examples above:

# Print vector lengths and shapes of resulting outer products
print("Length of Vector 1:", len(vector1))
print("Length of Vector 2:", len(vector2))
print("Shape of the outer product of Vector 1 and Vector 2:", outer_product_12.shape)

print("Length of Vector 3:", len(vector3))
print("Length of Vector 4:", len(vector4))
print("Shape of the outer product of Vector 3 and Vector 4:", outer_product_34.shape)


The dimensions of the resulting matrix from an outer product operation are determined directly by the lengths of the two input vectors:

Length of Vector 1: 3
Length of Vector 2: 3
Shape of the outer product of Vector 1 and Vector 2: (3, 3)

Length of Vector 3: 3
Length of Vector 4: 2
Shape of the outer product of Vector 3 and Vector 4: (3, 2)


It is important to note that the order of the vectors matters significantly when calculating the outer product. By switching the order of the vectors, we can observe how the resulting matrix’s shape will change:

import numpy as np

# Define Vector 3 as a 1x3 row vector
vector3 = np.array([7, 8, 9])

# Define Vector 4 as a 1x2 row vector
vector4 = np.array([2, 4])

# Calculate the outer product of Vector 4 and Vector 3 (reversed order)
outer_product_43 = np.outer(vector4, vector3)

# Print the outer product of the reversed order
print("Outer Product of a 2-dimensional Vector 4 and a 3-dimensional Vector 3:")
print(outer_product_43)
print("Length of Vector 4:", len(vector4))
print("Length of Vector 3:", len(vector3))
print("Shape of the outer product of Vector 4 and Vector 3:", outer_product_43.shape)


The output from the Python code will show the resulting matrix as well as the lengths of the vectors and the dimensions of the resulting matrix:

Outer Product of a 2-dimensional Vector 4 and a 3-dimensional Vector 3:

[[14 16 18]
[28 32 36]]

Length of Vector 4: 2
Length of Vector 3: 3

Shape of the outer product of Vector 4 and Vector 3: (2, 3)


The matrix generated from the outer product vector4 x vector3 is a 2 x 3 matrix. This contrasts with the the 3 x 2 matrix that resulted from the outer product of vector3 x vector4.

Understanding the outer product and its dependency on vector order is fundamental in linear algebra and its applications. The operation’s independence from the dimensions of the vectors involved adds a layer of flexibility to its use.