Python Program to Calculate the Dot Product?

Dot Product:

The dot product, commonly known as the scalar product in mathematics, is an algebraic operation that takes two equal-length sequences of numbers and produces a single number.

Given two vectors A and B, we must find the dot product of the two vectors.

For Example :

Let A = a1i+a2j+a3k

B= b1i+b2j+b3k

Where

i is the unit vector along the x-axis.

j is the unit vector along the y-axis.

k is the unit vector along the z-axis.

The Formula to calculate the dot product

Dot product = a1*b1+a2*b2+a3*b3

Calculating Dot Product of Two Vectors in Python

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

numpy.dot() is a method that accepts two sequences as arguments, whether they are vectors or multidimensional arrays, and displays the output, which is the dot product. To use this approach, we must first import Python’s numpy module.

Syntax:

numpy.dot(vector_a, vector_b, out = None )

Parameters

vector_a: It is an array. If a is complex, its complex conjugate is utilized to compute the dot product.

vector_b: It is an array. If b is complex, its complex conjugate is utilized to compute the dot product.

out: It is an array. This is optional. The output parameter must be C-contiguous, and its datatype must be the same as the datatype returned by dot (a,b).

Return Value:

Returns the Dot Product of given vectors a and b. Scalar is returned if vector a and vector b are both 1D.

How to find the Dot Product of Scalars?

Given two scalar values and use numpy.dot() function to display their dot product.

Approach:

  • Import numpy library using the import keyword.
  • Give the first scalar value as static input and store it in a variable.
  • Give the second scalar value as static input and store it in another variable.
  • Calculate the dot product of the given two scalar values using the numpy.dot() function by passing the given first and second scalar values as the arguments.
  • Store it in another variable.
  • Print the dot product of the given two scalar values.
  • The Exit of the Program.

Below is the implementation:

# Import numpy library using the import keyword
import numpy as np
# Give the first scalar value as static input and store it in a variable.
x = 4
# Give the second scalar value as static input and store it in another variable.
y = 6
# Calculate the dot product of the given two scalar values using the numpy.dot()
# function by passing the given first and second scalar values as the arguments.
# Store it in another variable.
dot_prdct = np.dot(x, y)
# Print the dot product of the given two scalar values.
print("The dot product of given scalars x{", x, "} and y {", y, "} = ")
print(dot_prdct)

Output:

The dot product of given scalars x{ 4 } and y { 6 } = 
24

How to find the Dot Product of Arrays?

Given two arrays, and the task is to calculate the dot product of the given two vectors.

These arrays might be one-dimensional, two-dimensional, or multi-dimensional. And we’ll compute their dot product with the help of dot(). For the dot product, we are evaluating two 2-D arrays.

Matrix multiplication is used to compute the dot product for 2-D arrays.

Approach:

  • Import numpy library using the import keyword
  • Give the 2- Dimensional array as static input and store it in a variable.
  • Give the other 2- Dimensional array as static input and store it in another variable.
  • Calculate the dot product of the given two arrays using the numpy.dot() function by passing the given two 2-D arrays as the arguments.
  • Store it in another variable.
  • Print the dot product of the given two 2- Dimensional arrays.
  • The Exit of the Program.

Below is the implementation:

# Import numpy library using the import keyword
import numpy as np
# Give the 2- Dimensional array as static input and store it in a variable.
x = [[3, 1], [5, 2]]
# Give the other 2- Dimensional array as static input and store it in another
# variable.
y = [[8, 5], [2, 4]]
# Calculate the dot product of the given two arrays using the numpy.dot()
# function by passing the given two 2-D arrays as the arguments.
# Store it in another variable.
dot_prdct = np.dot(x, y)
# Print the dot product of the given two 2- Dimensional arrays
print("The dot product of given two 2-D arrays x ", x, " and y ", y, " = ")
print(dot_prdct)

Output:

The dot product of given two 2-D arrays x [[3, 1], [5, 2]] and y [[8, 5], [2, 4]] = 
[[26 19]
[44 33]]

Note:

The dot product is not commutative for two-dimensional or multi-dimensional arrays. That means a.b does not equal b.a.

If we calculated dot product as a.b rather than b.a, the output is not the same for both of them. It is different for both.