Exponential and Logarithmic | Power and Square Root Functions | Numpy Array Operations with Example

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 array x.

Natural Logarithm:

  • np.log(x): Computes the natural logarithm (base e) of each element in array x.

Base-10 Logarithm:

  • np.log10(x): Computes the base-10 logarithm of each element in array x.

Base-2 Logarithm:

  • np.log2(x): Computes the base-2 logarithm of each element in array x.

Power and Square Root Functions:

Power Function:

  • np.power(x, y): Computes the exponentiation of elements in array x to the corresponding power in array y.

Square Root:

  • np.sqrt(x): Computes the square root of each element in array x.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *