NumPy hypot – A Complete Guide


Hello readers! Welcome to another tutorial of the series NumPy Mathematical Functions. In this tutorial, we will understand the hypot function of the NumPy Library.

We all must have calculated the value of the hypotenuse of a right-angled triangle in Mathematics. We used to apply the famous Pythagoras Theorem to find out the value of the hypotenuse or any other two sides i.e. perpendicular and the base.

This tutorial will explain how to use NumPy hypot function with many examples. So, let’s get started.

Also check: NumPy Arctan – A Complete Guide

Hypotenuse – A Quick Overview

  • A right-angled triangle has one of the angles equal to 90 degrees i.e. two sides are perpendicular to each other. In a right-angled triangle, the side opposite to the 90 degree angle is called the Hypotenuse of the triangle.
  • The value of the hypotenuse side in a right-angled triangle is the largest among all the sides.

Refer to the following image for a better understanding.


What is NumPy hypot?

  • NumPy provides a lot of Mathematical functions and numpy.hypot() is one of them. Here, hypot stands for the hypotenuse of a right-angled. triangle.
  • numpy.hypot() computes the value of the hypotenuse in a right-angled triangle given the values of the other two sides.

Syntax of NumPy hypot

numpy.hypot(x1, x2)

where x1 and x2 are the values of the two sides of a right-angled triangle.

This function computes the value of the hypotenuse side according to the Pythagoras Theorem. So, numpy.hypot(x1, x2) will be calculated as sqrt(x12+x22) where sqrt is the square root.

Note: x1 and x2 cannot be negative.

Working with NumPy hypot Function

Let’s now look at some practical examples to work with the numpy hypot function.

Calculating the hypotenuse of two lists of values

# Importing the numpy library
import numpy as np

x1 = [12 , 4 , 5 , 6]

x2 = [5 , 6 , 7 , 14]

a = np.hypot(x1 , x2)

print("Hypotenuse Values:\n",a)


Hypotenuse Values:
 [13.          7.21110255  8.60232527 15.23154621]

Calculating hypotenuse using an array of values

import numpy as np

a = np.array((4 , 5))

b = np.array((3 , 12))

c = np.hypot(a , b)

print("Hypotenuse Values :\n",c)


Hypotenuse Values :
 [ 5. 13.]

NumPy hypot Function with 2-D Array

Moving further, we’ll now calculate the hypotenuse using a 2D array.

Calculating hypotenuse of 2D lists

import numpy as np

# Creating two 3x3 arrays
x = [[1,2,3] , [4,5,6] , [7,8,9]]

y = [[10,11,12] , [13,14,15] , [16,17,18]]

z = np.hypot(x,y)

print("Hypotenuse Values:\n",z)


Hypotenuse Values:
 [[10.04987562 11.18033989 12.36931688]
 [13.60147051 14.86606875 16.15549442]
 [17.4642492  18.78829423 20.1246118 ]]

Calculating hypotenuse using the np.ones() data

import numpy as np

#Initializing a 3x3 NumPy Array with all the elements equal to 3
m = 3*np.ones((3,3))

#Initializing a 3x3 NumPy Array with all the elements equal to 4
n = 4*np.ones((3,3))

p = np.hypot(m , n)

print("Hypotenuse Values:\n",p)


Hypotenuse Values:
 [[5. 5. 5.]
 [5. 5. 5.]
 [5. 5. 5.]]

np.ones() function creates a NumPy array with all the elements equal to 1 and in the above code snippet, we have created a variable m which contains a 3×3 (2-D array) with all the elements equal to 3 and another variable n also contains a 3×3 (2-D array) with all the elements equal to 4.

The next step is simple where m and n are passed as an argument to the np.hypot() function and the Hypotenuse values are calculated for each pair of elements from both arrays.

Also read: NumPy Arcsin- A Complete Guide


So, that was all about the NumPy hypot function, it’s a pretty simple and straightforward function. Stay tuned and keep learning 🙂


NumPy Documentation – NumPy hypot