{"id":38605,"date":"2022-12-26T13:34:27","date_gmt":"2022-12-26T13:34:27","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38605"},"modified":"2023-01-29T09:27:33","modified_gmt":"2023-01-29T09:27:33","slug":"calculate-square-root-using-numpy","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/calculate-square-root-using-numpy","title":{"rendered":"How to Calculate Square Root using Numpy in Python?"},"content":{"rendered":"\n<p>There are many measures in mathematics that intrigue one. One such measure is the square root! While we can readily cry the number out loud when the question is to find the square root for the numbers which are the product of themselves, things take an ugly turn when they no longer comply with the aforementioned condition.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/rmse-root-mean-square-error\" data-type=\"post\" data-id=\"9258\">RMSE \u2013 Root Mean Square Error in Python<\/a><\/em><\/strong><\/p>\n\n\n\n<p>In this article, we would be exploring the different methods of putting into use the in-built function within the&nbsp;<em>numpy&nbsp;<\/em>library for calculating the square root of the given entities \u2013 the&nbsp;<em>sqrt( )&nbsp;<\/em>function!<\/p>\n\n\n\n<p>First things first! Import the\u00a0<a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\"><em>numpy\u00a0<\/em>library<\/a> before getting on with the deployment of the\u00a0<em>sqrt( )\u00a0<\/em>function 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<p>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&nbsp;<em>sqrt( )&nbsp;<\/em>function<\/strong><\/li>\n\n\n\n<li><strong>Calculating Square Root for One-Dimensional Array<\/strong><\/li>\n\n\n\n<li><strong>Calculating Square Root for N-Dimensional Array<\/strong><\/li>\n\n\n\n<li><strong>Calculating Square Root for Complex Numbers<\/strong><\/li>\n\n\n\n<li><strong>Limitations of&nbsp;<em>sqrt( )&nbsp;<\/em>function<\/strong><\/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\">Syntax of\u00a0the sqrt( )\u00a0function<\/h2>\n\n\n\n<p>Before diving into the details of how to use the&nbsp;<em>sqrt( )<\/em>&nbsp;function, let us get started by understanding the basic components that it needs for proper functioning.<\/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>where,<\/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 calculated<\/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, but could be used to store the results in the desired array which is of the same length as the output<\/li>\n\n\n\n<li><strong><em>where \u2013<\/em><\/strong>&nbsp;an optional construct which is used to calculate the universal function (ufunc) at the given position when set to&nbsp;<em>True&nbsp;<\/em>(default setting) or not calculate when set to&nbsp;<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 which is being used<\/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\">Calculating Square Root for One-Dimensional Array<\/h2>\n\n\n\n<p>After importing the&nbsp;<em>numpy&nbsp;<\/em>library let us construct a one-dimensional array with the elements for which the square root is to be calculated as shown below.<\/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>Now let us use the <em>sqrt( ) <\/em>function to deduce the results for the above array.<\/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<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"506\" height=\"70\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-One-Dimensional-Array.jpg\" alt=\"Square Root Calculated For One Dimensional Array\" class=\"wp-image-38614\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-One-Dimensional-Array.jpg 506w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-One-Dimensional-Array-300x42.jpg 300w\" sizes=\"auto, (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\">Calculating Square Root for N-Dimensional Array:<\/h2>\n\n\n\n<p>Similar to one-dimensional arrays, one can also calculate the square root of the given entities grouped together in N-dimensional arrays. Here is a two-dimensional array for which the square root is calculated using the <em>sqrt( ) <\/em>function as shown below.<\/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<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"428\" height=\"103\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-Two-Dimensional-Array.jpg\" alt=\"Square Root Calculated For Two Dimensional Array\" class=\"wp-image-38616\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-Two-Dimensional-Array.jpg 428w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-Two-Dimensional-Array-300x72.jpg 300w\" sizes=\"auto, (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\">Calculating Square Root for Complex Numbers<\/h2>\n\n\n\n<p>While a straightforward deduction of a square root can give us a run for the money, things get even trickier when we venture into the territory of complex numbers. To put things into perspective, the following is the formula used to calculate the square root of a complex number!<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"101\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Formula-for-Complex-Numbers.png\" alt=\"Square Root Formula For Complex Numbers\" class=\"wp-image-38617\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Formula-for-Complex-Numbers.png 406w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Formula-for-Complex-Numbers-300x75.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><figcaption class=\"wp-element-caption\">Square Root Formula For Complex Numbers<\/figcaption><\/figure>\n\n\n\n<p>To save us from misery, the <em>sqrt( )<\/em> function also has the capability to deduce the square root of complex numbers too. It has the ability to deduce a complex number directly from the given array, but the flip side to this characteristic is that the function goes on to consider every other element within that array also as a complex number.<\/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<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"497\" height=\"138\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-Complex-Numbers.jpg\" alt=\"Square Root Calculated For Complex Numbers\" class=\"wp-image-38619\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-Complex-Numbers.jpg 497w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Square-Root-Calculated-for-Complex-Numbers-300x83.jpg 300w\" sizes=\"auto, (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\u00a0sqrt( )\u00a0function<\/h2>\n\n\n\n<p>While it extends its abilities to complex numbers, it is not that the <em>sqrt( ) <\/em>function is all mighty, rather it also has its limitations too. One such is the disability to calculate the square root for negative numbers. <\/p>\n\n\n\n<p>Let us now attempt to find the square root of \u2018-6\u2019 and bear witness to its outcome.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"116\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Warning-NaN-Error-Appears.jpg\" alt=\"Warning NaN Error Appears\" class=\"wp-image-38620\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Warning-NaN-Error-Appears.jpg 466w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Warning-NaN-Error-Appears-300x75.jpg 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><figcaption class=\"wp-element-caption\">Warning &#038; 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 how to find the square root of array elements using the&nbsp;<em>sqrt( )&nbsp;<\/em>function from the&nbsp;<em>numpy&nbsp;<\/em>library. Here\u2019s another article that explains how to find the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-maximum-on-arrays\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-maximum-on-arrays\" target=\"_blank\" rel=\"noreferrer noopener\">maximum amongst a set of array elements using&nbsp;<em>numpy&nbsp;<\/em>in Python<\/a>. There are numerous other enjoyable &#038; equally informative articles in <a href=\"https:\/\/www.askpython.com\/\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">AskPython<\/a> 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","protected":false},"excerpt":{"rendered":"<p>There are many measures in mathematics that intrigue one. One such measure is the square root! While we can readily cry the number out loud when the question is to find the square root for the numbers which are the product of themselves, things take an ugly turn when they no longer comply with the [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":38606,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38605","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\/38605","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\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38605"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38605\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38606"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}