{"id":38987,"date":"2022-12-29T13:59:43","date_gmt":"2022-12-29T13:59:43","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38987"},"modified":"2023-01-28T15:08:58","modified_gmt":"2023-01-28T15:08:58","slug":"numpy-exmp1","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-exmp1","title":{"rendered":"Numpy Exmp1: A Complete Guide"},"content":{"rendered":"\n<p>Euler&#8217;s number, also known as &#8216;e&#8217;, is an important mathematical constant that was named after the 18th century mathematician, Leonhard Euler. This number has been estimated to have over a trillion digits of accuracy and is an important tool in mathematical calculations. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>2.718281828459045\u2026\u2026<\/p>\n<cite>Euler&#8217;s number<\/cite><\/blockquote>\n\n\n\n<p>The dots at the end indicate that the number of digits goes on &#038; on which provides the inference that <em>\u2018e\u2019 <\/em>falls into the category of irrational numbers. We shall get things started by first importing the <em>numpy <\/em>library using the following code.<\/p>\n\n\n\n<p>In this article, we will explore the history of Euler&#8217;s number and how it is used in the Numpy Exmp1() function.<\/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>Thereafter, we shall explore further the&nbsp;<em>expm1( )&nbsp;<\/em>function through each of the following sections.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Syntax of&nbsp;the<em> expm1( )&nbsp;<\/em>function<\/strong><\/li>\n\n\n\n<li><strong>Why use <em>expm1(x) <\/em>instead of <em>exp(x)-1<\/em>?<\/strong><\/li>\n\n\n\n<li><strong>Using <em>expm1( ) <\/em>on Scalars<\/strong><\/li>\n\n\n\n<li><strong>Using <em>expm1( ) <\/em>on N-Dimensional Arrays<\/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 expm1( )\u00a0function<\/h2>\n\n\n\n<p>The functionality of the <em>expm1( )<\/em> is similar to that of the <em>exp( )<\/em> function where \u2018<em>e\u2019 <\/em>is raised to the power of a given number followed by subtracting one (1) from it. Following is the syntax of the <em>expm1( ) <\/em>function which contains both the mandatory and the optional inputs required for its functioning.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.expm1(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>N-dimensional array or scalar which is to be raised to the power of <em>e<\/em><\/li>\n\n\n\n<li><strong><em>out \u2013<\/em><\/strong> an optional construct set to <em>none <\/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>* <\/em>\u2013 <\/strong>kwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function<\/li>\n\n\n\n<li><strong><em>where \u2013 <\/em><\/strong>an optional construct that is used to calculate the universal function (ufunc) at the given position when set to <em>True <\/em>(default setting) or not calculated when set to <em>False<\/em><\/li>\n\n\n\n<li><strong><em>dtype \u2013 <\/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\">Why use expm1(x) instead of exp(x)-1?<\/h2>\n\n\n\n<p>You may wonder what is the need to build a specific function for removing one from an exponential, rather than use the already available <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-exp\" data-type=\"post\" data-id=\"37466\">function <em>exp( )<\/em><\/a><em> <\/em>&#038; remove one from it! (i.e) <em>exp(x)-1. <\/em><\/p>\n\n\n\n<p>Let\u2019s do just that and find out whether there are any observable differences.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = 1.5 \nnp.exp(x)-1 \nnp.expm1(x)\n<\/pre><\/div>\n\n\n<p>Following are the results of the above code.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"208\" height=\"101\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Results-when-x1.5.jpg\" alt=\"Results When X1 5\" class=\"wp-image-39028\"\/><figcaption class=\"wp-element-caption\">Results when x=1.5<\/figcaption><\/figure>\n\n\n\n<p>Well, it seems that all is well and both results are synonymous. But, what if we reduce <em>\u2018x\u2019 <\/em>a bit and deploy the same functions? Will those be able to return the same results? Let\u2019s find out!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = 0.0001\nnp.exp(x)-1\nnp.expm1(x)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"249\" height=\"79\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Results-when-x0.0001.jpg\" alt=\"Results When X0 0001\" class=\"wp-image-39032\"\/><figcaption class=\"wp-element-caption\">Results when x=0.0001<\/figcaption><\/figure>\n\n\n\n<p>The results aren\u2019t the same now, are they? Buha haa haa haa! <em>(Sinister laughter)<\/em> <\/p>\n\n\n\n<p>The main reason behind this is that the exponential of any number close to \u20180\u2019 will be very near to \u20181\u2019. Thus, the <em>exp(x)-1<\/em> seems to be not that accurate in returning the results and a dedicated function <em>expm1( ) <\/em>is made available within the <em>numpy <\/em>library of Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using expm1( ) on Scalars<\/h2>\n\n\n\n<p>You can also find the outcome by assigning a negative number to the <em>expm1( ) <\/em>function. This would in turn return the equivalent of <em>exp(x), <\/em>reciprocate it &#038; then remove \u20181\u2019 from it. For those who would like to jump directly to the results, here they are!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = -10\nb = 0.000000009\nnp.expm1(a)\nnp.expm1(b)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"282\" height=\"121\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-expm1-on-Scalars.jpg\" alt=\"Using Expm1 On Scalars\" class=\"wp-image-39034\"\/><figcaption class=\"wp-element-caption\">Using <em>expm1<\/em>( ) on Scalars<\/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 expm1( ) on N-Dimensional Arrays<\/h2>\n\n\n\n<p>The <em>expm1( ) <\/em>function also holds good when applied to an N-dimensional array as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = np.array(&#x5B;&#x5B;1, 8, 0.09],\n                &#x5B;5.007, -3.303, 2]])\nnp.expm1(ar1)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"96\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-expm1-on-N-Dimensional-Arrays.jpg\" alt=\"Using Expm1 On N Dimensional Arrays\" class=\"wp-image-39037\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-expm1-on-N-Dimensional-Arrays.jpg 548w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-expm1-on-N-Dimensional-Arrays-300x53.jpg 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><figcaption class=\"wp-element-caption\">Using <em>expm1<\/em>( ) on N-Dimensional Arrays<\/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 use the&nbsp;<em>expm1( )&nbsp;<\/em>function from the&nbsp;<em>numpy&nbsp;<\/em>library. Here\u2019s another article that details the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-nextafter\" target=\"_blank\" type=\"URL\" id=\"https:\/\/www.askpython.com\/python\/how-to-use-numpy-nextafter-in-python\" rel=\"noreferrer noopener\">usage of <em>nextafter( ) <\/em>function from the&nbsp;<em>numpy&nbsp;<\/em>library<\/a> in Python. There are numerous other enjoyable and equally informative articles in <a href=\"https:\/\/www.askpython.com\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/\" rel=\"noreferrer noopener\">AskPython<\/a> that might be of great help to those who are looking to level up in Python.&nbsp;<em>Mazel tov<\/em>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Euler&#8217;s number, also known as &#8216;e&#8217;, is an important mathematical constant that was named after the 18th century mathematician, Leonhard Euler. This number has been estimated to have over a trillion digits of accuracy and is an important tool in mathematical calculations. 2.718281828459045\u2026\u2026 Euler&#8217;s number The dots at the end indicate that the number of [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":38989,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38987","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\/38987","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=38987"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38987\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38989"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}