Learning Objectives
By the end of this section, you will be able to:
- Compute the dot product of two vectors using both the algebraic and geometric definitions
- Interpret the dot product geometrically as a measure of how much two vectors point in the same direction
- Apply the dot product to find angles between vectors
- Determine when two vectors are orthogonal (perpendicular) using the dot product
- Calculate the projection of one vector onto another
- Use the dot product in physics applications, especially work and force
- Connect the dot product to cosine similarity in machine learning
The Big Picture: Measuring Alignment
"The dot product answers a fundamental question: How much do two vectors point in the same direction?"
Imagine you're pushing a heavy box across the floor. You push with a certain force, but the box only moves horizontally. How much of your effort actually moves the box? If you push straight down, you do no useful work — your force is perpendicular to the motion. If you push parallel to the floor, all your effort moves the box. For any angle in between, only part of your force contributes.
The dot product (also called the scalar product or inner product) precisely quantifies this relationship. It takes two vectors and produces a single number that measures their "alignment."
Where the Dot Product Appears
Physics
- Work = Force · Displacement
- Power = Force · Velocity
- Projection of fields onto surfaces
- Angle between velocity vectors
Computer Graphics
- Lighting calculations (Lambert's cosine law)
- Surface normals and visibility
- Collision detection
- Camera viewing angles
Machine Learning
- Cosine similarity for text
- Attention mechanisms in transformers
- Word embeddings comparison
- Recommendation systems
Engineering
- Stress analysis (force components)
- Structural mechanics
- Signal processing (correlation)
- Navigation systems
Historical Context: The Birth of Vector Algebra
The dot product emerged in the 19th century as mathematicians developed the modern concept of vectors. William Rowan Hamilton (1805–1865) invented quaternions in 1843, which contained both a scalar and vector part. When you "multiply" two quaternions, the scalar part of the result is related to what we now call the dot product.
Josiah Willard Gibbs (1839–1903) and Oliver Heaviside (1850–1925) later extracted the dot and cross products as separate operations, creating the vector algebra we use today. Gibbs developed this specifically for his work in physics, where he needed to express work and energy in terms of forces and displacements.
Why "Dot" Product?
The name comes from the notation: we write with a centered dot. This distinguishes it from the cross product , which we'll study next. The dot product returns a scalar (a number), while the cross product returns a vector.
Algebraic Definition
We begin with the component-by-component definition, which is how we actually compute dot products.
Definition: The Dot Product (Algebraic)
Let and be vectors in . The dot product of and is:
In 2D:
In general n-dimensions:
Example: Computing a Dot Product
Let and . Find .
Solution:
Interpretation: Since the dot product is zero, these vectors areorthogonal (perpendicular) to each other!
| Operation | Notation | Result Type |
|---|---|---|
| Dot Product | a · b | Scalar (number) |
| Cross Product | a × b | Vector |
| Scalar Multiplication | c·a | Vector |
| Vector Addition | a + b | Vector |
Interactive: Exploring the Dot Product
Use this interactive visualizer to develop intuition for the dot product. Drag the vector endpoints or use the sliders to see how the dot product changes with the angle between vectors.
Drag the vector endpoints to explore
Vector a = (3.0, 2.0)
|a| = 3.606
Vector b = (2.0, 4.0)
|b| = 4.472
Dot Product Calculation
a · b = axbx + ayby
= (3.0)(2.0) + (2.0)(4.0)
= 6.00 + 8.00
= 14.000
Angle: 29.7°
cos(θ) = 0.8682
Positive: vectors point in similar directions (acute angle)
What to Explore
- Make vectors point the same direction — the dot product becomes large and positive
- Make vectors perpendicular — the dot product becomes zero
- Make vectors point opposite directions — the dot product becomes negative
- Keep the angle fixed but change magnitudes — the dot product scales with both magnitudes
Geometric Interpretation
The algebraic definition tells us how to compute the dot product. The geometric definition tells us what it means.
The Dot Product (Geometric Form)
If is the angle between vectors and , then:
where and are the magnitudes (lengths) of the vectors.
Understanding the Geometric Formula
The formula reveals the true meaning of the dot product:
- The dot product measures alignment. When (parallel vectors), , and the dot product is maximized.
- Perpendicular vectors have zero dot product. When , .
- Opposite vectors give negative dot products. When , .
- The result scales with both magnitudes. Longer vectors produce larger (in absolute value) dot products.
| Angle θ | cos θ | Dot Product Sign | Interpretation |
|---|---|---|---|
| 0° | 1 | Positive (maximum) | Parallel, same direction |
| 0° < θ < 90° | 0 < cos θ < 1 | Positive | Acute angle |
| 90° | 0 | Zero | Perpendicular (orthogonal) |
| 90° < θ < 180° | -1 < cos θ < 0 | Negative | Obtuse angle |
| 180° | -1 | Negative (minimum) | Parallel, opposite directions |
Properties of the Dot Product
The dot product satisfies several important algebraic properties that make it useful for calculations.
Properties of the Dot Product
1. Commutativity:
2. Distributivity:
3. Scalar Multiplication:
4. Self Dot Product:
5. Zero Vector:
The Magnitude Connection
Property 4 is particularly useful: . This means we can find a vector's length by taking the dot product with itself and then the square root!
Finding Angles Between Vectors
One of the most important applications of the dot product is finding the angle between two vectors. Rearranging the geometric formula:
Therefore:
Example: Finding the Angle
Find the angle between and .
Step 1: Compute the dot product
Step 2: Find the magnitudes
Step 3: Find cos θ
Step 4: Find θ
Orthogonality: Perpendicular Vectors
Two vectors are orthogonal (perpendicular) if and only if their dot product is zero.
This is because when , , making the entire product zero.
Example: Testing for Orthogonality
Are and orthogonal?
Yes! Since the dot product is zero, these vectors are orthogonal.
Vector Projection
The projection of onto answers the question: "How much of lies in the direction of ?"
Scalar Projection (Component)
The scalar projection (or component) of onto is the signed length of the projection:
Vector Projection
The vector projection of onto is a vector in the direction of with the same length as the scalar projection:
Vector Decomposition
Any vector can be decomposed into components parallel and perpendicular to another vector:
where is the component of perpendicular to .
Interactive: Vector Projection
Explore how the projection of vector onto works. The purple vector shows the projection, and the green dashed line shows the perpendicular component.
Vector a = (4.0, 3.0)
|a| = 5.000
Vector b = (5.0, 0.0)
|b| = 5.000
Projection Formula
projba = (a·b / |b|²) b
= (20.00 / 25.00) b
= 0.8000 × (5.0, 0.0)
= (4.000, 0.000)
Scalar projection: compba = 4.0000
Perpendicular: (0.000, 3.000)
Physics Applications
Work Done by a Force
When a constant force moves an object through a displacement , the work done is:
This formula explains why:
- Pushing parallel to motion (): Maximum work ()
- Pushing perpendicular to motion (): No work done ()
- Pushing opposite to motion (): Negative work (), which slows the object
Example: Work on an Inclined Plane
A force of 50 N is applied at 30° above the horizontal to push a cart 10 m along a flat surface. How much work is done?
Solution:
The vertical component of the force ( N) does no work on horizontal motion.
Machine Learning Applications
The dot product is fundamental to modern machine learning, appearing in everything from basic similarity measures to the attention mechanisms that power transformers.
Cosine Similarity
Cosine similarity normalizes the dot product by the vector magnitudes:
This gives a similarity score between -1 (opposite) and 1 (identical direction), regardless of vector magnitudes. This is crucial in NLP where document length shouldn't affect similarity.
Applications in Modern ML
- Word Embeddings: Words like "king" and "queen" have similar vectors (high cosine similarity), while "king" and "car" are dissimilar
- Attention Mechanisms: In transformers, attention scores are computed using dot products between query and key vectors
- Recommendation Systems: Users with similar preference vectors receive similar recommendations
- Information Retrieval: Search results are ranked by cosine similarity between query and document vectors
2D Word Embedding Space
Click on words to compare their similarity
[0.85, 0.7]
[0.75, 0.82]
Cosine Similarity Formula
cos(θ) = (a · b) / (|a| |b|)
= (0.85 × 0.75 + 0.7 × 0.82) / ...
"king" and "queen" have very high similarity (99.0%).
In NLP, this means these words appear in similar contexts and have related meanings.
Information Retrieval
Search engines rank documents by cosine similarity to the query vector.
Recommendation Systems
Find similar items or users by comparing their feature vectors.
Attention Mechanisms
Transformers use dot products to compute attention scores between tokens.
Why Cosine Over Euclidean Distance?
Euclidean distance measures absolute separation, while cosine similarity measures directional alignment. For text, a longer document isn't necessarily more different — it just has more words. Cosine similarity captures semantic similarity regardless of document length.
Python Implementation
Computing Dot Products
Vector Projection
Cosine Similarity for NLP
Common Pitfalls
Pitfall 1: Confusing Dot and Cross Products
The dot product returns a scalar. The cross product returns a vector. They answer different questions and are not interchangeable.
Pitfall 2: Order Matters for Projection
(projection of a onto b) is different from (projection of b onto a). The first is in the direction of b; the second is in the direction of a.
Pitfall 3: Negative Dot Products
A negative dot product doesn't mean the vectors are "opposite" in length — it means the angle between them is obtuse (greater than 90°). Both vectors can point "generally forward" and still have a negative dot product if the angle exceeds 90°.
Numerical Precision
When computing angles using , numerical errors can push the argument slightly outside [-1, 1], causing NaN results. Always clamp the input: np.clip(cos_theta, -1, 1).
Test Your Understanding
What is the dot product of a = (3, 4) and b = (2, -1)?
Summary
The dot product is one of the most important operations in vector mathematics, connecting algebra, geometry, physics, and modern machine learning.
Key Formulas
| Formula | Name | Use |
|---|---|---|
| a · b = a₁b₁ + a₂b₂ + a₃b₃ | Algebraic definition | Computation |
| a · b = |a||b|cos θ | Geometric definition | Finding angles |
| θ = arccos(a·b / |a||b|) | Angle formula | Angle between vectors |
| a · b = 0 ⟺ a ⊥ b | Orthogonality test | Perpendicularity check |
| proj_b a = (a·b/|b|²)b | Vector projection | Component along b |
| comp_b a = a·b/|b| | Scalar projection | Length of projection |
| |a| = √(a·a) | Magnitude formula | Finding vector length |
Key Takeaways
- The dot product measures alignment between vectors — how much they point in the same direction
- Positive dot product: acute angle (similar directions)
- Zero dot product: right angle (orthogonal/perpendicular)
- Negative dot product: obtuse angle (opposite directions)
- Projection decomposes a vector into parallel and perpendicular components relative to another vector
- In physics, work = force · displacement
- In ML, cosine similarity uses the normalized dot product to compare text, images, and embeddings
Coming Next: In the next section, we'll explore the Cross Product, which answers a different question: Given two vectors, what vector is perpendicular to both? This is essential for finding normal vectors, computing torque, and understanding rotational physics.