# Numpy inner – Python NumPy inner() Function

NumPy inner() Function:

Numpy inner: The inner product of two arrays is returned by the inner() function of the NumPy module. It returns the ordinary inner product for 1-D arrays (without complex conjugation). A sum product over the last axes is returned for higher dimensions.

Syntax:

numpy.inner(a, b)

Parameters

a: This is required. It is the first array_like parameter given as input. If a and b are nonscalar, they must have the same last dimension.

b: This is required. It is the second array_like parameter given as input.

Return Value:

The inner product of a and b is returned.

Note: If the last dimension of a and b are different sizes, a ValueError exception is thrown.

## NumPy inner() Function in Python

Example1

np.inner: Here the two scalars passed as arguments for the inner() function.

Approach:

• Import numpy module using the import keyword.
• Pass two random numbers as the arguments to the inner () function of numpy module to calculate the inner product of the given two numbers.
• Store it in a variable.
• Print the inner product of the given two numbers.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass two random numbers as the arguments to the inner() function
# of numpy module to calculate the inner product of given two numbers
# Store it in a variable.
rslt = np.inner(4, 6)
# Print the inner product of the given two numbers
print("The inner product of given two numbers = ", rslt)

Output:

The inner product of given two numbers = 24

Example2

Approach:

• Import numpy module using the import keyword.
• Give the first array as static input and store it in a variable.
• Give the second array as static input and store it in another variable.
• Pass the given two array’s as an argument to the inner() function of numpy module to get the inner product of the given two arrays.
• Store it in another variable.
• Print the inner product of the given two arrays.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the first array as static input and store it in a variable.
fst_arry = [3, 9, 2]
# Give the second array as static input and store it in another variable.
scnd_arry = [4, 3, 5]
# Pass the given two array's as an argument to the inner() function of numpy module
# to get the inner product of the given two arrays.
# Store it in another variable.
rslt = np.inner(fst_arry, scnd_arry)
# Print the inner product of the given two arrays.
print("The inner product of the given two arrays = ", rslt)

Output:

The inner product of the given two arrays = 49

Example3

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass the random list of complex numbers as an argument to the array() function
# to create an array.
# Store it in a variable.
fst_arry = np.array([1+4j, 1+3j])
# Pass the random list of complex numbers as an argument to the array() function
# to create another array.
# Store it in another variable.
scnd_arry = np.array([2+1j, 3+2j])
# Pass the given two array's as the argument to the inner() function of numpy module
# to get the inner product of the given two arrays of complex numbers.
# Store it in another variable.
rslt = np.inner(fst_arry, scnd_arry)
# Print the inner product of the given two arrays of complex numbers.
print("The inner product of the given two arrays of complex numbers = ")
print(rslt)

Output:

The inner product of the given two arrays of complex numbers =
(-5+20j)

Example4

The function does inner product on the matrix’s last axes when two matrices are used.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass the random list(2D) as an argument to the array() function
# to create an array.
# Store it in a variable.
fst_arry = np.array([[5, 6],
[1, 7]])
# Pass the random list(2D) as an argument to the array() function
# to create another array.
# Store it in another variable.
scnd_arry = np.array([[2, 4],
[8, 7]])
# Pass the given two array's as the argument to the inner() function of numpy module
# to get the inner product of the given two 2D arrays
# Here it does the inner product on last axes of the matrix.
# Store it in another variable.
rslt = np.inner(fst_arry, scnd_arry)
# Print the inner product of given two arrays(matrices)
print("The inner product of given two arrays(matrices) = ")
print(rslt)

Output:

The inner product of given two arrays(matrices) =
[[34 82]
[30 57]]