The dot product: how much two vectors agree
Read these first if they are new to you
- Vectors: arrows made of numbers Basic
- Distance between two points Basic
- Sine waves Basic
The idea in one paragraph
The dot product takes two vectors of the same size and gives back one number. You multiply the matching components and add the results. The number is large and positive when the arrows point the same way, zero when they meet at a right angle, and negative when they point apart. So it measures how much two vectors agree. It is also the weighted sum behind shopping bills, matrices and much of machine learning.
Multiply matching components, then add
Start with a shopping bill. You buy 3 apples and 2 pears. Apples cost 50 cents and pears 80 cents. The bill is 3 × 50 + 2 × 80 = 310 cents. You multiplied each amount by its price and added the products.
That is the dot product of the amounts (3, 2) and the prices (50, 80). It is written with a dot between the vectors:
a · b = a1b1 + a2b2 + … + anbn
Both vectors must have the same number of components, n. The result is a single number, not a vector. A sum where each item is first multiplied by its own factor is called a weighted sum, and the factors are its weights.
Take a = (3, 1) and b = (1, 2).
- Multiply the first components: 3 × 1 = 3.
- Multiply the second components: 1 × 2 = 2.
- Add: 3 + 2 = 5. So a · b = 5.
Now take c = (−1, 3). Then a · c = 3 × (−1) + 1 × 3 = 0.
Your turn. Find (2, −1, 4) · (1, 3, 0.5).
Answer
2 × 1 + (−1) × 3 + 4 × 0.5 = 2 − 3 + 2 = 1.
The sign tells you whether two arrows point the same way
In the worked example, a · c came out zero. Draw the two arrows and you find they meet at a right angle. That is no accident. The dot product has a second meaning, in terms of the arrows' lengths and the angle θ (the Greek letter theta) between them:
a · b = |a| × |b| × cos θ
Here |a| is the length of a, from the distance unit. The cosine, cos θ, is 1 when the angle is 0°, 0 at 90°, and −1 at 180°; the sine-waves unit explains it. So, for arrows of fixed length:
- pointing the same way, the dot product is as large as it can be;
- at a right angle, it is zero;
- pointing in opposite directions, it is as negative as it can be.
a · b = angle
The yellow bar shows why this works. The dot product equals the length of a times the signed length of the shadow that b casts on the line through a. A shadow pointing back along a counts as negative. A vector at a right angle casts no shadow, so the product is zero.
"How much two vectors agree" mixes two things: the angle and the lengths. Doubling either vector doubles the dot product, even though the angle has not changed. To compare directions alone, divide by both lengths. The result, cos θ, is called the cosine similarity, and it always lies between −1 and 1.
dot.py: NumPy writes the dot product as np.dot(a, b) or a @ b
import numpy as np
a = np.array([3, 1])
b = np.array([1, 2])
print(np.dot(a, b)) # 3*1 + 1*2
print(a @ b) # the same, written with @
c = np.array([-1, 3])
print(a @ c) # 3*(-1) + 1*3 = 0: a right angle
cos = (a @ b) / (np.linalg.norm(a) * np.linalg.norm(b))
# np.arccos gives the angle, in radians, whose cosine is cos.
# np.degrees turns radians into degrees.
print(round(np.degrees(np.arccos(cos)), 1)) # the angle between a and b
5 5 0 45.0
Check yourself
-
Is the angle between (2, 3) and (3, −2) less than, equal to, or more than 90°?
Answer
Equal: 2 × 3 + 3 × (−2) = 0, so the arrows meet at a right angle.
-
What is a · a for a = (3, 4)? How does it relate to the length of a?
Answer
9 + 16 = 25, the square of the length 5. A vector dotted with itself is always its squared length.
-
A score is computed as 0.5 × size + 2 × color + (−1) × noise. Write it as a dot product.
Answer
(0.5, 2, −1) · (size, color, noise). The first vector holds the weights, the second the inputs.
Go further
Optional background, for readers who want more than this page.
-
The shadow picture, animated, and why the two meanings of the dot product agree.
-
Plain-language intuition for the dot product as "how much one vector goes along another."
Where this shows up
Units that build on this one
- Multiplying a matrix by a vector Basic
- Attention: queries, keys, values Advanced