Compute the Natural logarithm for complex-valued input 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, element-wise. 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([0+1.j, 1, 2+0.j])

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. The method returns the natural logarithm of x, elementwise. This is a scalar if x is a scalar −

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

Example

import numpy as np

# Creating a numpy array using the array() method
arr = np.array([0+1.j, 1, 2+0.j])

# 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...
[0.+1.j 1.+0.j 2.+0.j]

Dimensions of our Array...
1

Datatype of our Array object...
complex128

Shape of our Array...
(3,)

Result (log)...
[0. +1.57079633j 0. +0.j 0.69314718+0.j ]
raja
Updated on 24-Feb-2022 10:11:04

Advertisements