Free preview

59 lessons

Essential Linear Algebra for ML

Free preview

Essential Linear Algebra for ML · 59 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, AvA\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, AvA\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, AvA \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.

1. Scaling

Scaling changes the length of a vector but not its direction.

  • If the scale factor is greater than 1, the vector gets longer.
  • If it’s between 0 and 1, the vector gets shorter.
  • If it’s negative, the vector flips direction as well as changing length.

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.


2. Rotation

Rotation changes the direction of a vector but keeps its length the same.

The amount of rotation is measured in degrees or radians, and the rotation is around the origin.

Example:

A=[0110],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 9090^\circ counterclockwise.


3. Reflection

Reflection flips a vector over a line through the origin.

The length stays the same, but the direction changes to its mirror image.

Example:

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

The vector has been reflected over the xx-axis.


We can summarise this as:

  • Scaling: length changes, direction stays the same.
  • Rotation: direction changes, length stays the same.
  • Reflection: direction changes, length stays the same, but in a mirror-like way.

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.

Key Point

Because the transformation is linear, 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.

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=[1001].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