NumPy provides several functions for performing operations involving exponential and logarithmic calculations, power operations, and square root calculations on arrays. Here are some commonly used functions in NumPy:
Exponential and Logarithmic Functions:
Exponential Function:
np.exp(x)
: Computes the exponential value of each element in arrayx
.
Natural Logarithm:
np.log(x)
: Computes the natural logarithm (base e) of each element in arrayx
.
Base-10 Logarithm:
np.log10(x)
: Computes the base-10 logarithm of each element in arrayx
.
Base-2 Logarithm:
np.log2(x)
: Computes the base-2 logarithm of each element in arrayx
.
Power and Square Root Functions:
Power Function:
np.power(x, y)
: Computes the exponentiation of elements in arrayx
to the corresponding power in arrayy
.
Square Root:
np.sqrt(x)
: Computes the square root of each element in arrayx
.
Here’s an example demonstrating the usage of these functions:
import numpy as np x = np.array([1, 2, 3]) # Exponential Function exp_values = np.exp(x) print(exp_values) # [ 2.71828183 7.3890561 20.08553692] # Natural Logarithm log_values = np.log(x) print(log_values) # [0. 0.69314718 1.09861229] # Base-10 Logarithm log10_values = np.log10(x) print(log10_values) # [0. 0.30103 0.47712125] # Base-2 Logarithm log2_values = np.log2(x) print(log2_values) # [0. 1. 1.5849625] # Power Function power_values = np.power(x, 2) print(power_values) # [1 4 9] # Square Root sqrt_values = np.sqrt(x) print(sqrt_values) # [1. 1.41421356 1.73205081]
Run Code In Live & Test
Conclusion:
These functions provide a convenient way to perform exponential and logarithmic calculations, power operations, and square root calculations on arrays in NumPy.