Compute the inner product of vectors for-D arrays using NumPy in Python


Inner product is one of the most important operations in the linear algebra mathematical operations, which takes two vectors as input and gives the scalar value as the output. It is also known as the Dot product or the scalar product. The inner product of the two vectors is given as follows.

a . b = ||a|| ||b|| cos(Ø)

Where,

  • ||a|| and ||b|| are the magnitudes of the vectors a and b respectively

  • Ø is the angle between the vectors a and b

  • a . b is the dot product of a and b

Calculating Inner product

If we want to calculate the inner product or dot product of the arrays, it is given as the sum of the products of the respective elements of the arrays. Let’s take the two arrays a and b as follows.

a = [a1, a2, a3]
b = [b1, b2, b3]

Following is the mathematical expression of the arrays for calculating inner product.

a . b = a1 * b1 + a2 * b2 + a3 * b3

Calculating inner product using Numpy

We can calculate the dot product of the arrays using dot() in the Numpy library.

Syntax

Following is the syntax for calculating the inner product of two array elements using dot() function.

np.dot(arr1, arr2)

Where,

  • Numpy is the name of the library

  • np is the alias name of the library

  • dot is the function to find the inner product

  • arr1 and arr2 are the input arrays

Example

In this example, when we give two 1-d arrays as input arguments to the dot() function, then the scalar product or inner product will be returned as the output.

import numpy as np
a = np.array([12,30,45])
b = np.array([23,89,50])
inner_product = np.dot(a,b)
print("The Inner product of the two 1-d arrays:", inner_product)

Output

The Inner product of the two 1-d arrays: 5196

Example

Following is an example to work with the dot() function for calculating the inner product of the 1-d arrays.

import numpy as np
a = np.array([34,23,98,79,90,34,23,67])
b = np.array([22,1,95,14,91,5,24,12])
inner_product = np.dot(a,b)
print("The Inner product of the two 2-d arrays:",inner_product)

Output

The Inner product of the two 2-d arrays: 20903

Example

The dot() function only accepts the square arrays as its arguments. If we try to pass values other than square arrays it will raise an error.

import numpy as np
a = np.array([[34,23,98,79],[90,34,23,67]])
b = np.array([[22,1,95,14],[91,5,24,12]])
inner_product = np.dot(a,b)
print("The Inner product of the two 2-d arrays:",inner_product)

Error

Traceback (most recent call last):
  File "/home/cg/root/64d07b786d983/main.py", line 4, in <module>
inner_product = np.dot(a,b)
  File "<__array_function__ internals>", line 200, in dot
ValueError: shapes (2,4) and (2,4) not aligned: 4 (dim 1) != 2 (dim 0)

Example

In the following example we are trying to calculate the inner product of the 2-d arrays, using the dot() function.

import numpy as np
a = np.array([[34,23],[90,34]])
b = np.array([[22,1],[91,5]])
inner_product = np.dot(a,b)
print("The Inner product of the two 2-d arrays:", inner_product)

Output

The Inner product of the two 2-d arrays: [[2841  149][5074  260]]

Example

Now let’s try calculating the inner product of the vectors by passing 3-d array as the arguments to the dot() function.

import numpy as np
a = np.array([[[34,23],[90,34]],[[43,23],[10,34]]])
b = np.array([[[22,1],[91,5]],[[22,1],[91,5]]])
inner_product = np.dot(a,b)
print("The Inner product of the two 3-d arrays:", inner_product)

Output

The Inner product of the two 3-d arrays: [[[[2841  149]
   [2841  149]]

  [[5074  260]
   [5074  260]]]


 [[[3039  158]
   [3039  158]]

  [[3314  180]
   [3314  180]]]]

Updated on: 07-Aug-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements