Compute the natural logarithm with scimath in Python

PythonNumpyServer Side ProgrammingProgramming

<p>To compute the natural logarithm with scimath, use the np.emath.log() method in Python Numpy. The method returns the log of the x value(s). If x was a scalar, so is out, otherwise an array is returned. The 1st parameter, x is the value(s) whose log is (are) required.</p><h2>Steps</h2><p>At first, import the required libraries &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">import numpy as np</pre><p>Creating a numpy array using the array() method &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">arr = np.array([np.inf, -np.inf, np.exp(1), -np.exp(1)]) </pre><p>Display the array &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">print(&quot;Our Array... &quot;,arr)</pre><p>Check the Dimensions &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">print(&quot; Dimensions of our Array... &quot;,arr.ndim) </pre><p>Get the Datatype &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">print(&quot; Datatype of our Array object... &quot;,arr.dtype)</pre><p>Get the Shape &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">print(&quot; Shape of our Array object... &quot;,arr.shape) </pre><p>To compute the natural logarithm with scimath, use the np.emath.log() method in Python Numpy &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">print(&quot; Result (log)... &quot;,np.emath.log(arr))</pre><h2>Example</h2><pre class="demo-code notranslate language-numpy" data-lang="numpy">import numpy as np # Creating a numpy array using the array() method arr = np.array([np.inf, -np.inf, np.exp(1), -np.exp(1)]) # Display the array print(&quot;Our Array... &quot;,arr) # Check the Dimensions print(&quot; Dimensions of our Array... &quot;,arr.ndim) # Get the Datatype print(&quot; Datatype of our Array object... &quot;,arr.dtype) # Get the Shape print(&quot; Shape of our Array object... &quot;,arr.shape) # To compute the natural logarithm with scimath, use the np.emath.log() method in Python Numpy. print(&quot; Result (log)... &quot;,np.emath.log(arr))</pre><h2>Output</h2><pre class="result notranslate">Our Array... [ inf -inf 2.71828183 -2.71828183] Dimensions of our Array... 1 Datatype of our Array object... float64 Shape of our Array object... (4,) Result (log)... [inf+0.j inf+3.14159265j 1.+0.j 1.+3.14159265j]</pre>
raja
Updated on 01-Mar-2022 07:46:37

Advertisements