The hypot
function in NumPy calculates the Pythagorean hypotenuse of two values. It assumes that each pair corresponds to the lengths of the sides of a right-angled triangle.
To use the hypot
function, we must import the NumPy library at the beginning of the program:
import numpy as np
numpy.hypot(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'hypot'>
A universal function (ufunc) is a function that operates on ndarrays in an element-by-element fashion. The
hypot
method is a universal function.
The hypot
function only accepts the following arguments:
x1
and x2
: Array-like structures on the contents of which the hypot
function will be applied. For instance, The length of the triangles can be stored in x1
and their widths in x2
. The lengths of the two arrays must be identical.out
(optional): The function’s output is stored at this location.where
(optional): If set True
, a universal function is calculated at this position.casting
(optional): This enables the user to decide how the data will be cast. If set as same_kind, safe casting will take place.order
(optional): This determines the memory layout of the output. For example, if set as K, the function reads data in the order they are written in memory.dtype
(optional): This is the data type of the array.subok
(optional): To pass subclasses, subok
must be set as True
.The hypot
function returns an array with the same dimensions as x1
and x2
.
If
x1
andx2
are scalars, the return value is scalar as well. The returned array contains the result of thehypot
function, obtained by summing the squares of the values at identical indexes inx
andx2
and then taking the square root of this sum.
The following program demonstrates how to apply the hypot
function to calculate the hypotenuse length of three triangles.
The return value is printed using the built-in print
function that comes with Python.
import numpy as npprint(np.hypot([3, 2, 3], [4, 2, 3]))
Free Resources