{"id":38465,"date":"2023-01-10T17:13:02","date_gmt":"2023-01-10T17:13:02","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38465"},"modified":"2023-02-16T19:56:25","modified_gmt":"2023-02-16T19:56:25","slug":"numpy-real_if_close","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-real_if_close","title":{"rendered":"Numpy real_if_close &#8211; If the input is complex with all imaginary parts close to zero, return real parts"},"content":{"rendered":"\n<p>In this article, we implement the NumPy real_if_close function of the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-module\" data-type=\"post\" data-id=\"7694\">NumPy package in python<\/a>. Numpy consists of functions that can be utilized to perform operations on an array, numpy real_if_close returns the real part of a complex imaginary input. Below understand the syntax of NumPy real_if_close  and implements a few of its examples. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy real_if_close?<\/h2>\n\n\n\n<p>If the input is complex with all imaginary parts close to zero, we use <code>numpy.real_if_close<\/code> to return real parts of the input. &#8220;Close to zero&#8221; is defined as tol*(machine epsilon of the type for a). The return type is float if a has complex elements where a is the input array.<\/p>\n\n\n\n<p>Note:<strong>&nbsp;Machine epsilons&nbsp;<\/strong>represent the smallest positive float value greater than zero.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.real_if_close(a, tol=)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Parameters<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Parameter<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Required\/Optional<\/strong><\/td><\/tr><tr><td>a<\/td><td>input array <\/td><td>Required <\/td><\/tr><tr><td>tol<\/td><td>tolerance in machine epsilons for the complex part of the input array elements<\/td><td>Required <\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Numpy real_if_close parameters<\/figcaption><\/figure>\n\n\n\n<p><strong>Return Value<\/strong>: If the input array has complex elements <code>real_if_close()<\/code> returns a float value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of NumPy real_if_close<\/h2>\n\n\n\n<p><strong>Start by importing numpy<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr = np.array(&#x5B;2.1 + 4e-14j, 5.2 + 3e-15j,10.4 + 10e-14j,15.5 + 15e-15j])\n<\/pre><\/div>\n\n\n<p><strong>Displaying the details of the array <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;Input Array: \\n&quot;,arr)\nprint(&quot;\\nDimensions of input array: \\n&quot;,arr.ndim)\nprint(&quot;\\nDatatype of input array: \\n&quot;,arr.dtype)\nprint(&quot;\\nShape of input array: \\n&quot;,arr.shape)\n<\/pre><\/div>\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nInput Array:\n &#x5B; 2.1+4.0e-14j  5.2+3.0e-15j 10.4+1.0e-13j 15.5+1.5e-14j]\n\nDimensions of input array:\n 1\n\nDatatype of input array:\n complex128\n\nShape of input array:\n (4,)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 1: Sample testing of numpy real_if_close<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\n Result: \\n&quot;,np.real_if_close(arr, tol = 1000))\n<\/pre><\/div>\n\n\n<p>Here, the output will contain all the real parts of the elements of the array <code>arr<\/code> since all the imaginary parts are too small.<\/p>\n\n\n\n<p><strong>Output : <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nResult: \n&#x5B; 2.1 5.2 10.4 15.5]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 2: When the imaginary numbers are not close to zero.<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = np.array(&#x5B;1+5j,3-6j])\n\n# Displaying teh deatils of array\nprint(&quot;Input Array: \\n&quot;,arr)\n\nprint(&quot;\\nDimensions of input array: \\n&quot;,arr.ndim)\nprint(&quot;\\nDatatype of  input array: \\n&quot;,arr.dtype)\nprint(&quot;\\nShape of input array: \\n&quot;,arr.shape)\n\n# implementing numpy.real_if_close in Python\nprint(&quot;\\nResult...\\n&quot;,np.real_if_close(arr, tol = 1000))\n<\/pre><\/div>\n\n\n<p>Since here the imaginary parts are significantly large, <code>np.real_if_close()<\/code> returns back the original complex numbers.<\/p>\n\n\n\n<p><strong>Output : <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nInput Array:\n &#x5B;1.+5.j 3.-6.j]\n\nDimensions of input array:\n 1\n\nDatatype of input array:\n complex128\n\nShape of input array:\n (2,)\n\nResult...\n &#x5B;1.+5.j 3.-6.j]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we came across the theoretical and practical implementation of Numpy real_if_close(), a function of NumPy in Python. This function helps to return real parts of input with data type float. Given the input is complex with all imaginary parts close to zero.<\/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.real_if_close.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.real_if_close.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we implement the NumPy real_if_close function of the NumPy package in python. Numpy consists of functions that can be utilized to perform operations on an array, numpy real_if_close returns the real part of a complex imaginary input. Below understand the syntax of NumPy real_if_close and implements a few of its examples. What [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":39293,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38465","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\/38465","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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38465"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38465\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/39293"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}