The Dot Product
Projection and measuring similarity between vectors
Multiplying Vectors?
We know how to add vectors and scale them by numbers. But what does it mean to multiply two vectors together? Unlike regular numbers, there is no single obvious way to do this. The dot product is one answer, and it turns out to be remarkably useful.
The dot product takes two vectors and returns a single number. Not a vector, but a scalar. This number tells us something profound: how much the two vectors point in the same direction.
The Algebraic Definition
Let's start with the computation. Given two vectors and , their dot product is simply the sum of the products of their corresponding components:
In three dimensions, we just add another term:
The pattern continues in any number of dimensions. Multiply matching components, then add everything up. Simple enough to compute, but what does it mean?
Interactive: Component-wise Breakdown
Watch how each component contributes to the final dot product. The x-components multiply together, the y-components multiply together, and their sum gives the total. When components have the same sign, they contribute positively. When they have opposite signs, they subtract from the total.
The Geometric Definition
Here is where the dot product becomes beautiful. There is an equivalent geometric formula:
where is the angle between the two vectors, and and are their lengths (magnitudes).
This formula reveals the geometric soul of the dot product. It measures how aligned two vectors are, scaled by their magnitudes. When vectors point in the same direction (), the cosine is 1, and the dot product is maximized. When they are perpendicular (), the cosine is 0, so the dot product vanishes. When they point in opposite directions (), the cosine is -1, and the dot product is negative.
Interactive: Angle Between Vectors
Vectors point in similar directions — positive dot product
Notice how the dot product smoothly transitions from positive to zero to negative as the angle increases. The dot product is positive when vectors are less than 90° apart, zero at exactly 90°, and negative when they are more than 90° apart.
Projection: The Heart of the Dot Product
Perhaps the most intuitive way to understand the dot product is through projection. Imagine shining a light directly onto a line containing vector . The shadow that vector casts onto this line is called the projection of onto .
The length of this shadow, measured as a signed quantity (positive if it points in the same direction as , negative if opposite), is exactly:
If we want the actual projection vector (not just its length), we scale the unit vector in the direction of by this length:
Interactive: Projection of a onto b
Drag the tip of vector a to see how the projection changes
Drag vector around and watch its shadow on . When points somewhat in the direction of , the projection is long and positive. When is perpendicular to , the projection vanishes. When points somewhat opposite to , the projection becomes negative.
Orthogonality: When the Dot Product Is Zero
Two vectors are orthogonal (perpendicular) if and only if their dot product is zero. This is a direct consequence of the geometric formula: when vectors are at 90° to each other, .
This gives us a quick test for perpendicularity. Instead of measuring angles, just compute the dot product. If it is zero (or very close to zero in practice), the vectors are orthogonal.
Interactive: Orthogonality Detector
Drag either vector tip to explore orthogonality
Try to make the vectors perpendicular. When you get close, the indicator will turn green and show the right angle symbol. Orthogonality is fundamental in linear algebra, from defining coordinate systems to decomposing signals into independent components.
Why Are Both Definitions Equivalent?
It is not obvious why multiplying and adding components should give the same result as the cosine formula. The connection comes from the law of cosines and some careful algebra.
Consider the vector . Its length squared, by the law of cosines, is:
But we can also compute this using components:
Expanding and simplifying both expressions, we find that they are equal if and only if . The algebraic and geometric definitions are two sides of the same coin.
Applications
The dot product appears everywhere because it captures such a fundamental idea.
Work in physics. When a force moves an object along a displacement , the work done is . Only the component of force in the direction of motion does work. Push perpendicular to motion, and you do no work at all.
Similarity in machine learning. When comparing two vectors of features (say, representing documents or images), the dot product of their normalized versions (the cosine similarity) measures how alike they are. A similarity of 1 means identical directions, 0 means unrelated, and -1 means opposite.
Interactive: Cosine Similarity
Somewhat similar — vectors point in roughly the same direction
Lighting in graphics. The brightness of a surface depends on the angle between the incoming light and the surface normal. Specifically, the intensity is proportional to the dot product of the light direction and the normal vector. This is why surfaces facing toward light are bright and surfaces facing away are dark.
Detecting direction. In game development, the dot product tells you if an enemy is in front of or behind the player. If the dot product of the forward vector and the direction to the enemy is positive, they are ahead. If negative, they are behind.
Looking Ahead
The dot product becomes essential in later chapters. In orthogonality, we will build entire bases where all pairs of vectors are perpendicular—detected by zero dot products. In eigenvectors, we will find that symmetric matrices have eigenvectors that are mutually orthogonal. The dot product is the tool for measuring and enforcing perpendicularity.
Key Takeaways
- The dot product has two equivalent definitions: algebraic (sum of component products) and geometric (magnitude times cosine of angle)
- It measures how much two vectors point in the same direction — positive means similar, zero means perpendicular, negative means opposite
- Projection is the key geometric interpretation: the dot product with a unit vector gives the shadow length
- Two vectors are orthogonal (perpendicular) if and only if their dot product is zero
- Applications span physics (work), machine learning (similarity), graphics (lighting), and game development (direction detection)