.hypot()

Anonymous contributor's avatar
Anonymous contributor
Published Nov 16, 2024
Contribute to Docs

In Numpy, the .hypot() function returns the hypotenuse of a right triangle, given the legs.

Syntax

numpy.hypot(x1, x2, out=None, where=True)
  • x1, x2: These are the legs of the triangle(s). If the shapes of x1 and x2 are not identical, they must be broadcastable to a common shape.
  • out (Optional): This parameter specifies an array where the result will be stored. The shape of out must match the shape of the output. If not provided, a new array will be allocated for the result.
  • where (Optional): An optional condition that can be applied element-wise on the input arrays. The result will only be computed where the condition is True. If the condition is False for an element, that element’s result will be ignored.

Example 1

The below example calculates the hypotenuse of a right triangle:

import numpy as np
print("If the first leg is 3 and the second is 4, the hypotenuse is ", np.hypot(3, 4))

The code above generates the following output:

If the first leg is 3 and the second is 4, the hypotenuse is 5.0

Example 2

The following example calculates the hypotenuses of three right triangles:

import numpy as np
# Creating the sides of three triangles
triangle_leg1 = [3,5,6] # First leg of each triangle
triangle_leg2 = [4,12,8] # Second leg of each triangle
# Computing the hypotenuses
hypotenuses = np.hypot(array1, array2)
print("Calculated hypotenuses:", hypotenuses)
print("\nThe hypotenuses are {}, {} and {}, respectively.".format(hypotenuses[0], hypotenuses[1], hypotenuses[2]))

The code above generates the following output:

Calculated hypotenuses: [ 5. 13. 10.]
The hypotenuses are 5.0, 13.0 and 10.0, respectively.

Codebyte Example

In this codebyte, .hypot() computes the hypotenuses for two triangles. The first triangle has legs of 2 and 3, and the second triangle has legs of 8 and 15.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy