Scientia sine finibus

Linear Algebra / Unit 6, Orthogonality / Topic 6.1

Units and Topics

Unit 6. Orthogonality

6.1 Projections

This topic finds the point of a line, or of a subspace, that is closest to a given vector. That point is called the projection of the vector. Projections are the basis of least squares in Topic 6.4, and of the rules for measurement in quantum mechanics.

You need Topics 1.2, 3.3, 4.4, and 5.1 for this topic.

1. The Projection onto a Line

Let a be a vector that is not the zero vector, and let b be any vector. The line through a is the set of all multiples of a. The projection of b onto this line is the multiple p = ca that is closest to b.

The closest point is the one for which the error e = b − p is perpendicular to the line. So a · (b − ca) = 0, which gives the number c.

c = (a · b)/(a · a) p = (a · b)/(a · a) a

For example, take a = (2, 1) and b = (3, 4).

a · b = (2)(3) + (1)(4) = 10the dot product of a and ba · a = (2)(2) + (1)(1) = 5the dot product of a with itselfp = 10/5 21 = 42the projectione = b − p = −12the error

Check that the error is perpendicular to the line: (2, 1) · (−1, 2) = 0.

The vector b has been split into two perpendicular parts, b = p + e. The part p is along the line, and the part e is perpendicular to it. You have done this in physics when a weight is split into a component along an incline and a component perpendicular to it.

Try it: the projection onto a line

Drag the tip of the blue arrow to choose the line, and drag the red point to choose the vector b. The green arrow is the projection p, and the dashed purple segment is the error e. The error always meets the line at a right angle.

2. The Projection Matrix for a Line

The projection is a linear map, so it has a matrix. Write the formula with the transpose, as in Topic 3.3: the number a · b is aTb, and the product aaT of a column and a row is a square matrix.

p = Pb with P = aaT/aTa

For a = (2, 1),

aaT = 21 21 = 4221a column times a rowP = 1/5 4221divide by aTa = 5P 34 = 1/5 2010 = 42the same projection as before

The matrix depends only on the line. Replacing a by 3a gives the same P.

3. The Projection onto a Subspace

Now let the subspace be the column space of a matrix A whose columns a₁, …, aₙ are independent. The projection p is a combination of the columns, so p = Ax̂ for some vector of coefficients, which is written x̂. The symbol is read “x hat”.

As for a line, the closest point is the one for which the error e = b − Ax̂ is perpendicular to the subspace. It must be perpendicular to each column of A, and these conditions together are AT(b − Ax̂) = 0.

The coefficients of the projection satisfy AT Ax̂ = ATb. These are called the normal equations.

The matrix AT A is square and symmetric. It has an inverse when the columns of A are independent. Solving the normal equations gives the three results.

x̂ = (AT A)⁻¹ATb p = Ax̂ P = A(AT A)⁻¹AT

An example

Project b = (3, 1, 4) onto the plane in ℝ³ that is spanned by a₁ = (1, 1, 0) and a₂ = (0, 1, 1).

A = 101101the two vectors as columnsAT A = 2112the dot products of the columns with each otherATb = 45the dot products of the columns with b

The normal equations are 2x + y = 4 and x + 2y = 5, with the solution x̂ = (1, 2).

p = 1 110 + 2 011 = 132the projection Ax̂e = b − p = 2−22the error

Check that the error is perpendicular to both columns:

(2, −2, 2) · (1, 1, 0) = 2 − 2 + 0 = 0the first column(2, −2, 2) · (0, 1, 1) = 0 − 2 + 2 = 0the second column

The formula for a line is the special case in which A has one column. Then AT A is the number a · a, and dividing by it replaces the inverse.

4. Properties of a Projection Matrix

  • P² = P. Projecting a second time changes nothing, because p is already in the subspace.
  • PT = P. A projection matrix is symmetric.
  • The eigenvalues of P are 1 and 0. The vectors of the subspace are eigenvectors with the eigenvalue 1, and the vectors perpendicular to the subspace are eigenvectors with the eigenvalue 0.
  • The trace of P is the dimension of the subspace, because the trace is the sum of the eigenvalues.

For the line in Part 2:

P² = 1/25 2010105P times PP² = 1/5 4221 = Pdivide each entry by 5tr P = 4/5 + 1/5 = 1the dimension of a line

5. The Perpendicular Part

The set of all vectors that are perpendicular to each vector of a subspace is also a subspace. It is called the orthogonal complement of the subspace. Topic 4.4 gave two examples: the null space of a matrix is the orthogonal complement of its row space, and the left null space is the orthogonal complement of its column space.

Each vector b splits in exactly one way into a part p in the subspace and a part e in the orthogonal complement. The matrix P gives the first part, and the matrix I − P gives the second part, because e = b − Pb. The matrix I − P is the projection matrix of the orthogonal complement.

The two parts are perpendicular, so their lengths satisfy the Pythagorean theorem.

|b|² = |p|² + |e|²

For the example of Part 3:

|b|² = 9 + 1 + 16 = 26the vector (3, 1, 4)|p|² + |e|² = (1 + 9 + 4) + (4 + 4 + 4) = 26the projection (1, 3, 2) and the error (2, −2, 2)

In quantum mechanics, a measurement projects the state vector onto a subspace of eigenvectors, and the probability of each result is the square of the length of the projection. The equation above then says that the probabilities add to 1. Topic 9.2 develops this.

Common Mistakes

  • Dividing by a · b in place of a · a in the projection onto a line.
  • Writing the projection matrix as aTa. That product is a number. The matrix is aaT, divided by the number aTa.
  • Simplifying A(AT A)⁻¹AT by splitting the inverse into A⁻¹(AT)⁻¹. That is allowed only for a square matrix with an inverse, and then P is the identity.
  • Using the formula when the columns of A are dependent. Then AT A has no inverse. Remove the dependent columns first.
  • Giving x̂ as the projection. x̂ holds the coefficients. The projection is p = Ax̂.
  • Forgetting to check the result. The error b − p must be perpendicular to each column of A.

Summary

FactStatement
Projection onto a linep = ((a · b) / (a · a)) a
Projection matrix for a lineP = aaT / (aTa)
Normal equationsAT Ax̂ = ATb
Projection onto the column space of Ap = Ax̂, and P = A(AT A)⁻¹AT
PropertiesP² = P and PT = P
Errore = b − p is perpendicular to the subspace

Practice

Work each problem on paper first. Then open the answer to check your work.

1. Find the projection of b = (1, 7) onto the line through a = (3, 4), and find the error.

a · b = 3 + 28 = 31a · a = 9 + 16 = 25p = (31/25)(3, 4) = (3.72, 4.96)the projectione = (1, 7) − (3.72, 4.96) = (−2.72, 2.04)the error3(−2.72) + 4(2.04) = 0the check: the error is perpendicular to a

2. Write the projection matrix for the line through (1, 1), and use it to project (5, 1).

P = (1/2) 1111.

P(5, 1) = (1/2)(6, 6) = (3, 3)

3. Write the projection matrix for the line through (1, 2, 2).

a · a = 9P = 1/9 122244244aaT divided by a · atr P = (1 + 4 + 4)/9 = 1the dimension of a line

4. Project b = (1, 2, 6) onto the plane spanned by (1, 0, 0) and (0, 1, 1).

AT A = 1002ATb = (1, 8)x̂ = (1, 4)the solution of the normal equationsp = (1, 0, 0) + 4(0, 1, 1) = (1, 4, 4)the projectione = (0, −2, 2)the error

Check: e is perpendicular to (1, 0, 0) and to (0, 1, 1).

5. A projection matrix P projects onto a plane in ℝ³. What are its eigenvalues, its trace, and its determinant?

The eigenvalues are 1, 1, and 0.

The trace is 2, and the determinant is 0.

6. Show that if P is a projection matrix, then (I − P)² = I − P.

(I − P)² = I − 2P + P²multiply out(I − P)² = I − 2P + Pbecause P² = P(I − P)² = I − P

Python Practice

Step 1. Project a vector onto a line

The instruction np.dot calculates a dot product. The code below calculates the projection of b = (3, 4) onto the line through a = (2, 1), and the error, as in Part 1 of this topic.

Python

The projection is (4, 2), and the error is (−1, 2). The last result is 0, so the error is perpendicular to the line.

Now find the projection in Practice Problem 1: the vector (1, 7) onto the line through (3, 4).

Python

The last result may be a number such as 8.9e-16 and not exactly 0. This is a very small rounding error.

Step 2. Build a projection matrix

The instruction np.outer multiplies a column by a row. For one vector with itself, it gives the square matrix aaT of Part 2.

Python

The matrix P has the rows 0.8, 0.4 and 0.4, 0.2. The second result is the projection (4, 2). The third result is P again, because projecting twice is the same as projecting once.

In the next box, change the vector so that P is the projection matrix for the line through (1, 2, 2), as in Practice Problem 3. The last line prints the trace.

Python

Step 3. Project onto a plane

The code below solves the normal equations for the example of Part 3. The expression A.T is the transpose of A, the @ sign multiplies matrices and vectors, and np.linalg.solve solves a system.

Python

The coefficients are (1, 2), and the projection is (1, 3, 2). The last result is [0. 0.]: the error is perpendicular to both columns of A.

Now project the vector (1, 2, 6) onto the plane of Practice Problem 4, which is spanned by (1, 0, 0) and (0, 1, 1). The two vectors are the columns of A, and a matrix is typed by its rows.

Python

Step 4. The projection matrix of a plane

The code below builds P = A(ATA)⁻¹AT for the plane of Part 3. The instruction np.linalg.inv calculates an inverse. The instruction np.allclose compares two matrices and gives True when all of their entries agree, apart from rounding errors.

Python

The trace is 2, the dimension of the plane. The last two results are True: P² = P, and P is symmetric.

Step 5. Check the lengths of the two parts

The instruction np.linalg.norm calculates the length of a vector, and two stars mean a power. The code below checks the equation of Part 5 for the example.

Python

Both results are 26, or very close to it.

Quiz

This quiz has 5 questions on this topic. A question asks you either to choose one answer or to type numbers. The page grades your answers and explains each one. A number can be typed as a whole number, a decimal, a fraction such as 1/3, or a calculation such as sqrt(18)/2 or arccos(1/2). The page shows the value of a calculation next to the box. Angles in a calculation are in degrees. The Calculator button under each question opens a calculator. Your answers are saved in your browser, so they are still on the page when you come back to it.

Choose how your answers are checked

1. Find the projection p of b = (4, 2) onto the line through a = (1, 1).

2. For the same vectors, b = (4, 2) and a = (1, 1), find the error e = b − p.

3. Find the projection matrix P for the line through a = (1, 3). Write the four entries.

4. Which equation is true for each projection matrix P?

5. The matrix P projects onto a subspace of ℝ⁵ that has the dimension 3. Find the trace of P.

Next: Topic 6.2, Orthogonal Bases and the Gram-Schmidt Process