Linear Algebra

Dot Product

A single number that captures how much two vectors agree

The dot product takes two vectors and returns a single number that measures how much they point in the same direction. For vectors a and b in any dimension, a·b = |a||b|cosθ. It is the algebraic backbone of length, angle, projection, work in physics, and every neural-network layer. Discovered piecewise through the nineteenth century — Hamilton's quaternions, Grassmann's Ausdehnungslehre (1844), Gibbs' vector calculus (1881) — it is now the most-evaluated operation in scientific computing.

  • Also calledScalar product, inner product
  • Geometric formulaa·b = |a||b|cosθ
  • Algebraic formulaa·b = a₁b₁ + a₂b₂ + … + aₙbₙ
  • ResultA scalar (single number)
  • Commutative?Yes — a·b = b·a
  • Zero meansVectors are orthogonal

Watch the 60-second explainer

A condensed visual walkthrough — narrated, captioned, under a minute.

How the dot product works

Two vectors live in space. Place them tail-to-tail at the origin. The dot product asks one question: how much of one vector lies along the other? If they point in the same direction, the answer is large and positive. If they are perpendicular, the answer is zero. If they point in opposite directions, the answer is negative.

For vectors a = (a₁, a₂, …, aₙ) and b = (b₁, b₂, …, bₙ), there are two equivalent definitions:

  • Algebraic: a·b = a₁b₁ + a₂b₂ + … + aₙbₙ. Multiply matching components, add the products.
  • Geometric: a·b = |a||b|cosθ, where θ is the angle between the two vectors and |a|, |b| are their lengths.

The two formulas agree. The proof comes from the law of cosines applied to the triangle formed by a, b, and a − b, which yields |a − b|² = |a|² + |b|² − 2(a·b). Both sides expand into the same component sums.

The dot product builds nearly every other geometric quantity in linear algebra. Length is √(v·v). The cosine of the angle between two vectors is (a·b) / (|a||b|). Orthogonality is just a·b = 0. The component of u along v is (u·v) / (v·v) — a quotient of two dot products.

Worked example: the angle between two vectors

Take a = (1, 2, 3) and b = (4, 5, 6). Find the angle between them.

  1. Compute the dot product: a·b = 1·4 + 2·5 + 3·6 = 4 + 10 + 18 = 32.
  2. Compute the lengths: |a| = √(1 + 4 + 9) = √14 ≈ 3.742; |b| = √(16 + 25 + 36) = √77 ≈ 8.775.
  3. Apply the geometric formula: cosθ = 32 / (√14 · √77) = 32 / √1078 ≈ 32 / 32.833 ≈ 0.9746.
  4. Take the inverse cosine: θ = arccos(0.9746) ≈ 12.93°.

The two vectors are nearly aligned — they point in almost the same direction in three-space. A cosine close to 1 confirms tight alignment; a cosine close to 0 would mean perpendicularity; a cosine close to −1 would mean opposite directions.

Algebraic properties

  • Commutative. a·b = b·a, because cosθ is symmetric in its arguments and component sums don't care about order.
  • Distributive. a·(b + c) = a·b + a·c. Useful for breaking complicated combinations into pieces.
  • Compatible with scalar multiplication. (ka)·b = k(a·b) = a·(kb).
  • Positive definite on real vectors. v·v ≥ 0, with equality only when v = 0. This is what lets √(v·v) define a length.
  • Cauchy–Schwarz inequality. |a·b| ≤ |a||b|, with equality iff a and b are parallel. The cosine formula makes this obvious — |cosθ| ≤ 1.

Real dot product vs Hermitian inner product

The dot product as defined above works for vectors with real entries. Once you let entries become complex, you need a new operation that still gives a positive notion of length. The Hermitian (or complex) inner product fixes this by conjugating one factor.

Real dot productHermitian inner product
VectorsReal entries (ℝⁿ)Complex entries (ℂⁿ)
Formulaa·b = Σ aᵢ bᵢ⟨a,b⟩ = Σ ā ᵢ bᵢ
Symmetrya·b = b·a (symmetric)⟨a,b⟩ = conjugate of ⟨b,a⟩ (Hermitian)
v·v / ⟨v,v⟩Real, ≥ 0Real, ≥ 0 (always real)
LinearityLinear in both argumentsConjugate-linear in first, linear in second
DefinesLength, angle in ℝⁿLength, angle in ℂⁿ; quantum amplitudes
Used inGeometry, classical physics, MLQuantum mechanics, signal processing, FFT

Without the conjugate, the complex sum (i, 0)·(i, 0) = i² = −1, which would force a length of √(−1). The Hermitian fix makes ⟨(i,0),(i,0)⟩ = ī·i = (−i)(i) = 1, and length stays sane. Quantum mechanics depends entirely on this construction — wavefunctions live in a complex Hilbert space and ⟨ψ,ψ⟩ is a probability.

Where the dot product shows up

  • Work in physics. When a constant force F moves an object through displacement d, the work done is W = F·d. Only the component of force along the motion contributes; perpendicular force does no work. Carry a heavy backpack horizontally and gravity does zero work, even though it acts on you the whole time.
  • Projection. The component of u along v is (u·v / v·v) v. Least-squares regression, image compression via PCA, and Gram–Schmidt orthonormalization all build on projection.
  • Lighting in 3D graphics. The brightness of a surface lit by a directional light is N·L, the dot product of the surface normal and the light direction. Negative or zero means the surface faces away. Almost every shader written since 1980 uses this single line.
  • Cosine similarity. Search engines, recommendation systems, and word embeddings compare documents or items by computing the cosine of the angle between their feature vectors. Two vectors with cosine 0.9 are deemed nearly the same regardless of magnitude.
  • Neural networks. Every neuron computes w·x + b, a dot product followed by a bias. A modern transformer evaluates billions of dot products per forward pass.
  • Fourier coefficients. Decomposing a function f into sines and cosines is just dot products of f with each basis function in an inner-product space.

Common mistakes

  • Confusing dot and cross. The dot product returns a scalar; the cross product returns a vector. Different operation, different result type. Never write "a·b" when you mean a vector.
  • Assuming a non-zero result means alignment. Sign matters. A negative dot product means the vectors point more than 90° apart — they oppose, not align.
  • Forgetting the conjugate in complex spaces. The real formula computes the wrong thing on complex vectors. Quantum and DSP code that drops the conjugate gives nonsense.
  • Treating cosine similarity as a distance. It measures angular alignment, not Euclidean distance. Two unit vectors with cosine 0.99 are angularly close but their physical separation depends on length.
  • Using the algebraic sum without normalizing. The raw dot product mixes magnitude and angle. When you only care about direction, divide out the lengths first.

Frequently asked questions

Why does a·b = |a||b|cosθ?

Drop a perpendicular from the tip of b onto the line through a. That perpendicular cuts a segment of length |b|cosθ along a. The dot product multiplies that scalar projection by |a|. Both definitions — algebraic (sum of component products) and geometric (|a||b|cosθ) — agree because the law of cosines applied to |a − b|² yields |a|² + |b|² − 2(a·b), forcing a·b to equal |a||b|cosθ.

What does it mean when the dot product is zero?

The vectors are perpendicular (orthogonal). Since a·b = |a||b|cosθ, a zero result with nonzero magnitudes forces cosθ = 0, hence θ = 90°. This is the fastest orthogonality test in linear algebra — no need to compute the angle, just check whether the sum of products vanishes.

Is the dot product commutative?

Yes — a·b = b·a always. The geometric formula uses cosθ which is symmetric in a and b, and the algebraic sum a₁b₁ + a₂b₂ + … doesn't care about order. Commutativity distinguishes the dot product from the cross product, which flips sign when you swap arguments.

How is the dot product related to cosine similarity?

Cosine similarity is just the dot product after normalizing both vectors to unit length: cos(θ) = (a·b) / (|a||b|). It strips magnitude away and keeps only the directional alignment. Search engines, recommendation systems, and word embeddings rely on this scaled dot product to compare documents or vectors regardless of their raw size.

Does the dot product extend to complex vectors?

Yes, but it must be modified. For complex vectors, the standard real dot product fails to give a positive |v|² because the products vᵢ² can be negative or complex. The Hermitian inner product takes a complex conjugate on one factor: ⟨a,b⟩ = Σ ā ᵢ bᵢ. This guarantees ⟨v,v⟩ ≥ 0 (real and positive), and recovers length and angle in complex spaces.

How fast is the dot product on modern hardware?

It is one of the most heavily optimized operations in computing. SIMD instructions (AVX, NEON) compute 4 to 16 component products in parallel. GPUs reach trillions of dot products per second — every matrix multiplication and every neural-network layer is, at its core, a stack of dot products. BLAS level-1 routine ddot is the canonical implementation.