Skip to main content

Dot Product

note

In the following text, we will abbreviate the dot product of a vector with itself using exponent notation, i.e.

v2vv \vec{v}^2 \coloneqq \vec{v} \cdot \vec{v}

Introduction

The dot-product takes two vectors and returns the sum of the component-wise products of the vector's components. Given two vectors

a,bRn,a=(a1a2an),b=(b1b2bn) \vec{a}, \vec {b} \in \mathbb{R}^n, \vec{a} = \begin{pmatrix} a_1 \\ a_2 \\ \vdots \\ a_{n} \end{pmatrix}, \vec{b} = \begin{pmatrix} b_1 \\ b_2 \\ \vdots \\ b_{n} \end{pmatrix}

the dot product yields a scalar value.

ab=i=1naibi \vec{a} \boldsymbol{\cdot} \vec{b} = \sum_{i=1}^{n} a_ib_i

Geometric Interpretation

If ab=0\vec{a} \boldsymbol{\cdot} \vec{b} = 0, the two vectors are perpendicular to each other. We will derive this in the following.

Orthogonality of Unit Vectors

We will first have a look at a special case, namely when a,b\vec{a}, \vec{b} are both unit vectors.

In Figure 1, the radius of the unit circle is represented by the vectors a\vec{a} and b\vec{b}. Thus, for both a\vec{a} and b\vec{b} the following trivially holds:

a=1b=1 |\vec{a}| = 1 \\ |\vec{b}| = 1
Figure 1 Unit Circle with two vectors a and b, which both have a magnitude of 1
Plot-Code (Python)
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Arc

fig, ax = plt.subplots(figsize=(6, 6))

# Plot setup
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(-1.1, 1.1)
ax.set_xticks([-1, 0, 1])
ax.set_yticks([-1, 0, 1])
ax.set_aspect('equal')
ax.grid(True, linestyle=':', linewidth=0.5)

for spine in ax.spines.values():
spine.set_visible(False)

ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_visible(True)
ax.spines['left'].set_visible(True)

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

ax.text(1.05, 0.02, r"$x$", fontsize=14, va='bottom')
ax.text(0.02, 1.05, r"$y$", fontsize=14, ha='left')

# Vecors
origin = np.array([0, 0])
theta = np.radians(45)
cos_theta = np.cos(theta)
sin_theta = np.sin(theta)
opposite = np.array([0, np.sin(theta)])
v1 = np.array([np.cos(theta), np.sin(theta)])
v2 = np.array([1, 0])

ax.quiver(*origin, *v1, angles='xy', scale_units='xy', scale=1, color='r')
ax.quiver(*origin, *v2, angles='xy', scale_units='xy', scale=1, color='b')

offset = 0.01
ax.text(v1[0] - 0.5, v1[1] -0.3, r"$\vec{a}$", fontsize=12, color='r')
ax.text(v2[0] - 0.2, v2[1] -0.12, r"$\vec{b}$", fontsize=12, color='b')

# COSINE
ax.plot([0, cos_theta], [-0.02, -0.02], color='black', linestyle='--', linewidth=1)
ax.text(cos_theta / 2 - 0.05, -0.1, r"$\cos(\Theta)$", fontsize=10)

# SINE
start = np.array([cos_theta, 0])
end = start + opposite
ax.plot([start[0], end[0]], [start[1], end[1]], color='black', linewidth=1, linestyle='--')
ax.text(cos_theta + 0.05, sin_theta / 2 - 0.05, r"$\sin(\Theta)$", fontsize=10)


# Unit Circle
circle = plt.Circle((0, 0), 1, color='black', linewidth=0.5, fill=False, transform=ax.transData)
ax.add_patch(circle)

# Angle Arc
angle_deg = np.degrees(np.arccos(np.dot(v1, v2)))
arc = Arc(origin, 0.4, 0.4, angle=0, theta1=0, theta2=angle_deg, edgecolor='green')
ax.add_patch(arc)
ax.text(0.08, 0.02, r"$\Theta$", color='green')

plt.show()

Clearly, b=(10)\vec{b} = \begin{pmatrix} 1 \\ 0 \end{pmatrix}.

Additionally a=(cos(Θ)sin(Θ))\vec{a} = \begin{pmatrix} cos(\Theta) \\ sin(\Theta) \end{pmatrix} can easily be shown since

  • the cosine represents the quotient of the adjacent side and the hypothenuse: cos(Θ)=uacos(\Theta) = \frac{u}{|\vec{a}|}
  • the sine represents the quotient of the opposite side and the hypothenuse: sin(Θ)=vasin(\Theta) = \frac{v}{|\vec{a}|}

Solving for uu respective vv gives us

cos(Θ)a=cos(Θ)1=cos(Θ)=usin(Θ)a=sin(Θ)1=sin(Θ)=v cos(\Theta) |\vec{a}| = cos(\Theta) \cdot 1 = cos(\Theta) = u\\ sin(\Theta) |\vec{a}| = sin(\Theta) \cdot 1 = sin(\Theta) = v

For Θ=90°\Theta = 90 \degree, we can therefore deduce cos(90)=0cos(90) = 0.

Orthogonality of Vectors with arbitrary length

Let's take a look at the common case when a\vec{a} and b\vec{b} are of arbitrary length and recap the Law of Cosine:

c2=a2+b22abcos(Θ) c^2 = a^2 + b^2 - 2ab \cos(\Theta)

This relationship is shown in Figure 2

Figure 2 Three vectors a, b, c. The angle Theta between a and b can be calculated with the help of the dot-product.
Plot-Code (Python)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Arc


a = np.array([4, 1])
b = np.array([1, 5])
c = b - a

a_len = np.linalg.norm(a)
b_len = np.linalg.norm(b)

dot_prod = np.dot(a, b)
cos_theta = dot_prod / (a_len * b_len)
theta_rad = np.arccos(cos_theta)
theta_deg = np.degrees(theta_rad)

angle_a = np.degrees(np.arctan2(a[1], a[0]))

theta1 = angle_a
theta2 = angle_a + theta_deg

fig, ax = plt.subplots(figsize=(6, 6))

ax.quiver(0, 0, a[0], a[1], angles='xy', scale_units='xy', scale=1, color='red')
ax.text(a[0] / 2, a[1] / 2 - 0.5, r'$\vec{a}$', color='red', fontsize=12, ha='right', va='bottom')

ax.quiver(0, 0, b[0], b[1], angles='xy', scale_units='xy', scale=1, color='blue')
ax.text(b[0] / 2 - 0.5, b[1] / 2, r'$\vec{b}$', color='blue', fontsize=12, ha='left', va='bottom')

ax.quiver(a[0], a[1], c[0], c[1], angles='xy', scale_units='xy', scale=1, color='black')
ax.text(a[0] + c[0] / 2 + 1, a[1] + c[1] / 2, r'$\vec{c} = \vec{b} - \vec{a}$', color='black', fontsize=12, ha='center', va='top')

arc = Arc((0, 0), 1.0, 1.0, angle=0, theta1=theta1, theta2=theta2, edgecolor='green')
ax.add_patch(arc)
ax.text(0.11, 0.11, r"$\Theta$", color='green', fontsize=14)

ax.set_xlim(-1, 5)
ax.set_ylim(-1, 6)
ax.set_aspect('equal')
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.grid(True)

plt.show()

First of all, let's substitute the variables with scalar values of our vectors a,b\vec{a}, \vec{b} and c\vec{c}:

c2=a2+b22abcos(Θ) |\vec{c}|^2 = |\vec{a}|^2 + |\vec{b}|^2 - 2|\vec{a}||\vec{b}| \cos(\Theta)

Note that c=ba\vec{c} = \vec{b} - \vec{a}, so we can substitute this, too. Additionally, c2=c2|c|^2 = \vec{c}^2:

(ba)2=a2+b22abcos(Θ) (\vec{b} - \vec{a})^2 = |\vec{a}|^2 + |\vec{b}|^2 - 2|\vec{a}||\vec{b}| \cos(\Theta)

We will start with simplifying the terms:

2abcos(Θ)=(ba)2a2b2=b22ab+a2a2b2=b22ab+a2a2b2=2(ab)\begin{align*} - 2|\vec{a}||\vec{b}| \cos(\Theta) &= (\vec{b} - \vec{a})^2 - |\vec{a}|^2 - |\vec{b}|^2 \\ &= \vec{b}^2 - 2 \vec{a}\vec{b} + \vec{a}^2 - |\vec{a}|^2 - |\vec{b}|^2 \\ &= |\vec{b}|^2 - 2 \vec{a}\vec{b} + |\vec{a}|^2 - |\vec{a}|^2 - |\vec{b}|^2 \\ &= - 2 \cdot (\vec{a} \boldsymbol{\cdot} \vec{b}) \end{align*}

Observe that the resulting term on the right side represents the dot product (set in parentheses for clarity).

Finally, solve for cos(Θ)\cos(\Theta):

2abcos(Θ)=2ababcos(Θ)=abcos(Θ)=abab\begin{alignat*}{} & -2|\vec{a}||\vec{b}| \cos(\Theta) &&= - 2 \vec{a}\vec{b} \\ \Leftrightarrow \qquad & |\vec{a}||\vec{b}| \cos(\Theta) &&= \vec{a} \boldsymbol{\cdot} \vec{b} \\ \Leftrightarrow \qquad & \cos(\Theta) &&= \frac{\vec{a} \boldsymbol{\cdot} \vec{b}}{|\vec{a}||\vec{b}|} \\ \end{alignat*}{}

Clearly, this holds for any vector except for a=0b=0\vec{a} = \vec{0} \lor \vec{b} = \vec{0}.

We can now solve for the dot product:

ab=cos(Θ)ab\begin{alignat*}{} \vec{a} \boldsymbol{\cdot} \vec{b} = \cos(\Theta) \cdot |\vec{a}||\vec{b}| \end{alignat*}{}

The relation between the orthogonality of a\vec{a} and b\vec{b} and Θ=90°\Theta = 90 \degree becomes more apparent when we consider

cos(90°)=0=abab\begin{alignat*}{} \cos(90\degree) = 0 = \frac{\vec{a} \boldsymbol{\cdot} \vec{b}}{|\vec{a}||\vec{b}|} \end{alignat*}{}

Since a0b0|\vec{a}| \neq 0 \land |\vec{b}| \neq 0 by definition, it follows that ab=0\vec{a} \boldsymbol{\cdot} \vec{b} = 0. We obtain the equivalence:

ab=0cos(Θ)=0 \vec{a} \boldsymbol{\cdot} \vec{b} = 0 \Leftrightarrow \cos(\Theta) = 0
info

The dot product ab=cos(Θ)ab\vec{a} \boldsymbol{\cdot} \vec{b} = \cos(\Theta) \cdot |\vec{a}||\vec{b}| beautifully shows that Θ\Theta is invariant under changes in the magnitude of the related vectors: Using the associativity of the dot product and scalar multiplication, we can derive:

abab=(ab)1ab=a1ab1b=aabb\begin{align*} \frac{\vec{a} \boldsymbol{\cdot} \vec{b}}{|\vec{a}| \cdot |\vec{b}|} &= (\vec{a} \boldsymbol{\cdot} \vec{b}) \cdot \frac{1}{|\vec{a}| \cdot |\vec{b}|}\\[1.2em] &= \vec{a} \cdot \frac{1}{|\vec{a}|} \cdot \vec{b} \cdot \frac{1}{|\vec{b}|}\\[1.2em] &= \frac{\vec{a}}{|\vec{a}|} \cdot \frac{\vec{b}}{|\vec{b}|}\\[0.5em] \end{align*}

Hence, the cosine of the angle between a\vec{a} and b\vec{b}

cos(Θ)=aabb\begin{align*} \cos(\Theta) = \frac{\vec{a}}{|\vec{a}|} \cdot \frac{\vec{b}}{|\vec{b}|}\\[0.5em] \end{align*}

is simply the dot product of the corresponding unit vectors.

Referring back to the introductory example involving the unit circle, observe that a=b=1|\vec{a}| = |\vec{b}| = 1. In this case, the dot product conveniently simplifies:

ab=cos(Θ)ab=cos(Θ)1=cos(Θ)\begin{align*} \vec{a} \boldsymbol{\cdot} \vec{b} = \cos(\Theta) \cdot |\vec{a}||\vec{b}| = \cos(\Theta) \cdot 1 = \cos(\Theta) \end{align*}

Using the usual mathematical notation for unit vectors, this can be written as

a^b^=cos(Θ) \hat{a} \boldsymbol{\cdot} \hat{b} = \cos(\Theta)

Proofs

Proof 1

Let a,bR2\vec{a}, \vec{b} \in \mathbb{R}^2, aa=1\vec{a} \cdot \vec{a} = 1, bb=1\vec{b} \cdot \vec{b} = 1, ab=0\vec{a} \boldsymbol{\cdot} \vec{b} = 0.

Claim: There exists no v\vec{v} such that va\vec{v} \neq \vec{a}, vv=1\vec{v} \cdot \vec{v} = 1 and vb=0\vec{v} \cdot \vec{b} = 0

Disproof by counterexample:

Choose

a=(10)\vec{a} = \begin{pmatrix} 1 \\ 0 \end{pmatrix}, b=(01)\vec{b} = \begin{pmatrix} 0 \\ 1 \end{pmatrix}, v=(10)\vec{v} = \begin{pmatrix} -1 \\ 0 \end{pmatrix}.

Clearly, va\vec{v} \neq \vec{a} and vv=(11)+(00)=1\vec{v} \cdot \vec{v} = (-1 \cdot -1) + (0 \cdot 0) = 1. Moreover,

ab=(10)+(01)=0\vec{a} \boldsymbol{\cdot} \vec{b} = (1 \cdot 0) + (0 \cdot 1) = 0 and vb=(10)+(01)=0\vec{v} \cdot \vec{b} = (-1 \cdot 0) + (0 \cdot 1) = 0

This contradicts the assumption that no such vector v\vec{v} exists with av\vec{a} \neq \vec{v} and bv=0\vec{b} \cdot \vec{v} = 0. \Box

Proof 2

Let a,bR2\vec{a}, \vec{b} \in \mathbb{R}^2, aa=1\vec{a} \cdot \vec{a} = 1, bb=1\vec{b} \cdot \vec{b} = 1, ab=0\vec{a} \boldsymbol{\cdot} \vec{b} = 0.

Claim: There exists a v\vec{v} such that vv=1\vec{v} \cdot \vec{v} = 1, va=0\vec{v} \cdot \vec{a} = 0 and vb=0\vec{v} \cdot \vec{b} = 0

Proof by contradiction:

It is clear that a,b\vec{a}, \vec{b} must be perpendicular, or otherwise ab0\vec{a} \boldsymbol{\cdot} \vec{b} \neq 0.

Lemma 1:

a\vec{a} and b\vec{b} are an orthonormal basis of R2\mathbb{R}^2.

Proof of Lemma 1:

For a\vec{a}, b\vec{b} (and v\vec{v}), the following holds:

aa=(axax)+(ayay)=ax2+ay2=ax2+ay2ax2+ay2=a2\vec{a} \cdot \vec{a} = (a_x \cdot a_x) + (a _y \cdot a_y) = a^2_x + a^2_y = \sqrt{a^2_x + a^2_y} \cdot \sqrt{a^2_x + a^2_y} = \|a\|^2

Because of aa=1\vec{a} \cdot \vec{a} = 1 and 1=1\sqrt{1} = 1, it follows that a\vec{a}, b\vec{b} must be unit vectors. Since they are perpendicular, they also provide an orthonormal base for R2\mathbb{R}^2. \Box

Thus, we can write v\vec{v} as a linear combination of a\vec{a} and b\vec{b}:

v=xa+yb\vec{v} = x\vec{a} + y\vec{b}

We now show that

va=0\vec{v} \cdot \vec{a} = 0

vb=0\vec{v} \cdot \vec{b} = 0

cannot hold:

v\vec{v} is perpendicular to both a\vec{a} and b\vec{b}, which are basis vectors of R2\mathbb{R}^2, thus perpendicular to each other. For v\vec{v} to be perpendicular to both basis vectors, it follows that v\vec{v} itself must be 0\vec{0}, contrary to the assumption that vv=1\vec{v} \cdot \vec{v} = 1, which implies v0\vec{v} \neq \vec{0}.

info

Additionally, note how v\vec{v} is a linear combination of a\vec{a} and b\vec{b}:

v=xa+yb\vec{v} = x\vec{a} + y\vec{b}

For va=0\vec{v} \cdot \vec{a} = 0 and vb=0\vec{v} \cdot \vec{b} = 0 to hold, v\vec{v} must be perpendicular to a\vec{a} and to b\vec{b}.

Since a0\vec{a} \ne 0, but va=0\vec{v} \cdot \vec{a} = 0, it follows that x=0x = 0, which implies that v=yb\vec{v} = y \vec{b}. In this case, vb=0\vec{v} \cdot \vec{b} = 0 must hold, which can only be true if y=0y = 0 since b0\vec{b} \ne \vec{0}. This also shows that v\vec{v} must be 0\vec{0}.

\Box