{"id":37484,"date":"2022-11-28T08:50:29","date_gmt":"2022-11-28T08:50:29","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37484"},"modified":"2023-02-16T19:56:30","modified_gmt":"2023-02-16T19:56:30","slug":"numpy-square","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-square","title":{"rendered":"numpy.square() &#8211; Explained in a Simple Way"},"content":{"rendered":"\n<p>Hello everyone! In this tutorial, we will be learning about the NumPy square function. This is a simple function to understand and easy to use. So, let&#8217;s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy square?<\/h2>\n\n\n\n<p><code><strong>NumPy square<\/strong><\/code> is one of the Mathematical Functions of the NumPy Library that calculates the <strong>square value<\/strong> of the input number. Yes, it&#8217;s that simple by the definition. It can be used with both real and complex numbers. The square of a number is equal to the number multiplied by itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy square<\/h2>\n\n\n\n<p>Let&#8217;s look at the syntax of the NumPy square function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.square(input)\n<\/pre><\/div>\n\n\n<p>Here, the input can be a single number, a NumPy array of numbers as well as a complex number. Let&#8217;s write code to understand the function better.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working with NumPy square<\/h2>\n\n\n\n<p>Let&#8217;s now start working on the Numpy.square() method to understand it through examples. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy square of a single number<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nprint(&quot;Square of 5 is :&quot;,np.square(-5))\nprint(&quot;Square of 2.5 is :&quot;,np.square(2.5))\nprint(&quot;Square of 10 is :&quot;,np.square(10))\nprint(&quot;Square of 13.345 is :&quot;,np.square(13.345))\nprint(&quot;Square of -19.93 is :&quot;,np.square(-19.93))\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=\"\">\nSquare of 5 is : 25\nSquare of 2.5 is : 6.25\nSquare of 10 is : 100\nSquare of 13.345 is : 178.08902500000002\nSquare of -19.93 is : 397.2049\n<\/pre><\/div>\n\n\n<p>The above outputs are pretty obvious. In the last example, we have passed a negative number as input to the function and the output is a positive number. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy square of a NumPy array of numbers<\/h3>\n\n\n\n<p>Let&#8217;s look at some examples where we will use the NumPy array with the function. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport numpy as np \n\n# NumPy Array of Integral Values\na = np.array((3 , -4 , 5 , 12 , 499))\n\nprint(&quot;Input Array:\\n&quot;,a)\nprint(&quot;Square Values:\\n&quot;,np.square(a))\n\n# NumPy Array of Floating Point Values\nb = np.array((0.43 , -2.5 , 9.23 , 4.57 , 6.5))\n\nprint(&quot;Input Array:\\n&quot;,b)\nprint(&quot;Square Values:\\n&quot;,np.square(b))\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=\"\">\nInput Array:\n &#x5B;  3  -4   5  12 499]\nSquare Values:\n &#x5B;     9     16     25    144 249001]\nInput Array:\n &#x5B; 0.43 -2.5   9.23  4.57  6.5 ]\nSquare Values:\n &#x5B; 0.1849  6.25   85.1929 20.8849 42.25  ]\n<\/pre><\/div>\n\n\n<p>There is one task for you all, you have to use the NumPy square function with a 2-D NumPy array and observe the output. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy square with Complex Numbers<\/h3>\n\n\n\n<p>Now, let&#8217;s use the NumPy square function with Complex Numbers.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nprint(&quot;The square of 4+5j is:&quot;,np.square(4+5j))\n\nprint(&quot;The square of -1+0.5j is:&quot;,np.square(-1+0.5j))\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=\"\">\nThe square of 4+5j is: (-9+40j)\nThe square of -1+0.5j is: (0.75-1j)\n<\/pre><\/div>\n\n\n<p>The mathematics behind the calculation of the squares of the Complex Number is really interesting. It uses some basic formulae and simple identities of Complex Numbers. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Graphical Representation of NumPy square<\/h2>\n\n\n\n<p>Now, let&#8217;s plot the NumPy square curve using Matplotlib Library.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nimport matplotlib.pyplot as plt\n\na = np.linspace(-10 , 10 , 30)\n\nb = np.square(a)\n\nplt.plot(a , b , color = &quot;green&quot; , marker = &quot;o&quot;)\nplt.title(&quot;numpy.square()&quot;)\nplt.xlabel(&quot;X&quot;)\nplt.ylabel(&quot;Y&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/NumPy-Square-Plot.png\" alt=\"NumPy Square Plot\" class=\"wp-image-37498\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/NumPy-Square-Plot.png 640w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/NumPy-Square-Plot-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\"><strong>NumPy Square Plot<\/strong><\/figcaption><\/figure>\n\n\n\n<p>There you go, you have plotted the curve of NumPy square. With that, we have completed the NumPy square function, do perform the task given in the article, and practice codes along with reading the article. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.square.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy Documentation &#8211; NumPy square<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In this tutorial, we will be learning about the NumPy square function. This is a simple function to understand and easy to use. So, let&#8217;s get started. What is NumPy square? NumPy square is one of the Mathematical Functions of the NumPy Library that calculates the square value of the input number. Yes, [&hellip;]<\/p>\n","protected":false},"author":48,"featured_media":37500,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37484","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\/37484","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=37484"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37484\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37500"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}