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.
- Example: If and , then:
Transpose Notation ()
In machine learning literature, you will rarely see the dot symbol () used. Instead, researchers use transpose notation:
Here is why:
- In matrix algebra, vectors are assumed to be column matrices (size ).
- You cannot multiply two matrices directly.
- By transposing the first vector (), we turn it into a row matrix of size .
- We can then multiply a row matrix by an column matrix, resulting in a single number:
Writing the Norm as a Dot Product
An extremely useful trick in machine learning is re-writing the squared norm of a vector as a dot product of the vector with itself:
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.
Where 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
- Acute Angle (): The vectors point in similar directions. The dot product is positive ().
- Obtuse Angle (): The vectors point in opposite directions. The dot product is negative ().
- Perpendicular / Orthogonal (): The vectors are completely independent and perpendicular. The dot product is exactly zero ().
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 .
Interactive Dot Product Alignment Workspace
A · B = (4 · 2) + (1 · 3)
A · B = 11
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:
By dividing the dot product by the lengths of the vectors, we isolate the angle .
- A cosine similarity of means the vectors point in the exact same direction (perfect match).
- A similarity of means they are perpendicular (unrelated).
- A similarity of means they are completely opposite.
This allows us to measure similarity based purely on content profile (direction) rather than scale (magnitude).