Compute the logarithm base n with scimath in Python
In this article, we will be looking at the approach to computing the logarithm base n with scimath in Python.
The NumPy package provides us the numpy.lib.scimath.logn() method to compute the logarithmic base n. the method takes log base n of x. Let’s check the syntax to know the method better. The solution is computed and returned in the complex domain if x contains negative inputs.
syntax: lib.scimath.logn(n, x)
parameters:
- n: array like object. The base(s) in which the log is computed.
- x: array like object. The value(s) with the log base n must be present.
Result:
scalar or an array. if scalar is the input, output will be a scalar, if array is the input output will be an array.
Example 1:
In this example, the NumPy package is imported. An array is created using numpy.array() method and numpy.lib.scimath.logn() is used to compute the log of the given array by specifying an integer as n. The shape, datatype, and dimensions of the array can be found by .shape , .dtype, and .ndim attributes.
Python3
import numpy as np
array = np.array([ 1 , 2 , 3 ])
print (array)
print ( "Shape of the array is : " ,array.shape)
print ( "The dimension of the array is : " ,array.ndim)
print ( "Datatype of our Array is : " ,array.dtype)
print (np.lib.scimath.logn( 2 , array))
|
Output:
[1 2 3]
Shape of the array is : (3,)
The dimension of the array is : 1
Datatype of our Array is : int64
[0. 1. 1.5849625]
Example 2:
In this example, instead of passing an array, we can also pass scalar values. In the below example log2(100) is computed.
Python3
import numpy as np
n = 100
print (np.lib.scimath.logn( 2 , n))
|
Output:
6.643856189774725
Last Updated :
21 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...