Free preview

61 lessons

Essential Linear Algebra for ML

Free preview

Essential Linear Algebra for ML · 61 lessons

Master the linear algebra that powers modern ML

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

Projecting vectors onto a line

Geometric meaning of projection in 22 dimensions

Explanation

The scalar projection of a vector v\vec{v} onto another vector w\vec{w} tells us how much of v\vec{v} points in the direction of w\vec{w}, measured as a signed length.

The scalar projection is defined as:

projwv=vcos(θ)\text{proj}_{\vec{w}} \vec{v} = |\vec{v}| \cos(\theta)

where θ\theta is the angle between v\vec{v} and w\vec{w}. The line defined by w\vec{w} must pass through the origin, since projection formulas assume both vectors are positioned with their tails at the origin.

Geometrically, to find the scalar projection, we drop a perpendicular from the tip of v\vec{v} to the line defined by w\vec{w} (which extends infinitely in both directions). The point where this perpendicular meets the line is the "shadow" of v\vec{v}, and the distance from the origin to this point - measured along the line - is the scalar projection.

We denote this projection as projwv\text{proj}_{\vec{w}} \vec{v}.

Have a play with the interactive visualisation below and see how the scalar projection changes as v\vec{v} is moved around.

1234−1−2−3−41234−1−2−3−4
v
w
projwv
θ
|v|=2.83
projwv=2.00

In the example above, w\vec{w} points in the direction of the xx-axis, but in general, v\vec{v} and w\vec{w} can point in any direction. If v\vec{v} and w\vec{w} point in roughly opposite directions, the scalar projection will be negative.

To see this, suppose v=3|\vec{v}| = 3 and w\vec{w} makes an angle of 120120^\circ with v\vec{v}. Then we have:

projwv=vcos(120)=3cos(120)=1.5.\begin{align*} \text{proj}_{\vec{w}} \vec{v} &= |\vec{v}| \cos(120^\circ) \\ &= 3 \cos(120^\circ) \\ &= -1.5. \end{align*}

Have a play with the interactive visualisation below where both v\vec{v} and w\vec{w} are now draggable.

Notice how we drop a perpendicular from the tip of v\vec{v} to the line defined by w\vec{w}. The right angle is formed where this perpendicular meets the line, which may be "behind" the origin relative to w\vec{w} if the angle is greater than 9090^\circ. This is why the scalar projection can be negative.

1234−1−2−3−41234−1−2−3−4
v
w
projwv
θ
|v|=3.00
projwv=2.57
Intuition

Tip

A negative scalar projection means that v\vec{v} points at least partially in the opposite direction to w\vec{w}. The projection only considers movement along w\vec{w}, so if v\vec{v} "moves backward" along w\vec{w}, the result is negative.

Key points

Key Point

  • The scalar projection measures the component of v\vec{v} along w\vec{w}.
  • It is a scalar value.
  • The value can be positive or negative depending on the direction.

Example

Let v\vec{v} be a vector with v=9|\vec{v}| = 9.

Let w\vec{w} be a vector that makes an angle of 2π3\dfrac{2\pi}{3} with v\vec{v}.

Calculate projwv\text{proj}_{\vec{w}} \vec{v}.

Solution

To calculate the projection of v\vec{v} onto w\vec{w}, we use the formula:

projwv=vcos(θ)=9cos(2π3)=9(12)=92.\begin{align*} \text{proj}_{\vec{w}} \vec{v} &= |\vec{v}| \cos(\theta) \\ &= 9 \cos\left(\frac{2\pi}{3}\right) \\ &= 9 \left(-\frac{1}{2}\right) \\ &= -\frac{9}{2}. \end{align*}

Practice questions

3 questions

Let v\vec{v} be a vector with v=6|\vec{v}| = 6.

Let w\vec{w} be a vector that makes an angle of π4\dfrac{\pi}{4} with v\vec{v}.

Calculate projwv\text{proj}_{\vec{w}} \vec{v}.

Select the correct answer:

+ 2 more questions

Scalar and vector projection in 22 dimensions

Explanation

Now that we’ve understood the scalar projection as the signed length of the shadow of v\vec{v} on the direction of w\vec{w}, let’s connect this idea to the dot product. This will also set us up to properly describe what that blue arrow in our visualisations has actually been representing all along.

Dot product connection

The scalar projection of v\vec{v} onto w\vec{w} is the signed length of the "shadow" of v\vec{v} in the direction of w\vec{w}.

Also recall the dot product formula:

vw=vwcos(θ)\vec{v} \cdot \vec{w} = \|\vec{v}\| \|\vec{w}\| \cos(\theta)

we can solve for vcos(θ)\|\vec{v}\| \cos(\theta), the scalar projection:

vcos(θ)=vww\|\vec{v}\| \cos(\theta) = \frac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|}

So, the scalar projection can be computed directly as:

projwv=vww\text{proj}_{\vec{w}} \vec{v} = \frac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|}

This formula is especially useful because it works in any dimension and doesn’t require us to find the angle. The dot product measures how much two vectors "align", and dividing by w\|\vec{w}\| ensures the result is independent of the length of w\vec{w}.

From scalar projection to vector projection

So far we’ve only measured the length of the shadow. But in our diagrams above we also drew the blue arrow lying along w\vec{w}. Strictly speaking, that arrow isn’t the scalar projection at all - it’s the vector projection of v\vec{v} onto w\vec{w}.

  • The scalar projection tells us the “how long the shadow is” (a number) - this was the length of the blue vector.
  • The vector projection is the actual (blue) vector along w\vec{w} that represents this shadow.

The vector projection is given by:

projwvecv=(vww2)w.\text{proj}_{\vec{w}}^{\text{vec}} \vec{v} = \left( \frac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2} \right) \vec{w}.

In words: the vector projection is simply the scalar projection multiplied by the unit vector in the direction of w\vec{w}.

Derivation

Details

Note

The norm (length) of the vector projection is always the absolute value of the scalar projection:

projwvecv=projwv\left\| \text{proj}_{\vec{w}}^{\text{vec}} \vec{v} \right\| = \left| \text{proj}_{\vec{w}} \vec{v} \right|

Since the scalar projection measures the signed length of the shadow of v\vec{v} along w\vec{w}, and the vector projection points in the correct direction, taking the norm of the vector projection simply gives the (unsigned) length of that shadow.

This is a useful way to check our calculations - if we take the norm of the vector projection, it should match the absolute value of the scalar projection.

Key points

Key Point

  • The scalar projection gives the signed length of the shadow of v\vec{v} in the direction of w\vec{w}.
  • The vector projection gives the actual vector along w\vec{w}.
  • Using the dot product, both can be computed directly from the dot product, without needing to find the angle.

Example

Let v=[23]\vec{v} = \begin{bmatrix} 2 \\ 3 \end{bmatrix} and w=[40]\vec{w} = \begin{bmatrix} 4 \\ 0 \end{bmatrix}.

Calculate both the scalar projection and the vector projection of v\vec{v} onto w\vec{w}.

Solution

First, we compute the dot product:

vw=2×4+3×0=8+0=8.\begin{align*} \vec{v} \cdot \vec{w} &= 2 \times 4 + 3 \times 0 \\ &= 8 + 0 \\ &= 8. \end{align*}

Next, we find the norm of w\vec{w}:

w=42+02=16=4.\begin{align*} \|\vec{w}\| &= \sqrt{4^2 + 0^2} \\ &= \sqrt{16} \\ &= 4. \end{align*}

The scalar projection is then:

projwv=vww=84=2.\begin{align*} \text{proj}_{\vec{w}} \vec{v} &= \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|} \\ &= \dfrac{8}{4} \\ &= 2. \end{align*}

For the vector projection, we use the formula (noting that w2=16\|\vec{w}\|^2 = 16):

projwvecv=(vww2)w=(816)[40]=(12)[40]=[20].\begin{align*} \text{proj}_{\vec{w}}^{\text{vec}} \vec{v} &= \left( \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2} \right) \vec{w} \\ &= \left( \dfrac{8}{16} \right) \begin{bmatrix} 4 \\ 0 \end{bmatrix} \\ &= \left( \dfrac{1}{2} \right) \begin{bmatrix} 4 \\ 0 \end{bmatrix} \\ &= \begin{bmatrix} 2 \\ 0 \end{bmatrix}. \end{align*}

Practice questions

4 questions

Suppose a\vec{a} and b\vec{b} are non-zero vectors in R2\mathbb{R}^2.

Consider the following statements about the scalar and vector projection of a\vec{a} onto b\vec{b}:

A. The scalar projection of a\vec{a} onto b\vec{b} can be negative, zero, or positive depending on the angle between the vectors.

B. The vector projection of a\vec{a} onto b\vec{b} is always a scalar multiple of b\vec{b}.

C. If the scalar projection is zero, the vector projection is the zero vector.

D. The norm (length) of the vector projection is always equal to the scalar projection.

E. The vector projection of a\vec{a} onto b\vec{b} always points in the same direction as b\vec{b}.

Which of the above statements are true?

Select the correct answer:

+ 3 more questions

Generalisation to NN-dimensional vectors

Explanation

So far, we’ve focused on projections in two dimensions, but the same ideas and formulas apply in any number of dimensions. The dot product and norm work exactly the same way for NN-dimensional vectors, so the projection formulas generalise directly.

Recall the formula for the scalar projection of v\vec{v} onto w\vec{w}:

projwv=vww\text{proj}_{\vec{w}} \vec{v} = \frac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|}

and the vector projection of v\vec{v} in the direction of w\vec{w}:

projwvecv=(vww2)w\text{proj}_{\vec{w}}^{\text{vec}} \vec{v} = \left( \frac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2} \right) \vec{w}

These formulas work for any non-zero direction vector w\vec{w} in any dimension - we do not need to normalise w\vec{w} first, as the formulas automatically account for its length.

Example

Let v=[145]\vec{v} = \begin{bmatrix} -1 \\ 4 \\ 5 \end{bmatrix} and w=[022]\vec{w} = \begin{bmatrix} 0 \\ 2 \\ -2 \end{bmatrix}.

Calculate both the scalar projection and the vector projection of v\vec{v} onto w\vec{w}.

Solution

First, we compute the dot product:

vw=(1)(0)+(4)(2)+(5)(2)=0+810=2.\begin{align*} \vec{v} \cdot \vec{w} &= (-1)(0) + (4)(2) + (5)(-2) \\ &= 0 + 8 - 10 \\ &= -2. \end{align*}

Next, we find the norm of w\vec{w}:

w=02+22+(2)2=0+4+4=8=22.\begin{align*} \|\vec{w}\| &= \sqrt{0^2 + 2^2 + (-2)^2} \\ &= \sqrt{0 + 4 + 4} \\ &= \sqrt{8} \\ &= 2\sqrt{2}. \end{align*}

The scalar projection is then:

projwv=vww=222=12.\begin{align*} \text{proj}_{\vec{w}} \vec{v} &= \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|} \\ &= \dfrac{-2}{2\sqrt{2}} \\ &= -\dfrac{1}{\sqrt{2}}. \end{align*}

For the vector projection, we use the formula (noting that w2=(22)2=8\|\vec{w}\|^2 = (2\sqrt{2})^2 = 8):

projwvecv=(vww2)w=(28)[022]=(14)[022]=[01212].\begin{align*} \text{proj}_{\vec{w}}^{\text{vec}} \vec{v} &= \left( \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2} \right) \vec{w} \\ &= \left( \dfrac{-2}{8} \right) \begin{bmatrix} 0 \\ 2 \\ -2 \end{bmatrix} \\ &= \left( -\dfrac{1}{4} \right) \begin{bmatrix} 0 \\ 2 \\ -2 \end{bmatrix} \\ &= \begin{bmatrix} 0 \\[6pt] -\dfrac{1}{2} \\[6pt] \dfrac{1}{2} \end{bmatrix}. \end{align*}

Practice questions

3 questions

Let v=[3642]\vec{v} = \begin{bmatrix} 3 \\ -6 \\ 4 \\ 2 \end{bmatrix} and w=[2015]\vec{w} = \begin{bmatrix} 2 \\ 0 \\ -1 \\ 5 \end{bmatrix}.

What is the scalar projection of v\vec{v} onto w\vec{w}?

Select the correct answer:

+ 2 more questions