Compute the logarithm base 2 with scimath in Python


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...\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 object...\n",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("\nResult (log2)...\n",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...\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 object...\n",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("\nResult (log2)...\n",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]

Updated on: 01-Mar-2022

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements