AI, machine learning and deep learning · Introductory

Calculating Dot Products

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

The dot product is a fundamental operation that yields a scalar by summing the products of corresponding vector elements or by multiplying vector lengths by the cosine of the angle between them. It is essential for determining vector alignment and similarity, with applications in machine learning and data science. Proper implementation in Python and MATLAB requires attention to vector orientation to avoid computational errors.

Intuition

Think of the dot product as a measure of how much two vectors point in the same direction. If they are perfectly aligned, the result is large; if they are perpendicular, the result is zero because there is no alignment. Algebraically, this is just adding up the products of their parts, while geometrically, it scales their lengths by how close their directions are. This duality allows us to compute similarity efficiently in code while understanding the underlying spatial relationship.

Key equations

∑i=1naibi
(1)

The algebraic definition of the dot product, summing the products of corresponding elements.

|v||w|cos⁡θ
(2)

The geometric definition, multiplying vector lengths by the cosine of the angle between them.

Derivation

  1. The dot product is defined algebraically as the sum of the products of corresponding elements: ∑i=1naibi.
  2. Geometrically, it is defined as the product of the lengths of the vectors and the cosine of the angle between them: |v||w|cos⁡θ.
  3. These two definitions are equivalent and yield identical results.
  4. When the angle θ is 90° (π/2 radians), cos⁡θ=0, making the dot product 0, which indicates orthogonality.
  5. The length of a vector is calculated as the square root of the dot product of the vector with itself.

Worked example

Problem

Calculate the dot product of two 2D vectors a=[1,2] and b=[3,4] using the algebraic method, and determine if they are orthogonal.

  1. Identify corresponding elements: a1=1,a2=2 and b1=3,b2=4.
  2. Multiply corresponding elements: 1×3=3 and 2×4=8.
  3. Sum the products: 3+8=11.
  4. Check for orthogonality: Since the result is 11 (not 0), the vectors are not orthogonal.

Answer The dot product is 11. The vectors are not orthogonal.

Common mistakes

  • Using v'*w in MATLAB without ensuring both vectors are column vectors, which can result in an outer product or an error.
  • Confusing the algebraic and geometric definitions, although they are equivalent.
  • Forgetting that the dot product is 0 for orthogonal vectors (90° angle).

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.