Free cookie consent management tool by TermsFeed Finding dot product of two vectors using vdot() function of NumPy | Pythontic.com

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

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:

# 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:

Array1:

[[ 5 10 15]

 [20 25 30]

 [35 40 45]]

Array2:

[[ 2  4  6]

 [ 8 10 12]

 [14 16 18]]

Dot product:

2850

 


Copyright 2025 © pythontic.com