Compute the logarithm base 2 with scimath in Python

PythonNumpyServer Side ProgrammingProgramming

To compute the logarithm base 2 with scimath, use the np.emath.log2() method in Python Numpy. The method returns the log base 2 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 base 2 is (are) required.

Steps

At first, import the required libraries −

import numpy as np

Creating a numpy array using the array() method −

arr = np.array([np.inf, -np.inf, 16, np.exp(1), -np.exp(1), -32])

Display the array −

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

Check the Dimensions −

print("
Dimensions of our Array...
",arr.ndim)

Get the Datatype −

print("
Datatype of our Array object...
",arr.dtype)

Get the Shape −

print("
Shape of our Array object...
",arr.shape)

To compute the logarithm base 2 with scimath, use the np.emath.log2() method in Python Numpy. The method returns the log base 2 of the x value(s). If x was a scalar, so is out, otherwise an array is returned −

print("
Result (log2)...
",np.emath.log2(arr))

Example

import numpy as np

# Creating a numpy array using the array() method
arr = np.array([np.inf, -np.inf, 16, np.exp(1), -np.exp(1), -32])

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

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

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

# Get the Shape
print("
Shape of our Array object...
",arr.shape)

# To compute the logarithm base 2 with scimath, use the np.emath.log2() method in Python Numpy
# The method returns the log base 2 of the x value(s). If x was a scalar, so is out, otherwise an array is returned.
print("
Result (log2)...
",np.emath.log2(arr))

Output

Our Array...
[ inf -inf 16. 2.71828183 -2.71828183
-32. ]

Dimensions of our Array...
1

Datatype of our Array object...
float64

Shape of our Array object...
(6,)

Result (log2)...
[ inf+0.j inf+4.53236014j 4. +0.j
1.44269504+0.j 1.44269504+4.53236014j 5. +4.53236014j]
raja
Updated on 01-Mar-2022 07:47:55

Advertisements