How to Calculate the Length or Magnitude of a Vector in Python

How to Calculate the Length or Magnitude of a Vector in Python

The magnitude or length of a vector is a measure of its size. This metric is commonly referred to as the Euclidean norm of a vector, but there are other norms, each suited to different applications.

Here is how you can calculate the Euclidean norm of a vector using Python: 

import numpy as np

# Define a vector
vector = np.array([3, 4])

# Calculate the norm (magnitude)
magnitude = np.linalg.norm(vector)

print("Magnitude (norm) of the vector:", magnitude)

The np.linalg.norm() function, when applied to a numpy array or vector as shown above, outputs the Euclidean norm: 

Magnitude (norm) of the vector: 5.0

The formula and notation used to calculate the norm of a vector is:

In this formula, you might notice two different types of notation for “v”: bold v and italic v. Here is what each represents:

  • Bold v: This represents the vector as a whole, an entity composed of multiple components. In mathematical notation, vectors are often denoted in bold to differentiate them from scalars (single numbers). The boldface vstands for the entire vector, encompassing all its components in a single, unified mathematical object.
  • Italic v: Each italicized v refers to an individual component of the vector v. For a three-dimensional vector, there would be three italicized v’s, each with a subscript denoting its dimension. 
  • The magnitude (or norm) of the vector v is calculated by taking the square root of the sum of the squares of its components (i.e. the italicized v’s). 

In two-dimensional space, the Euclidean norm is simply the hypotenuse of a right triangle whose legs are the vector’s components. As in our example above, for the vector [3,4], the hypotenuse — or the norm — is 5.

While the Euclidean norm is the most commonly used measure of vector magnitude, there are other norms that serve various purposes in different contexts:

  • Manhattan Norm (L1 Norm): This norm sums the absolute values of the vector’s components.
  • Infinity Norm (L∞ Norm): This norm captures the maximum absolute value among the components of the vector. 

In addition to the Euclidean norm, you can also calculate the Manhattan Norm (L1 Norm) and the Infinity Norm (L∞ Norm) using Python. Here’s how to do it for the same vector [3,4]:

import numpy as np

# Define the vector
vector = np.array([3, 4])

# Calculate the Manhattan Norm (L1 Norm)
l1_norm = np.linalg.norm(vector, 1)

print("Manhattan Norm (L1 Norm) of the vector:", l1_norm)

# Calculate the Infinity Norm (L∞ Norm)
linf_norm = np.linalg.norm(vector, np.inf)

print("Infinity Norm (L∞ Norm) of the vector:", linf_norm)

In the NumPy function np.linalg.norm(), you can specify different norms by changing the second parameter, known as the ord parameter:

  • ord=1: Calculates the Manhattan Norm (L1 Norm)
  • ord=np.inf: Calculates for the Infinity Norm (L∞ Norm)

This flexibility provided by NumPy lets you compute various norms efficiently with a single function, tailored to different mathematical needs or applications. The code snippet above will calculate and output:

Manhattan Norm (L1 Norm) of the vector: 7.0
Infinity Norm (L∞ Norm) of the vector: 4.0

The L1 norm is calculated by summing the absolute values of the components of the vector, providing a total of 7 for the vector [3,4]. The Infinity Norm, on the other hand, takes the maximum absolute value among the components, which is 4 for this vector.

Understanding these norms and how to calculate them in Python equips you with foundational tools for analyzing and manipulating vectors in numerous contexts. 

Leave a Reply

Your email address will not be published. Required fields are marked *