Precalculus · Chapter 7

Chapter 7
Plane vectors and matrix algebra

Lectures 24–26  |  the bridge to linear algebra

In this chapter
  1. Vectors and the dot product
  2. Matrices as functions that move the plane
  3. Linear systems, determinants, and losing information

Every function in this course so far has eaten one number and produced one number. This chapter changes that. The input becomes a point in the plane and so does the output, and the function becomes a rule for moving every point at once.

That sounds like a modest extension. It is the doorway to linear algebra, and to nearly everything computational: rotating a photograph, straightening a scanned document, ranking a web page, compressing a video, and running a layer of a neural network are all the same operation, performed on a great many more than two numbers.


Lecture 24 — Vectors and the dot product

Cartoon: two ways to describe the same journeyOne stick figure gives directions as three east then four north. An arrow diagram shows both that route and the direct diagonal ending at the same place. Or: five, that way.
“Three east, then four north.” One destination, two descriptions.

A vector is a journey

definition

A vector in the plane is a displacement: a distance together with a direction. Writing it in components as \(\mathbf{v}=\langle v_1,v_2\rangle\) means "move \(v_1\) across and \(v_2\) up".

Its magnitude is its length, \(|\mathbf{v}|=\sqrt{v_1^{2}+v_2^{2}}\) — Pythagoras again — and its direction is the angle it makes with the positive \(x\)-axis.

spot check

What is the magnitude of \(\langle5,12\rangle\)?

decimals, fractions, pi and sqrt( ) are all fine

A vector is not a point. The point \((3,4)\) is a place; the vector \(\langle3,4\rangle\) is an instruction, and the same instruction can be carried out starting from anywhere. Drawn as an arrow, it can be picked up and moved about the plane without becoming a different vector, which is exactly what makes vectors useful for forces, velocities and routes.

the arithmetic

Addition: \(\mathbf{u}+\mathbf{v}=\langle u_1+v_1,\ u_2+v_2\rangle\) — do one journey, then the other.

Scalar multiple: \(c\mathbf{v}=\langle cv_1,\ cv_2\rangle\) — same direction, length scaled by \(|c|\), reversed if \(c<0\).

Unit vector: \(\dfrac{\mathbf{v}}{|\mathbf{v}|}\) has length 1 and the same direction — direction stripped of distance.

spot check

Compute \(2\langle1,3\rangle-\langle4,-1\rangle\). Give both components.

2 numbers, separated by a comma: first component, then second

Adding two vectors On the left, vector u drawn from the origin and vector v drawn starting where u ends, with the sum reaching from the origin to the final point. On the right, the same two vectors both drawn from the origin, forming two sides of a parallelogram whose diagonal is the sum. u v u + v head to tail or as a parallelogram — same answer
Two pictures of one rule. Head to tail says "do this journey, then that one"; the parallelogram says "these two pulls act at once". Addition of vectors is commutative for a visible reason: both routes around the parallelogram arrive at the same corner.
example 7.1 — a plane in a crosswind

An aircraft flies with velocity \(\langle 240,0\rangle\) km/h relative to the air, and the wind blows at \(\langle 0,-60\rangle\) km/h. Find the ground velocity, its speed, and the direction of travel.

Add them: \(\langle240,0\rangle+\langle0,-60\rangle=\langle240,-60\rangle\).

Speed: \(\sqrt{240^{2}+60^{2}}=\sqrt{57600+3600}=247.4\) km/h.

Direction: \(\tan\theta=\dfrac{-60}{240}=-0.25\), so \(\theta=-14.0^{\circ}\) — fourteen degrees south of the heading.

The aircraft is pointing east and travelling east-south-east. Correcting for that is the entire job of navigation, and it is vector subtraction: to make the ground velocity due east, the pilot must aim \(14^{\circ}\) into the wind.

Adding the aircraft velocity and the wind velocityA horizontal arrow of length 240 for the aircraft through the air, a short downward arrow of 60 for the wind attached to its tip, and the resultant arrow from the start to the final tip, 247.4 long and pointing 14 degrees below the horizontal. 14°air speed 240wind 60ground speed 247.4the aircraft points eastbut travels 14° south of itto make good a due east track,aim 14° into the wind
Head to tail, exactly as in the first figure of this lecture. The aircraft is pointing where its nose says, and travelling where the sum says — and the gap between those two statements is the whole business of navigation.

The dot product

There is no sensible way to multiply two journeys and get a third journey. But there is an extremely useful way to multiply two vectors and get a number, and it answers the question: how much do these two point the same way?

definition

The dot product of \(\mathbf{u}\) and \(\mathbf{v}\) is $$\mathbf{u}\cdot\mathbf{v}=u_1v_1+u_2v_2.$$ It can also be computed from the geometry: $$\mathbf{u}\cdot\mathbf{v}=|\mathbf{u}|\,|\mathbf{v}|\cos\theta,$$ where \(\theta\) is the angle between them. The two formulas always agree — the Law of Cosines from Lecture 20 is what proves it.

spot check

Compute \(\langle2,3\rangle\cdot\langle4,-1\rangle\).

decimals, fractions, pi and sqrt( ) are all fine

Read the second formula and the meaning falls out. The dot product is largest when the vectors point the same way (\(\cos0=1\)), zero when they are perpendicular (\(\cos90^{\circ}=0\)), and negative when they point away from each other. It is a similarity score.

spot check

If \(\mathbf{u}\cdot\mathbf{v}\) is negative, the angle between \(\mathbf{u}\) and \(\mathbf{v}\) is .

Two vectors, one number

Drag either arrowhead, or focus the figure and use the arrow keys. The shadow of \(\mathbf{u}\) along \(\mathbf{v}\) is its projection; the dot product is the length of that shadow times the length of \(\mathbf{v}\).

example 7.2 — angle between two vectors

Find the angle between \(\mathbf{u}=\langle3,4\rangle\) and \(\mathbf{v}=\langle-2,6\rangle\), and decide whether \(\langle3,4\rangle\) and \(\langle8,-6\rangle\) are perpendicular.

\(\mathbf{u}\cdot\mathbf{v}=3(-2)+4(6)=18\). Magnitudes: \(|\mathbf{u}|=5\), \(|\mathbf{v}|=\sqrt{40}=6.325\). So $$\cos\theta=\frac{18}{5(6.325)}=0.5692,\qquad \theta=55.3^{\circ}.$$

For the second pair: \(3(8)+4(-6)=24-24=0\). A dot product of zero means \(\cos\theta=0\), so yes, they are perpendicular — checked without drawing anything or finding a single angle.

That is the everyday use of the dot product. Testing perpendicularity by measuring two directions and subtracting is fiddly; testing it by multiplying and adding four numbers is not.

example 7.3 — a shadow, and some work

A crate is dragged 12 m along the ground by a rope pulling with a force of 80 N at \(25^{\circ}\) above the horizontal. How much work is done?

Only the part of the force pointing along the motion does any work — the upward part just tries to lift the crate. That part is the projection of the force onto the displacement, of length \(80\cos25^{\circ}=72.5\) N.

So the work is \(72.5\times12=870\) joules, which is exactly the dot product $$\mathbf{F}\cdot\mathbf{d}=|\mathbf{F}||\mathbf{d}|\cos\theta=80(12)\cos25^{\circ}=870\ \text{J}.$$

If the rope were vertical the work would be zero, however hard you pulled: \(\cos90^{\circ}=0\). Physics defines work as a dot product precisely because "how much of this points along that" is the quantity that matters.

idea — cosine similarity

Take three short documents and count how often each of a few words appears. Each document becomes a vector of counts — and now two documents can be compared by the angle between their vectors.

The angle, not the dot product itself, because a long document has big counts and would win every dot product simply by being long. Dividing by both magnitudes strips the length out and leaves only the direction: $$\text{similarity}=\frac{\mathbf{u}\cdot\mathbf{v}}{|\mathbf{u}||\mathbf{v}|}=\cos\theta.$$

This is called cosine similarity and it is everywhere: matching a search query to a page, recommending a film, finding duplicate submissions, deciding which sentences in a body of text are about the same thing. Modern systems use vectors with hundreds of components rather than a handful, and nothing about the formula changes — you simply add up more products. The geometry of Chapter 5 is doing the work, in a space no one can picture.

spot check

Two documents have count vectors \(\langle2,1\rangle\) and \(\langle20,10\rangle\). What is their cosine similarity?

decimals, fractions, pi and sqrt( ) are all fine

How a language model reads a sentence

Chapter 1 turned three sentences into arrows by counting words, and stopped there because we had no way to measure the angle between them. We do now.

A modern language model does the same thing, with one important improvement. Counting words treats car and automobile as completely unrelated, since they are different words. Instead, a model assigns every word its own list of a few hundred numbers — called an embedding — and those numbers are not counted, they are learned, by adjusting them until words that tend to appear in similar company end up pointing in similar directions.

Here is the idea with three numbers instead of three hundred.

Three word vectors, drawn with their true angles Three arrows from a common origin. The arrows for king and queen are only about four degrees apart. The arrow for pizza is about seventy three degrees away from both of them. kingqueenpizza 4.3° 73.1° king 〈0.90, 0.70, 0.10〉 queen 〈0.85, 0.75, 0.15〉 pizza 〈0.10, 0.20, 0.95〉 king · queen = 1.305, cos = 0.997 king · pizza = 0.325, cos = 0.291 a bigger dot product means a smaller angle, and closer meaning
The three directions drawn with their angles to scale. Words that mean similar things end up pointing in similar directions, so the angle between them is small and the dot product is large. Real systems use several hundred coordinates instead of three, and the arithmetic is identical — there are simply more products to add up.
spot check
Three unit-length word vectors have \(\mathbf{A}\cdot\mathbf{B}=0.9\) and \(\mathbf{A}\cdot\mathbf{C}=0.2\). Which word is closer in meaning to \(\mathbf{A}\)?
example 7.4 — which two words are related?

Using the embeddings \(\text{king}=\langle0.90,0.70,0.10\rangle\), \(\text{queen}=\langle0.85,0.75,0.15\rangle\) and \(\text{pizza}=\langle0.10,0.20,0.95\rangle\), compute the cosine similarity of each pair and rank them.

Magnitudes first: \(|\text{king}|=\sqrt{0.81+0.49+0.01}=1.145\), \(|\text{queen}|=\sqrt{0.7225+0.5625+0.0225}=1.143\), \(|\text{pizza}|=\sqrt{0.01+0.04+0.9025}=0.976\).

king and queen: the dot product is \(0.90(0.85)+0.70(0.75)+0.10(0.15)=0.765+0.525+0.015=1.305\), so $$\cos\theta=\frac{1.305}{1.145\times1.143}=0.997,\qquad \theta=4.3^{\circ}.$$

king and pizza: \(0.09+0.14+0.095=0.325\), giving \(\cos\theta=\dfrac{0.325}{1.145\times0.976}=0.291\) and \(\theta=73.1^{\circ}\).

queen and pizza: \(0.378\), giving \(\cos\theta=0.338\) and \(\theta=70.2^{\circ}\).

Ranking: king and queen are by far the closest, and pizza is roughly equally far from both. Note the direction of the comparison, because it is easy to get backwards: a larger dot product means a smaller angle, which means more similar. Similarity goes up with the dot product, not down.

idea — what this makes possible

Almost everything you have seen a language model do rests on this one measurement.

The famous demonstration is that arithmetic on these vectors sometimes behaves like arithmetic on meanings: take the vector for king, subtract man, add woman, and the closest vector to the result is often queen. That is vector addition from earlier in this lecture, performed on direction-encoded meaning. It does not always work, and why it works at all is not fully understood — which is a fair description of a good deal of the field.

exercises 7.1
  1. For \(\mathbf{u}=\langle5,-2\rangle\) and \(\mathbf{v}=\langle-1,4\rangle\), find \(\mathbf{u}+\mathbf{v}\), \(3\mathbf{u}-\mathbf{v}\), \(|\mathbf{u}|\) and the unit vector in the direction of \(\mathbf{u}\).
  2. Write the vector of length 6 making an angle of \(150^{\circ}\) with the positive \(x\)-axis in components, exactly.
  3. Find the angle between \(\langle1,3\rangle\) and \(\langle4,-2\rangle\).
  4. For what value of \(k\) are \(\langle2,k\rangle\) and \(\langle6,-4\rangle\) perpendicular?
  5. A boat heads due north at 14 km/h through a current running east at 5 km/h. Find the boat's speed and direction over the ground, and the heading it should take to travel due north.
  6. Two forces, \(\langle40,30\rangle\) N and \(\langle-10,25\rangle\) N, act on a bolt. Find the resultant force and its magnitude.
  7. Three documents have word-count vectors \(\langle3,1,0\rangle\), \(\langle6,2,0\rangle\) and \(\langle0,1,4\rangle\). Compute the cosine similarity of each pair and say which two are "about the same thing", and why the first two score as they do despite having different lengths.
  8. Toy embeddings: \(\text{dog}=\langle0.8,0.6,0.1\rangle\), \(\text{puppy}=\langle0.75,0.65,0.05\rangle\), \(\text{algebra}=\langle0.1,0.1,0.9\rangle\). Compute all three cosine similarities and say which pair a search engine would treat as a match.
  9. Show that \(\mathbf{u}\cdot\mathbf{u}=|\mathbf{u}|^{2}\), and explain what that says about the angle between a vector and itself.
  1. \(\langle4,2\rangle\); \(\langle16,-10\rangle\); \(|\mathbf{u}|=\sqrt{29}=5.385\); unit vector \(\left\langle\tfrac{5}{\sqrt{29}},\tfrac{-2}{\sqrt{29}}\right\rangle\).
  2. \(\langle6\cos150^{\circ},6\sin150^{\circ}\rangle=\langle-3\sqrt3,\ 3\rangle\).
  3. Dot product \(4-6=-2\); magnitudes \(\sqrt{10}\) and \(\sqrt{20}\). \(\cos\theta=\dfrac{-2}{\sqrt{200}}=-0.1414\), so \(\theta=98.1^{\circ}\) — slightly obtuse, which the negative dot product told us before any arithmetic.
  4. \(12-4k=0\), so \(k=3\).
  5. Ground velocity \(\langle5,14\rangle\): speed \(\sqrt{221}=14.87\) km/h, at \(\tan^{-1}(5/14)=19.7^{\circ}\) east of north. To go due north, the boat must aim \(\sin^{-1}(5/14)=20.9^{\circ}\) west of north.
  6. Resultant \(\langle30,55\rangle\) N, magnitude \(\sqrt{900+3025}=62.6\) N.
  7. First and second: dot \(20\), magnitudes \(\sqrt{10}\) and \(\sqrt{40}\), similarity \(\dfrac{20}{20}=1\) — a perfect match, because the second is exactly twice the first and points in precisely the same direction. First and third: dot \(1\), similarity \(\dfrac{1}{\sqrt{10}\sqrt{17}}=0.077\), essentially unrelated. Second and third similarly \(0.077\). Length was divided out, which is the whole point: a long document and a short one on the same subject should score as similar.
  8. \(\text{dog}\cdot\text{puppy}=0.6+0.39+0.005=0.995\), with magnitudes \(1.005\) and \(0.994\), so \(\cos\theta=0.996\) and \(\theta=4.9^{\circ}\). \(\text{dog}\cdot\text{algebra}=0.08+0.06+0.09=0.23\), giving \(\cos\theta=0.251\) and \(\theta=75.5^{\circ}\); \(\text{puppy}\cdot\text{algebra}=0.185\), giving \(\cos\theta=0.204\). Dog and puppy are the match, by a wide margin.
  9. \(\mathbf{u}\cdot\mathbf{u}=u_1^{2}+u_2^{2}=|\mathbf{u}|^{2}\). From the geometric formula this is \(|\mathbf{u}|^{2}\cos\theta\), so \(\cos\theta=1\) and the angle between a vector and itself is zero — which is reassuring, and is the check that the two formulas are consistent.

Lecture 25 — Matrices as functions that move the plane

Cartoon: a scanner straightening a tilted QR codeA stick figure holds a tilted QR code up to a phone, which shows the same code straightened. How did it know?
one matrix, applied to every corner at once

Lecture 1 defined a function as a rule taking an input to an output. Nothing in that definition said the input had to be a single number. Let the input be a vector and the output be a vector, and you have a function that moves the whole plane at once.

The linear ones — those that keep the origin fixed and keep straight lines straight and evenly spaced — turn out to be describable by just four numbers.

definition

A \(2\times2\) matrix acts on a vector by $$\begin{pmatrix}a & b\\ c & d\end{pmatrix}\begin{pmatrix}x\\ y\end{pmatrix} =\begin{pmatrix}ax+by\\ cx+dy\end{pmatrix}.$$ Each output coordinate is a dot product: the first row with the input, then the second row with the input.

spot check

Compute \(\begin{pmatrix}2&1\\0&3\end{pmatrix}\begin{pmatrix}1\\2\end{pmatrix}\).

2 numbers, separated by a comma: top entry, then bottom

idea — the columns tell you everything

Feed in \(\langle1,0\rangle\), the unit step east, and the answer is \(\langle a,c\rangle\) — the first column. Feed in \(\langle0,1\rangle\), the unit step north, and you get \(\langle b,d\rangle\) — the second column.

So a matrix is nothing more than a record of where the two basic steps land. And that is enough to determine the whole transformation, because every other vector is built from those two: \(\langle3,4\rangle\) is three east-steps and four north-steps, so its image is three times the first column plus four times the second.

When you read a matrix, read its columns as destinations. Almost every matrix in this chapter can be written down by asking "where do I want east to go, and where do I want north to go?" and putting those two answers side by side.

The columns of a matrix are where east and north landThe dashed unit square with its two edges, east and north, and the shaded parallelogram it becomes. The image of east is the arrow to the point 2, 0.5 and the image of north is the arrow to 1, 1.5. east lands herenorth lands here12312the matrix is just⟨2, 0.5⟩ and ⟨1, 1.5⟩written side by sideas its two columns
Read every matrix this way. Its first column is where the unit step east ends up, its second is where north ends up, and since every other vector is built from those two steps, those two answers determine the whole transformation.
spot check

A matrix sends east \(\langle1,0\rangle\) to \(\langle2,1\rangle\) and north \(\langle0,1\rangle\) to \(\langle-1,3\rangle\). Where does it send \(\langle1,1\rangle\)?

2 numbers, separated by a comma: x, then y

Type a matrix, watch the plane move

The four sliders are the four entries. The shaded square is the unit square, and the two coloured arrows are where east and north have landed — the columns of your matrix. Try the presets to see the standard transformations.

The standard transformations

four matrices worth knowing
effectmatrixwhere east and north go
scale by \(s\)\(\begin{pmatrix}s & 0\\ 0 & s\end{pmatrix}\)both stretched, directions unchanged
reflect in the \(x\)-axis\(\begin{pmatrix}1 & 0\\ 0 & -1\end{pmatrix}\)east stays, north flips to south
shear\(\begin{pmatrix}1 & k\\ 0 & 1\end{pmatrix}\)east stays, north leans over by \(k\)
rotate by \(\theta\)\(\begin{pmatrix}\cos\theta & -\sin\theta\\ \sin\theta & \cos\theta\end{pmatrix}\)east goes to \((\cos\theta,\sin\theta)\)
spot check
What does \(\begin{pmatrix}0&-1\\1&0\end{pmatrix}\) do to the plane?

Look hard at the rotation matrix. Its first column is exactly the terminal point of Chapter 4 — the place east ends up after turning through \(\theta\) is, by definition, \((\cos\theta,\sin\theta)\). The second column is that point turned a further quarter turn. The whole of the unit circle has walked into linear algebra and become a matrix.

example 7.5 — rotating a point by hand

Rotate the point \((4,1)\) by \(90^{\circ}\) anticlockwise about the origin, then by \(30^{\circ}\).

At \(90^{\circ}\): \(\cos90^{\circ}=0\) and \(\sin90^{\circ}=1\), so the matrix is \(\begin{pmatrix}0&-1\\1&0\end{pmatrix}\) and $$\begin{pmatrix}0&-1\\1&0\end{pmatrix}\begin{pmatrix}4\\1\end{pmatrix}=\begin{pmatrix}-1\\4\end{pmatrix}.$$ Sanity check: the point was to the right and slightly up; it is now up and slightly left. ✓

At \(30^{\circ}\): \(\begin{pmatrix}0.866&-0.5\\0.5&0.866\end{pmatrix}\begin{pmatrix}4\\1\end{pmatrix}=\begin{pmatrix}3.464-0.5\\2+0.866\end{pmatrix}=\begin{pmatrix}2.964\\2.866\end{pmatrix}\).

The length should not change under a rotation: \(\sqrt{4^{2}+1^{2}}=4.123\) and \(\sqrt{2.964^{2}+2.866^{2}}=4.123\). ✓ That check catches almost every sign error people make with rotation matrices.

example 7.6 — straightening a QR code

A QR code is photographed rotated \(30^{\circ}\) anticlockwise. Its bottom edge, which should run due east, appears in the photo as \(\langle0.866,0.500\rangle\), and its left edge, which should run due north, appears as \(\langle-0.500,0.866\rangle\). Find the matrix that distorted it, and the one the scanner must apply to undo it. Check on one corner.

The distortion. The columns of a matrix are the destinations of east and north, and we have been handed exactly those: $$M=\begin{pmatrix}0.866 & -0.500\\ 0.500 & 0.866\end{pmatrix}.$$ Those entries are \(\cos30^{\circ}\) and \(\sin30^{\circ}\), so \(M\) is the rotation matrix for \(30^{\circ}\) — which is what we expected, and a good sign that nothing has gone wrong.

The repair. To undo a rotation of \(30^{\circ}\), rotate by \(-30^{\circ}\): $$M^{-1}=\begin{pmatrix}\cos(-30^{\circ}) & -\sin(-30^{\circ})\\ \sin(-30^{\circ}) & \cos(-30^{\circ})\end{pmatrix} =\begin{pmatrix}0.866 & 0.500\\ -0.500 & 0.866\end{pmatrix}.$$ The inverse formula from this lecture agrees: \(\det M=0.866^{2}+0.500^{2}=1\), so \(M^{-1}=\begin{pmatrix}d&-b\\-c&a\end{pmatrix}\), which is the same matrix.

The check. A corner that should sit at \((1,0)\) appears in the photo at \((0.866,0.500)\). Applying \(M^{-1}\): $$\begin{pmatrix}0.866 & 0.500\\ -0.500 & 0.866\end{pmatrix}\begin{pmatrix}0.866\\0.500\end{pmatrix} =\begin{pmatrix}0.750+0.250\\-0.433+0.433\end{pmatrix}=\begin{pmatrix}1\\0\end{pmatrix}.$$ ✓

The scanner applies that one matrix to every pixel and the whole image comes upright at once. This is why the three chunky squares sit in the corners of every QR code: they are not data and not decoration, they are there so that a scanner can measure where east and north ended up, and therefore recover \(M\).

A tilted QR code and the same code straightenedOn the left, a square grid of code modules rotated thirty degrees anticlockwise, as a camera would see it. On the right, the same grid upright after the inverse matrix has been applied to every point. An arrow labelled with the inverse matrix runs between them. apply M⁻¹as the camera sees itas the decoder needs it
The same code, before and after. One matrix — the inverse of the one the tilt produced — is applied to every point of the image, and the whole grid comes upright at once. The three heavy squares at the corners are how the scanner works out what that matrix is.

A real photograph is usually worse than a pure rotation — hold the phone at an angle and the square becomes a trapezoid, which needs a slightly larger matrix than the ones in this chapter. The principle does not change: find where a few known points went, deduce the transformation, and invert it.

example 7.7 — turning a colour photograph grey

A pixel's colour is a vector \(\langle R,G,B\rangle\) of three brightnesses from 0 to 255. The standard conversion to greyscale is the dot product with \(\mathbf{w}=\langle0.299,0.587,0.114\rangle\). Find the grey value of the pixel \(\langle200,120,60\rangle\), and explain the choice of weights.

$$0.299(200)+0.587(120)+0.114(60)=59.8+70.4+6.8=137.$$

The weights add to 1, so white stays white and black stays black. They are unequal because the eye is far more sensitive to green than to blue: green carries nearly 60% of the perceived brightness and blue barely a tenth. Averaging the three channels equally would make blues look too light and greens too dark.

Notice what has happened to the information. Three numbers became one, so the map is nowhere near one-to-one — \(\langle200,120,60\rangle\) and thousands of other colours all give 137. There is no way back, which is exactly why colourising an old photograph is guesswork rather than arithmetic.

Multiplying matrices is doing one thing after another

Apply \(B\) and then \(A\). The result is another linear transformation, and its matrix is written \(AB\) — the matrix product. Its columns, as always, are the destinations of east and north: apply \(B\) to each column of the identity, then apply \(A\) to the result.

matrix multiplication $$\begin{pmatrix}a&b\\c&d\end{pmatrix}\begin{pmatrix}p&q\\r&s\end{pmatrix} =\begin{pmatrix}ap+br & aq+bs\\ cp+dr & cq+ds\end{pmatrix}$$

Every entry is a dot product of a row of the first with a column of the second. The order matters: \(AB\) means "do \(B\) first", and in general \(AB\neq BA\).

spot check
The product \(AB\) applied to a vector means…
example 7.8 — order matters, visibly

Let \(R\) rotate by \(90^{\circ}\) and \(F\) reflect in the \(x\)-axis. Compute \(RF\) and \(FR\), and describe each in words.

\(R=\begin{pmatrix}0&-1\\1&0\end{pmatrix}\), \(F=\begin{pmatrix}1&0\\0&-1\end{pmatrix}\).

$$RF=\begin{pmatrix}0&1\\1&0\end{pmatrix},\qquad FR=\begin{pmatrix}0&-1\\-1&0\end{pmatrix}.$$

They are not equal. \(RF\) swaps the coordinates, which is a reflection in the line \(y=x\); \(FR\) swaps and negates both, a reflection in \(y=-x\). Reflect-then-rotate and rotate-then-reflect give mirror images in perpendicular lines.

You met this in Lecture 4 with \(f\circ g\neq g\circ f\), and in Lecture 25 it has the same cause, because matrix multiplication is composition of functions. Socks and shoes, in two dimensions.

identity and inverse

The identity \(I=\begin{pmatrix}1&0\\0&1\end{pmatrix}\) leaves every vector alone: east goes to east, north to north.

The inverse \(A^{-1}\) undoes \(A\), meaning \(AA^{-1}=I\). For a \(2\times2\) matrix, $$A^{-1}=\frac{1}{ad-bc}\begin{pmatrix}d&-b\\-c&a\end{pmatrix},$$ provided \(ad-bc\neq0\). That condition is the subject of the next lecture, and it is not a technicality.

spot check

Find the inverse of \(\begin{pmatrix}4&1\\3&1\end{pmatrix}\). Enter its four entries row by row: top-left, top-right, bottom-left, bottom-right.

4 numbers, separated by commas, row by row

exercises 7.2
  1. Compute \(\begin{pmatrix}2&-1\\3&0\end{pmatrix}\begin{pmatrix}4\\5\end{pmatrix}\) and \(\begin{pmatrix}1&2\\0&1\end{pmatrix}\begin{pmatrix}-3\\2\end{pmatrix}\).
  2. Write down the matrix that (a) doubles everything; (b) reflects in the \(y\)-axis; (c) rotates by \(180^{\circ}\); (d) leaves east alone and sends north to \(\langle3,1\rangle\).
  3. Where does the unit square go under \(\begin{pmatrix}2&1\\0&1\end{pmatrix}\)? Give the images of all four corners and sketch the result.
  4. Compute \(AB\) and \(BA\) for \(A=\begin{pmatrix}1&2\\0&1\end{pmatrix}\) and \(B=\begin{pmatrix}1&0\\3&1\end{pmatrix}\), and confirm they differ.
  5. Show that rotating by \(\alpha\) and then by \(\beta\) gives the rotation matrix for \(\alpha+\beta\). Which identities from Chapter 6 appear, and where?
  6. Find the inverse of \(\begin{pmatrix}3&1\\5&2\end{pmatrix}\) and verify that the product with the original is the identity.
  7. A rotation matrix has \(ad-bc=\cos^{2}\theta+\sin^{2}\theta\). Evaluate it, and say in one sentence what that tells you about areas under a rotation.
  8. Explain, using columns, why the matrix of a transformation that sends both east and north to the same vector cannot be undone.
  1. \(\begin{pmatrix}3\\12\end{pmatrix}\) and \(\begin{pmatrix}1\\2\end{pmatrix}\).
  2. (a) \(\begin{pmatrix}2&0\\0&2\end{pmatrix}\); (b) \(\begin{pmatrix}-1&0\\0&1\end{pmatrix}\); (c) \(\begin{pmatrix}-1&0\\0&-1\end{pmatrix}\); (d) \(\begin{pmatrix}1&3\\0&1\end{pmatrix}\).
  3. \((0,0)\to(0,0)\), \((1,0)\to(2,0)\), \((0,1)\to(1,1)\), \((1,1)\to(3,1)\). The square becomes a leaning parallelogram twice as wide.
  4. \(AB=\begin{pmatrix}7&2\\3&1\end{pmatrix}\), \(BA=\begin{pmatrix}1&2\\3&7\end{pmatrix}\). Different.
  5. Multiplying the two rotation matrices gives entries \(\cos\alpha\cos\beta-\sin\alpha\sin\beta\) and \(\sin\alpha\cos\beta+\cos\alpha\sin\beta\) — which are exactly \(\cos(\alpha+\beta)\) and \(\sin(\alpha+\beta)\). The addition formulas of Lecture 23 are what makes rotation matrices compose correctly; equivalently, matrix multiplication proves them.
  6. \(ad-bc=6-5=1\), so the inverse is \(\begin{pmatrix}2&-1\\-5&3\end{pmatrix}\). Multiplying gives \(\begin{pmatrix}1&0\\0&1\end{pmatrix}\). ✓
  7. It equals 1. A rotation does not change area — which is obvious once said, and is exactly what the next lecture shows that number measures.
  8. If both columns are the same vector, then east and north land on top of each other, so the whole plane is squashed onto a single line. Two different starting vectors can then arrive at the same place, so the transformation is not one-to-one, and by Lecture 5 nothing that loses information can be undone.

Lecture 26 — Linear systems, determinants, and losing information

Cartoon: two parallel lines that never meetA stick figure waits beside two parallel lines drawn on a board. I’ll wait.
no solution is a solution, and the determinant saw it coming

Two linear equations in two unknowns is the oldest problem in algebra and the most common one in practice. It also turns out to be the same question this course opened with: is anything lost?

Two lines

Each equation \(ax+by=e\) describes a line. Solving the pair means finding the points on both lines, so geometry settles the matter completely before any algebra is done.

The three possible outcomes for two linear equations Three panels. In the first, two lines cross at a single point: one solution. In the second, two parallel lines that never meet: no solution. In the third, two lines lying exactly on top of each other: infinitely many solutions. exactly one solution no solution infinitely many solutions
There are only three possibilities, and no amount of algebra can produce a fourth. Two straight lines in a plane either cross once, never cross, or coincide.
spot check
Two lines have the same slope and different intercepts. How many solutions does the system have?
example 7.9 — all three outcomes

Solve each system, and say which picture it is.

(a) \(\begin{cases}2x+3y=12\\ x-y=1\end{cases}\)    (b) \(\begin{cases}2x+4y=6\\ x+2y=5\end{cases}\)    (c) \(\begin{cases}2x+4y=6\\ x+2y=3\end{cases}\)

(a) From the second equation \(x=y+1\). Substituting: \(2(y+1)+3y=12\), so \(5y=10\) and \(y=2\), \(x=3\). One crossing point, \((3,2)\).

(b) The second equation doubled is \(2x+4y=10\), which contradicts the first. No solution — the lines are parallel, with the same slope and different intercepts.

(c) The second equation doubled is the first. The two equations say the same thing, so every point on the line \(x+2y=3\) is a solution: infinitely many.

Cases (b) and (c) have identical left-hand sides. Whether a system has no solutions or infinitely many is decided by the constants on the right; whether it has exactly one is decided entirely by the coefficients on the left. That is the fact the determinant captures.

The same system, as a matrix

Write the system as \(A\mathbf{x}=\mathbf{b}\): $$\begin{pmatrix}a&b\\c&d\end{pmatrix}\begin{pmatrix}x\\y\end{pmatrix}=\begin{pmatrix}e\\f\end{pmatrix}.$$ Now the question changes character. It is no longer "where do two lines cross" but "which input did the transformation \(A\) send to \(\mathbf{b}\)" — and that is a question about running a function backwards. If \(A\) has an inverse, the answer is immediate: \(\mathbf{x}=A^{-1}\mathbf{b}\), and it is unique.

The determinant

definition

The determinant of \(A=\begin{pmatrix}a&b\\c&d\end{pmatrix}\) is $$\det A=ad-bc.$$ Geometrically, \(|\det A|\) is the area of the image of the unit square — the parallelogram spanned by the two columns. Its sign records whether the transformation flipped the plane over.

spot check

The unit square is sent through \(\begin{pmatrix}3&1\\2&4\end{pmatrix}\). By what factor are areas multiplied?

decimals, fractions, pi and sqrt( ) are all fine

The area the columns span

Drag either arrowhead. The shaded parallelogram is where the unit square has been sent, and its area is the determinant. Bring the two arrows into line and watch what happens.

example 7.10 — reading the determinant

Find the determinant of each and say what the transformation does to area: \(\begin{pmatrix}3&0\\0&2\end{pmatrix}\), \(\begin{pmatrix}1&5\\0&1\end{pmatrix}\), \(\begin{pmatrix}1&0\\0&-1\end{pmatrix}\), \(\begin{pmatrix}2&4\\1&2\end{pmatrix}\).

\(6\): areas are multiplied by 6, which is what stretching by 3 one way and 2 the other should do.

\(1\): a shear leans the square into a parallelogram of the same base and the same height, so the area is unchanged — a fact that is much easier to see than to compute.

\(-1\): area preserved, but the sign says the plane has been turned over. Reflections have negative determinant.

\(2(2)-4(1)=0\): the area is destroyed. The two columns \(\langle2,1\rangle\) and \(\langle4,2\rangle\) are parallel — the second is twice the first — so they span no area at all, and the whole plane has been flattened onto a single line.

idea — the thread from Lecture 5, tied off

Follow the chain. A determinant of zero means the image of the unit square has no area. No area means both columns lie along one line, so the entire plane has been squashed onto that line. Squashing means many different inputs arrive at the same output — so the transformation is not one-to-one. And Lecture 5 told us, back in Chapter 1, that a function which is not one-to-one has no inverse, because information has been destroyed and no rule can invent it back.

So these are all the same statement:

One number, computed by multiplying four entries and subtracting, answers all six questions at once. That is what a good definition does, and it is a fair summary of why linear algebra exists.

spot check
A system \(A\mathbf{x}=\mathbf{b}\) has \(\det A=0\). What can you conclude?
example 7.11 — solving with the inverse

Solve \(\begin{cases}3x+y=11\\ 5x+2y=19\end{cases}\) using the inverse matrix, then check the determinant against the geometry.

\(A=\begin{pmatrix}3&1\\5&2\end{pmatrix}\) has \(\det A=6-5=1\), so an inverse exists and the solution is unique: $$A^{-1}=\frac{1}{1}\begin{pmatrix}2&-1\\-5&3\end{pmatrix},\qquad \mathbf{x}=A^{-1}\begin{pmatrix}11\\19\end{pmatrix}=\begin{pmatrix}22-19\\-55+57\end{pmatrix}=\begin{pmatrix}3\\2\end{pmatrix}.$$

Check: \(3(3)+2=11\) ✓ and \(5(3)+2(2)=19\) ✓

For a \(2\times2\) system this is more work than elimination, and nobody would do it by hand. It matters because it scales: the same formula, with a much larger matrix, is how a computer solves ten thousand equations at once — and the determinant is still what tells it whether an answer exists before it starts.

example 7.12 — a code that needs an inverse

The Hill cipher encrypts text two letters at a time. Letters become numbers \(A=0,B=1,\ldots,Z=25\), each pair is multiplied by a key matrix, and the answers are reduced modulo 26 (that is, wrapped around the alphabet). Using $$K=\begin{pmatrix}3&2\\5&3\end{pmatrix},$$ encrypt the pair HI, then decrypt it again.

Encrypting. \(H=7\) and \(I=8\), so $$K\begin{pmatrix}7\\8\end{pmatrix}=\begin{pmatrix}21+16\\35+24\end{pmatrix}=\begin{pmatrix}37\\59\end{pmatrix} \equiv\begin{pmatrix}11\\7\end{pmatrix} \pmod{26},$$ which is LH.

Decrypting needs \(K^{-1}\). Here \(\det K=9-10=-1\), and working modulo 26 the reciprocal of \(-1\) is \(25\), since \(-1\times25=-25\equiv1\). So $$K^{-1}=25\begin{pmatrix}3&-2\\-5&3\end{pmatrix}=\begin{pmatrix}75&-50\\-125&75\end{pmatrix}\equiv\begin{pmatrix}23&2\\5&23\end{pmatrix}\pmod{26}.$$

Applying it to \(\langle11,7\rangle\): \(23(11)+2(7)=267\equiv7\) and \(5(11)+23(7)=216\equiv8\). That is \(\langle7,8\rangle\) — HI again. ✓

Now the point. A key matrix is only usable if it can be inverted, so its determinant must not be zero — and modulo 26 it must also share no factor with 26, ruling out even determinants and multiples of 13. Choose a key with \(\det K=0\) and you have built the shredder from Lecture 5: different messages encrypt to the same ciphertext, and not even the intended recipient can read it.

Lester Hill published this in 1929. It is far too weak for real use, but it was the first cipher to be built on matrix algebra, and it makes the determinant condition into something you can feel: a zero determinant is a lost message.

example 7.13 — animating a rotation

A game sprite has a corner at \((30,0)\) relative to its centre, and must spin at \(6^{\circ}\) per frame. Where is the corner after 1 frame and after 15 frames, and why does the studio store an angle rather than repeatedly multiplying?

One frame: \(\begin{pmatrix}\cos6^{\circ}&-\sin6^{\circ}\\ \sin6^{\circ}&\cos6^{\circ}\end{pmatrix}\begin{pmatrix}30\\0\end{pmatrix}=\begin{pmatrix}29.84\\3.14\end{pmatrix}\).

Fifteen frames is a rotation of \(90^{\circ}\), which sends \((30,0)\) to \((0,30)\) exactly. That follows from the exercise above: composing rotations adds the angles, so applying the \(6^{\circ}\) matrix fifteen times equals applying the \(90^{\circ}\) matrix once.

Why not just multiply repeatedly? Because each multiplication rounds off a little, and after a few thousand frames those roundings accumulate until the sprite is visibly stretched — the determinant drifts away from 1. Storing the total angle and rebuilding the matrix each frame keeps the determinant exactly 1 and the shape exactly rigid. This is a real bug that has shipped in real games.

history

Determinants appeared twice, independently, within a decade. Seki Takakazu wrote about them in Japan in 1683; Leibniz described the same idea in a letter in 1693. Both were trying to decide when a system of equations could be solved, and both found the same combination of coefficients.

Matrices themselves came much later — Cayley, in the 1850s — which is a strange order of events: the test for whether a matrix can be inverted was invented before anyone had written down a matrix.

for your toolbox

The collection is complete. You can now sketch, transform, and state the domain and range of: lines, power functions, roots, reciprocals and polynomials; exponentials and logarithms, with their asymptotes; and sine, cosine and tangent, with theirs. And you can now do to the plane what Lecture 4 did to a graph — scale it, shear it, reflect it, rotate it — with a single object of four numbers.

exercises 7.3
  1. Solve \(\begin{cases}4x-y=7\\ 2x+3y=7\end{cases}\) by elimination, and state the determinant of the coefficient matrix.
  2. For what value of \(k\) does \(\begin{cases}3x+ky=5\\ 6x+8y=9\end{cases}\) fail to have a unique solution? Describe the two lines at that value.
  3. Find the determinants: \(\begin{pmatrix}4&2\\3&1\end{pmatrix}\), \(\begin{pmatrix}0&-3\\2&0\end{pmatrix}\), \(\begin{pmatrix}6&9\\4&6\end{pmatrix}\). Which transformation cannot be undone, and why?
  4. A transformation sends the unit square to a parallelogram of area 7 and does not flip the plane. What is \(\det A\)? What is \(\det(A^{-1})\)?
  5. Show that \(\det(AB)=\det A\cdot\det B\) for \(A=\begin{pmatrix}2&0\\0&3\end{pmatrix}\) and \(B=\begin{pmatrix}1&4\\0&1\end{pmatrix}\), and explain the result in terms of areas.
  6. Find the area of the triangle with vertices \((0,0)\), \((5,1)\) and \((2,6)\), using a determinant. (Hint: the triangle is half the parallelogram.)
  7. Explain why a rotation matrix always has determinant 1 and a reflection always \(-1\).
  8. A system of two equations is found to have a determinant of zero. Someone concludes that it has no solution. Why is that wrong, and what are the two possibilities?
  1. Multiply the first by 3: \(12x-3y=21\); adding the second gives \(14x=28\), \(x=2\), then \(y=1\). Determinant \(4(3)-(-1)(2)=14\), non-zero, so the unique solution was guaranteed.
  2. \(\det=24-6k=0\) at \(k=4\). The left sides are then proportional but the right sides are not (\(9\neq10\)), so the lines are parallel and distinct: no solution.
  3. \(4-6=-2\); \(0-(-6)=6\); \(36-36=0\). The third cannot be undone: its columns \(\langle6,4\rangle\) and \(\langle9,6\rangle\) are parallel, so it flattens the plane onto a line and destroys information.
  4. \(\det A=7\). Since \(A^{-1}\) must undo the stretching, \(\det(A^{-1})=\tfrac17\).
  5. \(AB=\begin{pmatrix}2&8\\0&3\end{pmatrix}\), determinant 6, and \(\det A\cdot\det B=6\times1=6\). In words: apply one transformation that multiplies areas by 6 and another that leaves them alone, and areas end up multiplied by 6. Determinants multiply because area factors multiply.
  6. The parallelogram spanned by \(\langle5,1\rangle\) and \(\langle2,6\rangle\) has area \(|5(6)-1(2)|=28\), so the triangle has area 14.
  7. A rotation preserves every length and every angle, so it cannot change area, and it does not turn the plane over — determinant \(+1\). Computing it confirms this: \(\cos^{2}\theta+\sin^{2}\theta=1\). A reflection also preserves area but reverses orientation, so its determinant is \(-1\).
  8. Zero determinant rules out exactly one solution, not all solutions. The two lines are either parallel and distinct, giving none, or identical, giving infinitely many. Which one it is depends on the right-hand sides, as example 7.9 showed.

Where this leaves you

A vector is a journey, and the dot product measures how much two journeys agree — which is how a machine decides that two documents are about the same thing. A matrix is a function that moves the whole plane, and its columns say where east and north end up. Multiplying matrices is doing one thing after another. And the determinant is the area of what the unit square became, so a determinant of zero means the plane was flattened, information was lost, and nothing can be undone.

That last sentence is the one this course has been building toward since Lecture 5, when a shift cipher and a shredder illustrated the difference between a function you can run backwards and one you cannot.

idea — what comes next

Three doors are now open, and you have been standing in front of all of them.

Calculus takes every approximation in this course and completes it by letting a step size shrink: average rates become instantaneous ones, sums of rectangles become exact areas, polynomial approximations become series, and secant lines become tangents. Nothing will be unfamiliar; the objects are already in your hands.

Linear algebra takes this chapter and removes the restriction to two dimensions. The same columns, the same composition, the same determinant — with a thousand dimensions instead of two, which is where data actually lives.

Machine learning is the two of them at once: a model built from matrices, a loss function measuring its failure, and a hunt for the minimum by rolling downhill. You did that in Lecture 8 with a difference quotient and a step size chosen by hand.

And running underneath all of it is the thing this course has tried hardest to show: mathematics is not a finished object handed down complete. Every idea here was made by somebody, in a particular place, for a reason — in Baghdad, Kerala, Alexandria, Edo, Göttingen — and much of it was contested before it was accepted. It is still being made. You are meeting it in motion.

End of the course.