Projections

At the start of the chapter, we considered the projection of one vector onto the direction of another, and how to use that projection to decompose the vector into two orthogonal components. We then considered the higher dimensional analogy of projecting a vector onto a subspace and, using that projection to decompose the vector into orthogonal components. In this section, we concentrate on these concepts, and consider projections as linear transformations which have applications beyond vector decomposition.

A linear transformation T:RnRn is a projection if TT=T. The result of applying a projection once is the same as the result of applying it twice. We can understand the effect of a projection as the collapsing of a vector in Rn onto a subspace V. Once a vector has been projected onto V, any further projection onto V has no effect.

Example 1: Orthogonal projection in R2

We look first at a projection onto the x1-axis in R2. This transformation T:R2R2 can be defined with the following formula.

T([xy])=[x0]

This projection simply carries all vectors onto the x1-axis based on their first entry.

%matplotlib inline
import numpy as np
import laguide as lag
import matplotlib.pyplot as plt

x=np.linspace(-6,6,100)

fig, ax = plt.subplots()

options = {"head_width":0.1, "head_length":0.2, "length_includes_head":True}

ax.arrow(0,0,2,4,fc='b',ec='b',**options)
ax.arrow(0,0,2,0,fc='r',ec='r',**options)
ax.arrow(2,0,0,4,fc='r',ec='r',**options)

ax.set_xlim(-4,4)
ax.set_ylim(-2,6)
ax.set_aspect('equal')
ax.set_xticks(np.arange(-4,4,step = 1))
ax.set_yticks(np.arange(-2,6,step = 1))

ax.text(1.5,2,'$X$')
ax.text(1.3,-0.8,'$T(X)$')
ax.text(2.2,1.85,'$X - T(X)$')

ax.axvline(color='k',linewidth = 1)
ax.axhline(color='k',linewidth = 1)

ax.grid(True,ls=':')
_images/Projections_3_0.png

Notice that if we decompose X into the components T(X) and XT(X), then T(X) and XT(X) are orthogonal.

Example 2 Nonorthogonal projection in R2

Now let’s consider T:R2R2 defined by T(X)=PX where P is the following matrix.

[3/23/21/21/2]

To verify that T is indeed a projection, we need to check that P2X=PX for every X in R2. This will be true if P2=P.

P = np.array([[1.5, -1.5],[0.5, -0.5]])
print(P@P)
[[ 1.5 -1.5]
 [ 0.5 -0.5]]

Now that we know T is a projection, let’s consider the effect of T a bit more carefully. We observe that the second column is a multiple of the first. This means that C(P) is one-dimensional. Since P2=P, we know that any vector X in the column space will be unchanged when multiplied by P.

PX=[3/23/21/21/2][3kk]=[3kk]=X

Next, let’s observe the effect of T on a vector X that is not in C(P)

PX=[3/23/21/21/2][24]=[31]
X = np.array([[2],[4]])
print(P@X)
[[-3.]
 [-1.]]
fig, ax = plt.subplots()

ax.arrow(0,0,2,4,fc='b',ec='b',**options)
ax.arrow(0,0,-3,-1,fc='r',ec='r',**options)
ax.plot(x,x/3,ls=':')

ax.set_xlim(-4,4)
ax.set_ylim(-2,6)
ax.set_aspect('equal')
ax.set_xticks(np.arange(-4,4,step = 1))
ax.set_yticks(np.arange(-2,6,step = 1))

ax.text(1.5,2,'$X$')
ax.text(-1.2,-1,'$PX$')
ax.text(3,1.6,'$\mathcal{C}(P)$')

ax.axvline(color='k',linewidth = 1)
ax.axhline(color='k',linewidth = 1)

ax.grid(True,ls=':')
_images/Projections_9_0.png

We see of course that PX is in C(P), but in contrast to the projection in the previous example, this projections is not orthogonal. If we decompose X into components PX and XPX, we see that these components are not orthogonal.

V = P@X
E = X-V
print(lag.DotProduct(V,E))
-20.0
fig, ax = plt.subplots()

ax.arrow(0,0,2,4,fc='b',ec='b',**options)
ax.arrow(-3,-1,5,5,fc='r',ec='r',**options)
ax.arrow(0,0,-3,-1,fc='r',ec='r',**options)
ax.plot(x,x/3,ls=':')

ax.set_xlim(-4,4)
ax.set_ylim(-2,6)
ax.set_aspect('equal')
ax.set_xticks(np.arange(-4,4,step = 1))
ax.set_yticks(np.arange(-2,6,step = 1))

ax.text(1.5,2,'$X$')
ax.text(-1.2,-1,'$PX$')
ax.text(-1.5,2,'$X-PX$')
ax.text(3,1.6,'$\mathcal{C}(P)$')

ax.axvline(color='k',linewidth = 1)
ax.axhline(color='k',linewidth = 1)

ax.grid(True,ls=':')
_images/Projections_12_0.png

The transformation is projecting onto C(P), but not in the orthogonal direction. To reveal the direction of the projection, we look at N(P).

N(P)=Span of{[11]}

Any vector in N(P) is projected to the zero vector. The key point is that PXX must be in N(P) precisely because P represents a projection and P2=P.

P(PXX)=P2XPX=PXPX=0

In general, projections take all vectors onto one subspace, in the direction of another subspace.

Orthogonal projections

Having observed that projections can be carried out in an arbitrary direction, we now focus attention on how to define a matrix P that carries out an orthogonal projection onto a subspace V of Rn. That is, for a vector B in Rn, we want the projection matrix P such that ˆB=PB is in V and E=BˆB to be orthogonal to V. In fact, we have already seen the necessary calculations for these projections.

In the simplest example, V is one-dimensional with basis {V}, with V a vector of unit length. In our discussion of orthogonalization we worked out the following formula.

ˆB=(BVVV)V

Since V is a unit vector, we can use VV=1. We can also manipulate this order of the factors since BV=VB, and the order that we multiply the scalar and vector does not affect the result.

ˆB=V(VB)

In order to understand this as a matrix-vector multiplication, we can rewrite it using matrix multiplication for the dot products, and the associative property of matrix multiplication.

ˆB=V(VTB)=(VVT)B

The result is that if V is a unit vector, the matrix P=VVT orthogonally projects B onto V.

In the most general case, V is a m-dimensional subspace of Rn with basis {V1,V2,...,Vm}. This scenario was treated in the previous section on least squares solutions. If A is a matrix with the Vi as columns, and B is a vector that is not in C(A), we found that we can solve the normal equations ATAX=ATB in order to find a vector ˆX such that AˆX=ˆB and ˆB is the orthogonal projection of B onto C(A). If we now want to focus on the projection of B, we can write ˆB=PB where P=A(ATA)1AT is the projection matrix.

In the case that we have an orthonormal basis, projection onto a subspace is quite a bit simpler. If {U1,U2,...,Um} is an orthonormal basis for an m-dimensional subspace V, ˆB can be written in expressed in terms of the basis vectors, with coordinates computed using dot products.

ˆB=(BU1)U1+(BU2)U2+...+(BUm)Um

As we did in the 1-dimensional case, we can use the properties of dot products and matrix multiplications to rewrite this as ˆB=PB for some projection matrix P.

ˆB=U1(U1B)+U2(U2B)+...+Um(UmB)=U1(UT1B)+U2(UT2B)+...+Um(UTmB)=(U1UT1)B+(U2UT2)B+...+(UmUTm)B=(U1UT1+U2UT2+...+UmUTm)B

Each of the matrices UTiUi projects B onto the direction of Ui. The sum of all of the individual projections gives us ˆB, and the sum of all the projection matrices gives us P=U1UT1+U2UT2+...+UmUTm.

We can also realize that the formula P=A(ATA)1AT for the general case still applies. Let’s take Q to be the matrix with Ui as columns. Since Q is an orthogonal matrix, QTQ=I, and P=Q(QTQ)1QT=QQT.

Exercises

Exercise 1: Let U be the subspace of R3 spanned by {X1,X2} where X1 and X2 are given below. Find a matrix P such that PX=B where B is the orthogonal projection of X onto U. Verify your answer.

X1=[112]X2=[201]
## Code solution here

Exercise 2: Let W be a subspace of R4 with dimW=2. What will be the dimensions of a matrix P that projects vectors orthogonally onto W? Will the dimensions of P change if dimW=3?

## Code solution here 

Exercise 3:

(a) Let U={[x1x2x3,x4]T:x1+3x2+x32x4=0}. Find the matrix P that projects vectors orthogonally onto U.

(b) Demonstrate that your matrix is a projection matrix by showing that P2=P.

## Code solution here.

Exercise 4:

(a) Let V={[x1x2x3]T:x1+x2+x3=0}. Find the matrix P that projects vectors orthogonally onto V by two different methods.

(b) Demonstrate that your matrix is a projection matrix by showing that P2=P.

## Code solution here

Exercise 5:

(a) Find the matrix P1 that projects orthogonally onto the subspace spanned by {[21]}.

(b) Find the matrix P2 that projects orthogonally onto the subspace spanned by {[14]}.

(c) Is the product P1P2 also an orthogonal projection?

## Code solution here.

Exercise 6: Let U be the subspace spanned by {[010]} and W be the subspace spanned by {[211],[011]}

(a) Find a matrix P so that PX is the projection of X onto U along W.

(b) Check that P2=P

(c) Check that PX is in U for any XR3.

(d) Check that PW=0 for any WW.

## Code solution here.