OverviewΒΆ
The big concept introduced in Chapter 5.3 was the rank of a matrix, , which is the number of linearly independent columns of a matrix. The rank of a matrix told us the dimension of its column space, , which is the set of all vectors that can be created with linear combinations of βs columns.
The ideas of rank, column space, and linear independence are all closely related, and also all apply for matrices of any shape β wide, tall, or square. This is good, because data from the real world is rarely square: we usually have many more observations () than features ().
The big idea in Chapters 6.1 and 6.2 is that of the inverse of a matrix, , which only applies to square matrices. But, inverses and square matrices can still be useful for our rectangular matrices with real data, since the matrix is square, no matter the shape of . And, as we discussed in Chapter 5.3 and youβre seeing in Homework 6, the matrix has close ties to the correlations between the columns of , which you already know has some connection to linear regression.
Linear TransformationsΒΆ
But first, I want us to think about matrix-vector multiplication as something more than just number crunching.
In Chapter 5.3, the running example was the matrix
To multiply by a vector on the right, that vector must be in , and the result will be a vector in .
Put another way, if we consider the function , maps elements of to elements of , i.e.
Iβve chosen the letter to denote that is a linear transformation.
Every linear transformation is of the form . For our purposes, linear transformations and matrix-vector multiplication are the same thing, though in general linear transformations are a more abstract concept (just like how vector spaces can be made up of functions, for example).
For example, the function
is a linear transformation from to , and is equivalent to
The function is also a linear transformation, from to .
A non-example of a linear transformation is
because no matrix multiplied by will produce .
Another non-example, perhaps surprisingly, is
This is the equation of a line in , which is linear in some sense, but itβs not a linear transformation, since it doesnβt satisfy the two properties of linearity. For to be a linear transformation, weβd need
for any . But, if we consider and as an example, we get
which are not equal. is an example of an affine transformation, which in general is any function that can be written as , where is an matrix and .
Activity 1ΒΆ
Activity 1
Activity 1.1
True or false: if is a linear transformation, then .
Activity 1.2
For each of the following functions, determine whether it is a linear transformation. If it is, write it in the form . If it is not, explain why not.
takes , multiplies the first component by 2 and second component by 3, and returns the difference between the two, as a scalar.
Solutions
Activity 1.1
True or false: if is a linear transformation, then .
True. Since is a linear transformation, we have
for any and . Plugging in , we get
Activity 1.2
is a linear transformation.
is a linear transformation.
is not a linear transformation. Notice the constant term of -5 in the first component! This is instead an affine transformation.
is not a linear transformation once again; itβs an affine transformation. If you donβt believe that itβs not a linear transformation, plug in both and ; if were a linear transformation, the latter output would be exactly double the former output.
This description tells us that , and this is a perfectly valid linear transformation, from to .
Finding the matrix of a linear transformationΒΆ
So far, weβve started with a formula for a transformation and found a matrix that produces the same output. But we donβt actually need to know what the transformation does to every possible input. We just need to know what it does to the standard basis vectors!
Suppose is a linear transformation, and all I tell you is
Previously, we called these and . Here, weβll use and to avoid confusion with our notation for error vectors.
Can you find its matrix? Remember that multiplying a matrix by selects its first column, and multiplying it by selects its second column. So these two outputs give us the entire matrix:
Why are two outputs enough? Every input is a linear combination of the standard basis vectors. Since is a linear transformation,
The same idea works for any linear transformation . Apply to the standard basis vectors and put the outputs into columns:
Column tells us where the th standard basis vector goes. There are columns, each with entries, so has shape .
Different domain and codomainΒΆ
Before focusing on square matrices, letβs look at a transformation that changes the number of entries in a vector:
Its matrix is . It has three columns because an input has three entries, and two rows because an output has two entries.
Compare this with projection onto the -plane:
These look similar, but the outputs have different shapes! returns a vector in . returns a vector in whose third entry is zero.
The codomain of the transformation defined by is still , even though its outputs only fill the -plane. The set of outputs a matrix can actually produce is its column space. Here, is a plane in , while .
Lines stay linesΒΆ
Before looking at an application, letβs discuss a useful property of linear transformations: points that lie along a line in the domain still lie along a line in the output, unless the entire line collapses to a single point.
For example, consider
Follow three points on a line that does not pass through the origin as we apply :
All three inputs lie on a line, and all three outputs lie on another line. A different line than before, yes, but still a line nonetheless.

Why does this happen? Choose a point on the line and a nonzero direction vector . Every point on the line can be written as for some scalar . Since is a linear transformation,
So every output is obtained by starting at and adding a multiple of the same direction vector, . If , these outputs form a line. If , the entire line instead collapses to the single point .
In many applications, keeping straight lines straight is desirable. When drawing a 3D scene on a 2D screen, for example, we want a straight building edge to appear straight in the image. Letβs explore this idea.
Example: computer graphicsΒΆ
Computer games involve drawing a three-dimensional world on a two-dimensional screen. One way to do this is with a linear transformation from to .
Start with the overhead view below, then move View toward Game view. Change Building height, and turn on Show basis arrows. What happens to the three standard basis vectors?
/Users/surajrampure/miniforge3/envs/eecs245/lib/python3.10/site-packages/IPython/core/display.py:475: UserWarning: Consider using IPython.display.IFrame instead
warnings.warn("Consider using IPython.display.IFrame instead")
In game view, the matrix is
Where did these numbers come from? We chose how a one-unit step in each world direction should look on the screen:
A step in the -direction moves 40 units right and 20 units down.
A step in the -direction moves 40 units right and 20 units up.
A step in the -direction moves 40 units up.
Those three screen movements are just the columns of :
So, for any world point,
For example, the roof corner appears at . Increasing the buildingβs height changes its vertical position on the screen, but not its horizontal position.
In overhead view, the matrix is instead . Now height disappears entirely, since the third column is zero! Each setting of the View slider gives a different matrix, shown beneath the picture.
The numbers 40 and 20 are drawing choices; other numbers would give a different view or scale. Whatβs important is that choosing where the standard basis vectors go determines the entire transformation.
Linear Transformations from to ΒΆ
While linear transformations exist from to or to , itβs in some ways easiest to think about linear transformations with the same domain and codomain, i.e. transformations of the form . This will allow us to explore how transformations stretch, rotate, and reflect vectors in the same space. Linear transformations with the same domain () and codomain () are represented by matrices, which gives us a useful setting to think about the invertibility of square matrices, beyond just looking at a bunch of numbers.
To start, letβs consider the linear transformation defined by the matrix
What happens to a vector in when we multiply it by ? Letβs visualize the effect of on several vectors in .

scales, or stretches, the input space by a factor of 2 in the -direction and a factor of in the -direction.
ScalingΒΆ
Another way of visualizing is to think about how it transforms the two standard basis vectors of :
Note that is just the first column of , and is just the second column.

In addition to drawing and on the left and their transformed counterparts and on the right, Iβve also shaded in how the unit square, which is the square containing and , gets transformed. Here, it gets stretched from a square to a rectangle.
Remember that any vector is a linear combination of and . For instance,
So, multiplying by is equivalent to multiplying by a linear combination of and .
and the result is a linear combination of and with the same coefficients! If this sounds confusing, just remember that and are just the first and second columns of , respectively.
So, as we move through the following examples, think of the transformed basis vectors and as a new set of βbuilding blocksβ that define the transformed space (which is the column space of ).
is a diagonal matrix, which means it scales vectors. Note that any vector in can be transformed by , not just vectors on or within the unit square; Iβm just using these two basis vectors to visualize the transformation.
Rotations and Orthogonal MatricesΒΆ
What might a non-diagonal matrix do? Letβs consider

Just to continue the previous example, the vector is transformed into

is an orthogonal matrix, which means that its columns are unit vectors and are orthogonal to one another.
Orthogonal matrices preserve lengths and angles between vectors. In , they rotate vectors, reflect them, or combine these operations. For example, is orthogonal, but it reflects vectors across the -axis.
Our matrix is a rotation. In general, the matrix that rotates vectors by radians counterclockwise is
rotates vectors by radians, i.e. .
Rotations are more difficult to visualize in and higher dimensions, but in Homework 5, youβll prove that orthogonal matrices preserve norms, i.e. if is an orthogonal matrix and , then . So, even though an orthogonal matrix might be doing something harder to describe in , we know that it isnβt changing the lengths of the vectors itβs multiplying.
To drive home the point I made earlier, any vector , once multiplied by , ends up transforming into
Composing TransformationsΒΆ
We can even apply multiple transformations one after another. This is called composing transformations. For instance,
is just

Note that rotates the input vector, and then scales it. Read the operations from right to left, since .
is different from

Try changing the rotation angle and horizontal stretch below. Here, stretches horizontally and rotates counterclockwise. Compare (stretch, then rotate) with (rotate, then stretch). The order matters: in general, .
ShearsΒΆ

is a shear matrix. Think of a shear as a transformation that slants the input space along one axis, while keeping the other axis fixed. What helps me interpret shears is looking at them formulaically.
Note that the -coordinate of input vectors in remain unchanged, while the -coordinate is shifted by , which results in a slanted shape.
Similarly, is a shear matrix that keeps the -coordinate fixed, but shifts the -coordinate, resulting in a slanted shape that is tilted downwards.

Non-Invertible TransformationsΒΆ
So far weβve looked at scaling, rotation, and shear matrices. Letβs look at another type of transformation that behaves differently from what weβve seen so far. Consider the matrix below.

keeps the horizontal component of and throws away the vertical component. Note that maps the unit square to a line, not another four-sided shape.
Why have I called this a non-invertible transformation? This is the key idea that weβre building towards: given that , I have no way of knowing what is! It could be that , or , or , etc. These βs all get mapped to the same vector, , by . This is because, unlike the matrices weβve seen so far, is not all of , but rather itβs just a line in , since βs columns are not linearly independent.
below works similarly.

is the line spanned by , so will always be some vector on this line. Put another way, if , then is
but since and are both on the line spanned by , is really just a scalar multiple of .
Arbitrary MatricesΒΆ
Finally, Iβll comment that not all linear transformations have a nice, intuitive interpretation. For instance, consider

turns the unit square into a parallelogram. In fact, so did , , , , , and ; all of these transformations map the unit square to a parallelogram, with some additional properties (e.g. βs parallelogram was a rectangle, βs had equal sides, etc.).
Thereβs no need to memorize the names of these transformations β after all, they only apply in and perhaps where we can visualize.
Speaking of , an arbitrary matrix can be thought of as a transformation that maps the unit cube to a parallelepiped (the generalization of a parallelogram to three dimensions).
What do you notice about the transformation defined by , and how it relates to βs columns? (Drag the plot around to see the main point.)
Since βs columns are linearly dependent, maps the unit cube to a flat parallelogram.
The DeterminantΒΆ
It turns out that a single number tells us how a square matrix scales area or volume. That number is the determinant of , denoted .
More precisely,
For a matrix, is the area of the image of the unit square.
For a matrix, is the volume of the image of the unit cube.
For an matrix, gives the corresponding -dimensional volume.
Notice the absolute value! A determinant can be negative, but an area or volume canβt.
Why do we care? Remember, the goal of this section is to find the inverse of a square matrix , if it exists, and the determinant will give us one way to check if it does.
In the case of the matrices and above, their columns were linearly dependent, and so the transformations and mapped the unit square to a line with no area. Similarly above, mapped the unit cube to a flat parallelogram with no volume. In all other transformations, the matricesβ columns were linearly independent, so the resulting object had a non-zero area (in the case of matrices) or volume (in the case of matrices).
So, how do we find ? Unfortunately, the formula is only convenient for matrices.
For example, in the transformation
the area of the parallelogram formed by transforming the unit square is

The sign tells us about orientation. In , the standard basis vectors , in that order, make a counterclockwise turn. A positive determinant preserves this orientation; a negative determinant reverses it. Swapping two columns reverses the sign of the determinant while leaving its absolute value unchanged.
The determinant of an matrix can be expressed recursively using a weighted sum of determinants of smaller matrices, called minors. For example, if is the matrix
then is
The matrix is a minor of ; itβs formed by deleting the first row and first column of . Note the alternating signs in the formula. This formula generalizes to matrices, but is not practical for anything larger than . (What is the runtime of this algorithm?)
The computation of the determinant is not super important. The big idea is that the determinant of is a single number that tells us whether βs transformation βloses a dimensionβ or not.
Some useful properties of the determinant are that, for any matrices and ,
(notice the exponent!)
If results from swapping two of βs columns (or rows), then
The proofs of these properties are beyond the scope of our course. But, itβs worthwhile to think about what they mean in English in the context of linear transformations.
implies that the rows of (which are the columns of ) create the same βvolumeβ as the columns of .
matches our intuition that linear transformations can be composed. is the result of applying to , then to the result.
multiplies each column of by , and so the volume of the resulting object is scaled by .
If results from swapping two of βs columns (or rows), then reflects that swapping two columns reverses the orientation of the transformation, flipping the signed volume from positive to negative (or vice versa) while preserving its magnitude.
This YouTube playlist contains walkthrough videos of the following activity, which has 5 subparts. The first video, embedded below, contains a brief overview of determinants, summarizing the content above.
Activity 2ΒΆ
Activity 2
Activity 2.1
Find the determinant of the following matrices:
Walkthrough video
Activity 2.2
Suppose and are both matrices. Is in general?
Walkthrough video
Activity 2.3
Suppose we multiply βs 2nd column by 3. What happens to ?
Walkthrough video
Activity 2.4
If βs columns are linearly dependent, then find .
Walkthrough video
Activity 2.5
Find the determinant of (Hint: The answer does not depend on !).
is a orthogonal matrix. If is an orthogonal matrix, then what is ?
Walkthrough video
Now that we have a better understanding of the linear transformations that are implicitly defined by square matrices, we can return to the question of inverses, when they exist, and how to find them.