Index Ai Academy
Phase 2 · Math, when you need it · Linear algebra

Vector arithmetic and length

Add, scale and measure vectors.

About 25 minutes · A free account opens the whole lesson

Create a free account Try part of it here

Already have an account? Sign in to open this lesson

Why it matters

Most of what AI models do to data comes down to four moves: add vectors, stretch them, measure how long they are, and measure how far apart they are. "Find customers like this one" is a distance measurement. AI search compares the directions of word vectors. Even the famous trick "king − man + woman ≈ queen" is just subtracting and adding lists of numbers. Learn these four moves and you have the vocabulary for everything that follows.

What you'll be able to do

  • Add, subtract and scale vectors, in code and as pictures
  • Compute a vector's length (norm) and the distance between two vectors
  • Normalize a vector to length 1 and say why embeddings often are
  • Find the nearest neighbor of a vector by distance

What it covers

  1. Adding: one number at a time
  2. Scaling: multiply every number
  3. Length: how far does the arrow reach?
  4. Distance: how far apart are two things?
  5. Worked example: which shop is nearest?
  6. Unit vectors: keep the direction, drop the length
  7. One easy slip

Adding: one number at a time

Say you track a campaign's results as [clicks, sales]. Last week was [3, 1]. This week was [1, 2]. What's the two-week total? Add the clicks to the clicks and the sales to the sales:

  [3, 1]
+ [1, 2]
= [4, 3]

That's all vector addition is: add the numbers in the same position. Both vectors must be the same length, or there's nothing to pair up.

Subtracting works the same way. [1, 2] − [3, 1] = [−2, 1]: this week had 2 fewer clicks and 1 more sale. The difference is the change from one vector to the other.

On the map from the last lesson, adding means "follow the first set of directions, then the second". Where you end up is the sum.

In plain words: to add or subtract vectors, add or subtract the numbers that sit in the same position.

Try it now

Drag a and b. The purple sum is where you end up if you follow a, then b.

Quick check
What is [5, 2, 0] + [1, 1, 4]?
  1. [5, 2, 0, 1, 1, 4]
  2. [6, 3, 0]
  3. [6, 3, 4]
  4. 13
Show the answer

[6, 3, 4]

Add position by position: 5 + 1, 2 + 1, 0 + 4.

Scaling: multiply every number

Multiplying a vector by an ordinary number (mathematicians call it a scalar) multiplies every number inside it. Double a budget split [300, 100] across two channels and you get [600, 200]. Each channel doubles; the split between them stays the same.

As an arrow, scaling changes the length but keeps the arrow on the same line:

  • multiply by 2: twice as long, same direction;
  • multiply by 0.5: half as long;
  • multiply by −1: same length, pointing the opposite way.

In plain words: scaling a vector multiplies every number in it by the same amount. The arrow gets longer, shorter or flips, but never turns sideways.

Go deeper The notation
k v=[kv1, kv2]2⋅[3,1]=[6,2]k\,\mathbf{v} = [k v_1,\ k v_2] \qquad 2 \cdot [3, 1] = [6, 2]

Read it as: "k times v is the vector you get by multiplying each component of v by k."

Length: how far does the arrow reach?

Walk 3 blocks east and 4 blocks north. How far are you from where you started, as the crow flies? You may remember Pythagoras from school: square the two sides, add them, take the square root.

3 × 3 = 9
4 × 4 = 16
9 + 16 = 25
square root of 25 = 5

So the arrow [3, 4] is 5 long. The length of a vector is called its norm. The same recipe works for any number of numbers: square each one, add them all up, take the square root.

In plain words: a vector's length (its norm) is "square everything, add it up, square root".

Here it is in Python, first by the recipe and then with NumPy's shortcut:

import numpy as np

v = np.array([3, 4])
print(np.sqrt(np.sum(v ** 2)))     # by the formula
print(np.linalg.norm(v))           # NumPy's built-in
print(np.linalg.norm(2 * v))       # scaling by 2 doubles the length
It runs right here in your browser, with nothing to install.
Go deeper The formula for length

Math writes the norm with double bars:

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

Read it as: "the length of v is the square root of the sum of each component squared." The dots mean "and so on, for every component up to the last one, vnv_n."

Quick check
What does this print?
import math
v = [3, 4]
print(math.sqrt(sum(x * x for x in v)))
  1. 5.0
  2. 7.0
  3. 25
  4. 7
Show the answer

5.0

3 × 3 + 4 × 4 is 25, and the square root of 25 is 5.0. It's the 3-4-5 triangle: square each number, add them up, and take the square root.

Quick check
What's the length of the vector [6, 8]?
  1. 14
  2. 48
  3. 10
  4. 100
Show the answer

10

6² + 8² = 36 + 64 = 100, and the square root of 100 is 10.

Distance: how far apart are two things?

The distance between two points is the length of the arrow from one to the other. So: subtract one vector from the other, then measure the length of the result.

This is called Euclidean distance (after the Greek mathematician Euclid). It's how "find the customers most like this one" works: turn customers into vectors, measure the distance to each, and pick the smallest.

import numpy as np

customers = {
    "ana":  np.array([12, 64.0]),   # orders per year, average order value
    "ben":  np.array([2, 180.0]),
    "chen": np.array([10, 70.0]),
}
target = customers["ana"]
for name, v in customers.items():
    print(name, round(float(np.linalg.norm(target - v)), 2))

Ana is 0 away from herself. Chen is closest to her. Notice that order value, in dollars, swamps the number of orders: that's the scale problem again, which Phase 3 fixes.

In plain words: distance between two vectors = subtract them, then take the length.

Worked example

Worked example: which shop is nearest?

You're at [1, 1] on a city grid. Two shops are at [4, 5] and [6, 1]. Which is closer as the crow flies? Work it out by hand first, then check with Python.

import numpy as np

you = np.array([1, 1])
shop_a = np.array([4, 5])
shop_b = np.array([6, 1])

print("to shop A:", np.linalg.norm(shop_a - you))
print("to shop B:", np.linalg.norm(shop_b - you))
In the lesson you can change this and run it.

Shop A: the difference is [3, 4], so the distance is √(9 + 16) = 5. Shop B: the difference is [5, 0], so the distance is √25 = 5 too. They're exactly as far as each other. On a real map you'd break the tie some other way: that's what models do with more features.

Unit vectors: keep the direction, drop the length

Sometimes you only care which way an arrow points, not how long it is. Divide a vector by its own length and you get a unit vector: same direction, length exactly 1. This is called normalizing.

Why bother? Think of two product reviews about the same thing, one long and one short. Turned into vectors, the long one might be a longer arrow, but both point the same way. If every vector is normalized to length 1, only direction is left to compare, which is what you want for meaning.

Many AI embedding models already return normalized vectors. The practice API's do:

import numpy as np
import requests

embedding = requests.post("https://api.gradient.test/v1/embeddings",
                          json={"input": "free returns within 60 days"}).json()["data"][0]["embedding"]
e = np.array(embedding)
print("dimension:", e.shape[0], "length:", round(float(np.linalg.norm(e)), 6))

v = np.array([3.0, 4.0])
unit = v / np.linalg.norm(v)
print(unit, np.linalg.norm(unit))

[3, 4] has length 5, so dividing by 5 gives [0.6, 0.8], which has length 1.

Watch out: a vector of all zeros has no direction and length 0. Dividing by 0 is an error, so code that normalizes has to check for it.

Go deeper The notation
v^=v∥v∥\hat{\mathbf{v}} = \frac{\mathbf{v}}{\|\mathbf{v}\|}

Read it as: "v-hat (the little hat marks a unit vector) is v divided by its own length."

Quick check
Two customer vectors are a = [2, 5] and b = [6, 8]. What's the distance between them?
  1. 25
  2. 4
  3. 7
  4. 5
Show the answer

5

a − b = [−4, −3], and √(16 + 9) = 5.

One easy slip

Length needs the square root at the end. Without it, you have the squared length, which is a different number, and the units no longer match.

Practice what you just read

  • 4 code examples you can edit and run
  • a practice exercise that checks your code
  • a 5-question quiz
  • 4 review cards for spaced repetition

Create a free account

Try a quiz question

What does normalizing a vector change?

  1. Only its length (to 1); the direction stays
  2. Only its direction; the length stays
  3. Its dimension, by dropping small values
  4. Nothing, unless its length was already 1

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson