NumPy - inner() function
The NumPy inner() function returns the inner product of two arrays. For 1-D arrays, it returns ordinary inner product (without complex conjugation). For higher dimensions a sum product over the last axes is returned.
Syntax
numpy.inner(a, b)
Parameters
a |
Required. Specify first array-like argument. If a and b are nonscalar, their last dimensions must match. |
b |
Required. Specify second array-like argument. |
Return Value
Returns the inner product of a and b.
Exception
Raises ValueError exception, if the last dimension of a and b has different size.
Example: inner() function with scalars
The example below shows the result when two scalars are used with inner() function.
import numpy as np print(np.inner(5, 10))
The output of the above code will be:
50
Example: inner() function with 1-D arrays
When two 1-D arrays are used, the function returns inner product of the arrays.
import numpy as np Arr1 = [5, 8] Arr2 = [10, 20] #returns 5*10 + 8*20 = 210 print(np.inner(Arr1, Arr2))
The output of the above code will be:
210
Example: inner() function with complex numbers
The inner() function can be used with complex numbers. Consider the following example.
import numpy as np Arr1 = np.array([1+2j, 1+3j]) Arr2 = np.array([2+2j, 2+3j]) Arr3 = np.inner(Arr1, Arr2) print(Arr3)
The output of the above code will be:
(-9+15j)
The inner product is calculated as:
= (1+2j)*(2+2j) + (1+3j)*(2+3j) = (-2+6j) + (-7+9j) = (-9+15j)
Example: inner() function with matrix
When two matrix are used, the function performs inner product on last axes of the matrix.
import numpy as np Arr1 = np.array([[1, 2], [3, 4]]) Arr2 = np.array([[10, 20], [30, 40]]) Arr3 = np.inner(Arr1, Arr2) print(Arr3)
The output of the above code will be:
[[ 50 110] [110 250]]
The inner product is calculated as:
[[1*10+2*20 1*30+2*40] [3*10+4*20 3*30+4*40]] = [[ 50 110] [110 250]]
❮ NumPy - Functions