Vectors: arrows made of numbers
Before you start
You need nothing else first. This unit starts from scratch.
The idea in one paragraph
A point on a map is a list of numbers: how far east and how far north it lies. A vector is the same kind of list, read as an arrow: "go 3 east and 1 north." You add two vectors by putting one arrow after the other, which means adding the lists number by number. You stretch a vector by multiplying every number by the same amount. Computers store points, directions and many other things this way, in 2D, in 3D or with many more numbers.
A point is a list of numbers
Pick a starting corner on a sheet of squared paper and call it the origin. Any other spot on the sheet is then two numbers: how many squares to the right, and how many squares up. These numbers are the spot's coordinates, written in brackets as (3, 1). The first number is called x and the second y.
A room needs a third number for height, called z. So a point in 3D is a list of three numbers, such as (1.5, −2, 4). Nothing changes in the rules below when the list gets longer.
A vector is an arrow that says how far to move
A vector is a list of numbers read as a move: (3, 1) means "3 to the right and 1 up." You can draw it as an arrow. The arrow's position on the page does not matter; only its length and direction do. Each number in the list is a component of the vector.
A point and a vector look the same on paper. A point (3, 1) is where you end up if you start at the origin and follow the arrow (3, 1).
Adding vectors means walking one arrow and then the other. Put the tail of b at the tip of a. The arrow from the start of a to the tip of b is the sum a + b. In numbers, you add the matching components: the x's together, the y's together.
a + b = (a1 + b1, a2 + b2)
Here a1 is the first component of a and a2 its second. A bold letter stands for a whole vector. An italic letter with a small number stands for one of its components.
Multiplying by a number stretches the arrow
Multiply every component by the same number and the arrow keeps its direction but changes length. The number is called a scalar, because it scales the arrow. Doubling (3, 1) gives (6, 2), an arrow twice as long. A negative scalar also flips the arrow: −1 times (3, 1) is (−3, −1).
Subtracting is adding the flipped arrow. The arrow that leads from point a to point b is b − a, because a + (b − a) = b.
Take a = (3, 1) and b = (1, 2).
- Add: (3 + 1, 1 + 2) = (4, 3).
- Stretch: 2 × (3, 1) = (2 × 3, 2 × 1) = (6, 2).
- The arrow from a to b: (1 − 3, 2 − 1) = (−2, 1). Check it: (3, 1) + (−2, 1) = (1, 2), which is b.
Your turn. With a = (2, 5) and b = (−1, 3), find a + b, 3a, and the arrow from a to b.
Answer
(1, 8); (6, 15); (−1 − 2, 3 − 5) = (−3, −2).
This page treats a point and a vector as the same list of numbers, as most code does. Mathematicians keep them apart: adding two vectors makes sense, but adding two points (two places) does not. Code will happily add two points, so the meaning is up to you.
vectors.py: NumPy stores a vector as an array, and does the arithmetic component by component
import numpy as np
a = np.array([3, 1])
b = np.array([1, 2])
print(a + b) # add: tip to tail
print(2 * a) # stretch
print(b - a) # the arrow from point a to point b
p = np.array([1.5, -2.0, 4.0]) # a point in 3D
print(p.shape)
[4 3] [6 2] [-2 1] (3,)
Check yourself
-
What is (2, −1) + (−3, 4)? Sketch both arrows tip to tail and check that the sum points where you expect.
Answer
(2 − 3, −1 + 4) = (−1, 3): one square left and three up.
-
A drone starts at (1, 1, 0) and ends at (4, 5, 2). Which vector describes its trip?
Answer
End minus start: (4 − 1, 5 − 1, 2 − 0) = (3, 4, 2).
-
Which scalar turns (4, −2) into (−2, 1)? What does it do to the arrow?
Answer
−0.5. It halves the length and flips the arrow to point the opposite way.
Go further
Optional background, for readers who want more than this page.
-
The picture first and the numbers second, animated; the standard first look at vectors.
-
Drag arrows and watch the sum and its components update; 2D only.
Where this shows up
Units that build on this one
- Distance between two points Basic
- The gradient: the uphill direction Intermediate
- Normalizing data Basic
- Arrays in NumPy Intermediate
- Shapes as point clouds Intermediate
- 3D images made of voxels Intermediate
- Embeddings: things as vectors Intermediate
- Velocity fields: an arrow at every point Intermediate
- The dot product: how much two vectors agree Basic