dot product and cross product in Python

In this Python tutorial, we will discuss, dot product and cross product in Python. Also, we will discuss, dot product in Python without numpy and cross product in Python without numpy.

Dot Product in Python

The dot product in Python, also known as the scalar product, is an algebraic operation that takes two equal-length sequences of numbers (usually coordinate vectors) and returns a single number. This operation can be used in many different contexts, such as computing the projection of one vector onto another or determining the angle between two vectors.

In Python, the dot product can be computed in several ways. If you are working with lists or arrays of numbers, you can use a loop or a list comprehension to multiply the corresponding elements and sum the results. Here is an example of this approach:

a = [1, 2, 3]
b = [4, 5, 6]
dot_product = sum(i*j for i, j in zip(a, b))
print(dot_product)  # Outputs: 32

In this example, zip(a, b) pairs up the corresponding elements of a and b, the expression i*j for i, j in zip(a, b) multiplies each pair of elements, and sum() adds up these products to compute the dot product.

If you are working with vectors in a mathematical or scientific context, you might prefer to use the NumPy library, which provides a dot function to compute the dot product of two arrays:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
dot_product = np.dot(a, b)
print(dot_product)  # Outputs: 32

In this example, np.array() creates NumPy arrays from the lists of numbers, and np.dot(a, b) computes the dot product.

READ:  Python program to print pattern (10 Examples)

Dot Product in Python without Numpy

The dot product, also known as the scalar product, of two vectors a and b is defined as the sum of the products of their corresponding components. If a = [a1, a2, ..., an] and b = [b1, b2, ..., bn], the dot product is a1*b1 + a2*b2 + ... + an*bn.

In Python, this can be implemented without using NumPy by using a loop:

a = [1, 2, 3]
b = [4, 5, 6]

dot_product = sum(a[i] * b[i] for i in range(len(a)))

print(f"Dot product of {a} and {b} is {dot_product}")

You can see the output like below screenshot.

Dot Product in Python without Numpy
Dot Product in Python without Numpy

Dot Product of Two Lists in Python

Dot product of two lists in Python can be done using the zip function, which makes the code even cleaner:

a = [1, 2, 3]
b = [4, 5, 6]

dot_product = sum(x * y for x, y in zip(a, b))

print(f"Dot product of {a} and {b} is {dot_product}")

Dot Product of Two Vectors in Python

If you are working with more advanced mathematical vectors, you can use the numpy library. Numpy is a powerful library for numerical computing in Python.

import numpy as np

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

dot_product = np.dot(a, b)

print(f"Dot product of {a} and {b} is {dot_product}")

Dot Product of Two Vectors in Python without Numpy

You can compute the dot product of two vectors without using numpy. This is essentially the same as the dot product of two lists, since vectors can be represented as lists:

a = [1, 2, 3]
b = [4, 5, 6]

dot_product = sum(x * y for x, y in zip(a, b))

print(f"Dot product of {a} and {b} is {dot_product}")

Cross Product in Python

The cross product of two vectors a and b is a vector that is perpendicular to both a and b. The cross product can only be calculated for 3-dimensional vectors. If a = [a1, a2, a3] and b = [b1, b2, b3], the cross product c is [a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1].

Using numpy, this can be easily done:

import numpy as np

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

cross_product = np.cross(a, b)

print(f"Cross product of {a} and {b} is {cross_product}")

Cross Product in Python without Numpy

Without using numpy, you can calculate the cross product using the below formula in Python:

a = [1, 2, 3]
b = [4, 5, 6]

cross_product = [
    a[1] * b[2] - a[2] * b[1],
    a[2] * b[0] - a[0] * b[2],
    a[0] * b[1] - a[1] * b[0]
]

print(f"Cross product of {a} and {b} is {cross_product}")

In this Python tutorial, we learned, dot products and cross products in Python.

READ:  Xlsxwriter Read Excel

You may also like: