Linear AlgebraOrthogonality and Least Squares

Length and Orthogonality in Rⁿ

Vectors are orthogonal when their dot product is 0.

Not started Jump to practice

The dot product measured angles between arrows in the plane. The same formula works in $\mathbb{R}^n$ and brings geometry (length, distance, right angles) into every dimension. This unit builds on it all the way to data fitting.

For $\mathbf{u}, \mathbf{v}$ in $\mathbb{R}^n$, the inner product (dot product) is $\mathbf{u}\cdot\mathbf{v}$ $= \mathbf{u}^{T}\mathbf{v}$ $= u_1v_1 + \cdots + u_nv_n$. The length (norm) is $\lVert\mathbf{v}\rVert = \sqrt{\mathbf{v}\cdot\mathbf{v}}$, the distance between $\mathbf{u}$ and $\mathbf{v}$ is $\lVert\mathbf{u} - \mathbf{v}\rVert$, and a unit vector has length $1$: dividing any nonzero $\mathbf{v}$ by its length gives the unit vector in its direction.

Key idea

$\mathbf{u}$ and $\mathbf{v}$ are orthogonal (perpendicular) when $\mathbf{u}\cdot\mathbf{v} = 0$.

Why it works

Why does a zero dot product mean a right angle, even in $\mathbb{R}^{10}$? Expand $\lVert\mathbf{u} + \mathbf{v}\rVert^{2}$ $= (\mathbf{u} + \mathbf{v})\cdot(\mathbf{u} + \mathbf{v})$ $= \lVert\mathbf{u}\rVert^{2} + \lVert\mathbf{v}\rVert^{2} + 2\,\mathbf{u}\cdot\mathbf{v}$. The Pythagorean theorem $\lVert\mathbf{u} + \mathbf{v}\rVert^{2}$ $= \lVert\mathbf{u}\rVert^{2} + \lVert\mathbf{v}\rVert^{2}$, which is what "right angle" means for the triangle with legs $\mathbf{u}$ and $\mathbf{v}$, holds exactly when $\mathbf{u}\cdot\mathbf{v} = 0$. More generally $\mathbf{u}\cdot\mathbf{v} = \lVert\mathbf{u}\rVert\lVert\mathbf{v}\rVert\cos\theta$.

Worked example: Length and unit vector

Find $\lVert\mathbf{v}\rVert$ and the unit vector in the direction of $\mathbf{v} = \begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix}$.
  1. $\mathbf{v}\cdot\mathbf{v} = 1 + 4 + 4 = 9$, so $\lVert\mathbf{v}\rVert = 3$.
  2. Unit vector: $\frac{1}{3}\mathbf{v} = \begin{bmatrix} \frac{1}{3} \\ \frac{2}{3} \\ \frac{2}{3} \end{bmatrix}$. Check: $\frac{1}{9} + \frac{4}{9} + \frac{4}{9} = 1$.

Answer: $3$; $\left(\frac{1}{3}, \frac{2}{3}, \frac{2}{3}\right)$

Worked example: Orthogonal or not?

Is $(1, 2, 2)$ orthogonal to $(2, 1, -2)$? For which $k$ is $(1, 2, 2)$ orthogonal to $(4, k, 1)$?
  1. $(1)(2) + (2)(1) + (2)(-2)$ $= 0$: yes, orthogonal.
  2. $(1)(4) + (2)(k) + (2)(1)$ $= 6 + 2k$ $= 0$ gives $k = -3$.

Answer: Yes; $k = -3$.

Turn the picture: $(1, 2, 2)$ and $(2, 1, -2)$ meet at a right angle, and their dot product is $0$.

Worked example: Distance

Find the distance between $(1, 0, 3, -1)$ and $(2, 2, 1, -1)$ in $\mathbb{R}^4$.
  1. Difference: $(-1, -2, 2, 0)$.
  2. Length: $\sqrt{1 + 4 + 4 + 0} = 3$.

Answer: $3$

A set of vectors is orthogonal if every pair in it is orthogonal. Nonzero orthogonal vectors are automatically independent: if $c_1\mathbf{u}_1 + \cdots + c_p\mathbf{u}_p = \mathbf{0}$, dot both sides with $\mathbf{u}_1$; every term but the first vanishes, leaving $c_1\lVert\mathbf{u}_1\rVert^{2} = 0$, so $c_1 = 0$, and likewise for every $c_i$. That is why orthogonal bases are so convenient: each weight can be found by one dot product.

In higher dimensions the dot product is just as concrete. Two survey answers, two documents' word counts, or two days of stock returns are vectors in $\mathbb{R}^{n}$, and the angle between them (through $\cos\theta = \frac{\mathbf{u}\cdot\mathbf{v}}{\lVert\mathbf{u}\rVert\lVert\mathbf{v}\rVert}$) is a standard measure of how similar they are. Orthogonal means "no overlap at all".

Common mistake

Forgetting the square root in the length, or taking the square root of each entry separately. $\lVert(3, 4)\rVert = \sqrt{9 + 16} = 5$, not $3 + 4 = 7$ and not $9 + 16 = 25$. Check: the length is never less than the size of any single entry.
The rules $\mathbf{u}\cdot\mathbf{v} = \mathbf{v}\cdot\mathbf{u}$, $(\mathbf{u} + \mathbf{v})\cdot\mathbf{w} = \mathbf{u}\cdot\mathbf{w} + \mathbf{v}\cdot\mathbf{w}$, $(c\mathbf{u})\cdot\mathbf{v} = c(\mathbf{u}\cdot\mathbf{v})$ and $\mathbf{u}\cdot\mathbf{u} > 0$ for $\mathbf{u} \neq \mathbf{0}$ are all that the arguments in this unit use.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 1 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • Margalit and Rabinoff, Interactive Linear Algebra (Georgia Tech), §6.1 Dot Products and Orthogonality
  • MIT OpenCourseWare 18.06 Linear Algebra (Gilbert Strang, Spring 2010), Lecture 14: Orthogonal Vectors and Subspaces
@teddyrookbook GitHub