Vector Magnitude
Implement a function to compute the magnitude (also known as the Euclidean norm) of a given vector. The magnitude of a vector is a fundamental concept in linear algebra and vector calculus, as it represents the "length" or "size" of the vector.
The magnitude of a vector v=[v1β,v2β,...,vnβ] can be calculated using the formula β£β£vβ£β£=v12β+v22β+...+vn2ββ. To calculate this, follow these steps:
- Square each component of the vector.
- Sum the squared components.
- Take the square root of the sum.
This technique is widely used in computer vision for image and signal processing.
Example:
magnitude([3, 4])
5.0000
Step-by-step calculation using the Euclidean norm formula:
β₯vβ₯=βi=1nβvi2ββ
-
Square each element:
- 32=9
- 42=16
-
Sum the squared values: 9+16=25
-
Take the square root: 25β=5
-
Result: 5.0000
This is the classic 3-4-5 right triangle! The magnitude represents the length of the vector from origin to point (3,4).
Constraints:
- Vector length n where 1 β€ n β€ 1000
- Vector elements are floating-point numbers
- Return the result rounded to 4 decimal places
The magnitude (Euclidean norm) of a vector is computed by squaring each component, summing those squares, and taking the square root of the sum.
1. Background Knowledge
A vector in this context is an ordered list of numbers, often written as v=[v1β,v2β,β¦,vnβ]. You can think of it as a point in nβdimensional space, or as an arrow from the origin to that point.
The Euclidean norm (or magnitude or length) of a vector generalizes the Pythagorean theorem to higher dimensions. For 2D, the length of (x,y) is x2+y2β; for 3D, the length of (x,y,z) is x2+y2+z2β. In n dimensions, this becomes:
β₯vβ₯=v12β+v22β+β―+vn2ββThis quantity is heavily used in normalization (scaling a vector to have length 1) and in distance computations (the distance between two points is the norm of their difference).
2. Algorithm / General Approach
The algorithm pattern is:
- Iterate over all vector components.
- Accumulate the sum of their squares.
- Take the square root of the final sum to get the magnitude.
This is a simple single-pass reduction (fold) over the elements of the vector.
3. Step-by-Step Strategy
Assuming the input is a list/array v of length n:
- Initialize an accumulator:
sum_sq = 0.0
- For each component x in v:
- Square it and add to the accumulator:
sum_sq += x * x
- After the loop, compute the square root:
magnitude = math.sqrt(sum_sq)
- Return or print magnitude.
In pseudocode:
function vector_magnitude(v):
sum_sq = 0
for each x in v:
sum_sq = sum_sq + x * x
return sqrt(sum_sq)
4. Common Pitfalls
- Forgetting the square root: Summing squares alone gives you the squared norm, not the magnitude.
- Integer vs float:
- If components or the accumulator are integers, you might lose precision.
- Use floating-point (e.g., double / float) for sum_sq and the result.
- Empty vector:
- If the problem allows it, the norm of an empty vector is usually defined as 0; otherwise, the input will typically guarantee at least one element.
- Overflow for large values (less of a concern in Easy problems, but worth noting):
- Squaring large integers can exceed integer range; using floating-point can help in practice.
5. Time & Space Complexity
-
Time complexity: You process each of the n components once, so time complexity is O(n)
-
Space complexity: Aside from the input, you only keep a few scalar variables (sum_sq, magnitude), so space complexity is O(1)