Saturday, November 16, 2024
Google search engine
HomeLanguagesCompute the logarithm base n with scimath in Python

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 packages
import numpy as np
  
# Creating an array
array = np.array([1,2,3])
print(array)
  
# shape of the array is
print("Shape of the array is : ",array.shape)
  
# dimension of the array
print("The dimension of the array is : ",array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
  
# computing logn for the given array
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 packages
import numpy as np
  
# Creating an scalar
n = 100
  
# computing logn for the given scalar
print(np.lib.scimath.logn(2, n))


Output:

6.643856189774725

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments