I.3 · Mathematical Language
What does a vector actually store?
A vector is a displacement, and two numbers describe it. From those numbers we read its length with Pythagoras and its direction with atan2, and we find out why the obvious formula for direction quietly fails.
Drag the tip of
Coordinate plane from −7 to 7 on both axes; +x points right and +y points up. The vector v runs from the origin (0, 0) to the point (3, 2). Its horizontal component is 3 and its vertical component is 2.
- vector
- (3, 2)
- x-component
- 3
- y-component
- 2
- magnitude
- √133.605551…
- direction
- 0.588003 rad
- direction in degrees
- 33.690068°
= exact · ≈ rounded to 6 decimals
from −6 to 6
from −6 to 6
Show the work: computing
Start from the two components: how far right, how far up.
Try it before reading on. Pull the tip into each quadrant, then onto an axis, then onto the origin. Two numbers and are all the page stores; the length, the angle and the triangle are derived from them every time. By the end of the chapter you will know why the length is , why the direction needs a function called and not just , and what a vector is not.
Intuition: a vector is a move
Walk three blocks east and two blocks north. That instruction is a displacement: it says how far to move, not where you are. It means the same thing from your front door and from the library; only the place you end up changes.
A vector in the plane is exactly such a move, written as two numbers, a column:
The first number is the movement along (right is positive) and the second is the movement along (up is positive). We draw it as an arrow, but the arrow is a picture of the move; the two numbers are the move.
Points and displacements
A point is a place: names a location. A vector is a move. They are written with the same two numbers, which is the source of a lot of confusion, so it is worth separating them carefully.
The two are tied by two equations: applying a move to a point gives another point, and two points give the move between them.
Move the start , move the tip
Coordinate plane from −9 to 9 on both axes. Arrow from the point minus 2 comma minus 1 to the point 1 comma 1. Its displacement is 3 right and 2 up, length exactly the square root of 13. A dashed grey copy of the same arrow starts at the origin and ends at (3, 2): the same displacement drawn from the origin.
- start point (a place)
- (−2, −1)
- end point (a place)
- (1, 1)
- displacement (a vector)
- (3, 2)
- length of v
- √133.605551…
- direction of v
- 0.588003 rad
P and Q are points (places). v, ‖v‖ and θ describe the displacement, and do not change when only P moves.
from −4 to 4, up to 6 decimal places
from −4 to 4, up to 6 decimal places
from −4 to 4, up to 6 decimal places
from −4 to 4, up to 6 decimal places
Slide the arrow around and watch the inspector. The points and change; , its length and its direction do not. Now move only the tip: the displacement changes, because it is defined as the difference of the two points. An arrow drawn starting at the origin is the special case , called standard position; it is the one place where the point and the vector happen to have the same numbers.
Definition
A vector in the plane is an ordered pair of real numbers, written as a column,
where is the horizontal component and is the vertical component. Two vectors are equal exactly when both components are equal. The zero vector is the move that goes nowhere.
Throughout this book the plane has to the right and up, vectors are columns, and angles are in radians measured from the axis, counter-clockwise turns positive and clockwise turns negative (so has direction ). A vector has two derived quantities: its magnitude (how long the move is) and, when it is not the zero vector, its direction (which way).
Magnitude: Pythagoras on the components
The move goes across and up. Those two legs meet at a right angle, so the straight-line length of the arrow is the hypotenuse:
Pythagoras’ theorem says the areas of the squares on the legs add up to the area of the square on the hypotenuse. Here that is literal: drag the vector and compare the three squares.
Squares on the sides:
A right triangle with legs |x| = 3 and |y| = 2 and the vector (3, 2) as hypotenuse, with a square drawn on each side. The squares on the legs have areas 9 and 4; the square on the vector has area 13, the sum of the other two.
- area of the square on the x-leg
- 9
- area of the square on the y-leg
- 4
- area of the square on the arrow
- 13
- its side: the length
- √133.605551…
= exact · ≈ rounded to 6 decimals
from −4 to 4
from −4 to 4
Notice what the inspector says for : the square on the arrow has area , exactly, so the side is exactly, and is only a rounding. For the area is and the length is exactly . The book writes for the first kind of statement and for the second, and never mixes them.
Two properties follow at once from the formula. The length is never negative, and it is only for the zero vector, because a sum of squares is only when both squares are. And , since squaring erases signs: reversing a move does not change how long it is.
Direction: the angle from the x-axis
The direction of a nonzero vector is the signed angle from the positive axis to the arrow: counter-clockwise is positive, clockwise is negative. In the right triangle, is rise over run, so for a vector pointing into the right half-plane
That is a perfectly good formula, for a vector pointing right. The trouble is everything else.
Why is not enough
Take , which points up and to the left. Then and radians, which is a direction pointing down and to the right: exactly backwards. The vector has the identical ratio , so a formula that sees only the ratio cannot tell from . And is only able to answer between and , the right half-plane, so for a vector in the left half-plane it is wrong by exactly half a turn.
Where does go wrong?
Jump to
Coordinate plane from −7 to 7 on both axes. A dashed line through the origin carries the vector v and its opposite −v. Where x is not 0, v and −v share the same ratio y/x. The vector (−3, 2) is in quadrant II. A dashed caution-coloured arrow shows where the naive angle points: the opposite way.
- vector
- (−3, 2)
- the ratio y/x
- −0.666667
- naive angle, atan(y/x)
- −0.588003 rad
- true direction, atan2(y, x)
- 2.553590 rad
- what atan was missing
- π3.141593…
= exact · ≈ rounded to 6 decimals · ⚠ undefined
x < 0 (quadrant II): the vector points into the LEFT half-plane, but arctan can only answer in the right one, so it is half a turn off. atan2 adds π to fix it.
from −6 to 6
from −6 to 6
The function that fixes this is . It takes the two components separately, so it can see their signs, and returns the angle in . For it equals . For it equals when and when . When a vertical vector has a perfectly valid direction but no defined ratio ; does not divide, so for and for .
This identity needs , because must exist. The correction is exactly , or , decided by the signs of and alone. Here is the whole picture in one table, every cell computed by the same model the stage above uses.
| vector | where | y/x | atan(y/x) | atan2(y, x) | verdict |
|---|---|---|---|---|---|
| (3, 2) | quadrant I | 0.6667 | 0.5880 | 0.5880 | agree |
| (−3, 2) | quadrant II | −0.6667 | −0.5880 | 2.5536 | differ by +π |
| (−3, −2) | quadrant III | 0.6667 | 0.5880 | −2.5536 | differ by −π |
| (3, −2) | quadrant IV | −0.6667 | −0.5880 | −0.5880 | agree |
| (−4, 0) | the negative x-axis | 0 | 0 | 3.1416 | differ by +π |
| (0, 4) | the positive y-axis | undefined | undefined | 1.5708 | atan has no answer |
| (0, −4) | the negative y-axis | undefined | undefined | −1.5708 | atan has no answer |
| (0, 0) | the origin | undefined | undefined | undefined | no direction |
And there is one vector for which neither formula has a right answer: the zero vector. It has no direction, because it points nowhere. The stage shows this as an undefined state, not as the number .
Worked examples
Four vectors, worked step by step: the length first, then the direction. The last one, , is the case where does not exist.
Worked examples: length, then direction
Choose a vector
Start from the two components: how far right, how far up.
Executable reference code
Run the Python examples below to follow the calculations step by step. First the mechanics, written out so every step is visible.
import math
# A 2D vector is stored as two components. Here: v = (3, 2).
x = 3.0
y = 2.0
# Pythagoras: the hypotenuse of the right triangle with legs |x| and |y|.
magnitude = math.sqrt(x * x + y * y) # ‖v‖ = √(x² + y²)
# atan2 knows the quadrant of (x, y); atan(y / x) does not.
angle = math.atan2(y, x) # radians, in (−π, π]
print(magnitude, angle) # 3.605551275463989 0.5880026035475675import math
# v = (-3, 2): left of the y-axis, above the x-axis (quadrant II).
x, y = -3.0, 2.0
theta_naive = math.atan(y / x) # -0.588...: arctan only returns angles between -pi/2 and pi/2
theta = math.atan2(y, x) # 2.553...: atan2 looks at the signs of BOTH y and x
# -v = (3, -2) has exactly the same ratio y/x, so atan cannot tell v from -v apart.
theta_naive_of_minus_v = math.atan((-y) / (-x)) # the same -0.588... as for v
theta_of_minus_v = math.atan2(-y, -x) # -0.588...: atan2 DOES see the difference (it is theta - pi)
def direction(x, y):
"""The angle from the +x axis in (-pi, pi], or None for the zero vector (it has no direction)."""
if x == 0 and y == 0:
return None
return math.atan2(y, x)
zero_direction = direction(0.0, 0.0) # None: the mathematics has no answer
silent_zero = math.atan2(0.0, 0.0) # 0.0: the library quietly picks a value. Do not mistake it for a direction.
# A vertical vector has no ratio y/x at all, but it has a perfectly good direction.
try:
math.atan(2.0 / 0.0)
except ZeroDivisionError:
vertical_naive = "ZeroDivisionError"
vertical_atan2 = math.atan2(2.0, 0.0) # pi/2
print(theta_naive, theta, theta_of_minus_v, zero_direction, vertical_naive, vertical_atan2)import math
# A displacement vector says "go 3 right and 2 up". It is not a place.
v = (3.0, 2.0)
# Drawn from the origin, the arrow ends at the point (3, 2) ...
tip_from_origin = (0.0 + v[0], 0.0 + v[1])
# ... drawn from another tail P = (1, -1), the SAME displacement ends at a different point Q = P + v.
P = (1.0, -1.0)
tip_from_other_tail = (P[0] + v[0], P[1] + v[1]) # (4.0, 1.0)
# The displacement is recovered from the two points as Q - P: it does not depend on where the arrow starts.
v_from_other_tail = (tip_from_other_tail[0] - P[0], tip_from_other_tail[1] - P[1]) # (3.0, 2.0)
magnitude = math.hypot(*v_from_other_tail) # the length is a property of v, not of P
print(tip_from_origin, tip_from_other_tail, v_from_other_tail, magnitude)Then the practical form. NumPy stores many vectors at once, one per row, and works on all of them in a single call. (Rows are a storage layout for a batch; each vector is still a column in the mathematics.)
import numpy as np
# The practical form: once the mechanism is clear, the library does the bookkeeping.
v = np.array([3.0, 2.0])
magnitude = np.linalg.norm(v) # ‖v‖₂
angle = np.arctan2(v[1], v[0]) # radians, in (−π, π]
print(magnitude, angle) # 3.605551275463989 0.5880026035475675import numpy as np
# In data science code a collection of vectors is usually stored one per ROW. (This book writes a single
# vector as a column; the numbers are the same, only the layout differs.)
V = np.array([[3.0, 2.0], [-3.0, 2.0], [-3.0, -2.0], [3.0, -2.0]])
lengths = np.linalg.norm(V, axis=1) # one norm per row
angles = np.arctan2(V[:, 1], V[:, 0]) # one quadrant-aware angle per row: y first, then x
print(lengths) # four equal lengths, each sqrt(13)
print(angles) # one angle per quadrant: I, II, III, IVExplore: change the parameters
Use the stages above as a laboratory. These questions have definite answers; check them against the inspector.
- On the Pythagoras stage, which integer vectors have a whole-number length? Try , , . Which others can you find with components up to ?
- On the points stage, move to the origin. What do and now have in common?
- On the direction stage, press −x axis and then +y axis. Which has a ratio and which does not, and what does return for each?
- Walk a vector around the origin counter-clockwise through all four quadrants. Where does jump, and where does jump?
Common mistakes
Numerical considerations
Mathematically . In floating point, squaring first can lose the answer completely: is smaller than the smallest double, and is larger than the largest. The library function hypot avoids both by rescaling before it squares.
| vector (x, y) | √(x² + y²) | hypot(x, y) | naive formula |
|---|---|---|---|
| (1.0000e+0, 1.0000e+0) | 1.4142e+0 | 1.4142e+0 | fine |
| (1.0000e-100, 1.0000e-100) | 1.4142e-100 | 1.4142e-100 | fine |
| (1.0000e-200, 1.0000e-200) | 0 | 1.4142e-200 | ⚠ wrong |
| (1.0000e+150, 1.0000e+150) | 1.4142e+150 | 1.4142e+150 | fine |
| (1.0000e+200, 1.0000e+200) | ∞ | 1.4142e+200 | ⚠ wrong |
Two more points. Negative zero exists: equals in comparisons but atan2(0.0, -0.0) returns while atan2(0.0, 0.0) returns . The stages here normalise to so the displayed direction is stable. And the angle from atan2 jumps from to across the negative axis; that is a feature of the range, not a change of direction.
Connections to engineering and machine learning
- Features as vectors. A data point with two measurements is a vector; its length is its distance from the origin, the Euclidean norm that appears everywhere from nearest-neighbour search to regularisation.
- Normalisation. Dividing a vector by its length gives a unit vector that keeps the direction and discards the scale; cosine similarity compares directions only. The zero vector has no direction, so normalising it needs a guard (the usual
epsin code). - Robotics and graphics. Headings, velocities and forces are vectors;
atan2is how a robot turns a displacement to a target into a heading, and why it is the standard function in every such library. - Gradients. The gradient of a function is a vector; its length is the steepness and its direction is the way uphill.
Exercises
Answer each question and press Check. A wrong answer tells you which mistake it looks like. After one attempt you may open a worked solution.
Summary and checkpoints
- A vector in the plane is a column of two numbers, a displacement: how far, not where. Points are places; and .
- Its magnitude is , Pythagoras on the components. It is exact when the sum is a perfect square or a known radical, and otherwise a rounding.
- Its direction is , not , which is wrong by in the left half-plane and does not exist on the -axis.
- The zero vector has length and no direction.
- In code:
hypotfor length,atan2for direction, an explicit guard for zero.
Checkpoints. You are ready to move on when you can, without looking:
- Say why and have the same but different directions.
- Compute and state whether the answer is exact.
- Explain why sliding an arrow does not change the vector.
- Say what returns for , and .
You can revisit the interactive vector by itself in the foundation explorer.