How to Calculate Dot Product in Python?

How To Calculate Dot Product In Python

Hello Learner! In this article, we will see the python code to find the dot product of any given quantities, say vectors or arrays. Python programming language provides several ways to do this, some of them are discussed below.

Also read: Vectorization in Python – A Complete Guide

What is a Dot Product?

First, let’s understand “dot product.”

In mathematics, the Dot product (sometimes known as scalar product) is an algebraic operation that returns a single value from two equal-length sequences of numbers.

This single value is calculated as the sum of the products of the corresponding elements from both sequences. These sequences might be single-dimensional vectors, multi-dimensional vectors, or simply numbers.

Let’s take an example to understand this:

Suppose, two vectors A and B are 2-D arrays as –

A = [ [1 2 ] [3 4] ] and B = [ [5 6] [7 8] ]

Then, A.B is given as

[ [ 19 22] [ 43 50] ]

This is calculated as [ [ ((1*5)+(2*7)) ((1*6)+(2*8)) ] [ ((3*5)+(4*7)) ((3*6)+(4*8)) ] ]

Python code to find the dot product

Python provides an efficient way to find the dot product of two sequences which is numpy.dot() method of numpy library.

Numpy.dot() is a method that takes the two sequences as arguments, whether it be vectors or multidimensional arrays, and prints the result i.e., dot product. To use this method, we must import the numpy library of python. Let’s look at few examples :

Example 1: Dot product of scalars

In this example, we will take two scalar values, and print their dot product using numpy.dot().

The dot product of two scalars is obtained by simply multiplying them.

Say, Two scalars A = 7 and B = 6, then A.B = 42

#importing numpy library
import numpy as np

#Taking two scalars
a = 3
b = 8

#calculating dot product using dot()
print("The dot product of given scalars = a.b =",np.dot(a,b))

The output for the above code is :

The dot product of given scalars = a.b = 24

Example 2: Dot product of arrays

Here, we will be taking two arrays. These arrays can be 1-D, 2-D or multi-dimensional. And with the help of dot(), we will calculate their dot product. We are considering two 2-D arrays for the dot product.

The dot product for 2-D arrays is calculated by doing matrix multiplication.

#importing numpy library
import numpy as np

#Taking two 2-D arrays
a = [ [1, 2], [3, 4]]
b = [ [7, 6], [5, 4]]

#calculating dot product using dot()
print("The dot product of given arrays :")
np.dot(a,b))

The output is :

The dot product of given arrays :

array( [ [17, 14],
            [41, 34] ] )

NOTE:

For two-dimensional or multi-dimensional arrays, the dot product is not commutative. i.e., a.b is not equal to b.a In example 2, we have calculated dot product as a.b, and not b.a. This will give a completely different result.

Conclusion

So, Isn’t it simple to calculate dot products in Python? With the functions available, of course, it is. This was it from my side. I hope you understood this article. For more such articles, Stay tuned to https://www.askpython.com/

Till then, happy learning! 🙂