Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy hypot() Function



The NumPy hypot() function is used to calculate the Euclidean distance (or hypotenuse) between two points (x, y) in a 2D plane. It computes the value of sqrt(x2 + y2) for each pair of corresponding elements from two arrays or scalars.

This function is commonly used to calculate the distance between two points in geometry or to compute the magnitude of a 2D vector.

  • Domain: The function accepts two input arrays or scalars representing the x and y coordinates of points. Both inputs can be scalars, arrays, or a combination thereof. The domain is all real numbers.
  • Range: The output values are non-negative real numbers, representing the Euclidean distance or magnitude.

Syntax

Following is the syntax of the NumPy hypot() function −

numpy.hypot(x, y, /, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters

This function accepts the following parameters −

  • x: The first input array or scalar representing the x-coordinates of points.
  • y: The second input array or scalar representing the y-coordinates of points.
  • out (optional): A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the result will be computed. Otherwise, the result will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are processed. The dtype of x and y is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array where each element is the Euclidean distance (or hypotenuse) calculated from the corresponding pair of elements (x, y) from the input arrays. The result represents the distance between points (x, y) in the 2D plane.

Example: Basic Usage of hypot() Function

In the following example, we use the hypot() function to compute the Euclidean distance between points (x, y) in two 1-dimensional arrays −

import numpy as np

# Creating two 1-dimensional arrays for x and y
x = np.array([3, 4, 5])
y = np.array([4, 3, 12])

# Applying hypot to each pair (x, y)
result = np.hypot(x, y)
print(result)

The output obtained will be −

[ 5.  5. 13.]

Example: Hypotenuse with Scalars

In this example, we calculate the Euclidean distance (hypotenuse) for scalar values −

import numpy as np

# Scalar values for x and y
x = 3
y = 4

# Applying hypot to the scalar values
result = np.hypot(x, y)
print(result)

The output obtained is −

5.0

Example: Hypotenuse for Arrays of Different Shapes

In this example, we calculate the Euclidean distance for arrays of different shapes. NumPy will broadcast the arrays to have compatible shapes before performing the operation −

import numpy as np

# Creating 1-dimensional arrays for x and y
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

# Applying hypot to each pair (x, y)
result = np.hypot(x, y)
print(result)

This will produce the following result −

[4.12310563 5.38516481 6.70820393]

Example: Hypotenuse with Negative Values

In this example, we calculate the Euclidean distance for arrays containing negative values. The function handles negative values properly, as distance is always non-negative −

import numpy as np

# Negative values for x and y
x = np.array([-3, -4, -5])
y = np.array([4, 3, 12])

# Applying hypot to each pair (x, y)
result = np.hypot(x, y)
print(result)

This will produce the following result −

[ 5.  5. 13.]
numpy_trigonometric_functions.htm
Advertisements