Compute the outer product of two given vectors using NumPy in Python
Last Updated :
25 Apr, 2023
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]]
Similar Reads
How to compute the cross product of two given vectors using NumPy? Let's see the program to compute the cross product of two given vectors using NumPy. For finding the cross product of two given vectors we are using numpy.cross() function of NumPy library.Syntax: numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[Return: cross product of two (arrays of) vec
1 min read
Compute the inner product of vectors for 1-D arrays using NumPy in Python Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read
Compute the inner product of vectors for 1-D arrays using NumPy in Python Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read
Get the Outer Product of an array with vector of letters using NumPy in Python In this article let's see how to get the outer product of an array with a vector of letters in Python. numpy.outer() method The numpy.outer() method is used to get the outer product of an array with a vector of elements in Python. A matrix is the outer product of two coordinate vectors in linear alg
4 min read
Vector outer product with Einstein summation convention using NumPy in Python In this article, we will find vector outer product with Einstein summation convention in Python. numpy.einsum() method The numpy.einsum() method from the NumPy library is used to find the vector outer product with the Einstein summation convention in Python. Many common multi-dimensional, linear al
3 min read