{"id":23719,"date":"2023-10-26T16:20:15","date_gmt":"2023-10-26T10:50:15","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=23719"},"modified":"2023-10-26T16:23:08","modified_gmt":"2023-10-26T10:53:08","slug":"numpy-sqrt-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-sqrt-in-python\/","title":{"rendered":"numpy.sqrt() in Python: A Detailed Guide"},"content":{"rendered":"\n<p>Mathematics has a handful of parameters using which we can gauge numbers. One among those is the square root. It might be a walk to find the square root when the given numbers are the product of themselves, but when it no longer seems to be the case, things get a tad bit difficult. <\/p>\n\n\n\n<p>This article sets out to explore the different ways of using the built-in function &#8211;<strong> <em>sqrt( )<\/em><\/strong> from the <em>numpy <\/em>library of Python which is used to calculate the square roots in Python programming. We shall cover the aspects of the&nbsp;<em>sqrt( )&nbsp;<\/em>function in the following sections:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Syntax of <em>sqrt( )&nbsp;<\/em>function<\/strong><\/li>\n\n\n\n<li><strong>Using <em>sqrt( ) <\/em>function on One-Dimensional Array<\/strong><\/li>\n\n\n\n<li><strong>Using <em>sqrt( ) <\/em>function on N-Dimensional Array<\/strong><\/li>\n\n\n\n<li><strong>Using <em>sqrt( ) <\/em>function on Complex Numbers<\/strong><\/li>\n\n\n\n<li><strong>Limitations of <em>sqrt( )\u00a0<\/em>function<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Before that let us import the&nbsp;<em>numpy&nbsp;<\/em>library using the below code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of <em>sqrt( )&nbsp;<\/em>Function<\/h2>\n\n\n\n<p>We shall understand the construction of the <em>sqrt( ) <\/em>function using the syntax given below which details the basic constituents required for its effective functioning.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.sqrt(x, out=None, where=True, dtype=None)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>x \u2013&nbsp;<\/em><\/strong>input array or scalar entity for which the square root is to be deduced<\/li>\n\n\n\n<li><strong><em>out \u2013<\/em><\/strong>&nbsp;an optional construct set to&nbsp;<em>none&nbsp;<\/em>by default, and can be used to have the results stored in a specific array that is of the same length as the output<\/li>\n\n\n\n<li><strong><em>where \u2013<\/em><\/strong>&nbsp;an optional construct set to <em>True <\/em>&nbsp;by default and is used to pass the function at all positions that are declared <em>True <\/em>and retain those positions as is, which are set to <em>False<\/em><\/li>\n\n\n\n<li><strong><em>dtype \u2013&nbsp;<\/em><\/strong>an optional construct used to specify the data type that is to be returned<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using <em>sqrt( ) <\/em>Function on One-Dimensional Array<\/h2>\n\n\n\n<p>In this section, we shall construct a one-dimensional array using the below code in order to calculate the square root for its elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = &#x5B;&#x5B;12, 36, 71, 99]]\n<\/pre><\/div>\n\n\n<p>Once done, the above array is passed through the <em>sqrt( ) <\/em>function to find the results.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.sqrt(ar1)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"506\" height=\"70\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-One-Dimensional-Array.jpg\" alt=\"Square Root Calculated For One Dimensional Array\" class=\"wp-image-23723\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-One-Dimensional-Array.jpg 506w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-One-Dimensional-Array-300x42.jpg 300w\" sizes=\"(max-width: 506px) 100vw, 506px\" \/><figcaption class=\"wp-element-caption\">Square Root Calculated for One-Dimensional Array<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using <em>sqrt( ) <\/em>Function on N-Dimensional Array<\/h2>\n\n\n\n<p>The square root calculation for N-dimensional arrays shall be demonstrated in this section and it works in the same way as done earlier for the one-dimensional array. Given below is a two-dimensional array that shall be passed through the <em>sqrt( ) <\/em>function to determine the square root of its entities.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar2 = np.array(&#x5B;&#x5B;12.5, 33.3, 25],\n                &#x5B;26, 79, 14.5]], dtype = float)\nnp.sqrt(ar2)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"428\" height=\"103\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-Two-Dimensional-Array.jpg\" alt=\"Square Root Calculated For Two Dimensional Array\" class=\"wp-image-23724\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-Two-Dimensional-Array.jpg 428w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-Two-Dimensional-Array-300x72.jpg 300w\" sizes=\"(max-width: 428px) 100vw, 428px\" \/><figcaption class=\"wp-element-caption\">Square Root Calculated for Two-Dimensional Array<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using <em>sqrt( ) <\/em>Function on Complex Numbers<\/h2>\n\n\n\n<p>When calculating the square root for decimals with a pen and paper can give you a migraine, imagine what can the square root calculation of complex numbers give you. Just to have a fair idea of what is being implied here, have a look at the below formula to calculate the square root of the complex numbers.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"406\" height=\"101\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Formula-for-Complex-Numbers.png\" alt=\"Square Root Formula For Complex Numbers\" class=\"wp-image-23725\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Formula-for-Complex-Numbers.png 406w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Formula-for-Complex-Numbers-300x75.png 300w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><figcaption class=\"wp-element-caption\">Square Root Formula for Complex Numbers<\/figcaption><\/figure>\n\n\n\n<p>But Python being all the more friendly, has induced the capabilities in the <em>sqrt( ) <\/em>function for determining the square root of complex numbers too. But it comes with a price! When the input array has a complex number, then the <em>sqrt( ) <\/em>function goes on to consider every other element in the input as a complex number and deduce its corresponding square root.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar3 = &#x5B;&#x5B;12+5j, 33-3j, 25],\n       &#x5B;26, 7-9j, 14+5j]]\nnp.sqrt(ar3)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"497\" height=\"138\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-Complex-Numbers.jpg\" alt=\"Square Root Calculated For Complex Numbers\" class=\"wp-image-23726\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-Complex-Numbers.jpg 497w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Square-Root-Calculated-for-Complex-Numbers-300x83.jpg 300w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><figcaption class=\"wp-element-caption\">Square Root Calculated for Complex Numbers<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Limitations of <em>sqrt( )&nbsp;<\/em>Function<\/h2>\n\n\n\n<p>This function is limited to calculating the square roots of only positive numbers. When those that are negative come into the picture, then the below result comes up:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"466\" height=\"116\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Warning-NaN-Error-Appears.jpg\" alt=\"Warning NaN Error Appears\" class=\"wp-image-23727\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Warning-NaN-Error-Appears.jpg 466w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Warning-NaN-Error-Appears-300x75.jpg 300w\" sizes=\"(max-width: 466px) 100vw, 466px\" \/><figcaption class=\"wp-element-caption\">Warning &amp; NaN Error Appears!<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on the different ways to find the square root using the&nbsp;<em>sqrt( )&nbsp;<\/em>function from the&nbsp;<em>numpy&nbsp;<\/em>library. Here\u2019s another article that explains how to use the<a href=\"https:\/\/codeforgeek.com\/numpy-positive-in-python\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-positive-in-python\/\" rel=\"noreferrer noopener\"> <em>positive( ) <\/em>function for N-dimensional arrays<\/a> in Python. There are numerous other enjoyable &amp; equally informative articles in <em><a href=\"https:\/\/codeforgeek.com\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/\" rel=\"noreferrer noopener\">CodeforGeek<\/a><\/em> that might be of great help to those who are looking to level up in Python. Whilst you enjoy those,&nbsp;<em>adios<\/em>!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\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.sqrt.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.sqrt.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mathematics has a handful of parameters using which we can gauge numbers. One among those is the square root. It might be a walk to find the square root when the given numbers are the product of themselves, but when it no longer seems to be the case, things get a tad bit difficult. This [&hellip;]<\/p>\n","protected":false},"author":90,"featured_media":23757,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-23719","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy-sqrt-function.png",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy-sqrt-function-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy-sqrt-function-300x180.png",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy-sqrt-function-768x461.png",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy-sqrt-function.png",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy-sqrt-function.png",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy-sqrt-function.png",1000,600,false]},"uagb_author_info":{"display_name":"Arulius Savio","author_link":"https:\/\/codeforgeek.com\/author\/arulius\/"},"uagb_comment_info":0,"uagb_excerpt":"Mathematics has a handful of parameters using which we can gauge numbers. One among those is the square root. It might be a walk to find the square root when the given numbers are the product of themselves, but when it no longer seems to be the case, things get a tad bit difficult. This&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=23719"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23719\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/23757"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=23719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=23719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=23719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}