What is the numpy.hypot() function in Python?
Overview
The numpy.hypot() function in Python is used to compute the hypotenuse of a given right-angled triangle’s sides. The hypotenuse is opposite and adjacent to the other two sides of the triangle.
Diagrammatically
Mathematically
numpy.hypot() = sqrt[() + ()]
Syntax
numpy.hypot(x1, x2 /, out=None, *, where=True)
Parameters
The numpy.hypot() function takes the following parameter values:
x1andx2: These represent the legs of the triangle, that is, the opposite and adjacent sides of the triangle. These two values are required for the function to work.out: This represents a location where the result is stored. This is an optional parameter value.where: This is the condition over which the input is being broadcast. At a given location where this condition isTrue, the resulting array will be set to the ufunc result. Otherwise, the resulting array will retain its original value. This is also an optional parameter value.**kwargs: This represents the other keyword arguments.
Return type
The numpy.hypot() function returns the hypotenuse of the given triangle(s).
Code example
import numpy as np# creating an array having the legs of trianglesx1 = np.array([4, 5])x2 = np.array([3, 12])# taking the arcsin element-wisemyarray = np.hypot(x1, x2)print(myarray)
Code explanation
- Line 1: We import the
numpymodule. - Lines 4–5: We create the arrays
x1andx2, which contain the legs of triangles. - Line 8: We implement the
numpy.hypot()function on the arrays we created. We assign the result to a variable calledmyarray. - Line 10: We print the variable
myarrayto the console.
In the example above, one of the given triangles has its legs (opposite and adjacent sides) as 4 and 3.
Mathematically:
numpy.hypot(4, 3) = sqrt[() + ()] = sqrt(16 + 9) = sqrt(25) = 5.