NumPy | hypot method
hypot(~) method computes the hypotenuse given the two sides of the triangle.
The first side of the triangle.
The second side of the triangle.
Numpy array |
Instead of creating a new array, you can place the computed result into the array specified by
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.
A Numpy array that holds the hypotenuse.
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)aarray([1.41421356, 5. ])
Here, we've output the result into array
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])aarray([0., 0.])