PIXELBANKv8.2.1
Menu

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]\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:

Step-by-step calculation using the Euclidean norm formula:

v=i=1nvi2\|\mathbf{v}\| = \sqrt{\sum_{i=1}^{n} v_i^2}

  1. Square each element:

    • 32=93^2 = 9
    • 42=164^2 = 16
  2. Sum the squared values: 9+16=259 + 16 = 25

  3. Take the square root: 25=5\sqrt{25} = 5

  4. Result: 5.00005.0000

This is the classic 3-4-5 right triangle! The magnitude represents the length of the vector from origin to point (3,4)(3,4).

Constraints:

  • Vector length n where 1 ≤ n ≤ 1000
  • Vector elements are floating-point numbers
  • Return the result rounded to 4 decimal places
Editor

Test Results

0/0
Run code to see test results.