|
| 1 | +--- |
| 2 | +Title: '.hypot()' |
| 3 | +Description: 'Calculates the hypotenuse of a right triangle given the lengths of its two legs.' |
| 4 | +Subjects: |
| 5 | + - 'AI' |
| 6 | + - 'Computer Science' |
| 7 | + - 'Data Science' |
| 8 | +Tags: |
| 9 | + - 'AI' |
| 10 | + - 'Functions' |
| 11 | + - 'Pytorch' |
| 12 | + - 'Trigonometry' |
| 13 | +CatalogContent: |
| 14 | + - 'intro-to-py-torch-and-neural-networks' |
| 15 | + - 'paths/data-science' |
| 16 | +--- |
| 17 | + |
| 18 | +The **`torch.hypot`** function in PyTorch calculates the hypotenuse of right triangles, given the lengths of the two legs. |
| 19 | + |
| 20 | +Element-wise, `torch.hypot()` computes: |
| 21 | + |
| 22 | +$$ |
| 23 | +\text{out}_i = \sqrt{(\text{input}_i)^2 + (\text{other}_i)^2} |
| 24 | +$$ |
| 25 | + |
| 26 | +## Syntax |
| 27 | + |
| 28 | +```pseudo |
| 29 | +torch.hypot(input, other, *, out=None) |
| 30 | +``` |
| 31 | + |
| 32 | +**Parameters:** |
| 33 | + |
| 34 | +- `input`: The first input tensor. |
| 35 | +- `other`: The second input tensor. This must be broadcastable with `input`. |
| 36 | +- `out` (Optional): The output tensor to store the result. |
| 37 | + |
| 38 | +**Return value:** |
| 39 | + |
| 40 | +Returns a tensor containing the element-wise Euclidean norm: $\sqrt{(\text{input}_i)^2 + (\text{other}_i)^2}$. |
| 41 | + |
| 42 | +## Example 1: Basic Element-Wise Hypotenuse |
| 43 | + |
| 44 | +In this example, `torch.hypot()` calculates the hypotenuse for corresponding elements of two 1D tensors: |
| 45 | + |
| 46 | +```py |
| 47 | +import torch |
| 48 | + |
| 49 | +# Create input tensors |
| 50 | +x = torch.tensor ([3.0, 5.0, 8.0]) |
| 51 | +y = torch.tensor ([4.0, 12.0, 15.0]) |
| 52 | + |
| 53 | +# Perform element-wise operation |
| 54 | +hypotenuse = torch.hypot(x, y) |
| 55 | + |
| 56 | +# Print the result |
| 57 | +print(hypotenuse) |
| 58 | +``` |
| 59 | + |
| 60 | +This code would output the following: |
| 61 | + |
| 62 | +```shell |
| 63 | +tensor([5., 13., 17.]) |
| 64 | +``` |
| 65 | + |
| 66 | +## Example 2L 2D Distance Between Points |
| 67 | + |
| 68 | +In this example, `torch.hypot()` calculates the distance from the origin for 2D points stored as x, y coordinates: |
| 69 | + |
| 70 | +```py |
| 71 | +import torch |
| 72 | + |
| 73 | +# For the following array: |
| 74 | +points = torch.tensor([ |
| 75 | + [3.0, 4.0], |
| 76 | + [5.0, 12.0], |
| 77 | + [8.0, 15.0], |
| 78 | +]) |
| 79 | + |
| 80 | +# Split into x and y columns: |
| 81 | +x = points[:, 0] |
| 82 | +y = points[:, 1] |
| 83 | + |
| 84 | +distances = torch.hypot(x, y) |
| 85 | +print(distances) |
| 86 | +``` |
| 87 | + |
| 88 | +This will output: |
| 89 | + |
| 90 | +```shell |
| 91 | +tensor([ 5., 13., 17. ]) |
| 92 | +``` |
| 93 | + |
| 94 | +This example organizes the `6 x 1` tensor into `x, y` pairs, and calculates each one individually: |
| 95 | + |
| 96 | +- $\sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt {25} = 5$ |
| 97 | +- $\sqrt{5^2 + 12^2} = \sqrt{25 + 144} = \sqrt {169} = 13$ |
| 98 | +- $\sqrt{8^2 + 15^2} = \sqrt{64 + 225} = \sqrt {289} = 17$ |
0 commit comments