Math Core

Lesson 6.1 · Orthogonality and Least Squares

Inner products, length and orthogonality

So far, linear algebra has been about combining vectors: spans, bases and linear maps. None of that needed length or angle. But many real questions do: how far is a data point from a model, which vector in a subspace is closest to a given one, when do two directions meet at a right angle? This unit adds geometry to Rn\mathbb{R}^n, and it all starts with one simple product.

The inner product

Definition

Inner product (dot product)

For vectors u\mathbf{u} and v\mathbf{v} in Rn\mathbb{R}^n, the inner product is the number

u⋅v=uTv=u1v1+u2v2+⋯+unvn.\mathbf{u} \cdot \mathbf{v} = \mathbf{u}^T\mathbf{v} = u_1v_1 + u_2v_2 + \cdots + u_nv_n.

Multiply matching entries and add. The result is a scalar, not a vector. Writing it as uTv\mathbf{u}^T\mathbf{v} (a 1×n1 \times n matrix times an n×1n \times 1 matrix) is handy, because it lets you use matrix algebra on inner products.

The inner product has four properties you will use constantly. For all u,v,w\mathbf{u}, \mathbf{v}, \mathbf{w} in Rn\mathbb{R}^n and every scalar cc:

  1. u⋅v=v⋅u\mathbf{u} \cdot \mathbf{v} = \mathbf{v} \cdot \mathbf{u}
  2. (u+v)⋅w=u⋅w+v⋅w(\mathbf{u} + \mathbf{v}) \cdot \mathbf{w} = \mathbf{u} \cdot \mathbf{w} + \mathbf{v} \cdot \mathbf{w}
  3. (cu)⋅v=c(u⋅v)(c\mathbf{u}) \cdot \mathbf{v} = c(\mathbf{u} \cdot \mathbf{v})
  4. u⋅u≥0\mathbf{u} \cdot \mathbf{u} \ge 0, and u⋅u=0\mathbf{u} \cdot \mathbf{u} = 0 only when u=0\mathbf{u} = \mathbf{0}

Property 4 holds because u⋅u=u12+⋯+un2\mathbf{u} \cdot \mathbf{u} = u_1^2 + \cdots + u_n^2 is a sum of squares.

Length and distance

In R2\mathbb{R}^2, the Pythagorean theorem says the vector (a,b)(a, b) has length a2+b2\sqrt{a^2 + b^2}. That is exactly v⋅v\sqrt{\mathbf{v} \cdot \mathbf{v}}, and the same formula works in any dimension.

Length and distance

The length (or norm) of v\mathbf{v} is ∥v∥=v⋅v=v12+⋯+vn2\|\mathbf{v}\| = \sqrt{\mathbf{v} \cdot \mathbf{v}} = \sqrt{v_1^2 + \cdots + v_n^2}.

The distance between u\mathbf{u} and v\mathbf{v} is dist⁡(u,v)=∥u−v∥\operatorname{dist}(\mathbf{u}, \mathbf{v}) = \|\mathbf{u} - \mathbf{v}\|.

Scaling a vector scales its length: ∥cv∥=∣c∣ ∥v∥\|c\mathbf{v}\| = |c|\,\|\mathbf{v}\|. A vector of length 11 is a unit vector. Dividing a nonzero vector by its length, u=v∥v∥\mathbf{u} = \dfrac{\mathbf{v}}{\|\mathbf{v}\|}, gives the unit vector in the same direction. This is called normalizing v\mathbf{v}.

Worked example: Inner product, length and a unit vector

Let u=(1,−2,3)\mathbf{u} = (1, -2, 3) and v=(2,−1,2,4)\mathbf{v} = (2, -1, 2, 4). Find u⋅u\mathbf{u} \cdot \mathbf{u}, ∥u∥\|\mathbf{u}\|, and the unit vector in the direction of v\mathbf{v}.

u⋅u=1+4+9=14\mathbf{u} \cdot \mathbf{u} = 1 + 4 + 9 = 14, so ∥u∥=14\|\mathbf{u}\| = \sqrt{14}.

For v\mathbf{v} (a vector in R4\mathbb{R}^4): ∥v∥=4+1+4+16=25=5\|\mathbf{v}\| = \sqrt{4 + 1 + 4 + 16} = \sqrt{25} = 5. So the unit vector is

15v=(25,−15,25,45).\frac{1}{5}\mathbf{v} = \left(\tfrac{2}{5}, -\tfrac{1}{5}, \tfrac{2}{5}, \tfrac{4}{5}\right).

Check: 4+1+4+1625=1\tfrac{4 + 1 + 4 + 16}{25} = 1.

Worked example: Distance between two points

Find the distance between u=(7,1)\mathbf{u} = (7, 1) and v=(3,2)\mathbf{v} = (3, 2).

u−v=(4,−1)\mathbf{u} - \mathbf{v} = (4, -1), so dist⁡(u,v)=16+1=17\operatorname{dist}(\mathbf{u}, \mathbf{v}) = \sqrt{16 + 1} = \sqrt{17}.

Orthogonal vectors

When are u\mathbf{u} and v\mathbf{v} perpendicular? Geometrically, exactly when the distance from u\mathbf{u} to v\mathbf{v} equals the distance from u\mathbf{u} to −v-\mathbf{v}. Expand both squared distances:

∥u−v∥2=∥u∥2+∥v∥2−2 u⋅v,∥u+v∥2=∥u∥2+∥v∥2+2 u⋅v.\|\mathbf{u} - \mathbf{v}\|^2 = \|\mathbf{u}\|^2 + \|\mathbf{v}\|^2 - 2\,\mathbf{u} \cdot \mathbf{v}, \qquad \|\mathbf{u} + \mathbf{v}\|^2 = \|\mathbf{u}\|^2 + \|\mathbf{v}\|^2 + 2\,\mathbf{u} \cdot \mathbf{v}.

These are equal exactly when u⋅v=0\mathbf{u} \cdot \mathbf{v} = 0. That motivates the definition.

Orthogonality

Vectors u\mathbf{u} and v\mathbf{v} are orthogonal if u⋅v=0\mathbf{u} \cdot \mathbf{v} = 0.

Pythagorean theorem: u\mathbf{u} and v\mathbf{v} are orthogonal if and only if ∥u+v∥2=∥u∥2+∥v∥2\|\mathbf{u} + \mathbf{v}\|^2 = \|\mathbf{u}\|^2 + \|\mathbf{v}\|^2.

The zero vector is orthogonal to every vector, since 0⋅v=0\mathbf{0} \cdot \mathbf{v} = 0.

u · v = 2(−1) + 1(2) = 0, so u and v meet at a right angle.Open in grapher →

Angles

More generally, the law of cosines in the plane spanned by u\mathbf{u} and v\mathbf{v} gives

u⋅v=∥u∥ ∥v∥cos⁡θ,\mathbf{u} \cdot \mathbf{v} = \|\mathbf{u}\|\,\|\mathbf{v}\|\cos\theta,

where θ\theta is the angle between the vectors. So cos⁡θ=u⋅v∥u∥ ∥v∥\cos\theta = \dfrac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\|\,\|\mathbf{v}\|}. A positive inner product means an acute angle, a negative one means an obtuse angle, and zero means a right angle. In statistics, this cosine (for centered data) is the correlation coefficient.

Orthogonal complements

Definition

Orthogonal complement

If WW is a subspace of Rn\mathbb{R}^n, its orthogonal complement W⊥W^\perp ("W perp") is the set of all vectors orthogonal to every vector in WW.

Three facts make W⊥W^\perp easy to work with:

  • A vector is in W⊥W^\perp if and only if it is orthogonal to every vector in a spanning set of WW. (By linearity, orthogonal to the spanning vectors means orthogonal to all their combinations.)
  • W⊥W^\perp is a subspace of Rn\mathbb{R}^n, and dim⁡W+dim⁡W⊥=n\dim W + \dim W^\perp = n.
  • For any m×nm \times n matrix AA: (Row⁡A)⊥=Nul⁡A(\operatorname{Row} A)^\perp = \operatorname{Nul} A and (Col⁡A)⊥=Nul⁡AT(\operatorname{Col} A)^\perp = \operatorname{Nul} A^T.

The last fact is just matrix multiplication read row by row: Ax=0A\mathbf{x} = \mathbf{0} says each row of AA has inner product 00 with x\mathbf{x}. So to find W⊥W^\perp, put a spanning set of WW into the rows of a matrix and find its null space.

Worked example: Finding an orthogonal complement

Let W=Span⁡{(1,2,−1),(0,1,1)}W = \operatorname{Span}\{(1, 2, -1), (0, 1, 1)\} in R3\mathbb{R}^3. Find a basis for W⊥W^\perp.

Solve Ax=0A\mathbf{x} = \mathbf{0} with A=[12−1011]A = \begin{bmatrix} 1 & 2 & -1 \\ 0 & 1 & 1 \end{bmatrix}. Row 2 gives x2=−x3x_2 = -x_3. Row 1 gives x1=−2x2+x3=2x3+x3=3x3x_1 = -2x_2 + x_3 = 2x_3 + x_3 = 3x_3. With x3x_3 free,

x=x3[3−11],so {(3,−1,1)} is a basis for W⊥.\mathbf{x} = x_3\begin{bmatrix} 3 \\ -1 \\ 1 \end{bmatrix}, \quad \text{so } \{(3, -1, 1)\} \text{ is a basis for } W^\perp.

Check: (1,2,−1)⋅(3,−1,1)=3−2−1=0(1, 2, -1) \cdot (3, -1, 1) = 3 - 2 - 1 = 0 and (0,1,1)⋅(3,−1,1)=−1+1=0(0, 1, 1) \cdot (3, -1, 1) = -1 + 1 = 0. Geometrically, WW is a plane through the origin and W⊥W^\perp is its normal line. Note dim⁡W+dim⁡W⊥=2+1=3\dim W + \dim W^\perp = 2 + 1 = 3.

Common mistake

To be in W⊥W^\perp, a vector must be orthogonal to every vector in the spanning set, not just one of them. Put all the spanning vectors into the rows of one matrix, so the null space handles every condition at once. And put them in the rows, not the columns: Nul⁡A\operatorname{Nul} A is orthogonal to the rows of AA.

Tip

Always check a claimed orthogonal vector by taking inner products. It takes seconds and catches most arithmetic slips.

Practice

Practice 1

Compute u⋅v\mathbf{u} \cdot \mathbf{v} for u=(3,−1,2)\mathbf{u} = (3, -1, 2) and v=(1,4,−2)\mathbf{v} = (1, 4, -2).

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

Find ∥v∥\|\mathbf{v}\| for v=(1,−2,2,4)\mathbf{v} = (1, -2, 2, 4).

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

Find the distance between u=(2,5,−1)\mathbf{u} = (2, 5, -1) and v=(−1,1,11)\mathbf{v} = (-1, 1, 11).

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

Find the unit vector in the direction of v=(6,−2,3)\mathbf{v} = (6, -2, 3). Enter it as a triple.

Enter a point like (2, -3)

Practice 5

Find the value of kk that makes (2,k,−3)(2, k, -3) and (4,1,k)(4, 1, k) orthogonal.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

Find the angle, in degrees, between u=(1,1,0)\mathbf{u} = (1, 1, 0) and v=(1,0,1)\mathbf{v} = (1, 0, 1).

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

Suppose ∥u∥=3\|\mathbf{u}\| = 3, ∥v∥=4\|\mathbf{v}\| = 4 and u⋅v=2\mathbf{u} \cdot \mathbf{v} = 2. Find ∥u+v∥2\|\mathbf{u} + \mathbf{v}\|^2.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 8

Let W=Span⁡{(1,0,2),(0,1,−1)}W = \operatorname{Span}\{(1, 0, 2), (0, 1, -1)\}. The complement W⊥W^\perp is a line. Find the vector in W⊥W^\perp whose third entry is 11.

Enter a point like (2, -3)