TExcelTAD
Home › Calculus in Depth › The Dot Product

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 a=⟨a1,a2,a3⟩\mathbf{a} = \langle a_1, a_2, a_3 \rangle and b=⟨b1,b2,b3⟩\mathbf{b} = \langle b_1, b_2, b_3 \rangle,

a⋅b=a1b1+a2b2+a3b3\mathbf{a} \cdot \mathbf{b} = a_1b_1 + a_2b_2 + a_3b_3

The same formula works in R2\mathbb{R}^2 and in Rn\mathbb{R}^n with nn terms. Directly from the definition:

a⋅a=∣a∣2a⋅b=b⋅aa⋅(b+c)=a⋅b+a⋅c(ca)⋅b=c(a⋅b)=a⋅(cb)0⋅a=0\begin{aligned} \mathbf{a} \cdot \mathbf{a} &= \lvert \mathbf{a} \rvert^2 & \mathbf{a} \cdot \mathbf{b} &= \mathbf{b} \cdot \mathbf{a} \\ \mathbf{a} \cdot (\mathbf{b} + \mathbf{c}) &= \mathbf{a} \cdot \mathbf{b} + \mathbf{a} \cdot \mathbf{c} \\ (c\mathbf{a}) \cdot \mathbf{b} &= c(\mathbf{a} \cdot \mathbf{b}) = \mathbf{a} \cdot (c\mathbf{b}) & \mathbf{0} \cdot \mathbf{a} &= 0 \end{aligned}

The angle theorem

Theorem. If θ∈[0,π]\theta \in [0, \pi] is the angle between nonzero vectors a\mathbf{a} and b\mathbf{b}, then

a⋅b=∣a∣ ∣b∣cos⁡θ\mathbf{a} \cdot \mathbf{b} = \lvert \mathbf{a} \rvert \, \lvert \mathbf{b} \rvert \cos\theta

Proof. The vectors a\mathbf{a}, b\mathbf{b} and a−b\mathbf{a} - \mathbf{b} form a triangle. The Law of Cosines gives ∣a−b∣2=∣a∣2+∣b∣2−2∣a∣∣b∣cos⁡θ\lvert \mathbf{a} - \mathbf{b} \rvert^2 = \lvert \mathbf{a} \rvert^2 + \lvert \mathbf{b} \rvert^2 - 2\lvert \mathbf{a} \rvert \lvert \mathbf{b} \rvert \cos\theta. Expanding the left side with the rules above gives ∣a−b∣2=(a−b)⋅(a−b)=∣a∣2−2a⋅b+∣b∣2\lvert \mathbf{a} - \mathbf{b} \rvert^2 = (\mathbf{a} - \mathbf{b}) \cdot (\mathbf{a} - \mathbf{b}) = \lvert \mathbf{a} \rvert^2 - 2\mathbf{a} \cdot \mathbf{b} + \lvert \mathbf{b} \rvert^2. Compare the two expressions and cancel: a⋅b=∣a∣∣b∣cos⁡θ\mathbf{a} \cdot \mathbf{b} = \lvert \mathbf{a} \rvert \lvert \mathbf{b} \rvert \cos\theta. ∎

Consequences:

Sign of a⋅b\mathbf{a} \cdot \mathbf{b} Angle Meaning
>0> 0 acute, θ<90∘\theta < 90^\circ point roughly the same way
=0= 0 θ=90∘\theta = 90^\circ orthogonal (perpendicular)
<0< 0 obtuse point roughly opposite ways

The zero vector counts as orthogonal to every vector.

Cauchy–Schwarz and the triangle inequality

Cauchy–Schwarz. ∣a⋅b∣≤∣a∣ ∣b∣\lvert \mathbf{a} \cdot \mathbf{b} \rvert \le \lvert \mathbf{a} \rvert \, \lvert \mathbf{b} \rvert, with equality exactly when a\mathbf{a} and b\mathbf{b} are parallel.

In R3\mathbb{R}^3 it follows at once from ∣cos⁡θ∣≤1\lvert \cos\theta \rvert \le 1. That argument is circular in Rn\mathbb{R}^n, where the angle is defined via the dot product, so here is a proof that works in any dimension. For every real tt,

0≤∣a+tb∣2=∣b∣2t2+2(a⋅b) t+∣a∣20 \le \lvert \mathbf{a} + t\mathbf{b} \rvert^2 = \lvert \mathbf{b} \rvert^2 t^2 + 2(\mathbf{a} \cdot \mathbf{b})\, t + \lvert \mathbf{a} \rvert^2

A quadratic in tt that is never negative has at most one real root, so its discriminant is ≤0\le 0: 4(a⋅b)2−4∣a∣2∣b∣2≤04(\mathbf{a} \cdot \mathbf{b})^2 - 4\lvert \mathbf{a} \rvert^2 \lvert \mathbf{b} \rvert^2 \le 0. ∎

The triangle inequality follows: ∣a+b∣2=∣a∣2+2a⋅b+∣b∣2≤∣a∣2+2∣a∣∣b∣+∣b∣2=(∣a∣+∣b∣)2\lvert \mathbf{a} + \mathbf{b} \rvert^2 = \lvert \mathbf{a} \rvert^2 + 2\mathbf{a} \cdot \mathbf{b} + \lvert \mathbf{b} \rvert^2 \le \lvert \mathbf{a} \rvert^2 + 2\lvert \mathbf{a} \rvert \lvert \mathbf{b} \rvert + \lvert \mathbf{b} \rvert^2 = (\lvert \mathbf{a} \rvert + \lvert \mathbf{b} \rvert)^2.

python · try it yourself
import 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 α,β,γ\alpha, \beta, \gamma that a\mathbf{a} makes with i,j,k\mathbf{i}, \mathbf{j}, \mathbf{k} are its direction angles. Their cosines are the components of the unit vector:

cos⁡α=a1∣a∣,cos⁡β=a2∣a∣,cos⁡γ=a3∣a∣,cos⁡2α+cos⁡2β+cos⁡2γ=1\cos\alpha = \frac{a_1}{\lvert \mathbf{a} \rvert}, \quad \cos\beta = \frac{a_2}{\lvert \mathbf{a} \rvert}, \quad \cos\gamma = \frac{a_3}{\lvert \mathbf{a} \rvert}, \qquad \cos^2\alpha + \cos^2\beta + \cos^2\gamma = 1

Projections

The scalar projection of b\mathbf{b} onto a\mathbf{a} (also called the component of b\mathbf{b} along a\mathbf{a}) and the vector projection are

comp⁡ab=a⋅b∣a∣=∣b∣cos⁡θproj⁡ab=(a⋅b∣a∣2)a=(comp⁡ab)⋅a∣a∣\begin{aligned} \operatorname{comp}_{\mathbf{a}} \mathbf{b} &= \frac{\mathbf{a} \cdot \mathbf{b}}{\lvert \mathbf{a} \rvert} && = \lvert \mathbf{b} \rvert \cos\theta \\ \operatorname{proj}_{\mathbf{a}} \mathbf{b} &= \left(\frac{\mathbf{a} \cdot \mathbf{b}}{\lvert \mathbf{a} \rvert^2}\right) \mathbf{a} && = (\operatorname{comp}_{\mathbf{a}} \mathbf{b}) \cdot \frac{\mathbf{a}}{\lvert \mathbf{a} \rvert} \end{aligned}

The remainder b−proj⁡ab\mathbf{b} - \operatorname{proj}_{\mathbf{a}} \mathbf{b} is orthogonal to a\mathbf{a}, because a⋅(b−proj⁡ab)=a⋅b−(a⋅b/∣a∣2)∣a∣2=0\mathbf{a} \cdot (\mathbf{b} - \operatorname{proj}_{\mathbf{a}} \mathbf{b}) = \mathbf{a} \cdot \mathbf{b} - (\mathbf{a} \cdot \mathbf{b}/\lvert \mathbf{a} \rvert^2)\lvert \mathbf{a} \rvert^2 = 0. So b\mathbf{b} splits uniquely into a part parallel to a\mathbf{a} plus a part perpendicular to a\mathbf{a}. That decomposition is the heart of Gram–Schmidt, least squares and collision response.

Work. A constant force F\mathbf{F} moving an object along displacement D\mathbf{D} does work W=F⋅D=∣F∣∣D∣cos⁡θW = \mathbf{F} \cdot \mathbf{D} = \lvert \mathbf{F} \rvert \lvert \mathbf{D} \rvert \cos\theta. Only the component of the force along the motion does work.

python · try it yourself
import 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 brightness=max⁡(0,n⋅l)\text{brightness} = \max(0, \mathbf{n} \cdot \mathbf{l}) for the unit surface normal n\mathbf{n} and the unit light direction l\mathbf{l}. 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 cos⁡θ=a⋅b/(∣a∣∣b∣)\cos\theta = \mathbf{a} \cdot \mathbf{b}/(\lvert \mathbf{a} \rvert \lvert \mathbf{b} \rvert) 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 [−1,1][-1, 1].
  • Neural networks. Every neuron computes w⋅x+b\mathbf{w} \cdot \mathbf{x} + b, 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 ⟨1,1,0⟩\langle 1, 1, 0 \rangle and ⟨0,1,1⟩\langle 0, 1, 1 \rangle.

Answer

60∘60^\circ (π/3\pi/3)

Solution
a⋅b=0+1+0=1,∣a∣=∣b∣=2cos⁡θ=12⋅2=12→θ=π3\begin{aligned} \mathbf{a} \cdot \mathbf{b} &= 0 + 1 + 0 = 1, \qquad \lvert \mathbf{a} \rvert = \lvert \mathbf{b} \rvert = \sqrt{2} \\ \cos\theta &= \frac{1}{\sqrt{2} \cdot \sqrt{2}} = \frac{1}{2} \quad\to\quad \theta = \frac{\pi}{3} \end{aligned}

Exercise 2: Orthogonality with a parameter

For which cc is ⟨c,2,−1⟩\langle c, 2, -1 \rangle orthogonal to ⟨c,−c,3⟩\langle c, -c, 3 \rangle?

Answer

c=3c = 3 or c=−1c = -1.

Solution
c⋅c+2(−c)+(−1)(3)=c2−2c−3=(c−3)(c+1)=0c \cdot c + 2(-c) + (-1)(3) = c^2 - 2c - 3 = (c - 3)(c + 1) = 0

Exercise 3: Decompose a vector

Write b=⟨4,1,−2⟩\mathbf{b} = \langle 4, 1, -2 \rangle as the sum of a vector parallel to a=⟨1,2,2⟩\mathbf{a} = \langle 1, 2, 2 \rangle and a vector orthogonal to a\mathbf{a}.

Answer

b=⟨2/9,4/9,4/9⟩+⟨34/9,5/9,−22/9⟩\mathbf{b} = \langle 2/9, 4/9, 4/9 \rangle + \langle 34/9, 5/9, -22/9 \rangle

Solution
a⋅b=4+2−4=2,∣a∣2=9proj⁡ab=29⟨1,2,2⟩=⟨2/9,4/9,4/9⟩b−proj⁡=⟨36/9−2/9,9/9−4/9,−18/9−4/9⟩=⟨34/9,5/9,−22/9⟩check: (34+10−44)/9=0 ✓\begin{aligned} \mathbf{a} \cdot \mathbf{b} &= 4 + 2 - 4 = 2, \qquad \lvert \mathbf{a} \rvert^2 = 9 \\ \operatorname{proj}_{\mathbf{a}} \mathbf{b} &= \tfrac{2}{9}\langle 1, 2, 2 \rangle = \langle 2/9, 4/9, 4/9 \rangle \\ \mathbf{b} - \operatorname{proj} &= \langle 36/9 - 2/9, 9/9 - 4/9, -18/9 - 4/9 \rangle = \langle 34/9, 5/9, -22/9 \rangle \\ &\text{check: } (34 + 10 - 44)/9 = 0 \ \checkmark \end{aligned}

Exercise 4: Nearest embedding (code)

Given the query q=⟨0.2,0.9,0.1⟩\mathbf{q} = \langle 0.2, 0.9, 0.1 \rangle and documents d1=⟨1,0,0⟩\mathbf{d}_1 = \langle 1, 0, 0 \rangle, d2=⟨0.3,0.8,0.5⟩\mathbf{d}_2 = \langle 0.3, 0.8, 0.5 \rangle, d3=⟨0,2,0⟩\mathbf{d}_3 = \langle 0, 2, 0 \rangle, rank the documents by cosine similarity. Why does d3\mathbf{d}_3 win even though it is longest?

Answer

d3 (≈0.9705)>d2 (≈0.9041)>d1 (≈0.2157)\mathbf{d}_3\ (\approx 0.9705) > \mathbf{d}_2\ (\approx 0.9041) > \mathbf{d}_1\ (\approx 0.2157). Cosine similarity ignores length, and d3\mathbf{d}_3 points almost exactly along q\mathbf{q}'s dominant direction.

Solution
python · try it yourself
import 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

a⋅b=0\mathbf{a} \cdot \mathbf{b} = 0 for nonzero a\mathbf{a} and b\mathbf{b} means…

  1. a\mathbf{a} and b\mathbf{b} are parallel
  2. a\mathbf{a} and b\mathbf{b} are orthogonal
  3. a=−b\mathbf{a} = -\mathbf{b}
  4. ∣a∣=∣b∣\lvert \mathbf{a} \rvert = \lvert \mathbf{b} \rvert

What is the scalar projection of ⟨6,2,3⟩\langle 6, 2, 3 \rangle onto ⟨0,3,4⟩\langle 0, 3, 4 \rangle?

  1. 18
  2. 18/2518/25
  3. 18/518/5
  4. 6

Why is cosine similarity always between −1-1 and 1?

  1. Because embeddings are normalised by the model
  2. Because of the Cauchy–Schwarz inequality ∣a⋅b∣≤∣a∣∣b∣\lvert \mathbf{a} \cdot \mathbf{b} \rvert \le \lvert \mathbf{a} \rvert \lvert \mathbf{b} \rvert
  3. Because dot products are always positive
  4. Because vectors have three components