NumPy Tutorial NumPy Statistics NumPy References

NumPy - vdot() function



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

The vdot(a, b) function handles complex numbers differently than dot(a, b). If the first argument is complex the complex conjugate of the first argument is used for the calculation of the dot product.

The function does not perform a matrix product for multidimensional arrays. It flattens input arguments to 1-D vectors first and perform the dot product.

Syntax

numpy.vdot(a, b)

Parameters

a Required. Specify first array-like argument. If a is complex the complex conjugate is taken before calculation of the dot product.
b Required. Specify second array-like argument.

Return Value

Returns the dot product of a and b.

Example: vdot() function with scalars

The example below shows the result when two scalars are used with vdot() function.

import numpy as np
print(np.vdot(5, 10))

The output of the above code will be:

50

Example: vdot() function with 1-D arrays

When two 1-D arrays are used, the function returns inner product of the arrays.

import numpy as np
Arr1 = [5, 8]
Arr2 = [10, 20]

#returns 5*10 + 8*20 = 210
print(np.vdot(Arr1, Arr2))

The output of the above code will be:

210

Example: vdot() function with complex numbers

When the first argument is complex the function takes the conjugate of the first argument for calculating the dot product. Consider the example below.

import numpy as np
Arr1 = np.array([1+2j, 1+3j])
Arr2 = np.array([2+2j, 2+3j])

Arr3 = np.vdot(Arr1, Arr2)

print(Arr3)

The output of the above code will be:

(17-5j)

The vdot product is calculated as:

conjugate of Arr1 = [1-2j, 1-3j]

= (1-2j)*(2+2j) + (1-3j)*(2+3j)
= (6-2j) + (11-3j)
= (17-5j)

Example: vdot() function with matrix

When two matrix are used, the function first flattens the matrix to 1-D array then performs inner product.

import numpy as np
Arr1 = np.array([[1, 2], 
                 [3, 4]])
Arr2 = np.array([[10, 20], 
                 [30, 40]])
Arr3 = np.vdot(Arr1, Arr2)

print(Arr3)

The output of the above code will be:

300

The vdot product is calculated as:

#arrays after flattening
Arr1 = [1, 2, 3, 4]
Arr2 = [10, 20, 30, 40]

#vdot is calculated as
= 1*10 + 2*20 + 3*30 + 4*40
= 10 + 40 + 90 + 160 
= 300

❮ NumPy - Functions