TL;DR
The dot product takes two vectors of the same length, multiplies the entries in matching positions, and adds those products up. The result is one number, not a vector. It is large and positive when the vectors point in similar directions, zero when they are perpendicular, and negative when they point in opposite directions.
It is the core operation behind weighted sums, projections and the computation inside every neuron of a neural network.
Where you'll see it: in the score a neural network layer computes for each input, in cosine similarity used to compare embeddings, and in the work done by a force pushing an object along a path.
Intuition
A weighted total
Suppose a shop sells three items at prices 2, 3 and 5 pounds, and you buy 4, 1 and 2 of them. Multiply each price by its matching quantity and add: . Write the prices as a vector and the quantities as , and the total is the dot product .
Any time you compute "each thing times its weight, then summed", you are taking a dot product. That is why it is everywhere. A neuron's input is a weighted sum of the values coming into it, and the weights form a vector.
A measure of agreement
The dot product also measures how much two arrows agree in direction. Take a vector and a second vector . Shine a light straight down onto the line through and look at the shadow that casts on it. The dot product is the length of that shadow times the length of .
If points the same way as , the shadow is long and the answer is large and positive. If is at right angles to , the shadow is a point and the answer is zero. If points backwards, the answer is negative. The plot shows this with fixed at length 1 and turned through an angle .
- a · b / (|a||b|) = cos θ
This is the reason the dot product matters so much. It answers "how much of lies along ?" without ever computing an angle.
The math
Definition
For two vectors in , each with real entries, the dot product is
Here is the entry of in position , and the sum runs over all positions using summation notation. The two vectors must have the same length, because the pairing of entries is only defined then. The result is a single real number, a scalar.
Length from the dot product
Dotting a vector with itself gives its squared length:
So .
The geometric form
The entry-wise definition and the angle picture agree. Take a triangle with sides , and , and let be the angle between and . The law of cosines gives
Expand the left side using the previous section: . Setting the two expressions equal, the squared lengths cancel and leave
Rearranging gives the cosine of the angle directly:
Three consequences follow. If the angle is acute. If it is zero, the vectors are perpendicular (orthogonal), provided both are nonzero. If it is negative, the angle is obtuse.
Projection
The scalar projection of onto is the signed length of that shadow:
The vector projection keeps the direction of :
Algebraic rules
The dot product is commutative, , distributes over addition, , and pulls out scalars, . These rules are what let you rearrange sums in neural network and regression formulas.
Link to matrices
If is a matrix with rows and is a vector, then the -th entry of is . Each entry of a matrix-vector product is one dot product, which is why matrix multiplication is built from dot products.
Worked examples
Let and .
- Multiply matching entries: , , .
- Add: .
- Lengths: and .
- Cosine: .
- Angle: . The result is positive, so the vectors point broadly the same way, but not closely.
A neuron takes inputs , has weights and bias . Its pre-activation value is .
- Pair the entries: , , .
- Add the products: . This is .
- Add the bias: .
- A ReLU activation keeps only positive values, so the neuron outputs .
The neuron has a single dot product at its core. Training adjusts and so that the score is large for inputs the neuron should respond to.
Let and . How far does reach in the direction of ?
- Dot product: .
- Length of : .
- Scalar projection: . So reaches 3 units along .
- Vector projection: . The -part of , which is 4, lies entirely off the line of .
Tradeoffs and failure modes
The dot product answers one specific question; other situations call for something else.
| Situation | Problem | Better choice |
|---|---|---|
| Comparing vectors of very different lengths | The dot product grows with length, so a long vector scores high whatever its direction | Cosine similarity, which divides out the lengths |
| Entries in different units (metres, seconds, dollars) | The sum adds quantities that do not share a unit | Rescale or standardise the entries first |
| Measuring how far apart two points are | A dot product can be negative and is not a distance | Euclidean distance, |
| Very long, mostly-zero vectors (text bags of words) | Summing over every position wastes work | A sparse dot product that stores only the nonzero entries |
| Weighting dimensions unequally | The plain dot product treats every position equally | A weighted inner product |
Two mistakes are common. First, confusing the dot product with the entry-wise product . The entry-wise product is a vector; the dot product is its sum. Second, mixing lengths and directions: if you want only direction, normalise both vectors to length 1 first, and then the dot product is exactly the cosine.
History
In 1844 Hermann Grassmann published Die lineale Ausdehnungslehre, a general theory of products of quantities with direction and magnitude. It contained the idea behind what we now call the inner product. The vector notation most people learn came later. In the 1880s Josiah Willard Gibbs and Oliver Heaviside, working on vector analysis for electromagnetism, each developed a vector algebra with the dot product and the notation , and Gibbs's lecture notes were turned into the textbook Vector Analysis by Edwin Bidwell Wilson in 1901.
It caught on because physicists needed it immediately. Work done by a force is the force dotted with the displacement, and fields and flows are easier to describe with it. Later, mathematicians generalised the idea to inner product spaces, and that abstract form became the setting for quantum mechanics and for much of functional analysis.
Check yourself
QuestionWhat is the dot product of two nonzero vectors that are perpendicular to each other, and why?Show answer
It is zero. Perpendicular means the angle is , so and . The shadow of one vector on the other is a single point, so no part of one lies along the other.
QuestionCompute the dot product of and .Show answer
Pair the entries: , , . Add them: .
QuestionWhy is always non-negative, and what does it equal?Show answer
Each term in the sum is , and a square is never negative, so the total is never negative. It equals , the squared length of . That is why the length is the square root of .
QuestionA recommender represents a user's taste as a vector and each film as a vector. The dot product of with film A is 9.0, and with film B is 0.4. Film A's vector is also much longer than film B's. Which film should you recommend, and what would you check before trusting the ranking?Show answer
Film A has the larger dot product, but the length of its vector inflates that score. Before ranking by the raw dot product, check whether the lengths are doing the work. If film A is simply longer, normalise each film vector and compare cosines. A film whose direction matches the user's taste is the better recommendation, whatever its length.
Sources
- Dot product (Wikipedia)
- Cosine similarity (Wikipedia)
- Gilbert Strang, Introduction to Linear Algebra (Wellesley-Cambridge Press): https://math.mit.edu/~gs/linearalgebra/