From 2fee9fe267ae836c09d373835e771d27e4d72760 Mon Sep 17 00:00:00 2001 From: Luis Diego Monzon Montufar Date: Thu, 23 Oct 2025 09:39:30 -0600 Subject: [PATCH 1/6] created hypot directory and md file --- content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md diff --git a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md new file mode 100644 index 00000000000..e69de29bb2d From 6c420d74808d04f423955f331c0c79b4c524ed33 Mon Sep 17 00:00:00 2001 From: Luis Diego Monzon Montufar Date: Thu, 23 Oct 2025 11:36:33 -0600 Subject: [PATCH 2/6] created first edition of hypot.md awaiting revission --- .../tensor-operations/terms/hypot/hypot.md | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md index e69de29bb2d..eb94368b9be 100644 --- a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md +++ b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md @@ -0,0 +1,89 @@ +--- +Title: '.hypot()' +Description: 'It calculates the hypotenuse of a right traingle given its two legs' +Subjects: + - 'Data Science' + - 'AI' +Tags: + - 'AI' + - 'Trigonometry' + - 'Pytorch' + - 'Functions' +CatalogContent: + - 'intro-to-p-torch-and-neural-networks' + - 'paths/data-science' +--- + +The **`.hypot()`** function is an element-wise operation that calculates the hypotenuse of 2 given [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors). + +So, to put it simply, the **`.hypot()`** operator can be considered the same as +$$ +hypotenuse = \sqrt{x^2 + y^2} +$$ + +## Syntax + +```pseudo +torch.hypot(input, other, *, out=None) +``` +**Parameters:** +* `input` - is the first input tensor (i.e $x$ in $\sqrt{*x*^2 + y^2}$). +* `other` - is the second input tensor (i.e $y$ in $\sqrt{x^2 + *y*^2}$). +* `out` (Optional) - the output tensor. + +**Return Value** + +A tensor with the result of the hypotenuse between $\sqrt{x^2 + y^2}$. + +## Example 1 - Basic Element-Wise Hypotenuse + +```py +import torch + +# Create input tensors +x = torch.tensor ([3.0, 5.0, 8.0]) +y = torch.tensor ([4.0, 12.0, 15.0]) + +# Perform element-wise operation +hypotenuse = torch.hypot(x, y) + +# Print the result +print(hypotenuse) +``` + +This would output: +```shell +tensor([5., 13., 17.]) +``` + +## Example 2 - 2D Distance Between Points + +For 2D points stored as `x, y` coordinates, and we want to calculate each point's distance from the origin (0, 0): + +```py +import torch + +# For the following array: +points = torch.tensor([ + [3.0], [4.0], + [5.0], [12.0], + [8.0], [15.0], +]) + +# Split into x and y columns: +x = points[:, 0] +y = points[:, 1] + +distances = torch.hypot(x, y) +print(distances) +``` + +This will output: +```shell +tensor([ 5., 13., 17. ]) +``` + +This example organizes the `6 x 1` tensor into `x, y` pairs, and calculates each one individually: +* $\sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt {25} = 5$ +* $\sqrt{5^2 + 12^2} = \sqrt{25 + 144} = \sqrt {169} = 13$ +* $\sqrt{8^2 + 15^2} = \sqrt{64 + 225} = \sqrt {289} = 17$ \ No newline at end of file From e97ec44d8b36e2ccb7228ad53575e7a1588d3014 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 24 Oct 2025 16:55:40 +0530 Subject: [PATCH 3/6] Revise .hypot() documentation for clarity and accuracy Updated the description and examples for the .hypot() function in PyTorch documentation. --- .../tensor-operations/terms/hypot/hypot.md | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md index eb94368b9be..d2afc0f2313 100644 --- a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md +++ b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md @@ -1,24 +1,26 @@ --- Title: '.hypot()' -Description: 'It calculates the hypotenuse of a right traingle given its two legs' +Description: 'Calculates the hypotenuse of a right triangle given the lengths of its two legs.' Subjects: - - 'Data Science' - - 'AI' + - 'AI' + - 'Computer Science' + - 'Data Science' Tags: - - 'AI' - - 'Trigonometry' - - 'Pytorch' - - 'Functions' + - 'AI' + - 'Functions' + - 'Pytorch' + - 'Trigonometry' CatalogContent: - - 'intro-to-p-torch-and-neural-networks' - - 'paths/data-science' + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' --- -The **`.hypot()`** function is an element-wise operation that calculates the hypotenuse of 2 given [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors). +The **`torch.hypot`** function in PyTorch calculates the hypotenuse of right triangles, given the lengths of the two legs. + +Element-wise, `torch.hypot()` computes: -So, to put it simply, the **`.hypot()`** operator can be considered the same as $$ -hypotenuse = \sqrt{x^2 + y^2} +\text{out}_i = \sqrt{(\text{input}_i)^2 + (\text{other}_i)^2} $$ ## Syntax @@ -26,16 +28,20 @@ $$ ```pseudo torch.hypot(input, other, *, out=None) ``` + **Parameters:** -* `input` - is the first input tensor (i.e $x$ in $\sqrt{*x*^2 + y^2}$). -* `other` - is the second input tensor (i.e $y$ in $\sqrt{x^2 + *y*^2}$). -* `out` (Optional) - the output tensor. -**Return Value** +- `input`: The first input tensor. +- `other`: The second input tensor. This must be broadcastable with `input`. +- `out` (Optional): The output tensor to store the result. + +**Return value** + +Returns a tensor containing the element-wise Euclidean norm: $\sqrt{(\text{input}_i)^2 + (\text{other}_i)^2}$. -A tensor with the result of the hypotenuse between $\sqrt{x^2 + y^2}$. +## Example 1: Basic Element-Wise Hypotenuse -## Example 1 - Basic Element-Wise Hypotenuse +In this example, `torch.hypot()` calculates the hypotenuse for corresponding elements of two 1D tensors: ```py import torch @@ -51,14 +57,15 @@ hypotenuse = torch.hypot(x, y) print(hypotenuse) ``` -This would output: +This code would output the following: + ```shell tensor([5., 13., 17.]) ``` -## Example 2 - 2D Distance Between Points +## Example 2L 2D Distance Between Points -For 2D points stored as `x, y` coordinates, and we want to calculate each point's distance from the origin (0, 0): +In this example, `torch.hypot()` calculates the distance from the origin for 2D points stored as x, y coordinates: ```py import torch @@ -78,12 +85,14 @@ distances = torch.hypot(x, y) print(distances) ``` -This will output: +This will output: + ```shell tensor([ 5., 13., 17. ]) ``` This example organizes the `6 x 1` tensor into `x, y` pairs, and calculates each one individually: -* $\sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt {25} = 5$ -* $\sqrt{5^2 + 12^2} = \sqrt{25 + 144} = \sqrt {169} = 13$ -* $\sqrt{8^2 + 15^2} = \sqrt{64 + 225} = \sqrt {289} = 17$ \ No newline at end of file + +- $\sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt {25} = 5$ +- $\sqrt{5^2 + 12^2} = \sqrt{25 + 144} = \sqrt {169} = 13$ +- $\sqrt{8^2 + 15^2} = \sqrt{64 + 225} = \sqrt {289} = 17$ From 538828fc139b81a0b5da56c3f3f6f0060f2521db Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Fri, 31 Oct 2025 14:41:33 +0530 Subject: [PATCH 4/6] Apply suggestion from @avdhoottt --- .../pytorch/concepts/tensor-operations/terms/hypot/hypot.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md index d2afc0f2313..ce674ee8db1 100644 --- a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md +++ b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md @@ -72,9 +72,9 @@ import torch # For the following array: points = torch.tensor([ - [3.0], [4.0], - [5.0], [12.0], - [8.0], [15.0], + [3.0, 4.0], + [5.0, 12.0], + [8.0, 15.0], ]) # Split into x and y columns: From a01ba6e47a98334bee5a9ada54f3931e4de6dfab Mon Sep 17 00:00:00 2001 From: avdhoottt Date: Fri, 31 Oct 2025 14:52:28 +0530 Subject: [PATCH 5/6] Lint + Format --- .../pytorch/concepts/tensor-operations/terms/hypot/hypot.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md index ce674ee8db1..def0e5de67b 100644 --- a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md +++ b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md @@ -5,12 +5,12 @@ Subjects: - 'AI' - 'Computer Science' - 'Data Science' -Tags: +Tags: - 'AI' - 'Functions' - 'Pytorch' - 'Trigonometry' -CatalogContent: +CatalogContent: - 'intro-to-py-torch-and-neural-networks' - 'paths/data-science' --- From bfd7ab514f16740a3ae1f3376486532e6f8b1068 Mon Sep 17 00:00:00 2001 From: avdhoottt Date: Fri, 31 Oct 2025 14:57:49 +0530 Subject: [PATCH 6/6] Lint Issue --- content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md index def0e5de67b..5ed9a7e9f0c 100644 --- a/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md +++ b/content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md @@ -35,7 +35,7 @@ torch.hypot(input, other, *, out=None) - `other`: The second input tensor. This must be broadcastable with `input`. - `out` (Optional): The output tensor to store the result. -**Return value** +**Return value:** Returns a tensor containing the element-wise Euclidean norm: $\sqrt{(\text{input}_i)^2 + (\text{other}_i)^2}$.