Lesson 3.1.2 shone a light straight down onto the line through u. The shadow of v had signed length ∥u∥u⋅v. The projection of v onto the line is that shadow as a vector: its signed length times the unit vector ∥u∥u.
That divides by ∥u∥ twice, which is dividing by ∥u∥2=u⋅u. The formula needs no square roots.
Rule: Projection onto a line
p=u⋅uu⋅vu
p is a multiple of u, so it lies on the line through u. The number in front is the coefficient of the projection. It needs u=0: the zero vector gives no line.
For u=(3,1) and v=(1,2): u⋅v=5 and u⋅u=10, so p=0.5u=(1.5,0.5). Its length is 2.5≈1.5811, the shadow length of lesson 3.1.2.
The cases follow from the sign and size of the coefficient:
Behind the origin.(−2,1) has u⋅v=−5, so p=−0.5u=(−1.5,−0.5): the coefficient is negative.
Already on the line.(6,2)=2u projects onto itself.
Orthogonal to the line.(−1,3) has u⋅v=0, so p=0.
Only the line matters. With 2u in place of u the coefficient halves to 4010=0.25, and 0.25×2u is the same p. So is −0.5×(−u).
The residual is orthogonal
The residual is what is left of v after the projection: r=v−p. Here r=(1,2)−(1.5,0.5)=(−0.5,1.5), and u⋅r=−1.5+1.5=0. The residual is orthogonal to the line (lesson 3.1.2).
That is how the coefficient is chosen. Ask for the multiple cu that leaves a residual orthogonal to u:
u⋅(v−cu)=0u⋅v−c(u⋅u)=0c=u⋅uu⋅v
So v=p+r splits v into a part along u and a part at right angles to it. The two parts are the legs of a right-angled triangle with hypotenuse v, so Pythagoras gives ∥v∥2=∥p∥2+∥r∥2: here 5=2.5+2.5.
An example in space. Project v=(3,3,0) onto the line through u=(1,2,2). The dot product is 9 and u⋅u=9, so p=u=(1,2,2) and r=(2,1,−2). Check: u⋅r=2+2−4=0, and 18=9+9.
The nearest point on the line
Every point of the line is tu for some number t. Which one is closest to v=(1,2)? Here are five:
t
0
0.25
0.5
0.75
1
point tu
(0,0)
(0.75,0.25)
(1.5,0.5)
(2.25,0.75)
(3,1)
distance to v
2.2361
1.7678
1.5811
1.7678
2.2361
The smallest is at t=0.5, the projection. Algebra shows why: the squared distance is ∥v−tu∥2=5−10t+10t2, a parabola. Its lowest point is at t=2×1010=0.5 (lesson 1.2.2).
The projection p is the point of the line nearest v. The residual r meets the line at a right angle; any other point of the line is further from v.
The right angle is the reason in every case. For any other point q on the line, v−q=r+(p−q), and those two parts are orthogonal. So ∥v−q∥2=∥r∥2+∥p−q∥2, which is never less than ∥r∥2.
Rule: The nearest point
p is the point of the linenearest vdistance=∥v−p∥
The distance from a point to a line is the length of the residual, measured at right angles to the line.
The explorer starts on this example, with the projection as the dark arrow. Drag v onto the line and the projection becomes v; drag it to a right angle with u and the projection shrinks to 0. Then stretch or reverse u: the projection does not move.
Explore
Drag the tips of u and v. Watch the sign of u · v as the angle passes 90°. The dark arrow is the projection of v on u: the shadow v casts on the line through u.
u
(3, 1), length 3.1623
v
(1, 2), length 2.2361
u · v
3 × 1 + 1 × 2 = 5
Angle
cos θ = 0.7071, so θ = 45°
Projection of v on u
(1.5, 0.5), signed length u · v / |u| = 1.5811
So
The dot product is positive, so the angle is less than 90°.
Try it
proj(u, v) is the rule. The first lines print p, r, u⋅r and the three squared lengths.
The loop is the table of distances. The last line repeats the example in space.
⌘/Ctrl + Enter runs the cell
Projection onto a plane
A plane through the origin is the span of two independent vectors (lesson 3.5.1). Put them in the columns of a matrix A. Every point of the plane is Ax for some x: the plane is the column space of A.
To project b onto the plane, look for p=Ax^ whose residual b−p is orthogonal to the whole plane. It is enough to be orthogonal to both columns, since every vector of the plane is a combination of them. (x^ is read "x hat".)
The two conditions a1⋅(b−Ax^)=0 and a2⋅(b−Ax^)=0 are the two rows of one matrix equation. The rows of A⊤ are the columns of A (lesson 3.2.3), so
A⊤(b−Ax^)=0A⊤Ax^=A⊤b
A⊤A is square, and it has an inverse when the columns of A are independent. Lesson 3.5.3 meets the case where they are not.
Rule: Projection onto a column space
x^=(A⊤A)−1A⊤bp=Ax^A⊤(b−p)=0
A has independent columns. p is the point of the column space nearest b, and the residual is orthogonal to every column. With one column u, this is the rule for a line: A⊤A=u⋅u and A⊤b=u⋅b.
A worked example
Take lesson 3.5.1's plane, spanned by a1=(1,0,1) and a2=(0,1,1), and the point b=(0,1,4). It is off the plane: its third component is not the sum of the first two.
A=101011A⊤A=(2112),A⊤b=(45)
A⊤A is the symmetric matrix S of lesson 3.2.3, as every A⊤A is symmetric. Solving 2x1+x2=4 and x1+2x2=5 gives x^=(1,2). So p=a1+2a2=(1,2,3), a point lesson 3.5.1 found in the plane.
The residual is r=(0,1,4)−(1,2,3)=(−1,−1,1). Check: a1⋅r=−1+0+1=0 and a2⋅r=0−1+1=0. The distance from b to the plane is ∥r∥=3≈1.7321, and Pythagoras holds again: 17=14+3.
The projection of b onto a plane. The residual r is orthogonal to both columns, so it meets the plane at a right angle, and p is the nearest point.
Other points of the plane are further away. A(1.5,2)=(1.5,2,3.5) is 1.8708 from b, and A(1,1)=(1,1,2) is 2.2361 away.
In R3, a plane through the origin leaves only one direction at right angles to it, here that of (1,1,−1). So every residual for this plane is a multiple of (1,1,−1). A point already in the plane, such as (2,3,5), is its own projection, with residual 0.
Try it
The first lines build A⊤A and A⊤b, solve for x^, and print p, r, A⊤r and the distance.
The loop measures other points Ax of the plane. The last lines project two more points: one off the plane, whose residual is again along (1,1,−1), and one in it.
⌘/Ctrl + Enter runs the cell
The projection matrix
The formula p=A(A⊤A)−1A⊤b multiplies b by one matrix, the same for every b. It is the projection matrixP onto the column space.
Onto the line through u:P=u⊤uuu⊤, a column times a row (lesson 3.2.3). For u=(3,1), P=101(9331), and Pv=(1.5,0.5).
Onto an axis: for u=e1, P=(1000), the projection of lesson 3.2.2.
Onto the plane above:P=312−11−121112, and Pb=(1,2,3).
Projecting twice changes nothing: p is already in the plane, so its projection is itself. In symbols, P2=P. Every projection matrix is also symmetric, and I−P gives the residual, (I−P)b=r.
Rule: Projection matrix
P=A(A⊤A)−1A⊤P2=P,P⊤=P
Pb is the projection of b and (I−P)b its residual. A projection flattens space onto the column space, so unless that is all of Rn, P has no inverse (lesson 3.2.3).
Try it
The first block builds P for the line through (3,1), printed as 10P to show whole numbers. The second builds it for the plane, printed as 3P.
np.allclose checks P2=P and P⊤=P. The rank of the plane's P is 2: it flattens space onto a plane.
⌘/Ctrl + Enter runs the cell
Orthonormal columns
Vectors are orthonormal when each has length 1 and every pair is orthogonal. Write Q for a matrix whose columns q1,…,qk are orthonormal. Entry (i,j) of Q⊤Q is qi⋅qj, which is 1 when i=j and 0 otherwise, so Q⊤Q=I.
Then (A⊤A)−1 in the projection formula is I−1=I, and nothing needs solving.
Rule: Projection with orthonormal columns
Q⊤Q=Ip=QQ⊤b=(q1⋅b)q1+⋯+(qk⋅b)qk
With orthonormal columns, the projection onto the plane is the sum of the projections onto each line qi. That is lesson 3.5.1's shortcut for coordinates in an orthogonal basis: each coordinate is a dot product.
Making the columns orthonormal. The plane above has columns that are not orthogonal: a1⋅a2=1. Keep the direction of a1 and normalise it: q1=21(1,0,1).
Then take from a2 its projection onto a1. The residual a2−21a1=(−0.5,1,0.5) is orthogonal to a1 and still in the plane. Normalised, it is q2≈(−0.4082,0.8165,0.4082).
For b=(0,1,4), the two dot products are q1⋅b≈2.8284 and q2⋅b≈2.4495. So p=2.8284q1+2.4495q2=(1,2,3), as before.
Q here is 3×2, so Q⊤Q=I but QQ⊤ is not I: it is P. np.linalg.qr builds orthonormal columns for any independent columns; their signs can differ between computers, but QQ⊤ cannot. Lesson 3.6.3 meets square Q, for which Q⊤Q=QQ⊤=I.
Try it
The cell makes q1 and q2 as above and checks Q⊤Q=I, rounded.
Then Q⊤b holds the two dot products, and QQ⊤b is the projection. The last line compares QQ⊤ with P, for this Q and for the one from np.linalg.qr.
⌘/Ctrl + Enter runs the cell
Lesson 3.5.3 uses the projection onto a column space to solve systems that have no solution.
Common mistakes
The coefficient divides by u⋅u, not by ∥u∥. Dividing by the length once gives the shadow's signed length of lesson 3.1.2, a number; the projection is a vector.
The order matters.v=(1,2) onto the line through u=(3,1) is (1.5,0.5), but u onto the line through v is (1,2).
The residual is measured at right angles, not straight down. The point of the plane straight below (0,1,4) is (0,1,1), at distance 3; the nearest is (1,2,3), at 3.
Projecting onto each column and adding works only for orthogonal columns. For a1 and a2 above, the separate projections of b add up to (2,2.5,4.5), not (1,2,3).
Q⊤Q=I, but QQ⊤=I only for a square Q. For a tall Q, QQ⊤ is the projection matrix.
Check the right angle with a tolerance. A residual's dot product with a column can compute as 10−16 rather than 0 (lesson 3.1.2).
Orthogonal projection
0 of 12 correct
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
How to type answers
Powers with ^: x^7, 2^(1/3), a^-2.
Multiply with * or by writing side by side: 3x^2, 2(x+1).
ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
Your first answer to each question is saved: it counts towards your score and brings the question back for review.
Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.
Summary card
The projection of v onto the line through u is p=u⋅uu⋅vu. It depends only on the line, not on the length or sign of u.
The residual r=v−p is orthogonal to u, so ∥v∥2=∥p∥2+∥r∥2, and p is the nearest point of the line.
Onto the column space of A, with independent columns: A⊤Ax^=A⊤b, p=Ax^, and the residual is orthogonal to every column, A⊤(b−p)=0.
The projection matrix P=A(A⊤A)−1A⊤ has P2=P and P⊤=P; (I−P)b is the residual.
Orthonormal columns have Q⊤Q=I, and then p=QQ⊤b: one dot product per column.