14  Length, Distance, Dot Product, and Orthogonality

14.1 Opening question

Linear algebra is not only about solving equations and transforming vectors. It is also about geometry.

To understand geometry, we need ways to measure:

  • length,
  • distance,
  • angle,
  • perpendicularity,
  • similarity,
  • and error.

The central tool is the dot product.

The dot product turns algebraic vectors into geometric objects. It allows us to define orthogonality, projection, least squares, and many algorithms used in computation and data science.

The central question of this chapter is:

How do we measure geometry using vectors?

14.2 Learning goals

After studying this chapter, you should be able to:

  1. Compute dot products.
  2. Use the dot product to compute lengths and distances.
  3. Define orthogonality.
  4. Determine whether two vectors are orthogonal.
  5. Compute angles between vectors.
  6. Understand the geometric meaning of dot product.
  7. Use orthogonality to describe subspaces.
  8. Define orthogonal complements.
  9. Understand why orthogonality is important for computation.
  10. Use Python to visualize lengths, distances, angles, and orthogonal vectors.

14.3 12.1 The dot product

Let

\[ u= \begin{bmatrix} u_1\\ u_2\\ \vdots\\ u_n \end{bmatrix}, \qquad v= \begin{bmatrix} v_1\\ v_2\\ \vdots\\ v_n \end{bmatrix}. \]

The dot product of \(u\) and \(v\) is

\[ u\cdot v=u_1v_1+u_2v_2+\cdots+u_nv_n. \]

It can also be written as

\[ u^Tv. \]

NoteDefinition: dot product

For vectors \(u,v\in\mathbb{R}^n\),

\[ u\cdot v=u^Tv=\sum_{i=1}^n u_iv_i. \]

The dot product takes two vectors and produces one number.

14.4 12.2 First examples

Let

\[ u= \begin{bmatrix} 1\\ 2\\ 3 \end{bmatrix}, \qquad v= \begin{bmatrix} 4\\ -1\\ 2 \end{bmatrix}. \]

Then

\[ u\cdot v=1(4)+2(-1)+3(2)=4-2+6=8. \]

If

\[ a= \begin{bmatrix} 1\\ 2 \end{bmatrix}, \qquad b= \begin{bmatrix} 2\\ -1 \end{bmatrix}, \]

then

\[ a\cdot b=1(2)+2(-1)=0. \]

The value \(0\) will have a special geometric meaning: the vectors are perpendicular.

14.5 12.3 Algebraic properties of the dot product

For vectors \(u,v,w\in\mathbb{R}^n\) and scalars \(c\in\mathbb{R}\):

  1. Symmetry:

\[ u\cdot v=v\cdot u. \]

  1. Distributivity:

\[ u\cdot(v+w)=u\cdot v+u\cdot w. \]

  1. Scalar multiplication:

\[ (cu)\cdot v=c(u\cdot v). \]

  1. Positivity:

\[ u\cdot u\ge 0. \]

  1. Zero condition:

\[ u\cdot u=0 \]

if and only if

\[ u=0. \]

These properties make the dot product a useful measurement tool.

14.6 12.4 Length of a vector

The length, or norm, of a vector is defined using the dot product.

For

\[ u= \begin{bmatrix} u_1\\ u_2\\ \vdots\\ u_n \end{bmatrix}, \]

the length of \(u\) is

\[ \|u\|=\sqrt{u\cdot u}. \]

Thus

\[ \|u\|=\sqrt{u_1^2+u_2^2+\cdots+u_n^2}. \]

NoteDefinition: Euclidean norm

The Euclidean norm of \(u\in\mathbb{R}^n\) is

\[ \|u\|=\sqrt{u^Tu}. \]

For example,

\[ u= \begin{bmatrix} 3\\ 4 \end{bmatrix} \]

has length

\[ \|u\|=\sqrt{3^2+4^2}=5. \]

14.7 12.5 Python plot: vector length

import numpy as np
import matplotlib.pyplot as plt

u = np.array([3, 4], dtype=float)

plt.figure(figsize=(6, 6))
plt.arrow(0, 0, u[0], u[1], head_width=0.15, length_includes_head=True)
plt.plot([u[0], u[0]], [0, u[1]], linestyle="--")
plt.plot([0, u[0]], [0, 0], linestyle="--")
plt.text(u[0] + 0.1, u[1], "$u=(3,4)$")
plt.text(1.5, -0.3, "$3$")
plt.text(3.1, 2, "$4$")
plt.text(1.4, 2.2, "$\\|u\\|=5$")
plt.axhline(0, linewidth=0.8)
plt.axvline(0, linewidth=0.8)
plt.grid(True)
plt.axis("equal")
plt.title("Length of a vector")
plt.show()

14.8 12.6 Distance between vectors

The distance between two vectors \(u\) and \(v\) is the length of their difference:

\[ d(u,v)=\|u-v\|. \]

That is,

\[ d(u,v)=\sqrt{(u-v)\cdot(u-v)}. \]

In coordinates,

\[ d(u,v)= \sqrt{(u_1-v_1)^2+\cdots+(u_n-v_n)^2}. \]

NoteDefinition: Euclidean distance

For \(u,v\in\mathbb{R}^n\),

\[ d(u,v)=\|u-v\|. \]

14.8.1 Example

Let

\[ u= \begin{bmatrix} 1\\ 2 \end{bmatrix}, \qquad v= \begin{bmatrix} 4\\ 6 \end{bmatrix}. \]

Then

\[ u-v= \begin{bmatrix} -3\\ -4 \end{bmatrix}. \]

So

\[ d(u,v)=\sqrt{(-3)^2+(-4)^2}=5. \]

14.9 12.7 Python plot: distance between points

u = np.array([1, 2], dtype=float)
v = np.array([4, 6], dtype=float)

plt.figure(figsize=(6, 6))
plt.scatter([u[0], v[0]], [u[1], v[1]])
plt.plot([u[0], v[0]], [u[1], v[1]], linewidth=2)
plt.text(u[0] - 0.4, u[1], "$u$")
plt.text(v[0] + 0.1, v[1], "$v$")
plt.text(2.3, 4.3, "$\\|u-v\\|=5$")
plt.axhline(0, linewidth=0.8)
plt.axvline(0, linewidth=0.8)
plt.grid(True)
plt.axis("equal")
plt.title("Distance between two vectors")
plt.show()

14.10 12.8 Unit vectors

A vector with length \(1\) is called a unit vector.

NoteDefinition: unit vector

A vector \(u\) is a unit vector if

\[ \|u\|=1. \]

If \(v\ne 0\), then we can create a unit vector in the same direction by dividing by its length:

\[ \hat v=\frac{v}{\|v\|}. \]

This process is called normalization.

14.10.1 Example

Let

\[ v= \begin{bmatrix} 3\\ 4 \end{bmatrix}. \]

Then

\[ \|v\|=5. \]

So

\[ \hat v= \frac{1}{5} \begin{bmatrix} 3\\ 4 \end{bmatrix} = \begin{bmatrix} 3/5\\ 4/5 \end{bmatrix}. \]

Check:

\[ \|\hat v\|= \sqrt{(3/5)^2+(4/5)^2}=1. \]

14.11 12.9 Orthogonality

Two vectors are orthogonal if their dot product is zero.

NoteDefinition: orthogonal vectors

Vectors \(u,v\in\mathbb{R}^n\) are orthogonal if

\[ u\cdot v=0. \]

In \(\mathbb{R}^2\) and \(\mathbb{R}^3\), orthogonal means perpendicular.

14.11.1 Example

Let

\[ u= \begin{bmatrix} 1\\ 2 \end{bmatrix}, \qquad v= \begin{bmatrix} 2\\ -1 \end{bmatrix}. \]

Then

\[ u\cdot v=1(2)+2(-1)=0. \]

Therefore \(u\) and \(v\) are orthogonal.

14.12 12.10 Python plot: orthogonal vectors

u = np.array([1, 2], dtype=float)
v = np.array([2, -1], dtype=float)

plt.figure(figsize=(6, 6))
plt.arrow(0, 0, u[0], u[1], head_width=0.08, length_includes_head=True)
plt.arrow(0, 0, v[0], v[1], head_width=0.08, length_includes_head=True)
plt.text(u[0] + 0.1, u[1], "$u$")
plt.text(v[0] + 0.1, v[1], "$v$")
plt.axhline(0, linewidth=0.8)
plt.axvline(0, linewidth=0.8)
plt.grid(True)
plt.axis("equal")
plt.title("Orthogonal vectors have dot product zero")
plt.show()

print("u dot v =", np.dot(u, v))

u dot v = 0.0

14.13 12.11 Angle between vectors

The dot product is related to angle by the formula

\[ u\cdot v=\|u\|\|v\|\cos\theta, \]

where \(\theta\) is the angle between \(u\) and \(v\).

If \(u\) and \(v\) are nonzero, then

\[ \cos\theta= \frac{u\cdot v}{\|u\|\|v\|}. \]

Thus

\[ \theta=\cos^{-1}\left(\frac{u\cdot v}{\|u\|\|v\|}\right). \]

ImportantAngle formula

For nonzero vectors \(u,v\in\mathbb{R}^n\),

\[ \cos\theta= \frac{u\cdot v}{\|u\|\|v\|}. \]

If \(u\cdot v=0\), then

\[ \cos\theta=0, \]

so

\[ \theta=\frac{\pi}{2}. \]

That is why dot product zero means perpendicular.

14.14 12.12 Example: computing an angle

Let

\[ u= \begin{bmatrix} 1\\ 1 \end{bmatrix}, \qquad v= \begin{bmatrix} 1\\ 0 \end{bmatrix}. \]

Then

\[ u\cdot v=1. \]

Also,

\[ \|u\|=\sqrt{2}, \qquad \|v\|=1. \]

So

\[ \cos\theta= \frac{1}{\sqrt{2}}. \]

Therefore

\[ \theta=\frac{\pi}{4}. \]

The angle is \(45^\circ\).

14.15 12.13 Python: angle between vectors

u = np.array([1, 1], dtype=float)
v = np.array([1, 0], dtype=float)

cos_theta = np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))
theta = np.arccos(cos_theta)

theta, theta * 180 / np.pi
(0.7853981633974484, 45.00000000000001)

14.16 12.14 Cauchy–Schwarz inequality

The angle formula requires

\[ -1\le \frac{u\cdot v}{\|u\|\|v\|} \le 1. \]

Equivalently,

\[ |u\cdot v|\le \|u\|\|v\|. \]

This is the Cauchy–Schwarz inequality.

ImportantCauchy–Schwarz inequality

For all \(u,v\in\mathbb{R}^n\),

\[ |u\cdot v|\le \|u\|\|v\|. \]

Equality occurs when one vector is a scalar multiple of the other.

This inequality is one of the central inequalities in linear algebra, statistics, optimization, and machine learning.

14.17 12.15 Triangle inequality

The distance idea depends on the triangle inequality:

\[ \|u+v\|\le \|u\|+\|v\|. \]

ImportantTriangle inequality

For all \(u,v\in\mathbb{R}^n\),

\[ \|u+v\|\le \|u\|+\|v\|. \]

Geometrically, the direct path from the origin to \(u+v\) is no longer than the path that first goes to \(u\) and then follows \(v\).

14.18 12.16 Orthogonal sets

A set of vectors \(\{v_1,\ldots,v_k\}\) is an orthogonal set if every pair of distinct vectors is orthogonal:

\[ v_i\cdot v_j=0 \]

whenever \(i\ne j\).

If each vector also has length \(1\), the set is orthonormal.

NoteOrthogonal and orthonormal sets

A set \(\{v_1,\ldots,v_k\}\) is orthogonal if

\[ v_i\cdot v_j=0\quad \text{for } i\ne j. \]

It is orthonormal if it is orthogonal and

\[ \|v_i\|=1 \]

for every \(i\).

14.18.1 Example

The standard basis vectors

\[ e_1,e_2,\ldots,e_n \]

form an orthonormal set.

14.19 12.17 Orthogonal nonzero vectors are independent

ImportantOrthogonal nonzero vectors are independent

Any set of nonzero mutually orthogonal vectors is linearly independent.

Why?

Suppose

\[ c_1v_1+\cdots+c_kv_k=0. \]

Take the dot product with \(v_j\):

\[ (c_1v_1+\cdots+c_kv_k)\cdot v_j=0\cdot v_j. \]

Because the vectors are orthogonal, all terms vanish except one:

\[ c_j(v_j\cdot v_j)=0. \]

Since \(v_j\ne 0\),

\[ v_j\cdot v_j=\|v_j\|^2\ne 0. \]

Therefore

\[ c_j=0. \]

This works for every \(j\), so all coefficients are zero.

Thus the vectors are linearly independent.

14.20 12.18 Orthogonal complement

Let \(W\) be a subspace of \(\mathbb{R}^n\).

The orthogonal complement of \(W\) is the set of all vectors orthogonal to every vector in \(W\).

NoteDefinition: orthogonal complement

For a subspace \(W\subseteq\mathbb{R}^n\),

\[ W^\perp= \{x\in\mathbb{R}^n:x\cdot w=0\text{ for every }w\in W\}. \]

The orthogonal complement \(W^\perp\) is also a subspace.

14.21 12.19 Example: orthogonal complement of a line

Let

\[ W=\operatorname{span} \left\{ \begin{bmatrix} 2\\ 1 \end{bmatrix} \right\} \subseteq \mathbb{R}^2. \]

A vector

\[ x= \begin{bmatrix} a\\ b \end{bmatrix} \]

is in \(W^\perp\) if

\[ x\cdot \begin{bmatrix} 2\\ 1 \end{bmatrix} =0. \]

That is,

\[ 2a+b=0. \]

So

\[ b=-2a. \]

Thus

\[ x= \begin{bmatrix} a\\ -2a \end{bmatrix} = a \begin{bmatrix} 1\\ -2 \end{bmatrix}. \]

Therefore

\[ W^\perp= \operatorname{span} \left\{ \begin{bmatrix} 1\\ -2 \end{bmatrix} \right\}. \]

14.22 12.20 Python plot: a subspace and its orthogonal complement

w = np.array([2, 1], dtype=float)
wp = np.array([1, -2], dtype=float)

t = np.linspace(-2, 2, 100)
line_W = np.outer(t, w)
line_Wp = np.outer(t, wp)

plt.figure(figsize=(6, 6))
plt.plot(line_W[:, 0], line_W[:, 1], label="$W$")
plt.plot(line_Wp[:, 0], line_Wp[:, 1], label="$W^\\perp$")
plt.arrow(0, 0, w[0], w[1], head_width=0.08, length_includes_head=True)
plt.arrow(0, 0, wp[0], wp[1], head_width=0.08, length_includes_head=True)
plt.text(w[0] + 0.1, w[1], "$w$")
plt.text(wp[0] + 0.1, wp[1], "$w_\\perp$")
plt.axhline(0, linewidth=0.8)
plt.axvline(0, linewidth=0.8)
plt.grid(True)
plt.axis("equal")
plt.legend()
plt.title("A line and its orthogonal complement in R^2")
plt.show()

14.23 12.21 Orthogonal complements and matrices

The nullspace of a matrix is connected to orthogonality.

Let

\[ A= \begin{bmatrix} --- a_1^T ---\\ --- a_2^T ---\\ \vdots\\ --- a_m^T --- \end{bmatrix} \]

where the rows are \(a_1^T,\ldots,a_m^T\).

The equation

\[ Ax=0 \]

means

\[ a_1\cdot x=0, \]

\[ a_2\cdot x=0, \]

and so on.

Thus \(x\) is orthogonal to every row of \(A\).

Therefore

\[ \operatorname{Null}(A)=\operatorname{Row}(A)^\perp. \]

ImportantNullspace and row space

For any matrix \(A\),

\[ \operatorname{Null}(A)=\operatorname{Row}(A)^\perp. \]

This is one of the first appearances of the fundamental theorem of linear algebra.

14.24 12.22 Why orthogonality matters computationally

Orthogonality is powerful because it simplifies calculations.

If \(v_1,\ldots,v_k\) are orthogonal, then dot products between different basis directions vanish.

This makes many formulas simple.

For example, if \(\{q_1,\ldots,q_k\}\) is orthonormal and

\[ x=c_1q_1+\cdots+c_kq_k, \]

then

\[ c_j=x\cdot q_j. \]

Why?

Take the dot product with \(q_j\):

\[ x\cdot q_j = (c_1q_1+\cdots+c_kq_k)\cdot q_j = c_j(q_j\cdot q_j) = c_j. \]

This is the foundation of orthogonal projection, Fourier series, QR factorization, PCA, and the SVD.

14.25 12.23 Python: orthogonal coordinates

q1 = np.array([1, 0], dtype=float)
q2 = np.array([0, 1], dtype=float)
x = np.array([3, 4], dtype=float)

c1 = np.dot(x, q1)
c2 = np.dot(x, q2)

c1, c2
(3.0, 4.0)

For a rotated orthonormal basis:

q1 = np.array([1, 1], dtype=float) / np.sqrt(2)
q2 = np.array([1, -1], dtype=float) / np.sqrt(2)

x = np.array([3, 1], dtype=float)

c1 = np.dot(x, q1)
c2 = np.dot(x, q2)

coords = np.array([c1, c2])
coords
array([2.82842712, 1.41421356])

Reconstruct the vector:

c1*q1 + c2*q2
array([3., 1.])

The dot products give the coordinates because the basis is orthonormal.

14.26 12.24 Algorithmic summary

NoteAlgorithm: compute length

For \(u\in\mathbb{R}^n\),

\[ \|u\|=\sqrt{u\cdot u}. \]

NoteAlgorithm: compute distance

For \(u,v\in\mathbb{R}^n\),

\[ d(u,v)=\|u-v\|. \]

NoteAlgorithm: test orthogonality

Vectors \(u\) and \(v\) are orthogonal if

\[ u\cdot v=0. \]

NoteAlgorithm: compute angle

For nonzero vectors \(u\) and \(v\),

\[ \theta= \cos^{-1} \left( \frac{u\cdot v}{\|u\|\|v\|} \right). \]

NoteAlgorithm: find an orthogonal complement from equations

To find \(W^\perp\) when \(W=\operatorname{span}\{w_1,\ldots,w_k\}\):

  1. Let \(x\) be an unknown vector.
  2. Solve the system

\[ x\cdot w_1=0,\quad \ldots,\quad x\cdot w_k=0. \]

  1. Write the solution as a span.

14.27 12.25 Common mistakes

14.27.1 Mistake 1: confusing dot product with matrix multiplication

The dot product of two vectors produces a scalar.

Matrix-vector multiplication produces a vector.

14.27.2 Mistake 2: thinking orthogonal means unrelated

Orthogonal means dot product zero. In geometry, it means perpendicular. In statistics and data science, it often means no linear overlap in the chosen inner-product geometry.

14.27.3 Mistake 3: forgetting to normalize

Orthogonal vectors are perpendicular.

Orthonormal vectors are perpendicular and have length \(1\).

14.27.4 Mistake 4: using the angle formula with a zero vector

The angle formula requires nonzero vectors.

A zero vector does not have a direction.

14.27.5 Mistake 5: thinking \(W^\perp\) contains vectors outside the ambient space

If \(W\subseteq\mathbb{R}^n\), then \(W^\perp\subseteq\mathbb{R}^n\).

14.28 12.26 Chapter summary

The dot product is

\[ u\cdot v=u^Tv=\sum_{i=1}^n u_iv_i. \]

The length of a vector is

\[ \|u\|=\sqrt{u\cdot u}. \]

The distance between two vectors is

\[ d(u,v)=\|u-v\|. \]

Two vectors are orthogonal if

\[ u\cdot v=0. \]

For nonzero vectors,

\[ \cos\theta= \frac{u\cdot v}{\|u\|\|v\|}. \]

The Cauchy–Schwarz inequality says

\[ |u\cdot v|\le \|u\|\|v\|. \]

The orthogonal complement of a subspace \(W\) is

\[ W^\perp=\{x:x\cdot w=0\text{ for all }w\in W\}. \]

For a matrix \(A\),

\[ \operatorname{Null}(A)=\operatorname{Row}(A)^\perp. \]

Orthogonality simplifies computation and prepares the way for projections, least squares, QR factorization, PCA, and the SVD.

14.29 Exercises

14.29.1 A. Dot products

Compute each dot product.

\[ \begin{bmatrix}1\\2\\3\end{bmatrix} \cdot \begin{bmatrix}4\\-1\\2\end{bmatrix} \]

\[ \begin{bmatrix}1\\2\end{bmatrix} \cdot \begin{bmatrix}2\\-1\end{bmatrix} \]

\[ \begin{bmatrix}3\\4\end{bmatrix} \cdot \begin{bmatrix}3\\4\end{bmatrix} \]

\[ \begin{bmatrix}1\\-1\\2\\0\end{bmatrix} \cdot \begin{bmatrix}2\\3\\1\\5\end{bmatrix} \]

14.29.2 B. Length and distance

  1. Find the length of

\[ \begin{bmatrix}3\\4\end{bmatrix}. \]

  1. Find the length of

\[ \begin{bmatrix}1\\2\\2\end{bmatrix}. \]

  1. Find the distance between

\[ u=\begin{bmatrix}1\\2\end{bmatrix} \]

and

\[ v=\begin{bmatrix}4\\6\end{bmatrix}. \]

  1. Normalize

\[ v=\begin{bmatrix}3\\4\end{bmatrix}. \]

14.29.3 C. Orthogonality

Determine whether each pair is orthogonal.

\[ \begin{bmatrix}1\\2\end{bmatrix}, \begin{bmatrix}2\\-1\end{bmatrix} \]

\[ \begin{bmatrix}1\\1\\1\end{bmatrix}, \begin{bmatrix}1\\-1\\0\end{bmatrix} \]

\[ \begin{bmatrix}1\\0\\1\end{bmatrix}, \begin{bmatrix}1\\1\\-1\end{bmatrix} \]

\[ \begin{bmatrix}2\\-1\\3\end{bmatrix}, \begin{bmatrix}1\\2\\0\end{bmatrix} \]

14.29.4 D. Angles

Compute the angle between each pair of vectors.

\[ \begin{bmatrix}1\\0\end{bmatrix}, \begin{bmatrix}1\\1\end{bmatrix} \]

\[ \begin{bmatrix}1\\0\end{bmatrix}, \begin{bmatrix}0\\1\end{bmatrix} \]

\[ \begin{bmatrix}1\\2\end{bmatrix}, \begin{bmatrix}2\\1\end{bmatrix} \]

14.29.5 E. Orthogonal complements

Find a basis for \(W^\perp\).

\[ W=\operatorname{span} \left\{ \begin{bmatrix}2\\1\end{bmatrix} \right\} \subseteq\mathbb{R}^2. \]

\[ W=\operatorname{span} \left\{ \begin{bmatrix}1\\0\\1\end{bmatrix} \right\} \subseteq\mathbb{R}^3. \]

\[ W=\operatorname{span} \left\{ \begin{bmatrix}1\\0\\1\end{bmatrix}, \begin{bmatrix}0\\1\\1\end{bmatrix} \right\} \subseteq\mathbb{R}^3. \]

14.29.6 F. Nullspace and row space

For each matrix, find \(\operatorname{Null}(A)\) and verify that it is orthogonal to the row space.

\[ A= \begin{bmatrix} 1&2 \end{bmatrix} \]

\[ A= \begin{bmatrix} 1&0&1\\ 0&1&1 \end{bmatrix} \]

\[ A= \begin{bmatrix} 1&2&3\\ 2&4&6 \end{bmatrix} \]

14.29.7 G. Python exercises

  1. Use NumPy to compute dot products, norms, and distances.
  2. Plot two orthogonal vectors in \(\mathbb{R}^2\).
  3. Write a Python function that computes the angle between two nonzero vectors.
  4. Plot a line and its orthogonal complement in \(\mathbb{R}^2\).
  5. Use SymPy to find the nullspace of a matrix and check orthogonality with the rows.

14.29.8 H. Reflection

Write a short paragraph answering each question.

  1. Why does the dot product measure geometric alignment?
  2. Why does dot product zero mean perpendicular?
  3. Why are orthogonal vectors easier to use as building blocks?
  4. How does the nullspace relate to orthogonality?
  5. Why is orthogonality important for computation and AI?

14.30 AI-age connection

Orthogonality is one of the most important ideas in computational linear algebra. It makes coordinates stable, separates information into independent directions, and turns difficult fitting problems into geometric projection problems. Orthogonal bases appear in QR factorization, Fourier analysis, PCA, the SVD, and modern data compression. In high-dimensional data, dot products and angles are used to measure similarity, compare embeddings, and understand model geometry.