{"id":36768,"date":"2022-11-19T12:50:02","date_gmt":"2022-11-19T12:50:02","guid":{"rendered":"https:\/\/www.askpython.com\/?p=36768"},"modified":"2023-02-16T19:56:32","modified_gmt":"2023-02-16T19:56:32","slug":"numpy-hypot","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-hypot","title":{"rendered":"NumPy hypot &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Hello readers! Welcome to another tutorial of the series <a href=\"https:\/\/www.askpython.com\/python\/examples\/plot-mathematical-functions\" data-type=\"post\" data-id=\"21691\">NumPy Mathematical Functions<\/a>. In this tutorial, we will understand the hypot function of the NumPy Library.<\/p>\n\n\n\n<p>We all must have calculated the value of the hypotenuse of a right-angled triangle in Mathematics. We used to apply the famous Pythagoras Theorem to find out the value of the hypotenuse or any other two sides i.e. perpendicular and the base.<\/p>\n\n\n\n<p>This tutorial will explain how to use <strong>NumPy hypot<\/strong> function with many examples. So, let&#8217;s get started.<\/p>\n\n\n\n<p><strong><em>Also check: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-arctan\" data-type=\"post\" data-id=\"36072\">NumPy Arctan \u2013 A Complete Guide<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hypotenuse &#8211; A Quick Overview<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A right-angled triangle has one of the angles equal to 90 degrees i.e. two sides are perpendicular to each other. In a right-angled triangle, the side <strong>opposite to the 90 degree<\/strong> angle is called the <strong>Hypotenuse<\/strong> of the triangle.<\/li>\n\n\n\n<li>The value of the hypotenuse side in a right-angled triangle is the largest among all the sides.<\/li>\n<\/ul>\n\n\n\n<p>Refer to the following image for a better understanding.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Hypotenuse.png\" alt=\"Hypotenuse\" class=\"wp-image-36826\" width=\"421\" height=\"304\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Hypotenuse.png 1280w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Hypotenuse-300x217.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Hypotenuse-1024x739.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Hypotenuse-768x554.png 768w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><figcaption class=\"wp-element-caption\"><strong>Hypotenuse<\/strong><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy hypot?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>NumPy provides a lot of Mathematical functions and numpy.hypot() is one of them. Here, hypot stands for the hypotenuse of a right-angled. triangle.<\/li>\n\n\n\n<li>numpy.hypot() computes the value of the hypotenuse in a right-angled triangle given the values of the other two sides.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy hypot<\/h2>\n\n\n\n<p><code>numpy.hypot(x1, x2)<\/code> <\/p>\n\n\n\n<p>where x1 and x2 are the values of the two sides of a right-angled triangle.<\/p>\n\n\n\n<p>This function computes the value of the hypotenuse side according to the <strong>Pythagoras Theorem<\/strong>. So, <code>numpy.hypot(x1, x2)<\/code> will be calculated as sqrt(x1<sup>2<\/sup>+x2<sup>2<\/sup>) where sqrt is the square root.<\/p>\n\n\n\n<p><strong>Note:<\/strong> x1 and x2 cannot be negative.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working with NumPy hypot Function<\/h2>\n\n\n\n<p>Let&#8217;s now look at some practical examples to work with the numpy hypot function. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calculating the hypotenuse of two lists of values<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing the numpy library\nimport numpy as np\n\nx1 = &#x5B;12 , 4 , 5 , 6]\n\nx2 = &#x5B;5 , 6 , 7 , 14]\n\na = np.hypot(x1 , x2)\n\nprint(&quot;Hypotenuse Values:\\n&quot;,a)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nHypotenuse Values:\n &#x5B;13.          7.21110255  8.60232527 15.23154621]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Calculating hypotenuse using an array of values<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.array((4 , 5))\n\nb = np.array((3 , 12))\n\nc = np.hypot(a , b)\n\nprint(&quot;Hypotenuse Values :\\n&quot;,c)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nHypotenuse Values :\n &#x5B; 5. 13.]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">NumPy hypot Function with 2-D Array<\/h2>\n\n\n\n<p>Moving further, we&#8217;ll now calculate the hypotenuse using a <a href=\"https:\/\/www.askpython.com\/python\/array\/array-indexing-in-python\" data-type=\"post\" data-id=\"12186\">2D array.<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calculating hypotenuse of 2D lists<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Creating two 3x3 arrays\nx = &#x5B;&#x5B;1,2,3] , &#x5B;4,5,6] , &#x5B;7,8,9]]\n\ny = &#x5B;&#x5B;10,11,12] , &#x5B;13,14,15] , &#x5B;16,17,18]]\n\nz = np.hypot(x,y)\n\nprint(&quot;Hypotenuse Values:\\n&quot;,z)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nHypotenuse Values:\n &#x5B;&#x5B;10.04987562 11.18033989 12.36931688]\n &#x5B;13.60147051 14.86606875 16.15549442]\n &#x5B;17.4642492  18.78829423 20.1246118 ]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Calculating hypotenuse using the np.ones() data<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n#Initializing a 3x3 NumPy Array with all the elements equal to 3\nm = 3*np.ones((3,3))\n\n#Initializing a 3x3 NumPy Array with all the elements equal to 4\nn = 4*np.ones((3,3))\n\np = np.hypot(m , n)\n\nprint(&quot;Hypotenuse Values:\\n&quot;,p)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nHypotenuse Values:\n &#x5B;&#x5B;5. 5. 5.]\n &#x5B;5. 5. 5.]\n &#x5B;5. 5. 5.]]\n<\/pre><\/div>\n\n\n<p><code>np.ones()<\/code> function creates a NumPy array with all the elements equal to 1 and in the above code snippet, we have created a variable <strong>m<\/strong> which contains a 3&#215;3 (2-D array) with all the elements equal to 3 and another variable <strong>n<\/strong> also contains a 3&#215;3 (2-D array)  with all the elements equal to 4.<\/p>\n\n\n\n<p>The next step is simple where m and n are passed as an argument to the np.hypot() function and the Hypotenuse values are calculated for each pair of elements from both arrays.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-arcsin\" data-type=\"post\" data-id=\"35961\">NumPy Arcsin- A Complete Guide<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>So, that was all about the NumPy hypot function, it&#8217;s a pretty simple and straightforward function. Stay tuned and keep learning \ud83d\ude42<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.hypot.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy Documentation &#8211; NumPy hypot<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello readers! Welcome to another tutorial of the series NumPy Mathematical Functions. In this tutorial, we will understand the hypot function of the NumPy Library. We all must have calculated the value of the hypotenuse of a right-angled triangle in Mathematics. We used to apply the famous Pythagoras Theorem to find out the value of [&hellip;]<\/p>\n","protected":false},"author":48,"featured_media":37348,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-36768","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=36768"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36768\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37348"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=36768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=36768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=36768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}