Finding dot product of two vectors using vdot() function of NumPy

The vdot() function of NumPy computes the dot product of two vectors. The Python example computes the dot product of two 2-dimensional arrays using the vdot() function.

Linear Algebra Updated 28 Feb 2025 1 min read

Overview

  • The vdot() function returns the dot product of two vectors.
  • Two input vectors are provided as 1 to n-dimensional arrays. The dot product of multi-dimensional arrays are computed after flattening them to one-dimensional arrays.

Example

python
# Example Python program that computes the dot

            # product of two vectors. In this example the

            # parameters to the vdot() function are two

            # dimensional arrays.

            import numpy

# Create the first 2-d array

            nums = numpy.arange(5, 50, 5)

            array1 = nums.reshape(3, 3)

            print(array1)

# Create the second 2-d array

            nums1 = numpy.arange(2, 20, 2)

            array2 = nums1.reshape(3, 3)

            print(array2)

# Compute the dot product

            result = numpy.vdot(array1, array2)

            print(result)

Output

Output
Array1:

[[ 5 10 15]

 [20 25 30]

 [35 40 45]]

Array2:

[[ 2  4  6]

 [ 8 10 12]

 [14 16 18]]

Dot product:

2850