v9.1.1
Menu

Vector Magnitude

easyFREEVectorsLinear AlgebraCV: Mathematical Foundations

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]\mathbf{v} = [v_1, v_2,..., v_n] can be calculated using the formula ∣∣v∣∣=v12+v22+...+vn2||\mathbf{v}|| = \sqrt{v_1^2 + v_2^2 +... + v_n^2}. To calculate this, follow these steps:

  1. Square each component of the vector.
  2. Sum the squared components.
  3. Take the square root of the sum.
∣∣v∣∣=∑i=1nvi2 ||\mathbf{v}|| = \sqrt{\sum_{i=1}^{n} v_i^2}

This technique is widely used in computer vision for image and signal processing.

Example

Input
magnitude([3, 4])
Output
5.0000
Reasoning
  • Input: v is the vector [3,4][3, 4], 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 ∥v∥2=32+42=9+16=25\|\mathbf{v}\|^2 = 3^2 + 4^2 = 9 + 16 = 25.
  • Length: ∥v∥=25=5\|\mathbf{v}\| = \sqrt{25} = 5, reported to 4 decimal places as 5.0000.
  • Notice: squaring makes every term non-negative, so signs never cancel. The vector [−3,4][-3, 4] also has length 5, and the result is only 00 for the all-zero vector.
  • More dimensions: the same rule extends to any length. For example [1,1,1,1][1, 1, 1, 1] gives 1+1+1+1=4=2\sqrt{1+1+1+1} = \sqrt{4} = 2, 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

Learn this in

More from CV: Mathematical Foundations

solution.py

Test results

Sign in to run your code against the test cases.