Dot product of vectors

Linear Algebra

Introduction

In this article, we will study dot product of vectors and its relationship to the magnitude of the vectors and the angle between them. We will visualize these concepts with an interactive demo.

Prerequisites

To understand dot product of vectors, we recommend familiarity with the concepts in

Follow the above links to first get acquainted with the corresponding concepts.

Dot product

The dot product of two vectors is the sum of the element-wise product of the two vectors. So,

$$ \va \cdot \vb = \sum_{i=1}^n a_i b_i $$

Angle between two vectors

If \( \theta \) is the angle between two vectors \( \va \) and \( \vb \) in the Euclidean space, then \( \va \cdot \vb = ||\va||_2 ||\vb||_2 \cos\theta \).

Note that the unit vectors \( X \) and \( Y \) axes are \( \vx = [0,1] \) and \( \vy = [1,0] \), for the horizontal and vertical axis respectively. Observe that their dot product is zero. That is, \( \vx \cdot \vy = 0 \). No wonder, because the two axes are perpendicular to each other!

From dot product to magnitude

Consider the dot product of a vector with itself.

\begin{aligned} \vx \cdot \vx &= ||\vx||_2 ||\vx||_2 \cos 0 \\\\ &= ||\vx||_2 ||\vx||_2 \\\\ &= ||\vx||_{2}^2 \end{aligned}

This is because the angle between a vector and itself is \( 0 \) and \( \cos 0 = 1 \).

So, the dot product of a vector with itself is akin to finding the square of its magnitude. You will see this alternative way of arriving at vector magnitude in a number of places in machine learning literature.

Dot product: demo

Interact with the next demo to understand how the angle between two vectors relates to their dot products. The angle value varies between 0 and 180 degrees. The normalized product of two vectors is

  • positive when the angle between them is less than 90 degrees.
  • negative when the angle between them is more than 90 degrees.
  • zero when the angle between them is exactly 90 degrees.

Understand the effect of superimposing the two vectors, putting them at a right angle, or pointing them in opposite directions.

Drag the cricle to change the vector

Where to next?

Now that you dot product and angle between vectors, it is time to build expertise in other topics in linear algebra.

Please share

Let your friends, followers, and colleagues know about this resource you discovered.

Let's connect

Please share your comments, questions, encouragement, and feedback.