w3resource

NumPy: Calculate exp(x) - 1 for all elements in a given array

NumPy Mathematics: Exercise-32 with Solution

Write a NumPy program to calculate exp(x) - 1 for all elements in a given array

Sample Solution:-

Python Code:

import numpy as np
x = np.array([1., 2., 3., 4.], np.float32)
print("Original array: ")
print(x)
print("\nexp(x)-1 for all elements of the said array:")
r1 = np.expm1(x)
r2 = np.exp(x) - 1.
assert np.allclose(r1, r2)
print(r1)

Sample Output:

Original array: 
[1. 2. 3. 4.]

exp(x)-1 for all elements of the said array:
[ 1.7182817  6.389056  19.085537  53.59815  ]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to compute ex, element-wise of a given array.

Next: Write a NumPy program to calculate 2p for all elements in a given array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Getting rid of unwanted characters:

You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only.

str="+++Welcome python+++"
str.strip()

print(str)

Output:

+++Welcome python+++