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=1nvi2
-
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