INDEX: LINEAR-ALGEBRA-FOR-ML / MATH-LA-03READING_TIME: 15 mins

1.3 The Dot Product: Algebraic and Geometric Views

We have learned how to add vectors and scale them. Now, let's explore one of the most powerful operations in all of linear algebra: the Dot Product.

Instead of combining two vectors to create a new vector, the dot product multiplies two vectors to produce a single number (a scalar).

To master the dot product, we look at it from two different angles: algebraically (how we compute it) and geometrically (how we visualize it).


1. The Algebraic View: How to Calculate It

Algebraically, the dot product is calculated by multiplying the matching components of two vectors and adding the results.

vw=v1w1+v2w2++vnwn\mathbf{v} \cdot \mathbf{w} = v_1 w_1 + v_2 w_2 + \dots + v_n w_n

  • Example: If v=[41]\mathbf{v} = \begin{bmatrix} 4 \\ 1 \end{bmatrix} and w=[23]\mathbf{w} = \begin{bmatrix} 2 \\ 3 \end{bmatrix}, then: vw=(4×2)+(1×3)=8+3=11\mathbf{v} \cdot \mathbf{w} = (4 \times 2) + (1 \times 3) = 8 + 3 = 11

Transpose Notation (vTw\mathbf{v}^T \mathbf{w})

In machine learning literature, you will rarely see the dot symbol (\cdot) used. Instead, researchers use transpose notation:

vTw\mathbf{v}^T \mathbf{w}

Here is why:

  • In matrix algebra, vectors are assumed to be column matrices (size n×1n \times 1).
  • You cannot multiply two n×1n \times 1 matrices directly.
  • By transposing the first vector (vT\mathbf{v}^T), we turn it into a row matrix of size 1×n1 \times n.
  • We can then multiply a 1×n1 \times n row matrix by an n×1n \times 1 column matrix, resulting in a single 1×11 \times 1 number:

vTw=[v1v2vn][w1w2wn]=v1w1+v2w2++vnwn\mathbf{v}^T \mathbf{w} = \begin{bmatrix} v_1 & v_2 & \dots & v_n \end{bmatrix} \begin{bmatrix} w_1 \\ w_2 \\ \vdots \\ w_n \end{bmatrix} = v_1 w_1 + v_2 w_2 + \dots + v_n w_n

Writing the Norm as a Dot Product

An extremely useful trick in machine learning is re-writing the squared L2L_2 norm of a vector as a dot product of the vector with itself:

v22=vv=vTv\|\mathbf{v}\|_2^2 = \mathbf{v} \cdot \mathbf{v} = \mathbf{v}^T \mathbf{v}

This allows us to simplify complex error equations when we compute gradients for optimization algorithms like Gradient Descent.


2. The Geometric View: What It Measures

Geometrically, the dot product measures alignment and direction between two vectors.

vw=v2w2cos(θ)\mathbf{v} \cdot \mathbf{w} = \|\mathbf{v}\|_2 \|\mathbf{w}\|_2 \cos(\theta)

Where θ\theta is the angle between the two vectors. Depending on the direction the vectors point, the dot product falls into three distinct categories:

    Acute (dot > 0)          Perpendicular (dot = 0)        Obtuse (dot < 0)
        ▲                         ▲                              ▲
       ╱  w                       │                              │
      ╱                           │ w                            │
     ┼─────►                      ┼─────►                  ◄─────┼
      v                            v                       w      v
  1. Acute Angle (θ<90\theta < 90^\circ): The vectors point in similar directions. The dot product is positive (>0>0).
  2. Obtuse Angle (θ>90\theta > 90^\circ): The vectors point in opposite directions. The dot product is negative (<0<0).
  3. Perpendicular / Orthogonal (θ=90\theta = 90^\circ): The vectors are completely independent and perpendicular. The dot product is exactly zero (=0=0).

3. Hands-on Experiment

Interact with the visualizer below. Change the coordinates of Vector A (blue) and Vector B (green). Observe how the dot product value shifts from positive to negative as the angle sweeps past 9090^\circ.

Interactive Dot Product Alignment Workspace

-6-6-4-4-2-2224466A (4, 1)B (2, 3)
Vector Controls
Vector A
X: 4
Y: 1
Vector B
X: 2
Y: 3
Dot Product & Alignment
Algebraic Calculation:
A · B = (x₁ · x₂) + (y₁ · y₂)
A · B = (4 · 2) + (1 · 3)
A · B = 11
Acute Angle (Pointing in similar directions)
Angle between vectors (θ): 42.3°

4. Machine Learning Connection: Cosine Similarity

In machine learning, we often want to know if two objects are similar. For example:

  • In a movie recommendation engine: Is user A's taste similar to user B's taste?
  • In search engines: Is this document similar to the search query?

To do this, we represent items as vectors and calculate the Cosine Similarity:

cos(θ)=vwv2w2\cos(\theta) = \frac{\mathbf{v} \cdot \mathbf{w}}{\|\mathbf{v}\|_2 \|\mathbf{w}\|_2}

By dividing the dot product by the lengths of the vectors, we isolate the angle θ\theta.

  • A cosine similarity of 11 means the vectors point in the exact same direction (perfect match).
  • A similarity of 00 means they are perpendicular (unrelated).
  • A similarity of 1-1 means they are completely opposite.

This allows us to measure similarity based purely on content profile (direction) rather than scale (magnitude).


5. Check Your Understanding

Quiz / Test Your Knowledge

You are building a music recommendation system. User A has rated 5 rock songs as '5 stars'. User B has rated the same 5 rock songs as '1 star'. If you calculate the Cosine Similarity between their rating vectors, what will the result indicate, and why?