Skip to main content
NumPy provides a comprehensive set of exponential and logarithmic functions that operate element-wise on arrays.

Exponential Functions

numpy.exp

numpy.exp(x, /, out=None, *, where=True, **kwargs)
Calculate the exponential of all elements in the input array. Parameters:
  • x : array_like - Input values.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • out : ndarray or scalar - Output array, element-wise exponential of x.
Mathematical Formula: y=exy = e^x where e2.718281e \approx 2.718281 is Euler’s number, the base of the natural logarithm. Notes:
  • For real input, exp(x) is always positive.
  • For complex arguments x=a+ibx = a + ib, we can write ex=eaeibe^x = e^a e^{ib}.
  • The first term eae^a is real, the second term eib=cos(b)+isin(b)e^{ib} = \cos(b) + i\sin(b) has magnitude 1 and periodic phase.
Examples:
import numpy as np

np.exp(1.0)
# 2.718281828459045

np.exp([1, 2, 3])
# array([ 2.71828183,  7.3890561 , 20.08553692])

# Plot magnitude and phase in complex plane
import matplotlib.pyplot as plt
x = np.linspace(-2*np.pi, 2*np.pi, 100)
xx = x + 1j * x[:, np.newaxis]  # a + ib over complex plane
out = np.exp(xx)

plt.subplot(121)
plt.imshow(np.abs(out), extent=[-2*np.pi, 2*np.pi, -2*np.pi, 2*np.pi], cmap='gray')
plt.title('Magnitude of exp(x)')

plt.subplot(122)
plt.imshow(np.angle(out), extent=[-2*np.pi, 2*np.pi, -2*np.pi, 2*np.pi], cmap='hsv')
plt.title('Phase (angle) of exp(x)')
plt.show()

numpy.expm1

numpy.expm1(x, /, out=None, *, where=True, **kwargs)
Calculate exp(x) - 1 for all elements in the array. Parameters:
  • x : array_like - Input values.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • out : ndarray or scalar - Element-wise exponential minus one: exp(x) - 1.
Mathematical Formula: y=ex1y = e^x - 1 Notes: This function provides greater precision than exp(x) - 1 for small values of x, where direct computation would suffer from catastrophic cancellation. Examples:
import numpy as np

# For small x, expm1(x) is more accurate than exp(x) - 1
x = 1e-10
np.expm1(x)
# 1.0000000000500001e-10

np.exp(x) - 1  # Less accurate due to floating point precision
# 1.000000082740371e-10

numpy.exp2

numpy.exp2(x, /, out=None, *, where=True, **kwargs)
Calculate 2**p for all p in the input array. Parameters:
  • x : array_like - Input values.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • out : ndarray or scalar - Element-wise 2 to the power x.
Mathematical Formula: y=2xy = 2^x Examples:
import numpy as np

np.exp2([0, 1, 2, 3, 4])
# array([  1.,   2.,   4.,   8.,  16.])

# Powers of 2
np.exp2(np.arange(10))
# array([   1.,    2.,    4.,    8.,   16.,   32.,   64.,  128.,  256., 512.])

Logarithmic Functions

numpy.log

numpy.log(x, /, out=None, *, where=True, **kwargs)
Natural logarithm, element-wise. Parameters:
  • x : array_like - Input values.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - The natural logarithm of x, element-wise.
Mathematical Formula: y=ln(x)=loge(x)y = \ln(x) = \log_e(x) The natural logarithm is the inverse of the exponential function: if y=exy = e^x, then x=ln(y)x = \ln(y). Notes:
  • Logarithm is a multivalued function. The convention is to return the real part in the range (,](-\infty, \infty].
  • For real-valued input, log returns real output for positive values and nan for negative values.
  • For complex-valued input, has a branch cut along the negative real axis.
Examples:
import numpy as np

np.log(np.e)
# 1.0

np.log([1, np.e, np.e**2])
# array([0., 1., 2.])

# Natural log of 10
np.log(10)
# 2.302585092994046

numpy.log10

numpy.log10(x, /, out=None, *, where=True, **kwargs)
Return the base 10 logarithm of the input array, element-wise. Parameters:
  • x : array_like - Input values.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - The logarithm base 10 of x, element-wise.
Mathematical Formula: y=log10(x)y = \log_{10}(x) Examples:
import numpy as np

np.log10([1e-2, 0.1, 1, 10, 100])
# array([-2., -1.,  0.,  1.,  2.])

np.log10(10)
# 1.0

numpy.log2

numpy.log2(x, /, out=None, *, where=True, **kwargs)
Base-2 logarithm of x. Parameters:
  • x : array_like - Input values.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - Base-2 logarithm of x.
Mathematical Formula: y=log2(x)y = \log_2(x) Examples:
import numpy as np

np.log2([1, 2, 4, 8, 16])
# array([0., 1., 2., 3., 4.])

# Equivalent to log(x) / log(2)
x = 1024
np.log2(x)
# 10.0

numpy.log1p

numpy.log1p(x, /, out=None, *, where=True, **kwargs)
Return the natural logarithm of one plus the input array, element-wise. Parameters:
  • x : array_like - Input values.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - Natural logarithm of 1 + x, element-wise.
Mathematical Formula: y=ln(1+x)y = \ln(1 + x) Notes: For small values of x, log1p(x) provides greater precision than log(1 + x), where direct computation would suffer from catastrophic cancellation. Examples:
import numpy as np

# For small x, log1p(x) is more accurate than log(1 + x)
x = 1e-10
np.log1p(x)
# 9.999999999500001e-11

np.log(1 + x)  # Less accurate
# 9.99999993922529e-11

# log1p is the inverse of expm1
x = 0.5
np.log1p(np.expm1(x))
# 0.5

Relationship Between Functions

The exponential and logarithmic functions are inverses of each other:
Inverse Relationships:
  • log(ex)=x\log(e^x) = x
  • elog(x)=xe^{\log(x)} = x (for x>0x > 0)
  • log10(10x)=x\log_{10}(10^x) = x
  • 2log2(x)=x2^{\log_2(x)} = x (for x>0x > 0)
Change of Base Formula: To convert between different logarithm bases: logb(x)=log(x)log(b)=ln(x)ln(b)\log_b(x) = \frac{\log(x)}{\log(b)} = \frac{\ln(x)}{\ln(b)} Examples:
import numpy as np

# Verify inverse relationship
x = 5.0
np.log(np.exp(x))
# 5.0

np.exp(np.log(x))
# 5.0

# Change of base: log_5(100)
base = 5
x = 100
np.log(x) / np.log(base)
# 2.8613531161467867

# Verify: 5^2.861... ≈ 100
np.power(base, 2.8613531161467867)
# 100.0

Special Exponential Functions

numpy.logaddexp

numpy.logaddexp(x1, x2, /, out=None, *, where=True, **kwargs)
Logarithm of the sum of exponentials of the inputs. Mathematical Formula: y=log(ex1+ex2)y = \log(e^{x_1} + e^{x_2}) Notes: This function is useful in machine learning and statistics for numerical stability when working with log probabilities.

numpy.logaddexp2

numpy.logaddexp2(x1, x2, /, out=None, *, where=True, **kwargs)
Logarithm of the sum of exponentials of inputs in base-2. Mathematical Formula: y=log2(2x1+2x2)y = \log_2(2^{x_1} + 2^{x_2})

See Also

Arithmetic Functions

Basic arithmetic operations including power

Trigonometric Functions

sin, cos, tan, and their inverses

Rounding Functions

floor, ceil, round, and truncation

Special Functions

sqrt, square, and other mathematical utilities

Build docs developers (and LLMs) love