Return the dot product of One-Dimensional vectors in Python

PythonNumpyServer Side ProgrammingProgramming

To return the dot product of One-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 One-Dimensional array using the array() method −

arr1 = np.array([2+3j,5+6j])
arr2 = np.array([9+10j,11+12j])

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 One-Dimensional vectors, use the numpy.vdot() method −

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

Example

import numpy as np

# Creating two numpy One-Dimensional array using the array() method
arr1 = np.array([2+3j,5+6j])
arr2 = np.array([9+10j,11+12j])

# 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 One-Dimensional vectors, use the numpy.vdot() method in Python.
print("\nResult...\n",np.vdot(arr1, arr2))

Output

Array1...
[2.+3.j 5.+6.j]

Array2...
[ 9.+10.j 11.+12.j]

Dimensions of Array1...
1

Dimensions of Array2...
1

Shape of Array1...
(2,)

Shape of Array2...
(2,)

Result...
(175-13j)
raja
Updated on 01-Mar-2022 08:07:16

Advertisements