Compute the outer product of two given vectors using NumPy in Python

Compute the outer product of two given vectors using NumPy in Python

The outer product of two vectors a and b is a matrix M where each element Mij​ is the product of ai​ and bj​. In mathematical notation, Mij​=ai​bj​.

In NumPy, you can compute the outer product using the numpy.outer() function.

Here's a simple example demonstrating how to compute the outer product of two vectors using NumPy:

import numpy as np # Define two vectors a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) # Compute the outer product outer_product = np.outer(a, b) print(outer_product) 

Output:

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

In this example, the vectors a and b are of size 3, so the resulting outer product is a 3x3 matrix.


More Tags

asp.net-core-webapi datamodel automatic-ref-counting bootstrap-modal firemonkey dialog google-maps drive regression pointers

More Programming Guides

Other Guides

More Programming Examples