TExcelTAD
Home › Linear Algebra & Matrix Theory › The Dot Product

The Dot Product

The dot product multiplies two vectors and returns a single number. That number measures how much the vectors point the same way: it gives angles, tests for perpendicularity, and computes projections. In software it is probably the most executed formula there is: lighting in every 3D game and similarity search in every LLM application run on it.

The definition

For u=(u1,…,un)\mathbf{u} = (u_1, \ldots, u_n) and v=(v1,…,vn)\mathbf{v} = (v_1, \ldots, v_n):

u⋅v=u1v1+u2v2+⋯+unvn\mathbf{u} \cdot \mathbf{v} = u_1v_1 + u_2v_2 + \cdots + u_nv_n
(1,3,−2)⋅(4,−1,5)=4−3−10=−9(1, 3, -2) \cdot (4, -1, 5) = 4 - 3 - 10 = -9

Multiply matching components and add. The result is a scalar, which is why it is also called the scalar product or inner product.

Algebraic rules

Rule Formula
symmetric u⋅v=v⋅u\mathbf{u} \cdot \mathbf{v} = \mathbf{v} \cdot \mathbf{u}
distributive u⋅(v+w)=u⋅v+u⋅w\mathbf{u} \cdot (\mathbf{v} + \mathbf{w}) = \mathbf{u} \cdot \mathbf{v} + \mathbf{u} \cdot \mathbf{w}
scalars pull out (c⋅u)⋅v=c⋅(u⋅v)(c \cdot \mathbf{u}) \cdot \mathbf{v} = c \cdot (\mathbf{u} \cdot \mathbf{v})
length v⋅v=∥v∥2\mathbf{v} \cdot \mathbf{v} = \lVert \mathbf{v} \rVert^2, so v⋅v>0\mathbf{v} \cdot \mathbf{v} > 0 for v≠0\mathbf{v} \ne \mathbf{0}

The last rule connects dot product and length: ∥v∥=v⋅v\lVert \mathbf{v} \rVert = \sqrt{\mathbf{v} \cdot \mathbf{v}}.

The geometric meaning: angles

For non-zero vectors with angle θ\theta between them (0≤θ≤180∘0 \le \theta \le 180^\circ):

u⋅v=∥u∥ ∥v∥cos⁡θsocos⁡θ=u⋅v∥u∥ ∥v∥\mathbf{u} \cdot \mathbf{v} = \lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert \cos\theta \qquad\text{so}\qquad \cos\theta = \frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert}

Why: the law of cosines for the triangle with sides u\mathbf{u}, v\mathbf{v} and u−v\mathbf{u} - \mathbf{v} says ∥u−v∥2=∥u∥2+∥v∥2−2∥u∥∥v∥cos⁡θ\lVert \mathbf{u} - \mathbf{v} \rVert^2 = \lVert \mathbf{u} \rVert^2 + \lVert \mathbf{v} \rVert^2 - 2\lVert \mathbf{u} \rVert\lVert \mathbf{v} \rVert\cos\theta. Expanding the left side with the rules above gives ∥u−v∥2=(u−v)⋅(u−v)=∥u∥2−2u⋅v+∥v∥2\lVert \mathbf{u} - \mathbf{v} \rVert^2 = (\mathbf{u} - \mathbf{v}) \cdot (\mathbf{u} - \mathbf{v}) = \lVert \mathbf{u} \rVert^2 - 2\mathbf{u} \cdot \mathbf{v} + \lVert \mathbf{v} \rVert^2. Comparing the two expressions gives u⋅v=∥u∥∥v∥cos⁡θ\mathbf{u} \cdot \mathbf{v} = \lVert \mathbf{u} \rVert\lVert \mathbf{v} \rVert\cos\theta.

Sign of u⋅v\mathbf{u} \cdot \mathbf{v} Angle Meaning
positive θ<90∘\theta < 90^\circ roughly the same direction
zero θ=90∘\theta = 90^\circ orthogonal (perpendicular)
negative θ>90∘\theta > 90^\circ roughly opposite
python · try it yourself
import numpy as np

def angle_deg(u, v):
    c = np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))
    return float(np.degrees(np.arccos(np.clip(c, -1.0, 1.0))))

u = np.array([1.0, 1.0, 0.0])
print("u . v =", np.dot(u, [1.0, 0.0, 0.0]), " angle:", round(angle_deg(u, [1, 0, 0]), 4))
print("angle to (1,-1,5):", angle_deg(u, np.array([1.0, -1.0, 5.0])))
print("angle to (-2,-2,0):", angle_deg(u, np.array([-2.0, -2.0, 0.0])))
u . v = 1.0  angle: 45.0
angle to (1,-1,5): 90.0
angle to (-2,-2,0): 179.99999879258172

The last result should be exactly 180°. Near cos⁡θ=±1\cos\theta = \pm 1, arccos is extremely sensitive: a rounding error of about 10−1610^{-16} in the cosine becomes about 10−610^{-6} degrees in the angle. np.clip guards against rounding pushing the cosine slightly beyond ±1\pm 1, which would make arccos return NaN. Robust geometry code computes angles as atan2⁡(∥u×v∥,u⋅v)\operatorname{atan2}(\lVert \mathbf{u} \times \mathbf{v} \rVert, \mathbf{u} \cdot \mathbf{v}) instead (see Game Math: Angle Between).

Cauchy–Schwarz and the triangle inequality

Since ∣cos⁡θ∣≤1\lvert \cos\theta \rvert \le 1, we get the Cauchy–Schwarz inequality ∣u⋅v∣≤∥u∥ ∥v∥\lvert \mathbf{u} \cdot \mathbf{v} \rvert \le \lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert, valid in every Rn\mathbb{R}^n (it can also be proved purely algebraically, without angles, which is how one justifies the definition of angle in Rn\mathbb{R}^n). It implies the triangle inequality:

∥u+v∥2=∥u∥2+2 u⋅v+∥v∥2≤∥u∥2+2∥u∥∥v∥+∥v∥2=(∥u∥+∥v∥)2\lVert \mathbf{u} + \mathbf{v} \rVert^2 = \lVert \mathbf{u} \rVert^2 + 2\, \mathbf{u} \cdot \mathbf{v} + \lVert \mathbf{v} \rVert^2 \le \lVert \mathbf{u} \rVert^2 + 2\lVert \mathbf{u} \rVert\lVert \mathbf{v} \rVert + \lVert \mathbf{v} \rVert^2 = (\lVert \mathbf{u} \rVert + \lVert \mathbf{v} \rVert)^2

Projection

The projection of u\mathbf{u} onto a non-zero vector v\mathbf{v} is the "shadow" of u\mathbf{u} along v\mathbf{v}:

proj⁡v(u)=(u⋅vv⋅v)⋅v\operatorname{proj}_{\mathbf{v}}(\mathbf{u}) = \left(\frac{\mathbf{u} \cdot \mathbf{v}}{\mathbf{v} \cdot \mathbf{v}}\right) \cdot \mathbf{v}

The leftover u−proj⁡v(u)\mathbf{u} - \operatorname{proj}_{\mathbf{v}}(\mathbf{u}) is orthogonal to v\mathbf{v}. Projections split a vector into "along v\mathbf{v}" and "across v\mathbf{v}"; games use them to slide along walls, and they lead to least squares later.

In software: lighting, culling and similarity

python · try it yourself
import numpy as np

def unit(v):
    v = np.asarray(v, dtype=float)
    return v / np.linalg.norm(v)

# 1) Lambert diffuse lighting: brightness = max(0, n . l)
light = unit([1, 2, 2])              # direction towards the light
for name, n in [("facing up", [0, 0, 1]), ("tilted", [1, 0, 1]), ("facing away", [0, 0, -1])]:
    print(f"{name:11s} brightness = {max(0.0, float(np.dot(unit(n), light))):.3f}")

# 2) back-face culling: skip triangles whose normal points away from the camera
camera_dir = np.array([0.0, 0.0, -1.0])   # camera looks down -z
for n in ([0, 0, 1], [0, 0, -1]):
    visible = np.dot(n, camera_dir) < 0
    print("normal", n, "visible" if visible else "culled")
facing up   brightness = 0.667
tilted      brightness = 0.707
facing away brightness = 0.000
normal [0, 0, 1] visible
normal [0, 0, -1] culled
  • Diffuse lighting (Lambert's cosine law): a surface is brightest when its unit normal n\mathbf{n} points straight at the light, and the brightness falls off as cos⁡θ=n⋅l\cos\theta = \mathbf{n} \cdot \mathbf{l}. Negative values mean the light is behind the surface, so they are clamped to 0.
  • Back-face culling: a triangle whose normal points away from the viewer (n⋅view direction>0\mathbf{n} \cdot \text{view direction} > 0) cannot be seen on a closed mesh, so the GPU skips it, roughly halving the work. The normal itself comes from the cross product.

Cosine similarity in ML and LLMs is the cosine of the angle between two vectors:

python · try it yourself
import numpy as np

def cos_sim(a, b):
    return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)))

# toy 4-dimensional "embeddings" (real ones have hundreds of dimensions)
emb = {
    "cat":    np.array([0.9, 0.8, 0.1, 0.0]),
    "kitten": np.array([0.8, 0.9, 0.2, 0.1]),
    "car":    np.array([0.1, 0.0, 0.9, 0.8]),
}
for a, b in [("cat", "kitten"), ("cat", "car"), ("kitten", "car")]:
    print(f"{a:6s} vs {b:6s}: {cos_sim(emb[a], emb[b]):.3f}")
cat    vs kitten: 0.987
cat    vs car   : 0.123
kitten vs car   : 0.230

Semantic search and retrieval-augmented generation rank documents by exactly this number; with vectors pre-normalized to length 1 it is just a dot product. More in Embeddings and Cosine Similarity and Vector Search. A neural network's dense layer is a stack of dot products too: each output is w⋅x+b\mathbf{w} \cdot \mathbf{x} + b.

See also Dot and Cross Products, The Dot Product (in depth) and Game Math: Dot Product.

Exercises

Exercise 1: Compute and classify

Compute u⋅v\mathbf{u} \cdot \mathbf{v} and say whether the angle is acute, right or obtuse: (a) (2,−1,3)(2, -1, 3), (1,5,1)(1, 5, 1); (b) (1,2,3)(1, 2, 3), (−3,0,1)(-3, 0, 1); (c) (4,1)(4, 1), (−2,3)(-2, 3).

Answer

(a) 0, right angle; (b) 0, right angle; (c) −5, obtuse.

Solution
(a) 2−5+3=0orthogonal(b) −3+0+3=0orthogonal(c) −8+3=−5<0obtuse\begin{aligned} &\text{(a) } 2 - 5 + 3 = 0 && \text{orthogonal} \\ &\text{(b) } -3 + 0 + 3 = 0 && \text{orthogonal} \\ &\text{(c) } -8 + 3 = -5 < 0 && \text{obtuse} \end{aligned}

Exercise 2: An angle

Find the angle between (1,0,1)(1, 0, 1) and (1,1,0)(1, 1, 0).

Answer

60°.

Solution
u⋅v=1,∥u∥=∥v∥=2cos⁡θ=12 → θ=60∘\begin{aligned} &\mathbf{u} \cdot \mathbf{v} = 1, \quad \lVert \mathbf{u} \rVert = \lVert \mathbf{v} \rVert = \sqrt{2} \\ &\cos\theta = \tfrac{1}{2} \ \to\ \theta = 60^\circ \end{aligned}

Exercise 3: Projection

Project u=(3,4)\mathbf{u} = (3, 4) onto v=(1,0)\mathbf{v} = (1, 0) and onto w=(1,1)\mathbf{w} = (1, 1). Check that u−proj⁡w(u)\mathbf{u} - \operatorname{proj}_{\mathbf{w}}(\mathbf{u}) is orthogonal to w\mathbf{w}.

Answer

proj⁡v(u)=(3,0)\operatorname{proj}_{\mathbf{v}}(\mathbf{u}) = (3, 0); proj⁡w(u)=(3.5,3.5)\operatorname{proj}_{\mathbf{w}}(\mathbf{u}) = (3.5, 3.5); the remainder (−0.5,0.5)(-0.5, 0.5) is orthogonal to (1,1)(1, 1).

Solution
proj⁡v(u)=(3/1)⋅(1,0)=(3,0)proj⁡w(u)=(7/2)⋅(1,1)=(3.5,3.5)u−proj⁡w(u)=(−0.5,0.5),(−0.5)(1)+(0.5)(1)=0 ✓\begin{aligned} \operatorname{proj}_{\mathbf{v}}(\mathbf{u}) &= (3/1) \cdot (1, 0) = (3, 0) \\ \operatorname{proj}_{\mathbf{w}}(\mathbf{u}) &= (7/2) \cdot (1, 1) = (3.5, 3.5) \\ \mathbf{u} - \operatorname{proj}_{\mathbf{w}}(\mathbf{u}) &= (-0.5, 0.5), \quad (-0.5)(1) + (0.5)(1) = 0 \ \checkmark \end{aligned}

Exercise 4: Make it orthogonal

Find kk so that (k,2,−1)(k, 2, -1) is orthogonal to (3,k,4)(3, k, 4).

Answer

k=45k = \frac{4}{5}.

Solution
3k+2k−4=0 → 5k=4 → k=453k + 2k - 4 = 0 \ \to\ 5k = 4 \ \to\ k = \tfrac{4}{5}

Exercise 5: Most similar document (code)

Given a query vector q=(1,0,1,0)\mathbf{q} = (1, 0, 1, 0) and documents d1=(2,0,2,1)\mathbf{d}_1 = (2, 0, 2, 1), d2=(0,1,0,1)\mathbf{d}_2 = (0, 1, 0, 1), d3=(1,1,1,1)\mathbf{d}_3 = (1, 1, 1, 1), rank the documents by cosine similarity to q\mathbf{q}.

Answer

d1\mathbf{d}_1 (0.943), d3\mathbf{d}_3 (0.707), d2\mathbf{d}_2 (0.000).

Solution
python · try it yourself
import numpy as np

q = np.array([1, 0, 1, 0])
docs = {"d1": [2, 0, 2, 1], "d2": [0, 1, 0, 1], "d3": [1, 1, 1, 1]}
scores = {k: float(np.dot(q, d) / (np.linalg.norm(q) * np.linalg.norm(d))) for k, d in docs.items()}
for k in sorted(scores, key=scores.get, reverse=True):
    print(k, round(scores[k], 3))
d1 0.943
d3 0.707
d2 0.0

Test yourself

What is (2,−1,4)⋅(3,6,0)(2, -1, 4) \cdot (3, 6, 0)?

  1. 0
  2. 12
  3. (6,−6,0)(6, -6, 0)
  4. -6

In Lambert lighting, why is max⁡(0,n⋅l)\max(0, \mathbf{n} \cdot \mathbf{l}) used instead of n⋅l\mathbf{n} \cdot \mathbf{l}?

  1. To normalize the vectors
  2. A negative value means the light is behind the surface, which should be dark, not negative
  3. To speed up the computation
  4. Because n⋅l\mathbf{n} \cdot \mathbf{l} is always negative

For unit vectors a\mathbf{a} and b\mathbf{b}, the cosine similarity equals…

  1. ∥a−b∥\lVert \mathbf{a} - \mathbf{b} \rVert
  2. the angle in degrees
  3. a⋅b\mathbf{a} \cdot \mathbf{b}
  4. ∥a∥+∥b∥\lVert \mathbf{a} \rVert + \lVert \mathbf{b} \rVert