OKPEDIA PYTHON VECTOR

How to calculate the scalar product in Python

To calculate the scalar product between two vectors in the Python language use the dot() function in the NumPy module.

numpy.dot(v1,v2)

The arguments v1 and v2 are two vectors (array).

The function returns the dot product v1·v2.

What is the dot product? The inner product is a zero number when the two vectors are orthogonal. Two vectors are orthogonal when they form a 90 ° angle.

Example

Import the numpy library.

import numpy as np

Define two vectors (array).

a = np.array([1,2,3], float)
b = np.array([4,5,6], float)

Use the dot() function to calculate the dot product.

np.dot(a,b)

The result is the dot product a·b

32

It is a non-zero number. Therefore the vectors are not orthogonal to each other.

Example 2

Define two vectors orthogonal to each other

a = np.array([2,0], float)
b = np.array([0,4], float)

Calculate the dot product

np.dot(a,b)

In this case the scalar product a · b is zero because the two vectors are orthogonal, that is, they form an angle of 90 °.

0.0

https://how.okpedia.org/en/python/how-to-calculate-the-scalar-product-in-python


Report an error or share a suggestion to enhance this page


Python


FacebookTwitterLinkedinLinkedin