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 can be calculated using the formula . 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
- Input:
vis the vector , a point in 2-D space: 3 units along the first axis, 4 along the second. Its magnitude is the straight-line distance from the origin to that point. - Why squares: the two components are the legs of a right triangle and the vector is its hypotenuse, so Pythagoras gives .
- Length: , reported to 4 decimal places as
5.0000. - Notice: squaring makes every term non-negative, so signs never cancel. The vector also has length 5, and the result is only for the all-zero vector.
- More dimensions: the same rule extends to any length. For example gives , which is longer than any single component of 1.
Constraints
- Vector length n where 1 ≤ n ≤ 1000
- Vector elements are floating-point numbers
- Return the result rounded to 4 decimal places