Compute the outer product of two given vectors using NumPy in Python
Last Updated :
15 Jul, 2025
In Python, we can use the outer() function of the NumPy package to find the outer product of two matrices.
Syntax : numpy.outer(a, b, out = None)
Parameters :
a : [array_like] First input vector. Input is flattened if not already 1-dimensional.
b : [array_like] Second input vector. Input is flattened if not already 1-dimensional.
out : [ndarray, optional] A location where the result is stored.
Return : [ndarray] Returns the outer product of two vectors. out[i, j] = a[i] * b[j]
Example 1: Outer Product of 1-D array
Python3
# Importing library
import numpy as np
# Creating two 1-D arrays
array1 = np.array([6,2])
array2 = np.array([2,5])
print("Original 1-D arrays:")
print(array1)
print(array2)
# Output
print("Outer Product of the two array is:")
result = np.outer(array1, array2)
print(result)
Output:
Original 1-D arrays:
[6 2]
[2 5]
Outer Product of the two array is:
[[12 30]
[ 4 10]]
Time Complexity: O(n^2), where n is the length of the input arrays "array1" and "array2".
Space Complexity: O(n^2)
Example 2: Outer Product of 2X2 matrix
Python3
# Importing library
import numpy as np
# Creating two 2-D matrix
matrix1 = np.array([[1, 3], [2, 6]])
matrix2 = np.array([[0, 1], [1, 9]])
print("Original 2-D matrix:")
print(matrix1)
print(matrix2)
# Output
print("Outer Product of the two matrix is:")
result = np.outer(matrix1, matrix2)
print(result)
Output:
Original 2-D matrix:
[[1 3]
[2 6]]
[[0 1]
[1 9]]
Outer Product of the two matrix is:
[[ 0 1 1 9]
[ 0 3 3 27]
[ 0 2 2 18]
[ 0 6 6 54]]
Example 3: Outer Product of 3X3 matrix
Python3
# Importing library
import numpy as np
# Creating two 3-D matrix
matrix1 = np.array([[2, 8, 2], [3, 4, 8], [0, 2, 1]])
matrix2 = np.array([[2, 1, 1], [0, 1, 0], [2, 3, 0]])
print("Original 3-D matrix:")
print(matrix1)
print(matrix2)
# Output
print("Outer Product of the two matrix is:")
result = np.outer(matrix1, matrix2)
print(result)
Output:
Original 3-D matrix:
[[2 8 2]
[3 4 8]
[0 2 1]]
[[2 1 1]
[0 1 0]
[2 3 0]]
Outer Product of the two matrix is:
[[ 4 2 2 0 2 0 4 6 0]
[16 8 8 0 8 0 16 24 0]
[ 4 2 2 0 2 0 4 6 0]
[ 6 3 3 0 3 0 6 9 0]
[ 8 4 4 0 4 0 8 12 0]
[16 8 8 0 8 0 16 24 0]
[ 0 0 0 0 0 0 0 0 0]
[ 4 2 2 0 2 0 4 6 0]
[ 2 1 1 0 1 0 2 3 0]]
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice