# Compute the logarithm base n with scimath in Python

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

If x contains negative inputs, the answer is computed and returned in the complex domain. The 1st parameter, n is the integer base(s) in which the log is taken. The 2nd parameter, x is the value(s) whose log base n 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([-4, -8, 8])


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 n with scimath, use the scimath.logn() method in Python Numpy. The method returns the log base n of the x value(s). If x was a scalar, so is out, otherwise an array is returned −

print("\nResult (logn)...\n",np.emath.logn(2, arr))

## Example

import numpy as np

# Creating a numpy array using the array() method
arr = np.array([-4, -8, 8])

# 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 n with scimath, use the scimath.logn() method in Python Numpy
# The method returns the log base n of the x value(s). If x was a scalar, so is out, otherwise an array is returned.
print("\nResult (logn)...\n",np.emath.logn(2, arr))

## Output

Our Array...
[-4 -8 8]

Dimensions of our Array...
1

Datatype of our Array object...
int64

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

Result (logn)...
[2.+4.53236014j 3.+4.53236014j 3.+0.j ]

Updated on: 01-Mar-2022

283 Views