Get the Outer product of an array and a scalar in Python

To get the outer product of an array and a scalar, use the numpy.outer() method in Python. The outer product creates a matrix where each element of the first input is multiplied by each element of the second input.

Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product is ?

[[a0*b0 a0*b1 ... a0*bN ]
 [a1*b0 a1*b1 ... a1*bN ]
 [ ...   ...  ...  ... ]
 [aM*b0 aM*b1 ... aM*bN ]]

Syntax

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

Parameters:

  • a ? First input array (flattened if not 1-dimensional)
  • b ? Second input array or scalar (flattened if not 1-dimensional)
  • out ? Optional output array to store the result

Example with Array and Scalar

Let's create an identity matrix and compute its outer product with a scalar ?

import numpy as np

# Create an identity matrix
arr = np.eye(2)
print("Array:")
print(arr)

# Scalar value
val = 2

# Compute outer product
result = np.outer(arr, val)
print("\nOuter Product with scalar:")
print(result)
Array:
[[1. 0.]
 [0. 1.]]

Outer Product with scalar:
[[2. 0.]
 [0. 0.]
 [0. 2.]
 [0. 0.]]

Example with Two Arrays

Here's how outer product works with two arrays ?

import numpy as np

# Create two vectors
a = np.array([1, 2, 3])
b = np.array([4, 5])

print("Vector a:", a)
print("Vector b:", b)

# Compute outer product
outer_product = np.outer(a, b)
print("\nOuter Product:")
print(outer_product)
Vector a: [1 2 3]
Vector b: [4 5]

Outer Product:
[[ 4  5]
 [ 8 10]
 [12 15]]

Key Points

  • Input arrays are automatically flattened if they are not 1-dimensional
  • The resulting array has shape (M, N) where M and N are the lengths of flattened inputs
  • When using a scalar, it's treated as a single-element array
  • The outer product is useful in linear algebra and tensor operations

Conclusion

The numpy.outer() function efficiently computes outer products between arrays and scalars. It automatically handles array flattening and produces results useful for mathematical computations and matrix operations.

---
Updated on: 2026-03-26T20:10:27+05:30

288 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements