{"id":39260,"date":"2023-01-10T17:05:12","date_gmt":"2023-01-10T17:05:12","guid":{"rendered":"https:\/\/www.askpython.com\/?p=39260"},"modified":"2023-02-16T19:56:25","modified_gmt":"2023-02-16T19:56:25","slug":"numpy-ldexp","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-ldexp","title":{"rendered":"NumPy ldexp: A Complete Guide"},"content":{"rendered":"\n<p>Hello! In this tutorial, we are going learn about the <strong>ldexp <\/strong>method present in the NumPy module in Python. In our previous tutorials, we have learnt about the NumPy functions <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-exp\" target=\"_blank\" rel=\"noreferrer noopener\">exp()<\/a> and <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-exp2\" target=\"_blank\" rel=\"noreferrer noopener\">exp2()<\/a>. <\/p>\n\n\n\n<p>We know that, the <code>exp(x)<\/code> function returns <code>e^x<\/code>, where <em>e<\/em> is Euler&#8217;s constant having the value of approximately 2.718281 and the <code>exp2(x)<\/code> function calculates the value <code>2^x<\/code>.  <\/p>\n\n\n\n<p>Now, let us learn about the <code>ldexp()<\/code> function in NumPy.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy ldexp?<\/h2>\n\n\n\n<p>The <code>ldexp<\/code> function in NumPy takes two parameters <em>x1 <\/em>and <em>x2<\/em> and returns <em><strong>x1*2**x2<\/strong><\/em>, element-wise. Mathematically, we can represent it as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/ldexp-eqaution-1024x138.png\" alt=\"Ldexp Eqaution\" class=\"wp-image-39266\" width=\"-218\" height=\"-29\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/ldexp-eqaution-1024x138.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/ldexp-eqaution-300x41.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/ldexp-eqaution-768x104.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/ldexp-eqaution.png 1051w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Ldexp Equation<\/figcaption><\/figure>\n\n\n\n<p>Note that, in Python, the <strong>**<\/strong> operator has more precedence than the <strong>*<\/strong> operator. Hence, first the value <strong><em>2**x2<\/em><\/strong> is calculated and then it is multiplied by <strong><em>x1<\/em><\/strong> to compute the final result.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy ldexp<\/h2>\n\n\n\n<p>Let us have a look at the syntax of ldexp().<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.ldexp(x1, x2, \/, out=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None, subok=True&#x5B;, signature, extobj])\n<\/pre><\/div>\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>x1 <em>(array_like)<\/em><\/td><td>The array of multipliers.<\/td><td>Required<\/td><\/tr><tr><td>x2 <em>(array_like)<\/em><\/td><td>The array of exponents. <\/td><td>Required<\/td><\/tr><tr><td>out<\/td><td>An alternative output array in which to place the result. It must have the same shape as the expected output.<\/td><td>Optional<\/td><\/tr><tr><td>where<br><br><\/td><td>Takes an array-like object. At locations where it is True, the&nbsp;<code>out<\/code>&nbsp;array will be set to the&nbsp;<code>ufunc<\/code>&nbsp;result. Elsewhere, the&nbsp;<code>out<\/code>&nbsp;array will retain its original value.<\/td><td>Optional<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The shape of <em>x1<\/em> and <em>x2<\/em> should be equal.<\/p>\n\n\n\n<p><strong>Returns:<\/strong> <\/p>\n\n\n\n<p>An n-dimensional array storing the result of <em><strong>x1***x2<\/strong><\/em>. This is scalar if both <em><strong>x1<\/strong><\/em> and <strong><em>x2<\/em><\/strong> are scalars.<\/p>\n\n\n\n<p><strong>Note that<\/strong>, complex data types are not supported and if provided, will raise a <em>TypeError<\/em>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of NumPy ldexp<\/h2>\n\n\n\n<p>Now let us get right into the examples and understand how the function works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. When both the inputs are scalars<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# case 1\nx1_1 = 2\nx2_1 = 3\nans_1 = np.ldexp(x1_1, x2_1)\nprint(&quot;Case 1:\\nx1 = &quot;, x1_1, &quot;, x2 = &quot;, x2_1, &quot;, Result = &quot;, ans_1)\n\n# case 2\nx1_2 = -4\nx2_2 = 5\nans_2 = np.ldexp(x1_2, x2_2)\nprint(&quot;Case 2:\\nx1 = &quot;, x1_2, &quot;, x2 = &quot;, x2_2, &quot;, Result = &quot;, ans_2)\n\n# case 3\nx1_3 = 6.8\nx2_3 = 2\nans_3 = np.ldexp(x1_3, x2_3)\nprint(&quot;Case 3:\\nx1 = &quot;, x1_3, &quot;, x2 = &quot;, x2_3, &quot;, Result = &quot;, ans_3)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCase 1:\nx1 =  2 , x2 =  3 , Result =  16.0\nCase 2:\nx1 =  -4 , x2 =  5 , Result =  -128.0\nCase 3:\nx1 =  6.8 , x2 =  2 , Result =  27.2\n<\/pre><\/div>\n\n\n<p>The outputs are computed as follows:<\/p>\n\n\n\n<p><strong>Case 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n2**x2  = 2**3 = 8\nResult = x1*8 = 2*8 = 16\n<\/pre><\/div>\n\n\n<p><strong>Case 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n2**x2  = 2**5 = 32\nResult = x1*32 = (-4)*32 = -128\n<\/pre><\/div>\n\n\n<p><strong>Case 3:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n2**x2  = 2**2 = 4\nResult = x1*4 = 6.8*4 = 27.2\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. When both the inputs are arrays<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nlist_1 = &#x5B;-5, 2, 4, 6]\nlist_2 = &#x5B;4, 3, -2, 1]\n\nans = np.ldexp(list_1, list_2)\nprint(ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;-80.  16.   1.  12.]\n<\/pre><\/div>\n\n\n<p>The computation of the output is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0] = -5*2**4 = -5*16 = -80\nans&#x5B;1] = 2*2**3 = 2*8 = 16\nans&#x5B;2] = 4*2**-2 = 4*0.25 = 1\nans&#x5B;3] = 6*2**1 = 6*2 = 12\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. When one input is an array and the other is a  scalar<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nl = &#x5B;8, 12, 3, 10]\nx = 3\n\nans = np.ldexp(l, x)\nprint(ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;64. 96. 24. 80.]\n<\/pre><\/div>\n\n\n<p>Here, since the second argument <strong><em>x<\/em><\/strong> is a scalar, for each value in the answer, the element in the list is multiplied by <strong><em>2**x<\/em><\/strong> i.e. <strong><em>2**3 = 8.<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0] = 8*2**3 = 8*8 = 64\nans&#x5B;1] = 12*2**3 = 12*8 = 96\nans&#x5B;2] = 3*2**3 = 3*8 = 24\nans&#x5B;3] = 10*2**3 = 10*8 = 80\n<\/pre><\/div>\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>That\u2019s all! In this tutorial, we learned about the&nbsp;<strong>Numpy ldexp<\/strong>&nbsp;method and practiced different types of examples using the same. If you want to learn more about NumPy, feel free to go through our&nbsp;<a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy tutorials<\/a>.<\/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<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.ldexp.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy ldexp Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this tutorial, we are going learn about the ldexp method present in the NumPy module in Python. In our previous tutorials, we have learnt about the NumPy functions exp() and exp2(). We know that, the exp(x) function returns e^x, where e is Euler&#8217;s constant having the value of approximately 2.718281 and the exp2(x) [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":39272,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-39260","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\/39260","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\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=39260"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/39260\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/39272"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=39260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=39260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=39260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}