Python NumPy vdot() Function

NumPy vdot() Function:

The vdot() function of the NumPy module returns the dot product of two vectors.

The vdot(a, b) function deals with complex numbers differently than the dot(a, b).

If the first parameter is complex, the dot product is calculated using the complex conjugate of the first parameter.

For multidimensional arrays, the function does not perform a matrix product. It performs the dot product after flattening input parameters to 1-D vectors.

Syntax:

numpy.vdot(a, b)

Parameters

a: This is required. It is an array (array-like) given as input. If “a” is a complex number, the complex conjugate is used before the dot product is computed.

b: This is required. It is the second array (array-like) given as input.

NumPy vdot() Function in Python

Example1

Here we used two scalars with the vdot() function of numpy module

Approach:

  • Import numpy module using the import keyword.
  • Pass two random numbers as the arguments to the vdot() function of numpy module to get the dot product of the given two numbers.
  • Store it in a variable.
  • Print the dot product of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass two random numbers as the arguments to the vdot() function
# of numpy module to get the dot product of given two numbers
# Store it in a variable.
rslt = np.vdot(3, 6)
# Print the dot product of the given two numbers 
print("The dot product of given two numbers = ", rslt)

Output:

The dot product of given two numbers = 18

Example2: For 1D Arrays

The vdot() function of the numpy module returns the inner product of two arrays when two 1-D arrays are given.

Approach:

  • Import numpy module using the import keyword.
  • Give the first array as static input and store it in a variable.
  • Give the second array as static input and store it in another variable.
  • Pass the given two array’s as an argument to the vdot() function of numpy module to get the inner product of the given two arrays.
  • Store it in another variable.
  • Print the inner product of the given two arrays.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the first array as static input and store it in a variable.
fst_arry = [3, 8, 2]
# Give the second array as static input and store it in another variable.
scnd_arry = [6, 4, 5]
# Pass the given two array's as an argument to the vdot() function of numpy module 
# to get the inner product of the given two arrays.
# Store it in another variable.
rslt = np.vdot(fst_arry, scnd_arry)
# Print the inner product of the given two arrays.
print("The inner product of the given two arrays = ", rslt)

Output:

The inner product of the given two arrays = 60

Example3

When the first parameter is complex, the function calculates the dot product using the conjugate of the first argument.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass the random list of complex numbers as an argument to the array() function 
# to create an array. 
# Store it in a variable.
fst_arry = np.array([1+4j, 1+3j])
# Pass the random list of complex numbers as an argument to the array() function 
# to create another array. 
# Store it in another variable.
scnd_arry = np.array([2+1j, 3+2j])
# Pass the given two array's as the argument to the vdot() function of numpy module 
# to get the dot product of the given two arrays of complex numbers.
# Store it in another variable.
rslt = np.vdot(fst_arry, scnd_arry)
# Print the dot product of the given two arrays of complex numbers.
print("The dot product of the given two arrays of complex numbers = ")
print(rslt)

Output:

The dot product of the given two arrays of complex numbers = 
(15-14j)

Example4

When two matrices are used, the function flattens the matrices to a one-dimensional array before performing the inner product.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass the random list(2D) as an argument to the array() function 
# to create an array. 
# Store it in a variable.
fst_arry = np.array([[5, 6], 
                     [1, 7]])
# Pass the random list(2D) as an argument to the array() function 
# to create another array. 
# Store it in another variable.
scnd_arry = np.array([[2, 4], 
                      [8, 7]])
# Pass the given two array's as the argument to the vdot() function of numpy module 
# to get the inner product of the given two 2D arrays 
# Here it flattens the matrices to a 1D array before performing the inner product.
# Store it in another variable.
rslt = np.vdot(fst_arry, scnd_arry)
# Print the inner product of given two arrays(matrices)
print("The inner product of given two arrays(matrices) = ")
print(rslt)

Output:

The inner product of given two arrays(matrices) = 
91