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

NumPy exp() Function



The numpy.exp() function is used to calculate the exponential of all elements in an input array. It calculates ex, where "e" is Euler's number (approximately 2.71828), for each element "x" in the array.

This function can be applied to scalars, lists, or NumPy arrays and will return an array of the same shape with the exponential of each input value.

Syntax

Following is the syntax of the NumPy exp() function −

numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

This function accepts the following parameters −

  • x: The input array for which to compute the exponential.
  • out (optional): A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Otherwise, it will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are computed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array with the exponential of each element in the input array x. If out is provided, it returns a reference to out.

Example: Basic Usage of exp() Function

In the following example, we create a 1-dimensional array and use the exp() function to compute the exponential of each element −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([1, 2, 3, 4])

# Computing the exponential of each element
result = np.exp(arr)
print(result)

Following is the output obtained −

[ 2.71828183  7.3890561  20.08553692 54.59815003]

Example: exp() Function with Negative Numbers

In this example, we create a 1-dimensional array of negative numbers and use the exp() function to compute their exponentials −

import numpy as np

# Creating a 1-dimensional array of negative numbers
arr = np.array([-1, -2, -3, -4])

# Computing the exponential of each element
result = np.exp(arr)
print(result)

This will produce the following result −

[0.36787944 0.13533528 0.04978707 0.01831564]

Example: exp() Function with Zero

In this example, we create a 1-dimensional array with zero and use the exp() function to demonstrate that the exponential of zero is 1 −

import numpy as np

# Creating a 1-dimensional array with zero
arr = np.array([0])

# Computing the exponential of zero
result = np.exp(arr)
print(result)

Following is the output obtained −

[1.]

Example: exp() Function with Complex Numbers

In this example, we create a 1-dimensional array of complex numbers and use the exp() function to compute their exponentials −

import numpy as np

# Creating a 1-dimensional array of complex numbers
arr = np.array([1+2j, 3+4j, -5-6j])

# Computing the exponential of each element
result = np.exp(arr)
print(result)

This will produce the following result −

[-1.13120438e+00+2.47172667e+00j -1.31287831e+01-1.52007845e+01j6.46957650e-03+1.88268682e-03j]
numpy_arithmetic_operations.htm
Advertisements