Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1.6: Dot product and orthogonality

In Chapter 1.4, we learned how to add and subtract vectors and how to perform scalar multiplication. Scalar multiplication lets us stretch or compress a vector by multiplying it by a scalar; a negative scalar also reverses its direction. Each of these operations produces another vector. In Chapter 1.5, we learned how to find vector lengths and distances between points.

But what about multiplying a vector by another vector? We haven’t defined that yet! In this section, we’ll introduce one way to do it: the dot product.

The dot product will also help us answer a geometric question: what is the angle between two vectors? In particular, how can we tell whether two vectors are perpendicular without relying on a picture?


Computing the dot product

Algebraic definition

Multiply corresponding components, then add the products. For example, if

u⃗=[21],v⃗=[13],{\color{orange} \vec u}={\color{orange} \begin{bmatrix}2\\1\end{bmatrix}},\qquad {\color{#3d81f6} \vec v}={\color{#3d81f6} \begin{bmatrix}1\\3\end{bmatrix}},

then

u⃗⋅v⃗=2(1)+1(3)=5.{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}={\color{orange} 2}({\color{#3d81f6} 1})+{\color{orange} 1}({\color{#3d81f6} 3})=5.

The result is one number, not another vector. We’ll call this the algebraic definition, since it tells us how to calculate a dot product from the components, using algebra.

Basic properties

The first property says that the order doesn’t matter. The second lets us distribute over addition. The third lets us pull scalars outside the dot product. Remember that a dot product produces a scalar: (u⃗⋅v⃗)⋅w⃗({\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v})\cdot{\color{#d81a60} \vec w} is not a valid operation, since its first input is a scalar rather than a vector.

For example, suppose u⃗⋅v⃗=7{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}=7 and ∥u⃗∥=3\lVert{\color{orange} \vec u}\rVert={\color{orange} 3}. Then

u⃗⋅(2u⃗−4v⃗)=2(u⃗⋅u⃗)−4(u⃗⋅v⃗)=2∥u⃗∥2−4(7)=2(3)2−28=18−28=−10.\begin{aligned} {\color{orange} \vec u}\cdot(2{\color{orange} \vec u}-4{\color{#3d81f6} \vec v}) &=2({\color{orange} \vec u}\cdot{\color{orange} \vec u})-4({\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v})\\ &=2\lVert{\color{orange} \vec u}\rVert^2-4(7)\\ &=2({\color{orange} 3})^2-28={\color{orange} 18}-28=-10. \end{aligned}

In Chapter 1.5, we found the length of a linear combination by computing its components first. Here, the dot product properties let us calculate directly from the information we’re given!


Angles and cosine similarity

In R2\mathbb{R}^2 or R3\mathbb{R}^3, we can draw two nonzero vectors from the same starting point and look at the angle between their arrows. The dot product has a second, geometric definition that relates it to this angle.

Geometric definition

Why is this formula useful? We don’t typically know the angle between two vectors beforehand; we usually just have the vectors and their components. But, since the two definitions of the dot product are equal, we can compute the dot product and lengths from the components, then rearrange the geometric formula to solve for the angle. This gives us a way to measure how close the vectors are to pointing in the same direction, which has many useful applications.

In two and three dimensions, this gives the familiar geometric angle. What does an angle in R4\mathbb{R}^4 or R5\mathbb{R}^5 look like? We can’t directly picture those spaces, but we can still calculate the ratio: in higher dimensions, take this as the definition of the angle, choosing the unique angle between 0∘0^\circ and 180∘180^\circ with this cosine. The ratio always lies between -1 and 1.

If we’re asked for the cosine of the angle, or the cosine similarity, the ratio

u⃗⋅v⃗∥u⃗∥∥v⃗∥\frac{{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}}{\lVert{\color{orange} \vec u}\rVert\lVert{\color{#3d81f6} \vec v}\rVert}

is our answer. To find the angle itself, take cos⁡−1\cos^{-1} of the result. Here cos⁡−1\cos^{-1} means inverse cosine, not 1/cos⁡1/\cos. Let’s work through both steps.

Example in ℝ²

Let’s use the vectors

u⃗=[21],v⃗=[26].{\color{orange} \vec u}={\color{orange} \begin{bmatrix}2\\1\end{bmatrix}},\qquad {\color{#3d81f6} \vec v}={\color{#3d81f6} \begin{bmatrix}2\\6\end{bmatrix}}.

Their dot product and lengths are

u⃗⋅v⃗=2(2)+1(6)=10.{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}={\color{orange} 2}({\color{#3d81f6} 2})+{\color{orange} 1}({\color{#3d81f6} 6})=10.
∥u⃗∥=22+12=5,∥v⃗∥=22+62=40.\begin{aligned} \lVert{\color{orange} \vec u}\rVert&=\sqrt{{\color{orange} 2}^2+{\color{orange} 1}^2}={\color{orange} \sqrt{5}},\\ \lVert{\color{#3d81f6} \vec v}\rVert&=\sqrt{{\color{#3d81f6} 2}^2+{\color{#3d81f6} 6}^2}={\color{#3d81f6} \sqrt{40}}. \end{aligned}

Their cosine similarity is therefore

cos⁡θ=10540=10102=12.\cos\theta=\frac{10}{{\color{orange} \sqrt{5}}{\color{#3d81f6} \sqrt{40}}} =\frac{10}{10\sqrt{2}}=\frac{1}{\sqrt{2}}.

Since cos⁡45∘=1/2\cos45^\circ=1/\sqrt{2}, the angle is 45∘45^\circ.

Image produced in Jupyter

Example in ℝ³

Now, let

w⃗=[50−4],x⃗=[912].{\color{#3d81f6} \vec w}={\color{#3d81f6} \begin{bmatrix}5\\0\\-4\end{bmatrix}},\qquad {\color{orange} \vec x}={\color{orange} \begin{bmatrix}9\\1\\2\end{bmatrix}}.

We have

w⃗⋅x⃗=5(9)+0(1)+(−4)(2)=37,{\color{#3d81f6}\vec w}\cdot{\color{orange}\vec x}={\color{#3d81f6} 5}({\color{orange} 9})+{\color{#3d81f6} 0}({\color{orange} 1})+({\color{#3d81f6} -4})({\color{orange} 2})=37,

and

∥w⃗∥=52+02+(−4)2=41,∥x⃗∥=92+12+22=86.\begin{aligned} \lVert{\color{#3d81f6} \vec w}\rVert&=\sqrt{{\color{#3d81f6} 5}^2+{\color{#3d81f6} 0}^2+({\color{#3d81f6} -4})^2}={\color{#3d81f6} \sqrt{41}},\\ \lVert{\color{orange} \vec x}\rVert&=\sqrt{{\color{orange} 9}^2+{\color{orange} 1}^2+{\color{orange} 2}^2}={\color{orange} \sqrt{86}}. \end{aligned}

So their cosine similarity is

cos⁡θ=374186.\cos\theta=\frac{37}{{\color{#3d81f6} \sqrt{41}}{\color{orange} \sqrt{86}}}.

This is an exact answer. If we wanted the angle in degrees, we could use a calculator in degree mode:

θ=cos⁡−1(374186)≈51.5∘.\theta=\cos^{-1}\left(\frac{37}{{\color{#3d81f6} \sqrt{41}}{\color{orange} \sqrt{86}}}\right)\approx51.5^\circ.

Draw both vectors from the origin. The shading below shows part of the plane containing them, and the black arc marks their angle. Drag the figure to rotate it. The angle may look different from different viewpoints, but its value stays the same.

Loading...

Interpreting the sign

Since both lengths are positive, the dot product and cosine similarity have the same sign. We can interpret that sign geometrically:

Dot productAngle between nonzero vectors
Positive0∘≤θ<90∘0^\circ\leq\theta<90^\circ: the directions are less than a right angle apart.
Zeroθ=90∘\theta=90^\circ: the vectors are perpendicular.
Negative90∘<θ≤180∘90^\circ<\theta\leq180^\circ: the directions are more than a right angle apart.

A cosine similarity of 1 means the vectors point in the same direction; -1 means they point in opposite directions. The closer it is to 1, the smaller the angle between them.

Let’s keep u⃗=[21]{\color{orange} \vec u}={\color{orange} \begin{bmatrix}2\\1\end{bmatrix}} fixed and compare it with four different vectors.

Image produced in Jupyter

The first two dot products are positive, the third is zero, and the fourth is negative. In the fourth picture, the vectors point in exactly opposite directions, so the angle is 180∘180^\circ and their cosine similarity is -1.

Why divide by the lengths?

Be careful when comparing the sizes of dot products: they depend on the lengths as well as the angle. Doubling one vector doubles the dot product without changing its direction. Cosine similarity removes this dependence on length: its numerator and denominator both double, so the ratio stays the same. More generally, multiplying either vector by a positive scalar leaves their cosine similarity unchanged.

This connects directly to unit vectors, which describe direction. The cosine similarity is the dot product of the two vectors after normalizing them:

u⃗∥u⃗∥⋅v⃗∥v⃗∥=u⃗⋅v⃗∥u⃗∥∥v⃗∥.\frac{{\color{orange} \vec u}}{\lVert{\color{orange} \vec u}\rVert}\cdot\frac{{\color{#3d81f6} \vec v}}{\lVert{\color{#3d81f6} \vec v}\rVert} =\frac{{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}}{\lVert{\color{orange} \vec u}\rVert\lVert{\color{#3d81f6} \vec v}\rVert}.

Orthogonality

Orthogonal is the word we use for perpendicular vectors. The geometric formula gives us an especially useful test: for nonzero vectors, the angle is 90∘90^\circ exactly when the dot product is zero, because cos⁡90∘=0\cos 90^\circ=0.

This definition applies to any two vectors with the same number of components, no matter how many components they have. For example, two vectors u⃗,v⃗∈R15\vec u,\vec v\in\mathbb{R}^{15} are orthogonal exactly when u⃗⋅v⃗=0\vec u\cdot\vec v=0. We don’t need to picture 15-dimensional space to check this: orthogonal means their dot product is zero.

For example,

[21]⋅[−12]=2(−1)+1(2)=0,{\color{orange} \begin{bmatrix}2\\1\end{bmatrix}}\cdot{\color{#3d81f6} \begin{bmatrix}-1\\2\end{bmatrix}} ={\color{orange} 2}({\color{#3d81f6} -1})+{\color{orange} 1}({\color{#3d81f6} 2})=0,

so these vectors are orthogonal. The picture below shows these two perpendicular vectors.

Image produced in Jupyter

The definition also makes 0⃗\vec 0 orthogonal to every vector, since 0⃗⋅v⃗=0\vec 0\cdot{\color{#3d81f6} \vec v}=0.

Orthogonality and the Pythagorean theorem

In Chapter 1.5, we used the Pythagorean theorem to define the length of a vector. Here, we’ve already learned that the dot product operation and the length of a vector are closely related: for any vector x⃗\vec x,

x⃗⋅x⃗=∥x⃗∥2.\vec x\cdot\vec x=\lVert\vec x\rVert^2.

What happens if we apply this equality to the sum of two vectors, u⃗+v⃗{\color{orange} \vec u} + {\color{#3d81f6} \vec v}? Using the distributive property of the dot product,

∥u⃗+v⃗∥2=(u⃗+v⃗)⋅(u⃗+v⃗)=u⃗⋅u⃗+u⃗⋅v⃗+v⃗⋅u⃗+v⃗⋅v⃗=∥u⃗∥2+2u⃗⋅v⃗+∥v⃗∥2.\begin{aligned} \lVert{\color{orange} \vec u}+{\color{#3d81f6} \vec v}\rVert^2 &=({\color{orange} \vec u}+{\color{#3d81f6} \vec v})\cdot({\color{orange} \vec u}+{\color{#3d81f6} \vec v})\\ &={\color{orange} \vec u}\cdot{\color{orange} \vec u}+{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}+{\color{#3d81f6} \vec v}\cdot{\color{orange} \vec u}+{\color{#3d81f6} \vec v}\cdot{\color{#3d81f6} \vec v}\\ &=\lVert{\color{orange} \vec u}\rVert^2+2{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}+\lVert{\color{#3d81f6} \vec v}\rVert^2. \end{aligned}

If u⃗{\color{orange} \vec u} and v⃗{\color{#3d81f6} \vec v} are orthogonal, the middle term is zero, leaving

∥u⃗+v⃗∥2=∥u⃗∥2+∥v⃗∥2.\lVert{\color{orange} \vec u}+{\color{#3d81f6} \vec v}\rVert^2=\lVert{\color{orange} \vec u}\rVert^2+\lVert{\color{#3d81f6} \vec v}\rVert^2.

This is the Pythagorean theorem! For nonzero vectors in two or three dimensions, draw them tip-to-tail: they form the perpendicular legs of a right triangle, and their sum is the hypotenuse. The algebra also works in higher dimensions. It tells us exactly when we can add the squared lengths: when the two vectors are orthogonal.


The Cauchy–Schwarz inequality

How large can a dot product be? The answer depends on the lengths of the two vectors.

This says that the absolute value of the dot product is at most the product of the vectors’ lengths. For two nonzero vectors, both lengths are positive, so we can divide by their product to get

∣u⃗⋅v⃗∥u⃗∥∥v⃗∥∣≤1.\left|\frac{{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}}{\lVert{\color{orange} \vec u}\rVert\lVert{\color{#3d81f6} \vec v}\rVert}\right|\leq1.

The quantity inside the absolute value is the cosine similarity of u⃗{\color{orange} \vec u} and v⃗{\color{#3d81f6} \vec v}. So the cosine similarity has absolute value at most 1. Remember that ∣x∣≤1|x|\leq1 means −1≤x≤1-1\leq x\leq1. This is reassuring: the cosine similarity is the cosine of the angle between the vectors, and the cosine function only takes values between -1 and 1, inclusive.

We can also rewrite the original inequality without absolute values:

−∥u⃗∥∥v⃗∥≤u⃗⋅v⃗≤∥u⃗∥∥v⃗∥.-\lVert{\color{orange} \vec u}\rVert\lVert{\color{#3d81f6} \vec v}\rVert\leq{\color{orange} \vec u}\cdot{\color{#3d81f6} \vec v}\leq\lVert{\color{orange} \vec u}\rVert\lVert{\color{#3d81f6} \vec v}\rVert.

For nonzero vectors, we can interpret these bounds using the geometric formula. The upper bound occurs when the vectors point in the same direction, and the lower bound occurs when they point in opposite directions. If either vector is zero, the dot product and both bounds are zero.

For example, if ∥u⃗∥=5\lVert{\color{orange} \vec u}\rVert={\color{orange} 5} and ∥v⃗∥=12\lVert{\color{#3d81f6} \vec v}\rVert={\color{#3d81f6} 12}, their dot product must be between -60 and 60. It is 60 when they point in the same direction, and -60 when they point in opposite directions.

Homework 2 will have questions that develop this idea further.


Application: Work done by a force

Suppose you pull an object along a straight track. A force pointing in the direction of the displacement does positive work; a perpendicular force does no work.

For a constant force F⃗\vec F acting while an object undergoes displacement d⃗\vec d, the work done by that force is

W=F⃗⋅d⃗.W=\vec F\cdot\vec d.

If both vectors are nonzero, the geometric formula gives the familiar formula from physics,

W=∥F⃗∥∥d⃗∥cos⁡θ,W=\lVert\vec F\rVert\lVert\vec d\rVert\cos\theta,

where θ\theta is the angle between the force and displacement. If either vector is zero, the dot product tells us directly that the work is zero.

If force is measured in newtons and displacement in meters, work is measured in joules.

For example, suppose F⃗=[34]\vec F=\begin{bmatrix}3\\4\end{bmatrix} newtons and the object moves d⃗=[20]\vec d=\begin{bmatrix}2\\0\end{bmatrix} meters along a horizontal track. Then

W=3(2)+4(0)=6 joules.W=3(2)+4(0)=6\text{ joules}.

The vertical component of this force contributes no work because there is no vertical displacement.