Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Exponential Functions



NumPy Exponential Functions

In NumPy, exponential functions are provided to calculate powers of Euler's number (e), and to perform operations involving exponential growth or decay. NumPy provides the numpy.exp() function to calculate exponentials.

In this tutorial, we will explore how to use NumPy's exponential functions to calculate powers of e, and perform other related operations.

The numpy.exp() Function

The numpy.exp() function calculates the exponential of all elements in the input array. The function calculates the value of ex, where e is Euler's number (approximately 2.71828), and x is the exponent.

It is commonly used in applications that involve continuous growth or decay, such as calculating compound interest or solving differential equations.

Example: Exponential Function

In the following example, we calculate the exponential of an array of values using the exp() function −

import numpy as np

# Define an array of exponents
exponents = np.array([0, 1, 2, 3])

# Calculate the exponential of each element
exp_values = np.exp(exponents)

print("Exponential values:", exp_values)

The output will be −

Exponential values: [ 1.          2.71828183  7.3890561  20.08553692]
Note that the exponential of 0 is always 1, and as the exponent increases, the value grows exponentially.

Natural Logarithm Using log() Function

The numpy.log() function calculates the natural logarithm (base e) of all elements in the input array. It is the inverse of the numpy.exp() function. In mathematics, the natural logarithm of a number is the exponent to which e must be raised to obtain that number.

The numpy.log() function is useful for solving equations that involve exponential growth or decay.

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. We get the following output −

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

Base-10 Logarithm Using log10() Function

In addition to the natural logarithm, NumPy provides the numpy.log10() function to compute the base-10 logarithm of each element in the input array.

This function is commonly used in scientific fields that use logarithms with a base of 10, such as in sound intensity or earthquake magnitude calculations.

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. We get the following output −

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

Exponential Function with Base-2

In some cases, it is useful to calculate logarithms with base-2, especially in areas like computer science and information theory. NumPy provides the numpy.log2() function for this purpose.

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.]

Exponential Growth and Decay

Exponential functions can model both growth and decay. In growth, the quantity increases over time, such as the growth of a population or investment. In decay, the quantity decreases over time, such as the decay of a radioactive substance.

Exponential growth is typically modeled by the function y = y0 * ekt, where y0 is the initial value, k is the growth rate, and t is time. In decay, the function is y = y0 * e-kt.

Example: Exponential Growth

In the following example, we simulate exponential growth by using NumPy's exp() function. We assume an initial population size of 10, a growth rate of 0.1, and a time period of 10 units −

import numpy as np
import matplotlib.pyplot as plt

# Define initial parameters
# initial population size
y0 = 10  
# growth rate
k = 0.1  
# time array
t = np.linspace(0, 10, 100)  

# Calculate population size at each time point
population = y0 * np.exp(k * t)

# Plot the result
plt.plot(t, population)
plt.title("Exponential Growth")
plt.xlabel("Time")
plt.ylabel("Population Size")
plt.grid(True)
plt.show()

This code generates a plot showing the exponential growth of the population over time −

Exponential Function
Advertisements