The Dot Product
The dot product multiplies two vectors and returns a number. It measures how much two vectors point the same way, and it gives angles, orthogonality tests, projections and work. This lesson defines it, proves the angle formula and the Cauchy–Schwarz inequality, and derives projections.
For a gentler first pass, see Dot and Cross Products.
Definition and algebraic rules
For and ,
The same formula works in and in with terms. Directly from the definition:
The angle theorem
Theorem. If is the angle between nonzero vectors and , then
Proof. The vectors , and form a triangle. The Law of Cosines gives . Expanding the left side with the rules above gives . Compare the two expressions and cancel: . ∎
Consequences:
| Sign of |
Angle |
Meaning |
|
acute, |
point roughly the same way |
|
|
orthogonal (perpendicular) |
|
obtuse |
point roughly opposite ways |
The zero vector counts as orthogonal to every vector.
Cauchy–Schwarz and the triangle inequality
Cauchy–Schwarz. , with equality exactly when and are parallel.
In it follows at once from . That argument is circular in , where the angle is defined via the dot product, so here is a proof that works in any dimension. For every real ,
A quadratic in that is never negative has at most one real root, so its discriminant is : . ∎
The triangle inequality follows: .
python · try it yourselfimport numpy as np
rng = np.random.default_rng(7)
worst = 0.0
for _ in range(10_000):
a, b = rng.normal(size=5), rng.normal(size=5) # vectors in R^5
ratio = abs(a @ b) / (np.linalg.norm(a) * np.linalg.norm(b))
worst = max(worst, ratio)
print("largest |a·b| / (|a||b|) over 10000 random pairs:", round(float(worst), 4))
a = np.array([1.0, 2.0, 2.0]); b = -3 * a # parallel case
print("parallel:", abs(a @ b), "=", np.linalg.norm(a) * np.linalg.norm(b))
largest |a·b| / (|a||b|) over 10000 random pairs: 0.9947
parallel: 27.0 = 27.0
Direction cosines
The angles that makes with are its direction angles. Their cosines are the components of the unit vector:
Projections
The scalar projection of onto (also called the component of along ) and the vector projection are
The remainder is orthogonal to , because . So splits uniquely into a part parallel to plus a part perpendicular to . That decomposition is the heart of Gram–Schmidt, least squares and collision response.
Work. A constant force moving an object along displacement does work . Only the component of the force along the motion does work.
python · try it yourselfimport numpy as np
a = np.array([3.0, 0.0, 4.0])
b = np.array([2.0, 5.0, 1.0])
cos_t = a @ b / (np.linalg.norm(a) * np.linalg.norm(b))
print("angle:", round(float(np.degrees(np.arccos(cos_t))), 3), "degrees")
print("comp_a b =", a @ b / np.linalg.norm(a))
proj = (a @ b) / (a @ a) * a
perp = b - proj
print("proj_a b =", proj, " perpendicular part =", perp)
print("check perp · a =", round(float(perp @ a), 12) + 0.0)
F, D = np.array([10.0, 0.0, -2.0]), np.array([4.0, 3.0, 0.0]) # N and m
print("work W = F·D =", F @ D, "J")
angle: 68.583 degrees
comp_a b = 2.0
proj_a b = [1.2 0. 1.6] perpendicular part = [ 0.8 5. -0.6]
check perp · a = 0.0
work W = F·D = 40.0 J
In software
- Lighting. Lambert diffuse shading is for the unit surface normal and the unit light direction . Back-face culling and "is the enemy in front of me?" tests are sign checks of a dot product (Dot Product in games, Projection and Reflection).
- Embeddings and search. Cosine similarity compares word, sentence and image embeddings. Vector databases normalise the vectors once, so similarity becomes a plain dot product (Embeddings, Vector Search). Cauchy–Schwarz is why the score always lies in .
- Neural networks. Every neuron computes , and attention scores are dot products of queries and keys (Attention).
- Physics. Collision response splits velocity into normal and tangential parts with exactly the projection above.
Exercises
Exercise 1: Angle between vectors
Find the angle between and .
Answer
()
Solution
Exercise 2: Orthogonality with a parameter
For which is orthogonal to ?
Answer
or .
Solution
Exercise 3: Decompose a vector
Write as the sum of a vector parallel to and a vector orthogonal to .
Answer
Solution
Exercise 4: Nearest embedding (code)
Given the query and documents , , , rank the documents by cosine similarity. Why does win even though it is longest?
Answer
. Cosine similarity ignores length, and points almost exactly along 's dominant direction.
Solution
python · try it yourselfimport numpy as np
q = np.array([0.2, 0.9, 0.1])
docs = {"d1": [1, 0, 0], "d2": [0.3, 0.8, 0.5], "d3": [0, 2, 0]}
cos = lambda a, b: float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))
for name, d in sorted(docs.items(), key=lambda kv: -cos(q, np.array(kv[1], float))):
print(name, round(cos(q, np.array(d, float)), 4))
d3 0.9705
d2 0.9041
d1 0.2157
Test yourself
for nonzero and means…
- and are parallel
- and are orthogonal
What is the scalar projection of onto ?
- 18
- 6
Why is cosine similarity always between and 1?
- Because embeddings are normalised by the model
- Because of the Cauchy–Schwarz inequality
- Because dot products are always positive
- Because vectors have three components