# Return the dot product of two multidimensional vectors in Python

PythonNumpyServer Side ProgrammingProgramming

To return the dot product of two multi-dimensional vectors, use the numpy.vdot() method in Python. 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 vdot handles multidimensional arrays differently than dot: it does not perform a matrix product, but flattens input arguments to 1-D vectors first. Consequently, it should only be used for vectors.

The method returns the dot product of a and b. Can be an int, float, or complex depending on the types of a and b. The 1st parameter is a. If a is complex the complex conjugate is taken before calculation of the dot product. The b is the 2nd parameter to the dot product.

## Steps

At first, import the required libraries −

import numpy as np

Creating two numpy Multi-Dimensional array using the array() method −

arr1 = np.array([[5, 10],[15, 20]])
arr2 = np.array([[3, 6],[9, 12]])

Display the arrays −

print("Array1...\n",arr1)
print("\nArray2...\n",arr2)

Check the Dimensions of both the arrays −

print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)

Check the Shape of both the arrays −

print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)

To return the dot product of two multi-dimensional vectors, use the numpy.vdot() method in Python −

print("\nResult...\n",np.vdot(arr1, arr2))


## Example

import numpy as np

# Creating two numpy Multi-Dimensional array using the array() method
arr1 = np.array([[5, 10],[15, 20]])
arr2 = np.array([[3, 6],[9, 12]])

# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)

# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)

# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)

# To return the dot product of two multi-dimensional vectors, use the numpy.vdot() method in Python.
print("\nResult...\n",np.vdot(arr1, arr2))

## Output

Array1...
[[ 5 10]
[15 20]]

Array2...
[[ 3 6]
[ 9 12]]

Dimensions of Array1...
2

Dimensions of Array2...
2

Shape of Array1...
(2, 2)

Shape of Array2...
(2, 2)

Result...
450
Updated on 01-Mar-2022 08:05:57