NumPy | hypot method
Start your free 7-days trial now!
Numpy's hypot(~)
method computes the hypotenuse given the two sides of the triangle.
Parameters
1. x1
| array-like
The first side of the triangle.
2. x2
| array-like
The second side of the triangle.
3. out
| Numpy array
| optional
Instead of creating a new array, you can place the computed result into the array specified by out
.
4. where
| array
of boolean
| optional
Values that are flagged as False will be ignored, that is, their original value will be uninitialized. If you specified the out parameter, the behavior is slightly different - the original value will be kept intact. Since this is a source of confusion for many, check examples below.
Return value
A Numpy array that holds the hypotenuse.
Examples
Basic Usage
np.hypot(3,4)
5.0
np.hypot([1,3],[1,4])
array([1.41421356, 5. ])
Here, the hypothenuse of a right-angled triangle with sides 1 is the square root of 2.
Specifying an output array
a = np.zeros(2)np.hypot([1,3],[1,4], out=a)a
array([1.41421356, 5. ])
Here, we've output the result into array a
.
Specifying a boolean mask
np.hypot([1,3], [1,4], where=[False, True])
array([2.34e-324, 5. ])
Here, only the second number was used for calculation since it has a corresponding boolean of True
in the mask. You should notice how the values with False
yielded strange results - in fact, you should disregard them because they are uninitialized numbers that are of no practical use.
Now, if you specified the out
parameter, instead of uninitalized values, the original values will be left intact:
a = np.zeros(2)np.hypot([1,3], [1,4], out=a where=[False, True])a
array([0., 0.])