Vectors are the physical quantities possessing both magnitude and direction in the direction of movement of the object. They are predominantly used for navigating around different spaces & planes in the field of mathematics. While there are umpteen operations that can be carried out with the vectors, in this article, we will be exploring one such operation using an in-built function within the numpy library – the vector dot product!
Also read: Numpy dot() – A Complete Guide to Vectors, Numpy, And Calculating Dot Products
The function that is to be used from the numpy library for calculating the vector dot product is the vdot( ) function. Let’s start things by importing the numpy library using the below code.
import numpy as np
We shall further explore the vdot( ) function through each of the following sections.
- Syntax of vdot( ) function
- Calculating Vector Dot Product for N-Dimensional Arrays
- Calculating Vector Dot Product for Complex Numbers
Syntax of vdot( ) function
One can wonder why bother with the vdot( ) function when there is already a dot( ) function within the numpy library that serves the same purpose. Though these may seem synonymous initially, the devil is in the details.
The vdot( ) function deploys the complex conjugate technique if the inputs provided are complex in nature. The function utilizes the complex conjugate of the first input parameter to calculate the vector dot product of the given two vectors.
But the real difference kicks in when the N-dimensional arrays come into the picture. While the dot( ) function uses the matrix multiplication technique for calculating the dot product of the N-dimensional arrays, the vdot( ) function flattens the given N-dimensional arrays into their one-dimensional equivalents to calculate the dot product.
All this happens within the vdot( ) function whose syntax is as follows,
- a – n-dimensional array or complex number for the first input vector
- b – n-dimensional array or complex number for the second input vector
Calculating Vector Dot Product for N-Dimensional Arrays
After importing the numpy library let us find the dot product of two vectors using a couple of two-dimensional arrays as shown below.
ar1 = np.array([[12, 5], [5, 9]], dtype = int) ar2 = np.array([[21, 50], [8, 6]], dtype = int) np.vdot(ar1, ar2)
Once the above code is run, the following computation happens in the back end for calculating the dot product of the given two vectors.
- First element of the first input array gets multiplied with the first element of the second input array, such as ‘12×21’.
- The above step is repeated till each element in the first input array is multiplied with their corresponding elements in the second input array, such as ‘5×50’, ‘5×8’, ‘9×6’.
- The results of all these products are then added to print the vector dot product of the given two N- dimensional arrays viz. (12×21)+(5×50)+(5×8)+(9×6) = 596
The final answer deduced in the last step as stated above can also be seen as the result in the below image when the code is run.
Calculating Vector Dot Product for Complex Numbers
This section shall elaborate on the usage of complex numbers with the vdot( ) function. Let us assign a couple of variables & then use them for calculating the vector dot product as shown in the below code.
ar3 = [[2+21j, 3-16j]] ar4 = [[6-17j, 18+6j]] np.vdot(ar3, ar4)
The same technique stated in the syntax section is followed here multiplying the conjugate of the complex numbers to deduce the final result.
Now that we have reached the end of this article, hope it has elaborated on how to use the vdot( ) function from the numpy library to calculate the dot product of the given two vectors. Here’s another article that explains how to find the outer product of the given vectors using numpy in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, hasta luego!