TExcelTAD
Home › Linear Algebra & Matrix Theory › Length of a Vector

Length of a Vector

The length (or norm) of a vector measures how long its arrow is. In R2\mathbb{R}^2 and R3\mathbb{R}^3 it comes straight from the Pythagorean theorem, and the same formula works in any number of dimensions. With length come distances between points and unit vectors, which are pure directions.

The formula

For v=(v1,v2,…,vn)\mathbf{v} = (v_1, v_2, \ldots, v_n):

∥v∥=v12+v22+⋯+vn2\lVert \mathbf{v} \rVert = \sqrt{v_1^2 + v_2^2 + \cdots + v_n^2}

In R2\mathbb{R}^2 this is the hypotenuse of a right triangle with legs ∣v1∣\lvert v_1 \rvert and ∣v2∣\lvert v_2 \rvert (Pythagorean theorem). In R3\mathbb{R}^3, apply Pythagoras twice: first across the floor, v12+v22\sqrt{v_1^2 + v_2^2}, then up to the tip, (v12+v22)2+v32=v12+v22+v32\sqrt{\left(\sqrt{v_1^2 + v_2^2}\right)^2 + v_3^2} = \sqrt{v_1^2 + v_2^2 + v_3^2}.

∥(3,4)∥=9+16=5∥(1,2,2)∥=1+4+4=3∥(1,1,1,1)∥=4=2\begin{aligned} \lVert (3, 4) \rVert &= \sqrt{9 + 16} = 5 \\ \lVert (1, 2, 2) \rVert &= \sqrt{1 + 4 + 4} = 3 \\ \lVert (1, 1, 1, 1) \rVert &= \sqrt{4} = 2 \end{aligned}
python · try it yourself
import numpy as np
from sympy import Matrix, sqrt

v = np.array([2.0, -3.0, 6.0])
print("NumPy:", np.linalg.norm(v), "=", np.sqrt(np.sum(v ** 2)))

w = Matrix([1, 1, 2])
print("SymPy exact:", w.norm(), "≈", w.norm().evalf(6))
print("in 768 dimensions:", np.linalg.norm(np.ones(768)), "= sqrt(768) =", round(float(np.sqrt(768)), 4))
NumPy: 7.0 = 7.0
SymPy exact: sqrt(6) ≈ 2.44949
in 768 dimensions: 27.712812921102035 = sqrt(768) = 27.7128

Properties

For all vectors u,v\mathbf{u}, \mathbf{v} and scalars cc:

Property Formula
non-negative ∥v∥≥0\lVert \mathbf{v} \rVert \ge 0, and ∥v∥=0\lVert \mathbf{v} \rVert = 0 only for v=0\mathbf{v} = \mathbf{0}
scaling ∥c⋅v∥=∣c∣⋅∥v∥\lVert c \cdot \mathbf{v} \rVert = \lvert c \rvert \cdot \lVert \mathbf{v} \rVert
triangle inequality ∥u+v∥≤∥u∥+∥v∥\lVert \mathbf{u} + \mathbf{v} \rVert \le \lVert \mathbf{u} \rVert + \lVert \mathbf{v} \rVert

The scaling rule follows from the formula: (cv1)2+⋯=c2(v12+⋯ )=∣c∣⋅∥v∥\sqrt{(cv_1)^2 + \cdots} = \sqrt{c^2(v_1^2 + \cdots)} = \lvert c \rvert \cdot \lVert \mathbf{v} \rVert. The triangle inequality says that a detour is never shorter than the direct path; its proof uses the dot product (Cauchy–Schwarz inequality).

Distance between points

The distance between points PP and QQ is the length of the vector between them:

d(P,Q)=∥Q−P∥=(q1−p1)2+⋯+(qn−pn)2d(P, Q) = \lVert Q - P \rVert = \sqrt{(q_1 - p_1)^2 + \cdots + (q_n - p_n)^2}

This is the Euclidean distance. For P=(1,2,3)P = (1, 2, 3) and Q=(4,6,3)Q = (4, 6, 3): Q−P=(3,4,0)Q - P = (3, 4, 0), so d=5d = 5.

Unit vectors and normalizing

A unit vector has length 1. Dividing any non-zero vector by its length gives the unit vector in the same direction; this is called normalizing:

v^=v∥v∥e.g.(3,4)5=(0.6,0.8)\hat{\mathbf{v}} = \frac{\mathbf{v}}{\lVert \mathbf{v} \rVert} \qquad \text{e.g.} \quad \frac{(3, 4)}{5} = (0.6, 0.8)
python · try it yourself
import numpy as np

def normalize(v, eps=1e-12):
    n = np.linalg.norm(v)
    if n < eps:
        raise ValueError("cannot normalize the zero vector")
    return v / n

aim = np.array([3.0, 0.0, 4.0])
d = normalize(aim)
print("direction:", d, " length:", np.linalg.norm(d))

speed = 12.0
print("velocity at speed 12:", speed * d)

try:
    normalize(np.zeros(3))
except ValueError as e:
    print("error:", e)
direction: [0.6 0.  0.8]  length: 1.0
velocity at speed 12: [7.2 0.  9.6]
error: cannot normalize the zero vector

"Direction times speed" is the standard way to build a velocity in games. The zero check matters: normalizing (0,0,0)(0, 0, 0) divides by zero and fills a game object with NaNs.

Other norms

The Euclidean norm is not the only way to measure size. Machine learning uses several:

Norm Formula Used for
L2L^2 (Euclidean) ∑vi2\sqrt{\sum v_i^2} distances, ridge regularization, gradient clipping
L1L^1 ("taxicab") ∑∣vi∣\sum \lvert v_i \rvert lasso regularization (makes weights sparse), city-block distance
L∞L^\infty (max) max⁡∣vi∣\max \lvert v_i \rvert worst-case error, adversarial-robustness budgets
python · try it yourself
import numpy as np

v = np.array([3.0, -4.0, 1.0])
print("L2:", round(float(np.linalg.norm(v)), 4))
print("L1:", float(np.linalg.norm(v, 1)))
print("Linf:", float(np.linalg.norm(v, np.inf)))
L2: 5.099
L1: 8.0
Linf: 4.0

In software

  • Games: "is the enemy within 10 m?" Compare squared lengths (dx*dx + dy*dy < 100) to skip the square root, which is faster and just as correct since both sides are non-negative.
  • ML: k-nearest neighbours and k-means rely on distances; embeddings are often normalized to length 1 so that the dot product equals the cosine similarity (coming in The Dot Product).
  • Deep learning: gradient clipping rescales a gradient g\mathbf{g} to length at most cc by g←g⋅min⁡(1,c/∥g∥)\mathbf{g} \leftarrow \mathbf{g} \cdot \min(1, c/\lVert \mathbf{g} \rVert); layer normalization rescales activation vectors.

See also Game Math: Length and Normalize.

Exercises

Exercise 1: Lengths

Find ∥(−2,6,3)∥\lVert (-2, 6, 3) \rVert and ∥(1,−1,1,−1,2)∥\lVert (1, -1, 1, -1, 2) \rVert.

Answer

7 and 22≈2.8282\sqrt{2} \approx 2.828.

Solution
4+36+9=49→71+1+1+1+4=8→8=22\begin{aligned} 4 + 36 + 9 &= 49 && \to 7 \\ 1 + 1 + 1 + 1 + 4 &= 8 && \to \sqrt{8} = 2\sqrt{2} \end{aligned}

Exercise 2: Distance

Find the distance between (1,−2,4)(1, -2, 4) and (3,1,−2)(3, 1, -2).

Answer
Solution
Q−P=(2,3,−6),4+9+36=49,49=7Q - P = (2, 3, -6), \quad 4 + 9 + 36 = 49, \quad \sqrt{49} = 7

Exercise 3: Unit vector and scaling

Find the unit vector in the direction of (1,2,−2)(1, 2, -2), and a vector of length 15 pointing the opposite way.

Answer

(13,23,−23)(\frac{1}{3}, \frac{2}{3}, -\frac{2}{3}) and (−5,−10,10)(-5, -10, 10).

Solution
∥(1,2,−2)∥=9=3 → unit vector (13,23,−23)opposite, length 15: −15⋅(13,23,−23)=(−5,−10,10)Check: 25+100+100=15 ✓\begin{aligned} &\lVert (1, 2, -2) \rVert = \sqrt{9} = 3 \ \to\ \text{unit vector } (\tfrac{1}{3}, \tfrac{2}{3}, -\tfrac{2}{3}) \\ &\text{opposite, length 15: } -15 \cdot (\tfrac{1}{3}, \tfrac{2}{3}, -\tfrac{2}{3}) = (-5, -10, 10) \\ &\text{Check: } \sqrt{25 + 100 + 100} = 15 \ \checkmark \end{aligned}

Exercise 4: Gradient clipping (code)

Write clip(g, c) that returns g\mathbf{g} unchanged if ∥g∥≤c\lVert \mathbf{g} \rVert \le c, and otherwise g\mathbf{g} rescaled to length cc. Test with g=(6,8)\mathbf{g} = (6, 8) and c=5c = 5, and with g=(1,1)\mathbf{g} = (1, 1) and c=5c = 5.

Answer

(3,4)(3, 4) and (1,1)(1, 1).

Solution

∥(6,8)∥=10>5\lVert (6, 8) \rVert = 10 > 5, so multiply by 510\frac{5}{10}.

python · try it yourself
import numpy as np

def clip(g, c):
    n = np.linalg.norm(g)
    return g if n <= c else g * (c / n)

print(clip(np.array([6.0, 8.0]), 5.0), clip(np.array([1.0, 1.0]), 5.0))
[3. 4.] [1. 1.]

Test yourself

What is ∥(0,−5,12)∥\lVert (0, -5, 12) \rVert?

  1. 7
  2. 17
  3. 13
  4. 169

If ∥v∥=4\lVert \mathbf{v} \rVert = 4, what is ∥−3v∥\lVert -3\mathbf{v} \rVert?

  1. -12
  2. 12
  3. 1
  4. 7

Why do games often compare squared distances instead of distances?

  1. It avoids a square root and gives the same comparison result
  2. Squared distances are more accurate
  3. Distances can be negative
  4. Square roots are not available on GPUs