Python Program to Get dot product of multidimensional Vectors using NumPy
Last Updated :
05 Sep, 2024
Given two multidimensional Vectors, the task is to write a Python program to get the dot product of two multidimensional Vectors using NumPy.
Example: Lets take 2 vectors a = [2,5,3] and b = [6,3,1]
Dot Product(ab) = (a[0] * b[0])+ (a[1] * b[1]) + (a[2] * b[2]) = (2*6)+ (5*3) + (3*1) = 30
Dot Product is the sum of the product of elements at each position of the vector.
Dot Product of 1-D vectors:
Now let's implement this in python. But we don't need to code this from scratch, thanks to Numpy. Numpy module has a method dot which takes 2 vectors and returns the dot product of them
Python
# import numpy
import numpy as np
# initialize vectors
a = np.array([2,5,3])
b = np.array([6,3,1])
# calculating Dot product form np.dot
c = np.dot(a,b)
print("Dot product of a and b is: ",c)
Output:
Dot product of a and b is: 30
Dot Product of 2-Dimensional vectors:
The dot product of a 2-dimensional vector is simple matrix multiplication. In one dimensional vector, the length of each vector should be the same, but when it comes to a 2-dimensional vector we will have lengths in 2 directions namely rows and columns. Rows and columns of a 2-D vector not necessarily should be the same but the number of columns of the first vector should match the number of rows of the second vector.
Example:
Let's do the same thing with Python (Numpy module)
Python
# import numpy
import numpy as np
# initialize 2d vectors
a = np.array([[3, 9],
[2, 5]])
b = np.array([[6, 1],
[4, 8]])
# Checking the condition "No. of columns
# of first vector == No. of rows of the
# second vector"
if a.shape[1] == b.shape[0]:
# calculating Dot product using np.dot
c = np.dot(a, b)
print("Dot product of a and b is:\n", c)
else:
print("No. of columns of first vector should match\
with No. of rows of the second vector")
Output:
Dot product of a and b is:
[[54 75]
[32 42]]
Example:
In this example, with the same approach as shown in the above example, we are working with different data points and adding an if statement in case the code produces an error.
Python
# import numpy
import numpy as np
# initialize vectors
a = np.array([[3,9,6],
[2,5,2]])
b = np.array([[6,1],
[4,8],
[7,5]])
# Checking the condition "No. of columns
# of first vector == No. of rows of the
# second vector"
if a.shape[1] == b.shape[0]:
# calculating Dot product using np.dot
c = np.dot(a,b)
print("Dot product of a and b is:\n",c)
else:
print("No. of columns of first vector should match with \
No. of rows of the second vector")
Output:
Dot product of a and b is:
[[ 96 105]
[ 46 52]]
It will throw an error if the condition does not satisfy, let's remove our if block and calculate dot product with different shapes to see what is the error
Python
# import numpy
import numpy as np
# initialize vectors
a = np.array([[3,9],
[2,5]])
b = np.array([[6,1],
[4,8],
[7,5]])
print("Shape of vector a: ",a.shape)
print("Shape of vector b: ",b.shape)
# calculating Dot product using np.dot
c = np.dot(a,b)
print("Dot product of a and b is:\n",c)
Shape of vector a: (2, 2)
Shape of vector b: (3, 2)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-346715f1fc30> in <module>
12
13 # calculating Dot product using np.dot
---> 14 c = np.dot(a,b)
15 print("Dot product of a and b is:\n",c)
<__array_function__ internals> in dot(*args, **kwargs)
ValueError: shapes (2,2) and (3,2) not aligned: 2 (dim 1) != 3 (dim 0)
Similar Reads
Compute the inner product of vectors for 1-D arrays using NumPy in Python Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read
Compute the inner product of vectors for 1-D arrays using NumPy in Python Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read
How to compute the cross product of two given vectors using NumPy? Let's see the program to compute the cross product of two given vectors using NumPy. For finding the cross product of two given vectors we are using numpy.cross() function of NumPy library.Syntax: numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[Return: cross product of two (arrays of) vec
1 min read
Vector outer product with Einstein summation convention using NumPy in Python In this article, we will find vector outer product with Einstein summation convention in Python. numpy.einsum() method The numpy.einsum() method from the NumPy library is used to find the vector outer product with the Einstein summation convention in Python. Many common multi-dimensional, linear al
3 min read
Get the Outer Product of an array with vector of letters using NumPy in Python In this article let's see how to get the outer product of an array with a vector of letters in Python. numpy.outer() method The numpy.outer() method is used to get the outer product of an array with a vector of elements in Python. A matrix is the outer product of two coordinate vectors in linear alg
4 min read