np.inner: How to Use numpy inner() Method in Python

Numpy inner() method function returns the inner product of vectors for 1D arrays. For higher dimensions, it returns the sum-product over the last axes.

np.inner

The np.inner() is a numpy library method used to compute the inner product of two given input arrays. In the case of 1D arrays, the ordinary inner product of vectors is returned (without complex conjugation), whereas, in the case of higher dimensions, a sum-product over the last axes is returned as a result.

Syntax

numpy.inner(arr1, arr2)

Parameters

The inner() function takes at most two parameters:

 arr1: array_like, the first input array

 arr2: array_like, the second input array

if arr1 and arr2 are nonscalars, their last dimensions must match.

Return Value

out: ndarray

The method returns an n-dimensional array containing the inner product of two arrays arr1 and arr2. The shape of the output array can be calculated using the equation given as:

out.shape = arr1.shape[:-1] + arr2.shape[:-1]

Raises: ValueError

A ValueError is raised if the last dimension of arr1 and arr2 do not match.

Programming Example

Program to show the working of numpy.inner() method in case of 1D array/vectors

# importing the numpy module
import numpy as np

# first 1-D array arr1
arr1 = np.array([2, 4, 6, 8])

# second 1-D array arr2
arr2 = np.array([1, 3, 5, 7])

# calculating inner product
res = np.inner(arr1, arr2)
print("Resultant array is : ", res)

# first 1-D array arr1
arr3 = np.array([8+2j])

# second 1-D array arr2
arr4 = np.array([1+6j])

# calculating inner product'
out = np.inner(arr3, arr4)
print("Output array is : ", out)

Output

Resultant array is :  100
Output array is :  (-4+50j)

Explanation

In the above code, we have taken two one-dimensional input arrays named arr1 and arr2; we have displayed output by displaying the inner product of both the arrays. The result we get is a scalar, i.e., 100.

The calculation may be shown as:

2*1 + 4*3 + 6*5 + 8*7 = 100

Also, to display calculation in the case of complex numbers, we took another two vectors named arr3 and arr4 and then calculated its inner product. The result obtained is given as -4+50j. Result calculation can be shown as:

8*1 + 8*6j + 2j*1 + 2j*6j

= 8 + 48j + 2j -12

= -4 + 50j

Program to show the working of numpy.inner() method in case of a multi-dimensional array

# importing the numpy module
import numpy as np

# first 2D array arr1
arr1 = np.array([[3, 2], [0, 4]])
print("first array is :")
print(arr1)

# second 2D array arr1
arr2 = np.array([[1, 2], [3, 4]])
print("second array is :")
print(arr2)

# calculating inner product
res = np.inner(arr1, arr2)
print("Resultant array is :")
print(res)

Output

first array is :
[[3 2]
 [0 4]]
second array is :
[[1 2]
 [3 4]]
Resultant array is :
[[ 7 17]
 [ 8 16]]

Explanation

In the above program, we have taken two different two-dimensional arrays named arr1 and another named arr2. Then we have displayed output by displaying the inner product of both the arrays. The resultant array will also have the shape of (2,2).

The inner product for above example can be calculated as:

3*1 + 2*2, 3*3 + 2*4

0*1 + 4*2, 0*3 + 4*4

That’s it for this Numpy.inner() function.

See also

Numpy matmul()

Numpy convolve()

Numpy correlate()

Numpy polyfit()

Numpy linalg det()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.