Addition and scaling
Compute vector operations, lengths, angles and projections and explain what each measures.
THE IDEA
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.
Tip to tail: walk along a, then along b. The parallelogram shows the same total from the origin. Drag the dots.
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.
s = 1 changes nothing. The result always stays on the dashed line through a and the origin.
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.
TRY IT YOURSELF
Part 1. Do it on the plane
Drag the dots (or focus one and use the arrow keys) and move the slider. Tasks tick from the real numbers on the plane and stay ticked once earned.
Tip to tail: walk along a, then along b. The parallelogram shows the same total from the origin. Drag the dots.
- To do: Make a + b equal [5, 2], using two nonzero vectors where neither one is [5, 2] itself
- To do: Set the scalar s so that s·a points the opposite way from a and is twice as long
- To do: Make a + b equal the zero vector [0, 0] while a is not the zero vector
Part 2. Do it in code
- Write add(a, b), scale(s, a) and total_displacement(moves) using plain lists.
- add must raise ValueError when the two lists have different lengths (zip would silently drop the extra coordinate).
- total_displacement adds up a whole list of moves, like a cannon aimed by several pushes.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Add [2,1] and [-1,3].
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
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.