Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
python
numpy
communitycreator

What is the numpy.hypot() function in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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[(opposite2opposite ^{2}) + (Adjacent2Adjacent^{2})]

Syntax

numpy.hypot(x1, x2 /, out=None, *, where=True)

Parameters

The numpy.hypot() function takes the following parameter values:

  • x1 and x2: 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 is True, 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 triangles
x1 = np.array([4, 5])
x2 = np.array([3, 12])
# taking the arcsin element-wise
myarray = np.hypot(x1, x2)
print(myarray)
Implementing the numpy.hypot() function

Code explanation

  • Line 1: We import the numpy module.
  • Lines 4–5: We create the arrays x1 and x2, 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 called myarray.
  • Line 10: We print the variable myarray to 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[(424^{2}) + (323^{2})] = sqrt(16 + 9) = sqrt(25) = 5.

RELATED TAGS

function
python
numpy
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring