Dot product, explained with pictures
The dot product turns two arrows into one number. That number tells you how much the two arrows point the same way. Grab the tip of an arrow and drag it.
Short answer. The dot product turns two vectors into one number. Multiply the numbers in matching places, then add. (3, 1) · (1, 2) = 3×1 + 1×2 = 5. What it means: how far b goes in the direction of a (its shadow) × the length of a. As a formula, that is ‖a‖ × ‖b‖ × cos θ.
a = ( , )
b = ( , )
Drag the circles at the arrow tips, or change the numbers. On the bar, the top scale is the dot product (from −‖a‖×‖b‖ to ‖a‖×‖b‖) and the bottom scale is cosine similarity (from −1 to 1). The two scales always point to the same spot.
Definitions
The text below uses a plane, but the definitions are the same in ℝⁿ. a = (a₁, a₂, …, aₙ) and b = (b₁, b₂, …, bₙ) are vectors in ℝⁿ.
- dot product
- The real number you get by multiplying the components in the same position and adding them all.
- norm (length)
- The positive square root of the dot product of a vector with itself.
- angle between vectors
- When a ≠ 0 and b ≠ 0, the θ with 0° ≤ θ ≤ 180° that satisfies the first formula below is the angle between a and b. So the second formula holds.
- orthogonal
- a and b are orthogonal when their dot product is 0. The zero vector is orthogonal to every vector.
- projection
- When a ≠ 0, the projection of b onto a is the vector in the first formula below. The "length of the shadow" in the text is the value in the second formula, and it is negative when the angle is more than 90°.
- cosine similarity
- When a ≠ 0 and b ≠ 0, the dot product divided by the product of the two norms. It equals cos θ of the angle between them, and it is between −1 and 1.
1. Definition: multiply matching numbers, then add
A vector is a group of numbers. Drawn as an arrow, (3, 1) means "3 to the right, 1 up." If vectors are new to you, start with the vectors page. It gives a short intro to the dot product. This page goes deeper.
The dot product takes two vectors and gives back one number. Multiply across by across, multiply up by up, then add the two results.
It is written with a dot in the middle, which is where the name comes from. The result is a number, not an arrow.
Same formula in 3D, or in 1000 dimensions
There are just more places. Multiply the numbers in matching places and add them all. (1, 2, 3) · (4, 5, 6) = 1×4 + 2×5 + 3×6 = 4 + 10 + 18 = 32. In code it is one line.
const dot = (a, b) => a.reduce((sum, x, i) => sum + x * b[i], 0);
Both vectors need the same number of places. If they do not match, there is no dot product.
2. Meaning: shadow length × length
Look at the orange line in the lab. From the tip of b, a line drops onto the line of a at a right angle (the dashed line). The part from the origin to where it lands is the shadow. Think of light shining onto the line of a at a right angle. The shadow is what b leaves on that line.
The shadow length is "how far b goes in the direction of a." The dot product is that shadow length times the length of a.
The shadow length is the length of b times cos θ. θ (theta) is the angle between a and b. That gives the second formula.
‖a‖ is the length of a. cos θ is a number that is 1 at 0°, 0 at 90° and −1 at 180°. As the angle opens up, it goes down from 1 to −1. Past 90° it is negative, and the shadow falls behind a.
The two formulas always give the same number
However you drag the arrows, the top line of the readout (multiply and add) and the "‖a‖ × ‖b‖ × cos θ" line match. To see why, step through 3. How the formula is built.
The two formulas have different jobs. When you know the numbers, the first one is fast. Put that result into the second one and solve backwards, and you get the angle.
Try this. Start from a = (3, 1), b = (1, 2) and press the buttons. They keep the length of b at 2.24 and only turn it. ① "b at a right angle to a": b becomes (−0.71, 2.12), the shadow shrinks to a point and the dot product is 0. ② "Same direction": the shadow length becomes 2.24, the same as the length of b, and the dot product is at its largest, 7.07 (= ‖a‖ × ‖b‖ = √10 × √5). ③ "Opposite direction": the dot product is at its smallest, −7.07.
3. How the formula is built: why the two formulas match
The formula in section 1 (multiply matching numbers, then add) and the one in section 2 (‖a‖ × ‖b‖ × cos θ) look nothing alike. Here is why they always give the same number, one step at a time. The whole idea fits in one line: turn both arrows together until a lies flat on the horizontal axis, and the first formula turns into the second. The arrows are the lab's starting values, a = (3, 1) and b = (1, 2).
Press "Next" to go one step at a time. A line is added to the formula above (the orange background marks the new piece), the arrows turn to match, and the reason appears below. The faint arrows show where the arrows started. The turned arrows are called a' and b', and their across and up numbers are written a'x, a'y and so on. In step ② you can change the turn with the slider (+ means counterclockwise).
Written only as equations, the steps above look like this.
- Start:
a · b = ax×bx + ay×by.3×1 + 1×2 = 5. The lengths are‖a‖ = √10 ≈ 3.16and‖b‖ = √5 ≈ 2.24, and the angle between them is 45°. - Turn both together:
= a'x×b'x + a'y×b'y. Turn both arrows by the same angle, as if you turned the whole plane. Their lengths and the angle between them stay the same. The multiply-and-add value stays the same too. Turn by 90° and you get a' = (−1, 3) and b' = (−2, 1), and(−1)×(−2) + 3×1 = 2 + 3 = 5. Why: since the value does not change, you may turn things into an easier position before you work it out. - Lay a flat:
= ‖a‖×b'x + 0×b'y. Turn 18.43° clockwise and a lies on the horizontal axis. Its length does not change, soa' = (‖a‖, 0) = (3.16, 0), and b' is (1.58, 1.58). Why: the up number of a' is 0, so the second term drops out and the formula shrinks to‖a‖ × b'x. - b'x is the shadow:
= ‖a‖ × ‖b‖ × cos θ. Drop a line from the tip of b' straight down to the horizontal axis. From the origin to where it lands is b'x. Since a' lies on the horizontal axis, b' opens at θ from that axis. An arrow of length ‖b‖ that opens at θ from the horizontal axis goes‖b‖ × cos θacross. That is what cos means. Here:2.24 × cos 45° ≈ 1.58. Why: write the leftover b'x with length and angle, and you have the second formula. Past 90°, cos θ is negative, so b'x is negative too (the shadow falls behind a). If b' is below the axis, it still goes the same distance across. - Done:
a · b = ax×bx + ay×by = ‖a‖ × ‖b‖ × cos θ. The value before turning equals the value after, and the value after is‖a‖ × ‖b‖ × cos θ. With numbers:√10 × √5 × cos 45° = √50 × (1 ÷ √2) = √25 = 5, the same as the first formula. Turn back, and the shadow follows a onto its line. That is the "shadow length × length of a" from section 2.
Checking with algebra that turning keeps the value. Turn by an angle φ (phi) and the numbers become x' = x cos φ − y sin φ and y' = x sin φ + y cos φ. Put these into a'x×b'x + a'y×b'y and multiply out. The terms with cos φ × sin φ cancel each other, and ax×bx × (cos²φ + sin²φ) + ay×by × (sin²φ + cos²φ) is left. Since cos²φ + sin²φ = 1, that is the starting ax×bx + ay×by.
This page shows it on a plane (two places). In 3D the same path works: turn so that a lies on one axis and b lies in a flat plane through that axis. We do not prove that here.
4. Work it out by hand
Take a = (3, 1) and b = (1, 2), and work out both formulas. This is how the lab starts.
- Multiply matching numbers. Across: 3 × 1 = 3. Up: 1 × 2 = 2.
- Add. 3 + 2 = 5. So
a · b = 5. It is positive, so the two point roughly the same way. - Find the lengths.
‖a‖ = √(3² + 1²) = √10 ≈ 3.16,‖b‖ = √(1² + 2²) = √5 ≈ 2.24. - Find cos θ. Solve the second formula backwards:
cos θ = 5 ÷ (√10 × √5) = 5 ÷ √50 ≈ 5 ÷ 7.07 ≈ 0.71. The angle whose cos is 0.71 (exactly 1 ÷ √2) is 45°. - Find the shadow length.
5 ÷ ‖a‖ = 5 ÷ 3.16 ≈ 1.58. b goes 1.58 in the direction of a. - Check with the second formula.
‖a‖ × ‖b‖ × cos 45° = √50 × 0.707 ≈ 7.07 × 0.707 ≈ 5. Same as the first formula.
Written as equations, the same work looks like this.
5. The sign: positive, 0, negative
‖a‖ and ‖b‖ are never negative. So the sign of the dot product comes from the sign of cos θ, and that comes from the angle.
| Dot product | Angle between | Shadow | Meaning |
|---|---|---|---|
| Positive | Less than 90° | Falls on a's side | They point roughly the same way |
| 0 | 90° | Just a point | They are at a right angle |
| Negative | More than 90° | Falls behind a | They point roughly opposite ways |
Testing for a right angle
To check if two arrows are at a right angle, just check whether the dot product is 0. You do not need the angle. It is only multiplying and adding.
Example: (3, 1) · (−1, 3) = 3×(−1) + 1×3 = 0. So they are at a right angle. On a plane, if you swap the two numbers of (x, y) and put a minus on one, (−y, x) is always at a right angle to it. That is because x×(−y) + y×x = 0.
The zero vector (0, 0) gives 0 with anything. That is why textbooks say (0, 0) is orthogonal to every vector. But it has no arrow, so there is no angle. The lab does not show an angle in that case either.
Try this. Start from the beginning. ① Set b to (−1, 3). The dot product is 0 and the angle is 90°. ② Set b to (−2, 1). You get 3×(−2) + 1×1 = −5. The angle is 135° and the shadow length is −1.58, so it falls behind a. ③ Set a to (0, 0). The dot product is 0 and no angle is shown.
6. Length and angle from the dot product
Take the dot product of a vector with itself and you get its length squared. a · a = ax² + ay², and by the Pythagorean theorem that is the length squared. So
Example: (3, 4) · (3, 4) = 9 + 16 = 25, so the length is √25 = 5. It works the same way in 3D and beyond.
Put the two formulas together and you get the angle too.
You get cos θ with only multiplying, adding and a square root. No protractor. If you need the angle itself, use the function that undoes cos (arccos, or Math.acos in code). Math.acos answers in radians, so multiply by 180 ÷ π to get degrees.
When you write the code. Rounding errors can make a · b ÷ (‖a‖ × ‖b‖) come out a tiny bit over 1, like 1.0000000000000002. Then Math.acos returns NaN. Clamp the value between −1 and 1 first. If a vector has length 0, you would divide by 0, so handle that case on its own.
7. Cosine similarity: the dot product without length
The dot product grows with length. Make b twice as long and the dot product doubles. But often you only want to know "how much do they point the same way?" Then divide the dot product by both lengths. It is the same cos θ as above, and it is called cosine similarity. 1 means the same direction, 0 means a right angle, −1 means exactly opposite.
The bar in the lab shows this. The end of the top scale (dot product) is ‖a‖ × ‖b‖. The end of the bottom scale (cosine similarity) is 1. Since dot product = ‖a‖ × ‖b‖ × cos θ, the two scales always point to the same spot.
If both lengths are 1, the dot product is the cosine similarity. There is nothing to divide by. Example: (3, 4) and (4, 3) have a dot product of 24, and a cosine similarity of 24 ÷ (5 × 5) = 0.96. Shrink them to length 1 and you get (0.6, 0.8) and (0.8, 0.6). Their dot product is 0.48 + 0.48 = 0.96, and their cosine similarity is also 0.96. Dividing a vector by its own length to make its length 1 is called normalizing.
In AI search
AI turns sentences, images and products into vectors of hundreds to thousands of numbers. These are called embeddings. They are built so that things with similar meanings point in similar directions. So a search works like this.
- Turn the question into a vector the same way.
- For each stored vector, find its cosine similarity with the question vector.
- Pick the few with the highest values.
If the vectors are normalized ahead of time, step 2 is a single dot product. No division and no square root. Some embedding services already give you vectors with length 1. When a vector database asks you to pick a "distance metric" from cosine, dot product (inner product) and euclidean, the first two are what this page is about. "Cosine distance" is 1 − cosine similarity, so smaller means closer.
Try this. Start from the beginning and set b to (2, 4), twice as long. The dot product doubles from 5 to 10, but cos θ stays at 0.71 and the angle stays at 45°. The marker on the bar stays in the same spot. Only the number at the end of the top scale changes, from 7.07 to 14.14.
8. Rules worth knowing
The examples use a = (3, 1), b = (1, 2), c = (−1, 3) and k = 2.
| Rule | Formula | With numbers |
|---|---|---|
| Order does not matter | a · b = b · a | (1, 2) · (3, 1) = 3 + 2 = 5 |
| Add first or do each and add: same result | a · (b + c) = a · b + a · c | (3, 1) · (0, 5) = 5 = 5 + 0 |
| A number multiplied in can come out front | (k × a) · b = k × (a · b) | (6, 2) · (1, 2) = 10 = 2 × 5 |
| With itself, it is the length squared | a · a = ‖a‖² | (3, 1) · (3, 1) = 10 = (√10)². It is 0 only for (0, 0) |
| 0 means a right angle (when neither length is 0) | a · b = 0 | (3, 1) · (−1, 3) = 0 |
Because of the first rule, you can drop the shadow either way. a · b is also "how far a goes in the direction of b × the length of b."
Where is it used?
- AI search and recommendations. See section 7. Questions and documents, or people and products, are turned into vectors. The ones with the highest cosine similarity or dot product are picked.
- Neural networks. One neuron takes the dot product of the input vector and its weight vector, then adds one number (the bias). Matrix multiplication is also many dot products, one row with one column each time.
- Work in physics. Work = force · movement (both are vectors). Only the part of the force that points along the movement does work. Pull a box 5 m with a force of 10 N, angled 60° up from the floor, and the work is
10 × 5 × cos 60° = 10 × 5 × 0.5 = 25 J. If you hold a box and walk sideways, the holding force (up) and the movement (sideways) are at a right angle, so that force does 0 work. - Brightness in graphics. A surface is brighter the more directly it faces the light. Take the direction the surface faces and the direction from the surface toward the light (both length 1). Their dot product is the brightness ratio. If it is negative, the light is behind the surface, so it is set to 0.
- Games: in front or behind? Take the direction you are facing and the direction from you to an enemy. If their dot product is positive, the enemy is in front. If negative, it is behind.
Common questions
What is the dot product?
A way to turn two vectors into one number. Multiply the numbers in matching places, then add. The result equals how far one vector goes along the other (its shadow) times the length of the other. You can also get it from ‖a‖ × ‖b‖ × cos θ.
What does a dot product of 0 mean?
The two vectors are at a right angle. One does not go along the other at all, so the shadow is just a point. The dot product is also 0 when one of them has length 0.
What is the difference between the dot product and cosine similarity?
Cosine similarity is the dot product divided by the two lengths multiplied together. It ignores length and only looks at direction, and it is always between −1 and 1. If both vectors have length 1, the dot product and cosine similarity are the same number.
Is it the same formula in 3 or 1000 dimensions?
Yes. There are just more places. Multiply the numbers in matching places and add them all. (1, 2, 3) · (4, 5, 6) = 4 + 10 + 18 = 32. Length, angle and cosine similarity also use the same formulas.
What is the difference between the dot product and the cross product?
The dot product gives one number and measures how much two vectors point the same way. The cross product works in 3D and gives a vector at a right angle to both. Its length is the area of the parallelogram the two vectors make.
Words
| Word used here | Word used in textbooks |
|---|---|
| dot product | dot product, scalar product. Written a · b. "Inner product" is a wider term, and the dot product is the most common kind |
| numbers in matching places (across, up) | components |
| length | magnitude, norm. Written ‖a‖ or ‖a‖ |
| angle between, θ | angle between vectors. θ is read "theta" |
| shadow | projection |
| shadow length | scalar projection (it can be negative) |
| at a right angle | perpendicular, orthogonal. Written a ⊥ b |
| making the length 1 | normalization. A vector of length 1 is a unit vector |
| cosine similarity | cosine similarity |
| number of places | dimension |
| order does not matter · add first or after, same result | commutative law · distributive law |