np.linalg.norm() for vector norms in NumPy - Time & Space Complexity
We want to understand how the time to calculate a vector's length grows as the vector gets bigger.
How does the work needed change when the vector size increases?
Analyze the time complexity of the following code snippet.
import numpy as np
vector = np.array([1, 2, 3, 4, 5])
norm_value = np.linalg.norm(vector)
This code calculates the length (norm) of a vector using numpy's built-in function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Summing the squares of each element in the vector.
- How many times: Once for each element in the vector (n times).
As the vector gets longer, the work grows in a straight line with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 multiplications and additions |
| 100 | About 100 multiplications and additions |
| 1000 | About 1000 multiplications and additions |
Pattern observation: Doubling the vector size roughly doubles the work needed.
Time Complexity: O(n)
This means the time to find the vector length grows directly with the number of elements.
[X] Wrong: "Calculating the norm takes the same time no matter how big the vector is."
[OK] Correct: The function must look at every element to compute the sum of squares, so bigger vectors take more time.
Knowing how vector length calculation scales helps you understand performance in many data tasks, like measuring distances or normalizing data.
"What if we calculate the norm of a matrix row-wise instead of a single vector? How would the time complexity change?"