Development

Python math.hypot() Method

Captain Salem 2 min read

Python math.hypot() Method

The math.hypot() method is a built-in Python method that allows us to calculate the Euclidean norm, sqrt(sum(x**2 for x in inputs)). In simple terms, the method allows us to calculate the length of the vector from the origin to a point in N-dimensional space.

In this tutorial, we will explore how we can use this function in Python to perform various operations using various N-dimensional vectors.

Import the Math Module

Before using math.hypot(), ensure you have the math module imported in your project. You can use the import keyword as shown:

import math

Python hypot() Method

Syntax

The syntax of the hypot() function is expressed below:

math.hypot(*coordinates)
  • The *coordinates parameter is a variable-length argument, allowing for any number of numerical input values. These represent coordinates in N-dimensional space.

Function Return Value

The function returns a float value representing the Euclidean norm of the given coordinates.

Example 1 - Two-dimensional space (Euclidean distance)

import math
# coordinates in 2D space
x = 3
y = 4

# calculate Euclidean norm (hypotenuse in right angle triangle)
result = math.hypot(x, y)

print(result)

Output:

5.0

In the example above, we pass two arguments to the math.hypot() function. These arguments are interpreted as coordinates in a two-dimensional space.

The function thus calculates the Euclidean distance from the origin (0, 0) to the point (3, 4), which is equivalent to finding the hypotenuse in a right-angle triangle with other sides of lengths 3 and 4.

Example 2 - Three-dimensional space

import math

# coordinates in 3D space
x = 3
y = 4
z = 5

# calculate Euclidean norm 
result = math.hypot(x, y, z)

print(result)

Output:

7.0710678118654755

In this case, we pass three arguments to the math.hypot() function. The function will then interpret the arguments as coordinates in 3-dimensional space.

It then calculates the Euclidean distance from the origin (0, 0, 0) to the point (3, 4, 5).

Example 3 - N-dimensional space

import math

# coordinates in N-dimensional space
coordinates = [3, 4, 5, 6, 7]

# calculate Euclidean norm 
result = math.hypot(*coordinates)

print(result)

Output:

11.61895003862225

In the example above, the function calculates the Euclidean distance in a space of arbitrary (N) dimensions.

NOTE: In this example, the function will interpret the provided parameter as coordinates of a 5-dimensional space. This means the function will return the Euclidean distance from the origin to the point specified in the coordinates.

Keep in mind that the math.hypot() function is not limited to only two or three arguments. It can accept any number of arguments and it will calculate the Euclidean norm (distance from the origin) in that N-dimensional space.

Error and Exception

The math.hypot() function will throw a TypeError if any of the arguments passed to it are not numeric (either integer or float).

import math

# non-numeric input
x = "3"
y = 4

# calculate Euclidean norm
result = math.hypot(x, y)

Output:

TypeError                                 Traceback (most recent call last)
...
      5 y = 4
      7 # calculate Euclidean norm
----> 8 result = math.hypot(x, y)

TypeError: must be real number, not str

Remember to always ensure that the arguments passed to the math.hypot() function are of a numeric type to avoid exceptions.

Conclusion

In this tutorial, we explored the fundamentals of working with the hypot() function in the Python’s math module to calculate the Euclidean distance between origin and the specified coordinates.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.