Free preview

64 lessons

Essential Linear Algebra for ML

Free preview

Essential Linear Algebra for ML · 64 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

Matrices as linear transformations

What does a matrix do to a vector?

Explanation

When we multiply a vector by a matrix, we are applying a linear transformation to that vector.

A linear transformation is a rule that takes a vector and produces a new one, in a way that is consistent for every vector. For a single vector, v⃗\vec{v}, this might change its length, its direction, or both.

For example:

A=[2001],v⃗=[34]A = \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix}, \quad \vec{v} = \begin{bmatrix} 3 \\ 4 \end{bmatrix}

Multiplying:

Av⃗=[2001][34]=[2×3+0×40×3+1×4]=[64].A\vec{v} = \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} 3 \\ 4 \end{bmatrix} = \begin{bmatrix} 2 \times 3 + 0 \times 4 \\ 0 \times 3 + 1 \times 4 \end{bmatrix} = \begin{bmatrix} 6 \\ 4 \end{bmatrix}.

Here, the xx‑coordinate has been doubled, while the yy‑coordinate is unchanged. The arrow that pointed to (3,4)(3,4) now points to (6,4)(6,4).

In this way, a matrix takes a vector and maps it to a new vector. The transformation preserves the fundamental structure of vector addition and scaling - this is what makes it linear.

The numbers in the matrix tell us exactly how to build each coordinate of the new vector from the coordinates of the original, in 2D:

  • The first row tells us how to calculate the new xx-coordinate - it’s a weighted mix of the old xx and yy.
  • The second row tells us how to calculate the new yy-coordinate - again, a weighted mix of the old xx and yy.

The same intuition applies in any number of dimensions.

Think of each row as a recipe: take the old coordinates, multiply them by the numbers in the row, and add them up to get the new coordinate.


Have a play around with the interactive chart below. Notice how varying the entries of AA creates a new vector, Av⃗A\vec{v}.

2468−2−4−6−82468−2−4−6−8
A=[2001]
v→
Av→

Drag the blue vector to change v→. Edit the matrix A below or click "Random Matrix" to explore.

Example

Given the matrix:

A=[2001]A = \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix}

and the vector:

v⃗=[11].\vec{v} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}.

What is the effect of transforming v⃗\vec{v} with AA?

Solution

Let's compute the resulting vector, Av⃗A\vec{v}:

Av⃗=[2001][11]=[2×1+0×10×1+1×1]=[21].A\vec{v} = \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 2 \times 1 + 0 \times 1 \\ 0 \times 1 + 1 \times 1 \end{bmatrix} = \begin{bmatrix} 2 \\ 1 \end{bmatrix}.

Thus the resulting vector, Av⃗A \vec{v}, has increased its length and changed its direction.

We can visualise this as:

0.511.522.5−0.5−1−1.50.511.522.5−0.5−1−1.5−2−2.5−2−2.5
v→
Av→
A=[2001]

Practice questions

3 questions

Given the matrix:

A=[3003],A = \begin{bmatrix} 3 & 0 \\ 0 & 3 \end{bmatrix},

and the vector:

v⃗=[21].\vec{v} = \begin{bmatrix} 2 \\ 1 \end{bmatrix}.

What is the effect of transforming v⃗\vec{v} with AA?

Select the correct answer:

+ 2 more questions

Recognising common transformation types

Explanation

We have just seen that multiplying a vector by a matrix changes its length, its direction, or both.

Now we’ll put names to three of the most common types of changes we'll see.

Scaling

Definition

A scaling multiplies each coordinate by a fixed factor:

[sx00sy][xy]=[sxxsyy].\begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} s_x x \\ s_y y \end{bmatrix}.

When the two factors are equal, sx=sy=ks_x = s_y = k, every vector stays on the same line through the origin and its length is multiplied by ∣k∣|k|.

Taking equal factors sx=sy=ks_x = s_y = k:

  • If k>1k > 1, the vector gets longer and points the same way.
  • If 0<k<10 < k < 1, the vector gets shorter and points the same way.
  • If k<0k < 0, the vector also reverses direction.

Example:

A=[2002],v⃗=[13]A = \begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix}, \quad \vec{v} = \begin{bmatrix} 1 \\ 3 \end{bmatrix} Av⃗=[26].A\vec{v} = \begin{bmatrix} 2 \\ 6 \end{bmatrix}.

The new vector points in the same direction as the original but is twice as long.


Rotation

Definition

A rotation turns every vector about the origin through the same angle θ\theta, measured anticlockwise, and leaves every length unchanged. In 2 dimensions its matrix is

[cos⁡θ−sin⁡θsin⁡θcos⁡θ].\begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}.

The angle θ\theta can be given in degrees or radians.

Example:

A=[0−110],v⃗=[10]A = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, \quad \vec{v} = \begin{bmatrix} 1 \\ 0 \end{bmatrix} Av⃗=[01].A\vec{v} = \begin{bmatrix} 0 \\ 1 \end{bmatrix}.

The vector has been rotated 90∘90^\circ anticlockwise.


Reflection

Definition

A reflection sends every vector to its mirror image in a fixed line through the origin, leaving lengths unchanged. The two cases we meet here are the axes: reflecting in the xx-axis sends [xy]\begin{bmatrix} x \\ y \end{bmatrix} to [x−y]\begin{bmatrix} x \\ -y \end{bmatrix}, with matrix [100−1]\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}, and reflecting in the yy-axis sends it to [−xy]\begin{bmatrix} -x \\ y \end{bmatrix}, with matrix [−1001]\begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix}.

Example:

A=[100−1],v⃗=[23]A = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}, \quad \vec{v} = \begin{bmatrix} 2 \\ 3 \end{bmatrix} Av⃗=[2−3].A\vec{v} = \begin{bmatrix} 2 \\ -3 \end{bmatrix}.

The vector has been reflected over the xx-axis.


We can summarise this as:

  • Scaling: each coordinate is multiplied by a fixed factor; with equal factors the vector stays on its line through the origin and only its length changes.
  • Rotation: every vector turns through the same angle about the origin; lengths stay the same.
  • Reflection: every vector is sent to its mirror image in a fixed line through the origin; lengths stay the same.

Have a play with the interactive visualisation below to get a feel for the types of transformations.

1234−1−2−3−41234−1−2−3−4
A=[sx00sy]=[2000.5]
v→
Av→

Drag the blue vector to change v→. Use the controls to change the transformation matrix A and see how it affects v→.

Practice questions

3 questions

Look at the visualisation below, what sort of transformation does applying the matrix AA to v⃗\vec{v} correspond to?

1234−1−2−3−41234−1−2−3−4
v→
Av→

Select the correct answer:

+ 2 more questions

Visualising transformations

Explanation

Up to now, we’ve only looked at what happens when a matrix multiplies one vector.

But a matrix doesn’t have a special rule for each vector - it applies the same rule to any vector we give it.

That means if we could somehow see what happens to lots of different vectors at once, we’d get a much clearer picture of what the matrix is really doing.

From one vector to the whole picture

Tip

Think of the plane as being filled with arrows (i.e. vectors) starting at the origin, each pointing to a different location.

When we apply a matrix, every arrow moves in a consistent way - stretched, rotated, flipped, or skewed according to the same pattern.

Looking at just one arrow is like looking at a single frame from a film - we get a hint of the story, but not the whole plot.

Seeing what happens to many arrows at once shows us the character of the transformation.

We can visualise this:

246−2−4−6246−2−4−6

Edit the matrix A below or click "Random Matrix" to see how it transforms the grid and vectors.

This diagram shows how a matrix (a linear transformation) changes vectors in the plane:

  • The blue grid and solid arrows show the original positions.
  • The red grid and dashed arrows show where those same vectors move when multiplied by the matrix AA.
  • Each arrow starts at the origin and points to a specific vector before and after the transformation.
  • Use the controls to change the entries of AA and see how the transformation affects the entire grid and all the vectors at once. Try changing a single entry first and watch its effect.

Whatever entries we choose for AA, some features of the picture never change.

Linear transformations fix the origin and preserve straight and parallel lines

Theorem

For any linear transformation, the origin stays fixed, straight lines remain straight, and parallel lines stay parallel. The grid’s structure is preserved - only its shape, size, and orientation change. The one exception is a matrix that collapses the plane onto a line or onto the origin, where the grid collapses with it.

Grid explainer

Details

Example

The unit square has corners at:

(0,0), (1,0), (0,1), (1,1)(0,0),\ (1,0),\ (0,1),\ (1,1)

Given the matrix:

A=[2001]A = \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix}
  1. Apply AA to each corner of the unit square.
  2. Write down the new coordinates.
  3. Describe the overall change to the shape.

Solution

Step 1 - Apply AA to each corner

  1. Corner (0,0)(0,0):
A[00]=[00]A \begin{bmatrix} 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}
  1. Corner (1,0)(1,0):
A[10]=[20]A \begin{bmatrix} 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 2 \\ 0 \end{bmatrix}
  1. Corner (0,1)(0,1):
A[01]=[01]A \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \end{bmatrix}
  1. Corner (1,1)(1,1):
A[11]=[21]A \begin{bmatrix} 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 2 \\ 1 \end{bmatrix}

Step 2 - New coordinates

The transformed corners are:

(0,0), (2,0), (0,1), (2,1)(0,0),\ (2,0),\ (0,1),\ (2,1)

Step 3 - Interpretation

Every point’s xx-coordinate has been doubled, while the yy-coordinate stayed the same.

The unit square has become a rectangle twice as wide - the whole grid is stretched horizontally.

We can visualise this as:

0.511.522.5−0.50.511.52−0.5

Practice questions

3 questions

The unit square has corners at:

(0,0), (1,0), (0,1), (1,1).(0,0),\ (1,0),\ (0,1),\ (1,1).

Given the matrix:

A=[100−1].A = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}.

Which option best describes what happens to the unit square after applying AA to it?

Select the correct answer:

+ 2 more questions