Math for AILoading progress…

Addition and scaling

Compute vector operations, lengths, angles and projections and explain what each measures.

A vector is a list of numbers with a job

A vector is an ordered list of numbers, called coordinates. The order matters: [2, 1] and [1, 2] are different vectors. What the numbers mean depends on the job. As a displacement, [2, 1] says "go 2 across and 1 up". As a data record, [35, 1.7] could be an age and a height. As a learned representation, a few hundred numbers can describe a word or an image. The arithmetic is identical in all three cases.

In this chapter we draw two-dimensional vectors as arrows from the origin, because you can see them. Everything we compute works the same with 3, 300 or 3000 coordinates. One rule never changes: two vectors can be added or compared only if they have the same number of coordinates.

Adding vectors

Add vectors coordinate by coordinate: a + b = [a₁ + b₁, a₂ + b₂]. Picture it as two walks. Walk along a, then walk along b from where you stopped (tip to tail). Where you end up is the sum. Drawing both arrows from the origin and completing the parallelogram gives the same point.

-6-6-4-4-2-2224466aba + b
a = [2, 1]   b = [-1, 3]
a + b = [a₁ + b₁, a₂ + b₂]
= [2 + (-1), 1 + 3]
= [1, 4]
b + a = [1, 4] (the same: order does not matter)

Tip to tail: walk along a, then along b. The parallelogram shows the same total from the origin. Drag the dots.

Drag the tips of a and b. Compare the two constructions: the green sum is the same either way, and swapping the order does not change it.

Predict first. For a = [4, -1] and b = [-3, 2], what is a + b?

Scaling: one number changes the length, maybe the direction

Multiplying a vector by a plain number (a scalar) multiplies every coordinate: s·a = [s·a₁, s·a₂]. The result lies on the same line through the origin. A scalar bigger than 1 stretches the vector, a scalar between 0 and 1 shrinks it, a negative scalar flips it around the origin, and 0 collapses it to the zero vector. Scaling never adds a new coordinate and never turns the vector sideways.

-6-6-4-4-2-2224466as·a
a = [2, 1]
s·a = [s·a₁, s·a₂] = [1·2, 1·1]
= [2, 1]
length: |s|·‖a‖ = 1 · 2.236 = 2.236

s = 1 changes nothing. The result always stays on the dashed line through a and the origin.

Move the slider through negative values and through 0. Watch the length factor |s| and the direction.

Predict first. What is (-2)·[1, -3]?

The common mistake: treating vectors as plain lists

In Python, + on two lists joins them. That is not vector addition, and it fails silently:

>>> [2, 1] + [-1, 3]
[2, 1, -1, 3]          # four coordinates, not [1, 4]

>>> import numpy as np
>>> np.array([2, 1]) + np.array([-1, 3])
array([1, 4])          # coordinate-wise, as intended

>>> np.array([2, 1]) + np.array([1, 2, 3])
ValueError: operands could not be broadcast together with shapes (2,) (3,)
Chapter context

A vector is an ordered set of coordinates in a specified space. It can describe a displacement, a data record or a learned representation. Operations are meaningful only when the coordinates refer to compatible dimensions.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

a⋅b=∑iaibi,∥a∥2=∑iai2.a\cdot b=\sum_i a_i b_i,\quad \|a\|_2=\sqrt{\sum_i a_i^2}. cos⁡(a,b)=a⋅b∥a∥2∥b∥2,proj⁡ba=a⋅bb⋅bb.\cos(a,b)=\frac{a\cdot b}{\|a\|_2\|b\|_2},\quad \operatorname{proj}_b a=\frac{a\cdot b}{b\cdot b}b.

Both vectors must have the same dimension; cosine and projection need the denominator to be nonzero.

Worked example

For a=[2,3] and b=[4,1], matching products are 8 and 3, so the dot product is 11. Squared norms are 13 and 17. The cosine is 11/sqrt(221), approximately 0.73994. The projection of [3,4] onto [1,0] keeps only the first coordinate.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import sqrt, isclose
from operator import mul
a, b = [2, 3], [4, 1]
dot = sum(map(mul, a, b))
assert dot == 11
cosine = dot / (sqrt(sum(x*x for x in a))*sqrt(sum(x*x for x in b)))
assert isclose(cosine, 11/sqrt(221))
assert isclose(cosine, 0.7399400734, abs_tol=1e-9)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Dot product is not elementwise multiplication alone. Similarity and distance have opposite ordering conventions. Zero vectors cannot be normalized by dividing by their norm.