NumPy – Using exp() and exp2() functions (4 examples)

Updated: February 25, 2024 By: Guest Contributor Post a comment

Overview

NumPy, a core library for numerical computations in Python, offers a plethora of functions designed to facilitate the manipulation and transformation of numeric data. Among these utilities, the exp() and exp2() functions are fundamental for exponential operations. This tutorial will navigate through their usage, illustrated with 4 progressively complex examples.

Purposes of NumPy’s exp() and exp2() Functions

Before diving into the examples, it’s important to understand what these functions do. exp(x) calculates e to the power of x, where e is the base of natural logarithms (approximately 2.71828). Similarly, exp2(x) computes 2 to the power of x. These functions are vectorized, meaning they can operate on arrays element-wise efficiently.

Example 1: Basic Usage of exp()

The first example demonstrates the basic application of the exp() function. Let’s calculate the exponent of an array of values.

import numpy as np

# Create an array
arr = np.array([0, 1, 2, 3])

# Apply exp() function
result = np.exp(arr)

# Output the result
print(result)

Output:

[ 1.          2.71828183  7.3890561  20.08553692]

This example illustrates how exp() transforms each element of the array, scaling them according to their exponent value with the base e.

Example 2: Comparing exp() and exp2()

Next, let’s compare the outputs of exp() and exp2() using the same array.

import numpy as np

arr = np.array([0, 1, 2, 3])

# Apply exp() and exp2() functions
exp_result = np.exp(arr)
exp2_result = np.exp2(arr)

# Display both results
print("exp() result:", exp_result)
print("exp2() result:", exp2_result)

Output:

exp() result: [ 1.          2.71828183  7.3890561  20.08553692]
exp2() result: [1. 2. 4. 8.]

The comparison clearly shows the difference in scaling between the two functions, emphasizing the base of the exponent.

Example 3: Advanced Mathematical Operations

Moving into more complex territory, this example demonstrates the integration of exp() and exp2() functions within a broader mathematical operation involving logarithms and array manipulation.

import numpy as np

# Create complex array
arr = np.linspace(1, 5, 5)

# Exponential and logarithmic operations
log_result = np.log(np.exp(arr))

# Simultaneous use of exp and exp2
combined_result = np.exp(arr) / np.exp2(arr)

# Display results
print("Logarithm after exp():", log_result)
print("Combination of exp() and exp2():", combined_result)

Output:

Logarithm after exp(): [1. 2. 3. 4. 5.]
Combination of exp() and exp2(): [0.5        1.35914091  2.74011222  4.65725016  7.21341853]

This exhibits the flexibility of combining these functions for sophisticated analytical tasks, showcasing the prowess of NumPy in handling mathematical computations efficiently.

Example 4: Plotting Exponential Curves

For our final example, we’ll visualize the effects of the exp() and exp2() functions by plotting their curves. Utilizing Matplotlib alongside NumPy, we can graphically represent these exponential transformations.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 4, 100)

# Calculate exp() and exp2() results
y_exp = np.exp(x)
y_exp2 = np.exp2(x)

# Plotting
plt.plot(x, y_exp, label='exp(x)')
plt.plot(x, y_exp2, label='exp2(x)')
plt.legend()
plt.title('Exponential Curves with NumPy')
plt.show()

This graphical representation vividly illustrates the growth patterns governed by the base of the exponent, providing intuitive understanding of these mathematical concepts.

Conclusion

The applications of exp() and exp2() in NumPy extend beyond simple exponentiation. Through a series of examples, we’ve explored their utility in basic mathematics, comparative analysis, complex operations, and data visualization. These tools are essential in the arsenal of anyone working in data science, physics, engineering, or any field that relies on numeric computation.