How to Compute the logarithm Base 10 with NumPy scimath in Python?

The NumPy package includes numpy.lib.scimath.log10() function, which allows us to compute the logarithm base 10 with scimath in Python.

Syntax:

lib.scimath.log10(x)

This function returns the “primary value” of (see numpy.log10). If real x > 0, this is a real number (log10(0) = -inf, log10(np.inf) = inf). If none of the above requirements are met, the complicated principle value is returned.

Parameters
x: It is the array given as input.

Return Value:

This function returns the log base 10 of the x value. If x was a scalar, out is also a scalar; otherwise, an array object is returned.

Python Program to Compute the logarithm Base 10 with NumPy scimath

Approach:

  • Import numpy module using the import keyword
  • Pass some random list of numbers as an argument to the array() function of the numpy module to create an array.
  • Store it in a variable.
  • Print the given array.
  • Apply the shape attribute on the given array to get the shape of the given array and print the result
  • Apply ndim attribute on the given array to get the dimension of the given array and
    print the result
  • Apply dtype attribute on the given array to get the datatype of all the elements of the given array and print the result
  • Pass the given array as an argument to the log10() function of scimath of the numpy module to calculate the logarithm base 10 values of the given input array and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Pass some random list of numbers as an argument to the array() function
# of the numpy module to create an array.
# Store it in a variable
gvn_arry = np.array([5, 10, 15])

# Print the given array
print("The given array is:")
print(gvn_arry)

# Apply the shape attribute on the given array to get the shape of the given array and
# print the result
print("The Shape of the given array ",gvn_arry," = ", gvn_arry.shape)

# Apply ndim attribute on the given array to get the dimension of the given array and
# print the result
print("The dimension of the given array ",gvn_arry," = ",gvn_arry.ndim)

# Apply dtype attribute on the given array to get the datatype of all the elements of the
# given array and print the result
print("The datatype of the given array ",gvn_arry," = ",gvn_arry.dtype)
print()
# Pass the given array as an argument to the log10() function of scimath of the numpy module
# to calculate the logarithm base 10 values of the given input array and print it.
print("The logarithm base 10 values of the given input array:")
print(np.lib.scimath.log10(gvn_arry))

Output:

The given array is:
[ 5 10 15]
The Shape of the given array [ 5 10 15] = (3,)
The dimension of the given array [ 5 10 15] = 1
The datatype of the given array [ 5 10 15] = int64

The logarithm base 10 values of the given input array:
[0.69897 1. 1.17609126]

As previously stated, np.lib.scimath.log10() function returns

  • infinity – when np.inf is passed
  • -inf is returned – when 0 is passed
  • inf – when -inf is passed

Example

# Import numpy module using the import keyword
import numpy as np

# Calculate log10 values when np.inf is passed an an argument
print(np.lib.scimath.log10(np.inf))
# Passing -np.inf as an argument
print(np.lib.scimath.log10(-np.inf))
# Passing 0 as an argument
print(np.lib.scimath.log10(0))

Output:

inf
(inf+1.3643763538418412j)
-inf