Search

Search papers and lessons

Dot Product

Level 1 · 9 min read

The dot product multiplies two vectors entry by entry, adds the results into one number, and measures how much the two vectors point the same way.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

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: 2⋅4+3⋅1+5⋅2=212 \cdot 4 + 3 \cdot 1 + 5 \cdot 2 = 21. Write the prices as a vector p=(2,3,5)\mathbf{p} = (2, 3, 5) and the quantities as q=(4,1,2)\mathbf{q} = (4, 1, 2), and the total is the dot product p⋅q\mathbf{p} \cdot \mathbf{q}.

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 b\mathbf{b} and a second vector a\mathbf{a}. Shine a light straight down onto the line through b\mathbf{b} and look at the shadow that a\mathbf{a} casts on it. The dot product is the length of that shadow times the length of b\mathbf{b}.

If a\mathbf{a} points the same way as b\mathbf{b}, the shadow is long and the answer is large and positive. If a\mathbf{a} is at right angles to b\mathbf{b}, the shadow is a point and the answer is zero. If a\mathbf{a} points backwards, the answer is negative. The plot shows this with b\mathbf{b} fixed at length 1 and a\mathbf{a} turned through an angle θ\theta.

00.511.522.53−1−0.500.51angle θ between a and b (radians)a · b for unit vectors
  • a · b / (|a||b|) = cos θ
For unit-length vectors the dot product is cos θ. It is 1 when the vectors align (θ = 0), 0 at right angles, and −1 when they point opposite ways.

This is the reason the dot product matters so much. It answers "how much of a\mathbf{a} lies along b\mathbf{b}?" without ever computing an angle.

The math

Definition

Definition: Dot product

For two vectors a,b\mathbf{a}, \mathbf{b} in Rn\mathbb{R}^n, each with nn real entries, the dot product is

a⋅b=∑i=1naibi=a1b1+a2b2+⋯+anbn.\mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i b_i = a_1 b_1 + a_2 b_2 + \cdots + a_n b_n.

Here aia_i is the entry of a\mathbf{a} in position ii, and the sum runs over all nn 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:

a⋅a=a12+a22+⋯+an2=∥a∥2.\mathbf{a} \cdot \mathbf{a} = a_1^2 + a_2^2 + \cdots + a_n^2 = \|\mathbf{a}\|^2.

So ∥a∥=a⋅a\|\mathbf{a}\| = \sqrt{\mathbf{a} \cdot \mathbf{a}}.

The geometric form

The entry-wise definition and the angle picture agree. Take a triangle with sides a\mathbf{a}, b\mathbf{b} and a−b\mathbf{a} - \mathbf{b}, and let θ\theta be the angle between a\mathbf{a} and b\mathbf{b}. The law of cosines gives

∥a−b∥2=∥a∥2+∥b∥2−2 ∥a∥ ∥b∥cos⁡θ.\|\mathbf{a} - \mathbf{b}\|^2 = \|\mathbf{a}\|^2 + \|\mathbf{b}\|^2 - 2\,\|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta.

Expand the left side using the previous section: ∥a−b∥2=(a−b)⋅(a−b)=∥a∥2−2 a⋅b+∥b∥2\|\mathbf{a} - \mathbf{b}\|^2 = (\mathbf{a} - \mathbf{b}) \cdot (\mathbf{a} - \mathbf{b}) = \|\mathbf{a}\|^2 - 2\,\mathbf{a} \cdot \mathbf{b} + \|\mathbf{b}\|^2. Setting the two expressions equal, the squared lengths cancel and leave

a⋅b=∥a∥ ∥b∥cos⁡θ.\mathbf{a} \cdot \mathbf{b} = \|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta.

Rearranging gives the cosine of the angle directly:

cos⁡θ=a⋅b∥a∥ ∥b∥.\cos\theta = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\|\,\|\mathbf{b}\|}.

Three consequences follow. If a⋅b>0\mathbf{a} \cdot \mathbf{b} \gt 0 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 a\mathbf{a} onto b\mathbf{b} is the signed length of that shadow:

comp⁡ba=a⋅b∥b∥.\operatorname{comp}_{\mathbf{b}} \mathbf{a} = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{b}\|}.

The vector projection keeps the direction of b\mathbf{b}:

proj⁡ba=a⋅bb⋅b b.\operatorname{proj}_{\mathbf{b}} \mathbf{a} = \frac{\mathbf{a} \cdot \mathbf{b}}{\mathbf{b} \cdot \mathbf{b}}\,\mathbf{b}.

Algebraic rules

The dot product is commutative, a⋅b=b⋅a\mathbf{a} \cdot \mathbf{b} = \mathbf{b} \cdot \mathbf{a}, distributes over addition, a⋅(b+c)=a⋅b+a⋅c\mathbf{a} \cdot (\mathbf{b} + \mathbf{c}) = \mathbf{a} \cdot \mathbf{b} + \mathbf{a} \cdot \mathbf{c}, and pulls out scalars, (ka)⋅b=k (a⋅b)(k\mathbf{a}) \cdot \mathbf{b} = k\,(\mathbf{a} \cdot \mathbf{b}). These rules are what let you rearrange sums in neural network and regression formulas.

If AA is a matrix with rows r1,…,rm\mathbf{r}_1, \dots, \mathbf{r}_m and x\mathbf{x} is a vector, then the ii-th entry of AxA\mathbf{x} is ri⋅x\mathbf{r}_i \cdot \mathbf{x}. Each entry of a matrix-vector product is one dot product, which is why matrix multiplication is built from dot products.

Worked examples

Example
Dot product and angle of two vectors

Let a=(1,2,3)\mathbf{a} = (1, 2, 3) and b=(4,−5,6)\mathbf{b} = (4, -5, 6).

  1. Multiply matching entries: 1⋅4=41 \cdot 4 = 4, 2⋅(−5)=−102 \cdot (-5) = -10, 3⋅6=183 \cdot 6 = 18.
  2. Add: a⋅b=4−10+18=12\mathbf{a} \cdot \mathbf{b} = 4 - 10 + 18 = 12.
  3. Lengths: ∥a∥=14≈3.742\|\mathbf{a}\| = \sqrt{14} \approx 3.742 and ∥b∥=77≈8.775\|\mathbf{b}\| = \sqrt{77} \approx 8.775.
  4. Cosine: cos⁡θ=12/(3.742×8.775)=12/32.83≈0.3655\cos\theta = 12 / (3.742 \times 8.775) = 12 / 32.83 \approx 0.3655.
  5. Angle: θ≈68.6∘\theta \approx 68.6^\circ. The result is positive, so the vectors point broadly the same way, but not closely.
Example
One neuron as a dot product

A neuron takes inputs x=(0.5,−1,2)\mathbf{x} = (0.5, -1, 2), has weights w=(0.4,0.3,−0.1)\mathbf{w} = (0.4, 0.3, -0.1) and bias b=0.2b = 0.2. Its pre-activation value is w⋅x+b\mathbf{w} \cdot \mathbf{x} + b.

  1. Pair the entries: 0.4⋅0.5=0.20.4 \cdot 0.5 = 0.2, 0.3⋅(−1)=−0.30.3 \cdot (-1) = -0.3, −0.1⋅2=−0.2-0.1 \cdot 2 = -0.2.
  2. Add the products: 0.2−0.3−0.2=−0.30.2 - 0.3 - 0.2 = -0.3. This is w⋅x\mathbf{w} \cdot \mathbf{x}.
  3. Add the bias: −0.3+0.2=−0.1-0.3 + 0.2 = -0.1.
  4. A ReLU activation keeps only positive values, so the neuron outputs max⁡(0,−0.1)=0\max(0, -0.1) = 0.

The neuron has a single dot product at its core. Training adjusts w\mathbf{w} and bb so that the score is large for inputs the neuron should respond to.

Example
How much of one vector lies along another

Let a=(3,4)\mathbf{a} = (3, 4) and b=(5,0)\mathbf{b} = (5, 0). How far does a\mathbf{a} reach in the direction of b\mathbf{b}?

  1. Dot product: a⋅b=3⋅5+4⋅0=15\mathbf{a} \cdot \mathbf{b} = 3 \cdot 5 + 4 \cdot 0 = 15.
  2. Length of b\mathbf{b}: ∥b∥=5\|\mathbf{b}\| = 5.
  3. Scalar projection: 15/5=315 / 5 = 3. So a\mathbf{a} reaches 3 units along b\mathbf{b}.
  4. Vector projection: (15/25) (5,0)=(3,0)(15 / 25)\,(5, 0) = (3, 0). The yy-part of a\mathbf{a}, which is 4, lies entirely off the line of b\mathbf{b}.

Tradeoffs and failure modes

The dot product answers one specific question; other situations call for something else.

SituationProblemBetter choice
Comparing vectors of very different lengthsThe dot product grows with length, so a long vector scores high whatever its directionCosine similarity, which divides out the lengths
Entries in different units (metres, seconds, dollars)The sum adds quantities that do not share a unitRescale or standardise the entries first
Measuring how far apart two points areA dot product can be negative and is not a distanceEuclidean distance, ∥a−b∥\|\mathbf{a} - \mathbf{b}\|
Very long, mostly-zero vectors (text bags of words)Summing over every position wastes workA sparse dot product that stores only the nonzero entries
Weighting dimensions unequallyThe plain dot product treats every position equallyA weighted inner product a⊤Wb\mathbf{a}^\top W \mathbf{b}

Two mistakes are common. First, confusing the dot product with the entry-wise product (a1b1,…,anbn)(a_1 b_1, \dots, a_n b_n). 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 a⋅b\mathbf{a} \cdot \mathbf{b}, 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 θ\theta is 90∘90^\circ, so cos⁡θ=0\cos\theta = 0 and a⋅b=∥a∥ ∥b∥cos⁡θ=0\mathbf{a} \cdot \mathbf{b} = \|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta = 0. 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 a=(2,−1,4)\mathbf{a} = (2, -1, 4) and b=(3,6,0.5)\mathbf{b} = (3, 6, 0.5).Show answer

Pair the entries: 2⋅3=62 \cdot 3 = 6, (−1)⋅6=−6(-1) \cdot 6 = -6, 4⋅0.5=24 \cdot 0.5 = 2. Add them: 6−6+2=26 - 6 + 2 = 2.

QuestionWhy is a⋅a\mathbf{a} \cdot \mathbf{a} always non-negative, and what does it equal?Show answer

Each term in the sum is ai⋅ai=ai2a_i \cdot a_i = a_i^2, and a square is never negative, so the total is never negative. It equals ∥a∥2\|\mathbf{a}\|^2, the squared length of a\mathbf{a}. That is why the length is the square root of a⋅a\mathbf{a} \cdot \mathbf{a}.

QuestionA recommender represents a user's taste as a vector u\mathbf{u} and each film as a vector. The dot product of u\mathbf{u} 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