Chapter 1
Vectors
What is a vector?
You've used arrays of numbers in code a thousand times. But what is a vector, really? And why should you care about thinking of it any other way?
To a programmer, a vector looks like [2, 1] — an array of numbers. You index into it, loop over it, pass it to functions. That view works fine until you need to understand what a matrix does, or why a dot product measures similarity, or how a transformation preserves structure. Then you need the geometric picture.
A vector is an arrow in space. It has a direction and a length. The numbers inside it are instructions: go 2 units right, 1 unit up. That's the arrow [2, 1]. It starts at the origin and ends at the point (2, 1).
This geometric view is the key to everything that follows in linear algebra. Every definition, every operation, every theorem has a visual meaning. Start here, and the rest clicks into place.
An arrow from the origin
The simplest picture: one vector, one arrow. The vector (2, 1) means "go 2 units along the x-axis, then 1 unit up." The tip of the arrow lands at the point (2, 1).
A vector is an arrow from the origin. The numbers tell you how far to go in each direction.
The dashed lines show the two components separately: 2 units along x, 1 unit along y. The vector is both of those movements combined into a single arrow.
Many arrows, many directions
One vector is one arrow. Change the numbers and you get a different arrow — different direction, different length. Here are four vectors, each pointing somewhere else in the plane.
Vectors have direction and magnitude. Different numbers, different arrows.
Notice how negative x-components point left and negative y-components point down. The sign tells you the direction along each axis. The size of each number tells you how far. Together, they give every vector its unique direction and length.
Adding vectors: tip-to-tail
What does it mean to add two vectors? Geometrically, you place the second arrow at the tip of the first. The result is a new arrow from the origin to wherever you end up.
Take and . To add them, start at the origin, follow to its tip at (1, 2), then follow from there. You land at (4, 3). The sum is the arrow from the origin to (4, 3).
To add vectors, place the second at the tip of the first. The result arrow goes from the origin to the final point.
The dashed lines form a parallelogram. You can add in either order — follow first then , or first then — and you end up in the same place. That's why vector addition is commutative.
Numerically, you just add the components: . The geometry and the arithmetic agree.
Scaling a vector
Multiplying a vector by a number — a scalar — stretches or shrinks the arrow. Multiply by 2 and the arrow doubles in length, keeping the same direction. Multiply by -1 and it flips to point the opposite way.
Scaling stretches or shrinks the arrow. Negative scaling flips it around.
All three arrows lie on the same line through the origin. Scaling never changes the direction (unless you negate it). It only changes the length. This is a theme you'll see again: scalars control "how much," directions control "which way."
Basis vectors: the building blocks
Here's the punchline of the geometric view. Every 2D vector can be built from just two special vectors:
- — one unit to the right
- — one unit up
These are the basis vectors. Any vector is just . The coordinates are literally telling you: "take this much of and that much of and add them together."
Every 2D vector is a combination of these two unit vectors. The coordinates tell you how much of each.
This idea — building vectors from basis vectors — is the seed of nearly everything in linear algebra. When we later talk about linear transformations, we'll ask: "What happens to and ?" If you know that, you know what happens to every vector. But we're getting ahead of ourselves.
The formal bit
Now that you have the picture, here's the notation.
A vector in two dimensions is written as a column:
Vector addition adds component by component:
That's the tip-to-tail picture in symbols. Each component of the result is the sum of the corresponding components of the inputs.
Scalar multiplication scales each component:
That's the stretching-the-arrow picture. Every component gets multiplied by the same scalar, so the arrow grows or shrinks uniformly.
Basis decomposition writes any vector as a weighted sum of and :
The coordinates are the weights. That's what the numbers mean geometrically.
Worked example: movement in a 2D game
In a 2D game, a character sits at position . Each frame, it moves with velocity — one unit left, two units up.
After one frame, the new position is:
That's vector addition: old position plus velocity equals new position.
The blue arrow is where the character was. The green arrow is how it moved. The orange arrow is where it ends up. Every frame of a physics simulation is doing exactly this: adding velocity vectors to position vectors.
In code, that's:
const pos = [3, 1];
const vel = [-1, 2];
const newPos = [pos[0] + vel[0], pos[1] + vel[1]]; // [2, 3]
Same operation. The array version and the arrow version are two views of the same thing.
Key takeaway: A vector is an arrow with direction and magnitude. The numbers are instructions for how to build that arrow from basis vectors. Addition places arrows tip-to-tail. Scaling stretches or flips them.
What's next
We can add vectors and scale them. But what happens when we combine scaling and adding — say, ? That's called a linear combination, and it unlocks the idea of span: the set of all vectors you can reach by mixing a given set of vectors. That's where Chapter 2 picks up.