Compute the Natural Logarithm in Python

PythonServer Side ProgrammingProgramming

The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e. The method returns the natural logarithm of x, elementwise. This is a scalar if x is a scalar. The 1st parameter is the input value, array-like. The 2nd parameter is out, a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

Logarithm is a multivalued function − for each x there is an infinite number of z such that exp(z) = x. The convention is to return the z whose imaginary part lies in [-pi, pi]. For real-valued input data types, log always returns real output. For each value that cannot be expressed as a real number or infinity, it yields nan and sets the invalid floating point error flag. For complex-valued input, log is a complex analytical function that has a branch cut [-inf, 0] and is continuous from above on it. log handles the floating-point negative zero as an infinitesimal negative number, conforming to the C99 standard.

Steps

At first, import the required library −

import numpy as np

Creating a numpy array using the array() method −

arr = np.array([1, 2, np.e, np.e**2])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array...\n",arr.shape)

The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e −

print("\nResult (log)...\n",np.log(arr))

Example

import numpy as np

# Creating a numpy array using the array() method
arr = np.array([1, 2, np.e, np.e**2])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array...\n",arr.shape)

# The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e.
print("\nResult (log)...\n",np.log(arr))

Output

Our Array...
[1. 2. 2.71828183 7.3890561 ]

Dimensions of our Array...
1

Datatype of our Array object...
float64

Shape of our Array...
(4,)

Result (log)...
[0. 0.69314718 1. 2. ]
raja
Updated on 24-Feb-2022 07:28:53

Advertisements