Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

NumPy - Logarithmic Functions



NumPy Logarithmic Functions

logarithmic functions are the inverse of exponential functions. They are used to determine the power to which a number (called the base) must be raised to produce a given value. The most common logarithmic functions are the natural logarithm (ln), with base 10.

In NumPy, several logarithmic functions are available for various types of logarithms. These functions help calculate natural, base-10, and base-2 logarithms, which are useful in solving mathematical equations involving logarithmic relationships.

Natural Logarithm Using log() Function

The numpy.log() function calculates the natural logarithm (base e) of all elements in the input array. The natural logarithm is commonly used in calculus and other mathematical computations involving continuous growth or decay.

In mathematics, the natural logarithm of a number is the exponent to which the base e (Euler's number, approximately 2.71828) must be raised to obtain that number.

Example: Natural Logarithm

In the following example, we calculate the natural logarithm of an array of values using NumPy's log() function −

import numpy as np

# Define an array of values
values = np.array([1, np.e, np.e**2, np.e**3])

# Calculate the natural logarithm of each element
log_values = np.log(values)

print("Natural Logarithm values:", log_values)

As expected, the natural logarithm of ex is simply x. The output will be −

Natural Logarithm values: [0. 1. 2. 3.]

Base-10 Logarithm Using log10() Function

The numpy.log10() function calculates the base-10 logarithm of all elements in the input array.

The base-10 logarithm, also known as the common logarithm, is the inverse of the exponential function with base 10. It is widely used in fields that deal with large or small numbers, like sound intensity or the Richter scale for earthquake magnitudes.

Example: Base-10 Logarithm

In the following example, we calculate the base-10 logarithm of an array of values using NumPy's log10() function −

import numpy as np

# Define an array of values
values = np.array([1, 10, 100, 1000])

# Calculate the base-10 logarithm of each element
log10_values = np.log10(values)

print("Base-10 Logarithm values:", log10_values)

As expected, the base-10 logarithm of powers of 10 follows the rule log10(10x) = x. The output is −

Base-10 Logarithm values: [0. 1. 2. 3.]

Base-2 Logarithm Using log2() Function

The numpy.log2() function calculates the base-2 logarithm of each element in the input array. Base-2 logarithms are used in various fields, including computer science, information theory, and coding theory, as binary systems and algorithms often rely on base-2 operations.

In computer science, the base-2 logarithm of a number is the number of times you can divide the number by 2 until you get 1. This function is often used in applications that involve binary data or computational complexity.

Example: Base-2 Logarithm

In the following example, we calculate the base-2 logarithm of an array of values using NumPy's log2() function −

import numpy as np

# Define an array of values
values = np.array([1, 2, 4, 8])

# Calculate the base-2 logarithm of each element
log2_values = np.log2(values)

print("Base-2 Logarithm values:", log2_values)

As expected, the base-2 logarithm of powers of 2 follows the rule log2(2x) = x. The output is −

Base-2 Logarithm values: [0. 1. 2. 3.]

Logarithm with a Custom Base

Although NumPy doesn't provide a direct function for logarithms with arbitrary bases, you can compute the logarithm with a custom base by using the change of base formula −

logbase(x) = loge(x) / loge(base)

This allows you to compute logarithms with any base by dividing the natural logarithm of the number by the natural logarithm of the base.

Example: Custom Base Logarithm

In the following example, we compute the logarithm of an array of values with a custom base (e.g., base 3) using the change of base formula −

import numpy as np

# Define an array of values and the custom base
values = np.array([1, 3, 9, 27])
base = 3

# Calculate the logarithm with the custom base using the change of base formula
log_base3_values = np.log(values) / np.log(base)

print("Logarithm with base 3 values:", log_base3_values)

As expected, the logarithm of powers of 3 with base 3 follows the rule log3(3x) = x

Logarithm with base 3 values: [0. 1. 2. 3.]

Handling Logarithms of Zero or Negative Numbers

Logarithms of zero or negative numbers are undefined in real numbers. If you try to compute the logarithm of 0 or a negative number using NumPy, it will return nan (Not a Number) or -inf (negative infinity) depending on the context.

To avoid errors, it is often helpful to use the numpy.errstate() function to handle such cases gracefully, suppressing warnings or handling invalid operations explicitly.

Example: Handling Invalid Logarithms

In the following example, we attempt to calculate the logarithms of 0 and negative values −

import numpy as np

# Define an array with a zero and a negative value
values = np.array([0, -1, 1, 10])

# Calculate the natural logarithm of each element
log_values = np.log(values)

print("Logarithm values:", log_values)

The output shows that the logarithm of 0 results in nan, and the logarithm of a negative number results in -inf

Logarithm values: [ nan -inf   0.   2.30258509]
Advertisements