Open In App

Compute the Inner Product of Vectors for 1D Arrays using NumPy in Python

Last Updated : 10 Dec, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

Inner product (or dot product) of two 1D arrays is the sum of the products of their corresponding elements. NumPy provides the inner() function to calculate this efficiently.

Example: In this example, we compute the inner product of two small arrays with values.

Python
import numpy as np

x = np.array([1, 2])
y = np.array([3, 4])
res = np.inner(x, y)
print(res)

Output
11

Explanation: np.inner(x, y) multiplies corresponding elements (1*3 + 2*4) and sums them to get 11.

Syntax

numpy.inner(array1, array2)

  • Parameters: array1, array2 -> 1D arrays whose inner product is to be calculated.
  • Returns: A single scalar value representing the inner product.

Examples

Example 1: Calculating the inner product of two small arrays.

Python
import numpy as np

a = np.array([6, 2])
b = np.array([2, 5])
res = np.inner(a, b)
print(res)

Output
22

Explanation: np.inner(a, b) multiplies corresponding elements (6*2 + 2*5) and sums them to get 22.

Example 2: In this example, the inner product of two arrays with more elements is calculated.

Python
import numpy as np

a = np.array([1, 3, 5])
b = np.array([0, 1, 5])
res = np.inner(a, b)
print(res)

Output
28

Explanation: np.inner(a, b) calculates (1*0 + 3*1 + 5*5) which equals 28.

Example 3: In this example, we compute the inner product of arrays containing varied values.

Python
import numpy as np

a = np.array([1, 2, 2, 8])
b = np.array([2, 1, 0, 6])
res = np.inner(a, b)
print(res)

Output
52

Explanation: np.inner(a, b) multiplies elements (1*2 + 2*1 + 2*0 + 8*6) and sums them to give 52.


Article Tags :

Explore