NumPy hypot() Function:
Numpy hypot: The hypotenuse of a right-angled triangle is calculated using the hypot() function of the numpy module.
Mathematically, It is calculated as shown below:
hypotenuse = sqrt(b**2 + p**2)
Here, b= base of a right-angled triangle
p= perpendicular of a right-angled triangle
In the output array, each value of the hypotenuse is copied.
Syntax:
numpy.hypot(arr1, arr2, out)
Parameters
arr1: This is required. It’s the input array of bases for the right-angled triangles given.
arr2: This is required. It’s the input array of perpendiculars for the right-angled triangles given.
out: This is optional. This is the output array’s shape.
Return Value:
An array with the hypotenuses of the right-angled triangles given is returned.
- Python Calculation of Pythagorean Theorem
- Python NumPy log10() Function
- Python NumPy log2() Function
NumPy hypot() Function in Python
Example
Approach:
- Import numpy module using the import keyword.
- Give the array of bases as static input and store it in a variable.
- Give the array of perpendiculars as static input and store it in another variable.
- Print the given array of bases.
- Print the given array of perpendiculars.
- Pass the above-given bases and perpendiculars array’s as the argument to the hypot() function of the numpy module to get the hypotenuse values array for the given bases and perpendiculars array elements respectively.
- Store it in another variable.
- Print the hypotenuses array for the given bases and perpendiculars array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Give the array of bases as static input and store it in a variable gvn_bases = [8, 3, 6] # Give the array of perpendiculars as static input and store it in another variable gvn_perpendclrs = [4, 12, 5] # Print the given array of bases print("The given array of bases = ", gvn_bases) # Print the given array of perpendiculars print("The given array of perpendiculars = ", gvn_perpendclrs) # Pass the above given bases and perpendiculars array's as the argument to # the hypot() function of the numpy module to get the hypotenuse values array # for the given bases and perpendiculars array elements respectively # Store it in another variable. rslt_hypptenus = np.hypot(gvn_bases, gvn_perpendclrs) # Print the hypotenuses array for the given bases and perpendiculars array print("The hypotenuses array for the given bases and perpendiculars array:") print(rslt_hypptenus)
Output:
The given array of bases = [8, 3, 6] The given array of perpendiculars = [4, 12, 5] The hypotenuses array for the given bases and perpendiculars array: [ 8.94427191 12.36931688 7.81024968]