NumPy hypot()
The numpy.hypot()
function computes the hypotenuse of a right triangle given its two perpendicular sides. It is equivalent to calculating sqrt(x1**2 + x2**2)
element-wise.
Syntax
</>
Copy
numpy.hypot(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1 , x2 | array_like | Legs of the right triangle(s). If x1 and x2 have different shapes, they must be broadcastable. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store results. If None , a new array is created. |
where | array_like, optional | Boolean mask specifying where to compute the hypotenuse. Elements where where=False retain their original value. |
casting | str, optional | Specifies casting behavior during computation. |
order | str, optional | Specifies memory layout order of the output array. |
dtype | data-type, optional | Specifies the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array containing the hypotenuse values computed element-wise. If both x1
and x2
are scalars, a scalar is returned.
Examples
1. Computing the Hypotenuse of a Right Triangle
Computing the hypotenuse of a right triangle with given side lengths.
</>
Copy
import numpy as np
# Define the lengths of the two perpendicular sides
x1 = 3
x2 = 4
# Compute the hypotenuse
hypotenuse = np.hypot(x1, x2)
# Print the result
print("Hypotenuse length:", hypotenuse)
Output:
Hypotenuse length: 5.0

2. Computing Hypotenuse for Arrays
Computing the hypotenuse for multiple sets of right triangle sides using arrays.
</>
Copy
import numpy as np
# Define arrays of perpendicular side lengths
x1 = np.array([3, 6, 8])
x2 = np.array([4, 8, 15])
# Compute hypotenuse for each pair of sides
hypotenuse_values = np.hypot(x1, x2)
# Print the results
print("Side 1:", x1)
print("Side 2:", x2)
print("Hypotenuse:", hypotenuse_values)
Output:
Side 1: [3 6 8]
Side 2: [ 4 8 15]
Hypotenuse: [ 5. 10. 17.]

3. Using the out
Parameter
Using an output array to store the hypotenuse values instead of creating a new array.
</>
Copy
import numpy as np
# Define arrays of perpendicular sides
x1 = np.array([5, 12, 9])
x2 = np.array([12, 5, 40])
# Create an output array with the same shape
output_array = np.ndarray(shape=[1, 3])
# Compute hypotenuse and store in output_array
np.hypot(x1, x2, out=output_array)
# Print the results
print("Computed hypotenuse values:", output_array)
Output:
Computed hypotenuse values: [13. 13. 41.]

4. Using the where
Parameter
Using a condition to compute the hypotenuse only for selected elements.
</>
Copy
import numpy as np
# Define arrays of perpendicular sides
x1 = np.array([3, 5, 9, 12])
x2 = np.array([4, 12, 40, 5])
# Define a mask (compute hypotenuse only where mask is True)
mask = np.array([True, False, True, False])
# Compute hypotenuse values where mask is True
result = np.hypot(x1, x2, where=mask)
# Print the results
print("Computed hypotenuse values with mask:", result)
Output:
Computed hypotenuse values with mask: [ 5. 0. 41. 0.]

The hypotenuse values are computed only for elements where mask=True
.