VOOZH about

URL: https://www.geeksforgeeks.org/python/compute-the-outer-product-of-two-given-vectors-using-numpy-in-python/

⇱ Compute the Outer Product of Two Given Vectors using NumPy in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Compute the Outer Product of Two Given Vectors using NumPy in Python

Last Updated : 6 Dec, 2025

The outer product of two vectors is a matrix where each element [i, j] is the product of the ith element of the first vector and the jth element of the second vector. NumPy provides the outer() function to calculate this efficiently.

Example: In this example, we calculate the outer product of two small 1D arrays.


Output
[[3 4]
 [6 8]]

Explanation: np.outer(x, y) multiplies each element of x with each element of y to form a 2x2 matrix.

Syntax

numpy.outer(a, b, out=None)

Parameters:

  • a: First input vector (1D array or flattened).
  • b: Second input vector (1D array or flattened).
  • out (Optional): array to store the result.

Returns: A 2D array where each element is a[i] * b[j].

Examples

Example 1: In this example, the outer product of two small arrays is calculated.


Output
[[12 30]
 [ 4 10]]

Explanation: np.outer(a, b) multiplies each element of a with each element of b to form a 2x2 matrix.

Example 2: Here, two 2x2 matrices are flattened automatically and the outer product is computed.


Output
[[ 0 1 1 9]
 [ 0 3 3 27]
 [ 0 2 2 18]
 [ 0 6 6 54]]

Explanation: np.outer(a, b) first flattens both matrices and then computes the outer product, producing a 4x4 matrix.

Example 3: In this example, two 3x3 matrices are flattened and their outer product is calculated.


Output
[[ 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 ...

Explanation: np.outer(a, b) flattens both 3x3 matrices into 1D arrays and calculates the outer product, resulting in a 9x9 matrix.

Comment