np.exp() and np.log() in NumPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to run np.exp() and np.log() changes as the input size grows.
How does the cost of these functions grow when we apply them to bigger arrays?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.random.rand(n)
exp_arr = np.exp(arr)
log_arr = np.log(arr + 1e-10) # avoid log(0)
This code creates an array of size n, then applies the exponential and logarithm functions element-wise.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying np.exp() and np.log() to each element of the array.
- How many times: Once for each of the n elements in the array.
As the array size n grows, the number of operations grows roughly the same way, because each element needs one exponential and one logarithm calculation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (10 exp + 10 log) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The total work grows directly with n, doubling n doubles the work.
Time Complexity: O(n)
This means the time to run these functions grows linearly with the size of the input array.
[X] Wrong: "np.exp() and np.log() run in constant time no matter the input size."
[OK] Correct: These functions are applied to every element in the array, so the total time grows with the number of elements.
Knowing how vectorized functions like np.exp() and np.log() scale helps you understand performance when working with large datasets.
"What if we applied np.exp() only to a fixed-size subset of the array instead of the whole array? How would the time complexity change?"
Practice
np.exp(x) compute in NumPy?Solution
Step 1: Understand the purpose of np.exp()
The functionnp.exp()calculates e (Euler's number, approximately 2.718) raised to the power of the input value.Step 2: Compare with other options
Other options describe different functions: natural log isnp.log(), square root isnp.sqrt(), sine isnp.sin().Final Answer:
The value of e raised to the power x -> Option BQuick Check:
np.exp(x) = e^x [OK]
- Confusing np.exp() with np.log()
- Thinking np.exp() calculates logarithm
- Mixing up with square root or trigonometric functions
arr?Solution
Step 1: Identify the function for natural logarithm
The natural logarithm in NumPy is computed usingnp.log().Step 2: Check other options for correctness
np.exp()calculates exponentials,np.ln()does not exist, andnp.log10()calculates base-10 logarithm.Final Answer:
np.log(arr) -> Option AQuick Check:
Natural log = np.log() [OK]
- Using np.ln() which is not a valid NumPy function
- Confusing natural log with base-10 log
- Using np.exp() instead of np.log()
import numpy as np arr = np.array([1, 2, 3]) result = np.log(np.exp(arr)) print(result)
Solution
Step 1: Understand the inner function np.exp(arr)
Applyingnp.exp()to [1, 2, 3] gives [e^1, e^2, e^3] ≈ [2.718, 7.389, 20.086].Step 2: Apply np.log() to the result
Taking the natural log of these values returns the original array [1, 2, 3] because log and exp are inverse functions.Final Answer:
[1. 2. 3.] -> Option AQuick Check:
np.log(np.exp(x)) = x [OK]
- Expecting the exponential values instead of original
- Confusing output with zeros
- Thinking it causes an error
import numpy as np arr = np.array([-1, 0, 1]) result = np.log(arr) print(result)
Solution
Step 1: Check input values for np.log()
Natural logarithm is undefined for zero and negative numbers. The array contains -1 and 0, which cause errors or warnings.Step 2: Understand the error behavior
NumPy will return -inf or NaN for zero or negative inputs, which is usually an error or warning in calculations.Final Answer:
np.log() cannot take zero or negative values -> Option CQuick Check:
Log input must be positive [OK]
- Ignoring domain restrictions of log function
- Confusing np.log() with np.exp()
- Assuming code runs without warnings or errors
data. You want to normalize it by applying the natural logarithm, then reverse the transformation after some processing. Which sequence of operations correctly achieves this?Solution
Step 1: Understand the normalization step
Applyingnp.log(data)transforms data to a logarithmic scale, useful for normalization.Step 2: Reverse transformation
To get back original data, applynp.exp()to the logged data because exp is the inverse of log.Step 3: Check other options
Applynp.exp(data)first, thennp.log()on the result to reverse reverses the order incorrectly, Applynp.log10(data)first, thennp.exp()on the result to reverse mixes log base 10 with exp (base e), Applynp.sqrt(data)first, thennp.log()on the result to reverse uses sqrt which is unrelated.Final Answer:
Apply np.log(data) first, then np.exp() on the result to reverse -> Option DQuick Check:
log then exp returns original data [OK]
- Reversing the order of log and exp
- Mixing log base 10 with exp
- Using unrelated functions like sqrt
