Flow Lab

Syllabus / Module 1: Linear Algebra, Reloaded

Lesson 1.2

Matrices as Transformations

A matrix is a function that moves space: columns are where the basis vectors land. Matrix multiplication is composition.

Written byClaude Opus

🌊
Why this matters for simulation: The velocity gradient tensor is a matrix that describes how a tiny blob of fluid is stretched, sheared and rotated.

By the end of this lesson you can…

  • Explain what makes a transformation linear, both with the grid picture and with the rule T(av + bw) = aT(v) + bT(w).
  • Build the matrix of a linear transformation by recording where î and ĵ land as its columns.
  • Compute a matrix–vector product two ways: as a combination of columns and as row-by-vector dot products.
  • Write down rotation, shear, scaling, reflection and projection matrices and recognize them on sight.
  • Interpret matrix multiplication as composition, read products right to left, and show that AB ≠ BA in general.
  • Describe how the velocity gradient matrix stretches, shears and rotates a small blob of fluid.
On this page
·
  1. Functions that move vectors
  2. Things that are not linear
  3. Columns are where î and ĵ land
  4. Worked example: from a picture to a matrix
  5. The matrix–vector product, two ways
  6. Worked example: both pictures
  7. Playing with the transformer
  8. A gallery of transformations
  9. Worked example: projection onto the diagonal
  10. Matrix multiplication is composition
  11. Worked example: rotate, then shear
  12. Order matters: AB ≠ BA
  13. Three dimensions, briefly
  14. Matrices in code
  15. The velocity gradient: a matrix inside every fluid parcel
  16. Key Takeaways

In 1.1 Vectors, Linear Combinations & Span a vector was a single arrow, and the state of a simulation was one big vector. Every simulation also does something to its state on every step: a rigid body’s orientation gets rotated, a camera projects 3D points onto the screen, and a fluid solver’s diffusion step averages neighbors. Most of these operations are linear transformations, and every linear transformation is a matrix. Your first course probably treated a matrix as a rectangle of numbers to row-reduce. This lesson is about what that rectangle does: a matrix is a function that moves every vector in space at once, and its columns tell you exactly how. Once you can see a matrix as a motion, systems of differential equations like x⃗˙=Ax⃗\dot{\vec x} = A\vec x turn into pictures of flows instead of symbol-pushing.

▶ Video A matrix moves the whole plane: its columns are where î and ĵ landcaptions only
Transcript
  1. In your first linear algebra course, a matrix was a grid of numbers you row-reduced. Today we see what a matrix really is: a machine that moves space.
  2. Think of a transformation as a function. You feed in a vector, and it hands you back another vector. The word transformation invites you to picture that as motion.
  3. And a matrix does this to every vector at the same time. So instead of one arrow, watch the whole grid move.
  4. Linear transformations are special. Grid lines stay straight, they stay parallel and evenly spaced, and the origin does not move.
  5. Something like this, which bends the grid lines into curves, is not linear, and it is not something a matrix can do.
  6. Straight, evenly spaced grid lines are the picture of a rule called linearity. Transforming a v plus b w gives the same thing as a times the transformed v plus b times the transformed w.
  7. That rule has a stunning consequence. Watch just two vectors: i hat and j hat, the unit steps along the axes.
  8. Apply a transformation. i hat lands on two, one. j hat lands on negative one, one.
  9. Write those two landing spots side by side as columns, and you have the matrix. That is all a matrix is: the first column is where i hat lands, the second is where j hat lands.
  10. Now where does any other vector go? Take v equals one, two. Before the move, v is one i hat plus two j hats.
  11. Because the grid lines stay parallel and evenly spaced, after the move v is still one transformed i hat plus two transformed j hats. It lands on zero, three.
  12. That is the column picture of a matrix times a vector: use the entries of the vector as weights on the columns of the matrix.
  13. The rule you memorized, row times column, gives the same answer. Each output entry is the dot product of a row with the vector. Two pictures, one computation.
  14. Now we can read any matrix by its columns. Rotate by ninety degrees: i hat goes up to zero, one, and j hat swings over to negative one, zero.
  15. A shear keeps i hat fixed and pushes j hat sideways to one, one. Squares lean over into parallelograms, like a deck of cards pushed from the top.
  16. A diagonal matrix just scales. Here x is doubled and y is halved.
  17. This one flips space over the x axis: j hat lands on negative one times j hat. Notice the orientation has reversed.
  18. And a projection squashes the whole plane onto a line. j hat lands on zero. Information is destroyed, and no matrix can undo it. We will dig into that next lesson.
  19. What happens if you apply two transformations in a row? First rotate by ninety degrees, then shear.
  20. The result is one single linear transformation. Read off where i hat and j hat ended up, and you get its matrix, which is the product S times R.
  21. Notice the order. The rotation acts first, but it is written on the right, next to the vector. Read matrix products from right to left, like function composition.
  22. Now swap the order: shear first, then rotate. The dashed arrows show where the basis vectors went last time.
  23. Different landing spots, different matrix. R times S is not S times R. Matrix multiplication does not commute, because doing things in a different order gives a different motion.
  24. Everything carries over to three dimensions. A three by three matrix has three columns: where i hat, j hat and k hat land.
  25. This one is a shear in space: it slides the top of a box sideways while the floor stays put.
  26. Here is why a fluid simulator cares. Zoom in on a tiny blob of fluid in a shear flow, where the top moves faster than the bottom.
  27. Nearby velocities differ by a matrix times the separation. That matrix, the velocity gradient, stretches, shears and rotates the blob. The circle becomes a tilted ellipse.
  28. Later, in fluid kinematics, we will split this matrix into a symmetric part that strains the blob and an antisymmetric part that spins it. Same matrix, read geometrically.
  29. So: a matrix is a function that moves every vector at once. Its columns are where the basis vectors land, and multiplying matrices means doing one motion after another.
  30. Next time we run the machine backwards: given an output b, which inputs land on it? That is what solving A x equals b really means.

Functions that move vectors

A transformation TT from Rn\mathbb{R}^n to Rm\mathbb{R}^m is just a function: it takes an input vector x⃗\vec x and returns an output vector T(x⃗)T(\vec x). In calculus you graphed functions from numbers to numbers. A function from R2\mathbb{R}^2 to R2\mathbb{R}^2 has no useful graph (it would need four dimensions), so instead we picture it as motion: every point of the plane slides to its new location, and we watch the whole grid deform.

Most such functions bend the grid into curves. The ones linear algebra studies are much tamer.

In words: transforming a linear combination gives the same linear combination of the transformed vectors. Linear maps don’t care whether you combine first and then transform, or transform first and then combine.

The grid picture and the algebraic rule describe the same thing:

  • The origin stays fixed. Take c=0c = 0 in the scaling rule: T(0⃗)=T(0⋅v⃗)=0⋅T(v⃗)=0⃗T(\vec 0) = T(0\cdot\vec v) = 0\cdot T(\vec v) = \vec 0.
  • Lines stay lines. The line through p⃗\vec p in direction d⃗\vec d is the set of points p⃗+td⃗\vec p + t\vec d. Linearity sends it to T(p⃗)+t T(d⃗)T(\vec p) + t\,T(\vec d), which is again a line (or a single point, if T(d⃗)=0⃗T(\vec d) = \vec 0).
  • Parallel, evenly spaced grid lines stay parallel and evenly spaced. The grid lines x=kx = k are the points k ı^+t ȷ^k\,\hat\imath + t\,\hat\jmath. Their images are k T(ı^)+t T(ȷ^)k\,T(\hat\imath) + t\,T(\hat\jmath): all parallel to T(ȷ^)T(\hat\jmath), and shifted by equal steps of T(ı^)T(\hat\imath).

So a quick visual test: if the origin moves, or any grid line bends, the transformation is not linear.

Things that are not linear

  • Translation T(x⃗)=x⃗+b⃗T(\vec x) = \vec x + \vec b with b⃗≠0⃗\vec b \neq \vec 0 moves the origin, so it isn’t linear. (Graphics programmers get around this with 3×33\times3 “homogeneous” matrices, a trick worth knowing for your flight simulator.)
  • Anything with products or powers of components, like T(x,y)=(xy, x)T(x, y) = (xy,\, x). Doubling the input quadruples the first output instead of doubling it.
  • Absolute values, clamps, sin⁡\sin, and so on. These bend grid lines.
✅ Concept checkselect all that apply

Which of these maps from $\mathbb{R}^2$ to $\mathbb{R}^2$ are linear? Select all that apply.

Columns are where î and ĵ land

Here is the big payoff of linearity. Every vector in the plane is a combination of the basis vectors, x⃗=(x,y)=x ı^+y ȷ^\vec x = (x, y) = x\,\hat\imath + y\,\hat\jmath. Apply a linear TT and use the rule:

T(x⃗)=T(x ı^+y ȷ^)=x T(ı^)+y T(ȷ^).T(\vec x) = T(x\,\hat\imath + y\,\hat\jmath) = x\,T(\hat\imath) + y\,T(\hat\jmath).

Once you know where the two vectors ı^\hat\imath and ȷ^\hat\jmath land, you know where every vector lands. A linear transformation of the plane is completely described by four numbers: the two coordinates of T(ı^)T(\hat\imath) and the two of T(ȷ^)T(\hat\jmath). We record them as the columns of a matrix:

T(ı^)=[ac],T(ȷ^)=[bd]⟹A=[abcd].T(\hat\imath) = \begin{bmatrix} a \\ c \end{bmatrix},\quad T(\hat\jmath) = \begin{bmatrix} b \\ d \end{bmatrix} \qquad\Longrightarrow\qquad A = \begin{bmatrix} a & b \\ c & d \end{bmatrix}.

This works in any dimension. A linear map from Rn\mathbb{R}^n to Rm\mathbb{R}^m is an m×nm \times n matrix: nn columns (one per input basis vector), each with mm entries (a vector in the output space).

Worked example: from a picture to a matrix

A linear transformation sends ı^\hat\imath to (2,1)(2, 1) and ȷ^\hat\jmath to (−1,1)(-1, 1). Find its matrix and the image of x⃗=(1,2)\vec x = (1, 2).

The columns are the landing spots, so

A=[2−111].A = \begin{bmatrix} 2 & -1 \\ 1 & 1 \end{bmatrix}.

Since x⃗=1 ı^+2 ȷ^\vec x = 1\,\hat\imath + 2\,\hat\jmath, linearity gives

Ax⃗=1[21]+2[−11]=[03].A\vec x = 1\begin{bmatrix} 2 \\ 1 \end{bmatrix} + 2\begin{bmatrix} -1 \\ 1 \end{bmatrix} = \begin{bmatrix} 0 \\ 3 \end{bmatrix}.

This is exactly the example in the video. Before the move, x⃗\vec x is “one step along ı^\hat\imath, two steps along ȷ^\hat\jmath”. After the move it is still one step along the new ı^\hat\imath and two steps along the new ȷ^\hat\jmath. The coordinates of a point relative to the moving grid never change. Only the grid moves.

The matrix–vector product, two ways

The worked example is the definition of the matrix–vector product. For a general 2×22\times2 matrix:

[abcd][xy]=x[ac]+y[bd]=[ax+bycx+dy].\begin{bmatrix} a & b \\ c & d \end{bmatrix}\begin{bmatrix} x \\ y \end{bmatrix} = x\begin{bmatrix} a \\ c \end{bmatrix} + y\begin{bmatrix} b \\ d \end{bmatrix} = \begin{bmatrix} ax + by \\ cx + dy \end{bmatrix}.

Read that equation from both ends and you get two pictures of the same computation.

The column picture. Ax⃗A\vec x is a linear combination of the columns of AA, with the entries of x⃗\vec x as the weights:

Ax⃗=x1 a⃗1+x2 a⃗2+⋯+xn a⃗n,A\vec x = x_1\,\vec a_1 + x_2\,\vec a_2 + \cdots + x_n\,\vec a_n,

where a⃗j\vec a_j is the jj-th column. This is the geometric picture: walk x1x_1 steps along where ı^\hat\imath went, then x2x_2 steps along where ȷ^\hat\jmath went, and so on. It also connects straight back to span: the set of all outputs Ax⃗A\vec x is the span of the columns. That set is called the column space, and it gets a full treatment in 1.3 Linear Systems, Kernel & Column Space.

The row picture. The ii-th entry of Ax⃗A\vec x is the dot product of row ii with x⃗\vec x:

(Ax⃗)i=ai1x1+ai2x2+⋯+ainxn=(row i)⋅x⃗.(A\vec x)_i = a_{i1}x_1 + a_{i2}x_2 + \cdots + a_{in}x_n = (\text{row } i)\cdot\vec x .

This is the “row times column” rule you memorized. It’s the efficient way to compute one entry at a time, and it’s how you’d think about a linear system: each row is one equation.

Both pictures always give the same numbers, because they add up the same products aijxja_{ij}x_j, just grouped differently. The column picture groups by jj (by column), and the row picture groups by ii (by row).

Worked example: both pictures

Compute Ax⃗A\vec x for A=[31−24]A = \begin{bmatrix} 3 & 1 \\ -2 & 4 \end{bmatrix} and x⃗=(2,−1)\vec x = (2, -1).

Columns:

2[3−2]+(−1)[14]=[6−1−4−4]=[5−8].2\begin{bmatrix} 3 \\ -2 \end{bmatrix} + (-1)\begin{bmatrix} 1 \\ 4 \end{bmatrix} = \begin{bmatrix} 6 - 1 \\ -4 - 4 \end{bmatrix} = \begin{bmatrix} 5 \\ -8 \end{bmatrix}.

Rows: (3,1)⋅(2,−1)=6−1=5(3, 1)\cdot(2, -1) = 6 - 1 = 5 and (−2,4)⋅(2,−1)=−4−4=−8(-2, 4)\cdot(2, -1) = -4 - 4 = -8. Same answer, as it must be.

✅ Concept check

Let $A = \begin{bmatrix} 1 & 4 \\ -3 & 2 \end{bmatrix}$ and $\vec x = (2, 3)$. What is the second entry of $A\vec x$?

Playing with the transformer

The sim below shows the whole plane moving under a 2×22\times2 matrix. The faint grid is the original plane, and the violet grid is where it goes. The arrows are Aı^A\hat\imath (first column) and Aȷ^A\hat\jmath (second column).

◆ Interactive Matrix transformer: drag where î and ĵ land, or pick a preset

Loading simulation…

Try these:

  1. Drag the tip of ı^\hat\imath and watch the sliders aa and cc change. The first column of the matrix is the tip of ı^\hat\imath, literally. Do the same with ȷ^\hat\jmath.
  2. Drag the faint input vector xx. The green arrow Ax⃗A\vec x is built tip to tail from x1x_1 copies of the first column and x2x_2 copies of the second. That construction is the column picture.
  3. Preset → Rotation, then slide θ\theta. The square stays a square and the circle stays a circle. Rotations preserve lengths and angles.
  4. Preset → Projection. The plane collapses onto a line, the square’s area goes to 00, and many inputs land on the same output. Keep this picture in mind for the next lesson.
  5. Mode → Compose, with A = Shear and B = Rotation by 90°. The two panels apply the same two motions in opposite orders and end up in different places.

With “columns = landing spots”, building a matrix for a geometric operation becomes a matter of asking where ı^\hat\imath and ȷ^\hat\jmath should go.

Rotation by θ\theta (counterclockwise). Rotating ı^=(1,0)\hat\imath = (1,0) by θ\theta lands it at (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta) on the unit circle. ȷ^\hat\jmath starts 90∘90^\circ ahead, so it lands at (cos⁡(θ+90∘),sin⁡(θ+90∘))=(−sin⁡θ,cos⁡θ)(\cos(\theta + 90^\circ), \sin(\theta + 90^\circ)) = (-\sin\theta, \cos\theta). Therefore

R(θ)=[cos⁡θ−sin⁡θsin⁡θcos⁡θ],R(90∘)=[0−110].R(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}, \qquad R(90^\circ) = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}.

Shear. Keep ı^\hat\imath fixed and push ȷ^\hat\jmath sideways to (k,1)(k, 1):

Sk=[1k01],Sk[xy]=[x+kyy].S_k = \begin{bmatrix} 1 & k \\ 0 & 1 \end{bmatrix}, \qquad S_k\begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} x + ky \\ y \end{bmatrix}.

Each horizontal layer slides by an amount proportional to its height, like a deck of cards pushed from the top. Squares become parallelograms with the same base and height, so area is unchanged.

Scaling. A diagonal matrix stretches each axis independently:

D=[s100s2].D = \begin{bmatrix} s_1 & 0 \\ 0 & s_2 \end{bmatrix}.

The unit circle becomes an ellipse with semi-axes ∣s1∣|s_1| and ∣s2∣|s_2|. Diagonal matrices are the easiest matrices there are, and much of this module (eigenvectors, diagonalization) is about finding a viewpoint from which a matrix looks diagonal.

Reflection. Flipping across the xx-axis sends ȷ^\hat\jmath to −ȷ^-\hat\jmath, and flipping across the line y=xy = x swaps ı^\hat\imath and ȷ^\hat\jmath:

[100−1],[0110].\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}, \qquad \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}.

Across the line at angle φ\varphi to the xx-axis, the reflection is [cos⁡2φsin⁡2φsin⁡2φ−cos⁡2φ]\begin{bmatrix} \cos 2\varphi & \sin 2\varphi \\ \sin 2\varphi & -\cos 2\varphi \end{bmatrix}. A reflection reverses orientation: after it, turning from Aı^A\hat\imath to Aȷ^A\hat\jmath goes clockwise instead of counterclockwise.

Projection. Squash every vector onto a line through the origin, along the perpendicular direction. Onto the xx-axis this sends ȷ^\hat\jmath to 0⃗\vec 0:

Px=[1000].P_x = \begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix}.

Onto the line through a unit vector u^=(u1,u2)\hat u = (u_1, u_2), the shadow of x⃗\vec x is (x⃗⋅u^) u^(\vec x\cdot\hat u)\,\hat u (from the dot product in lesson 1.1). Its columns are the shadows of ı^\hat\imath and ȷ^\hat\jmath, namely u1u^u_1\hat u and u2u^u_2\hat u:

P=[u12u1u2u1u2u22].P = \begin{bmatrix} u_1^2 & u_1u_2 \\ u_1u_2 & u_2^2 \end{bmatrix}.

Worked example: projection onto the diagonal

Find the matrix that projects onto the line y=xy = x, and check that projecting twice is the same as projecting once.

The unit vector along the line is u^=(1/2, 1/2)\hat u = (1/\sqrt2,\, 1/\sqrt2), so u12=u1u2=u22=12u_1^2 = u_1u_2 = u_2^2 = \tfrac12:

P=[12121212].P = \begin{bmatrix} \tfrac12 & \tfrac12 \\ \tfrac12 & \tfrac12 \end{bmatrix}.

Check: Pı^=(12,12)P\hat\imath = (\tfrac12, \tfrac12), which is on the line and directly “below” (1,0)(1,0) along the perpendicular. Now P(12,12)=(14+14, 14+14)=(12,12)P(\tfrac12,\tfrac12) = (\tfrac14 + \tfrac14,\ \tfrac14 + \tfrac14) = (\tfrac12, \tfrac12). A point already on the line doesn’t move, so projecting twice changes nothing: P2=PP^2 = P.

Notice also that P(1,−1)=(0,0)P(1, -1) = (0, 0). The whole perpendicular line gets crushed to the origin. A projection destroys information: from the output alone you can’t recover the input, so no matrix can undo PP. That set of crushed inputs (the kernel) and the question of when a matrix can be undone (the inverse) are the heart of 1.3 Linear Systems, Kernel & Column Space.

✅ Concept check

A matrix sends $\hat\imath$ to $(0, 2)$ and $\hat\jmath$ to $(-1, 0)$. What does it do geometrically?

✏️ Pen & paperBuild the matrix●○○

Write down the 2×22\times2 matrix for each transformation by finding where ı^\hat\imath and ȷ^\hat\jmath land.

  1. Reflection across the line y=xy = x.
  2. Rotation by 45∘45^\circ counterclockwise.
  3. Projection onto the line through (3,4)(3, 4).
  4. Horizontal shear that moves the point (0,2)(0, 2) to (3,2)(3, 2).
Show worked solution
  1. Reflection across y=xy = x swaps the axes: ı^↦(0,1)\hat\imath \mapsto (0,1) and ȷ^↦(1,0)\hat\jmath \mapsto (1,0), so the matrix is [0110]\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}.
  2. cos⁡45∘=sin⁡45∘=22\cos 45^\circ = \sin 45^\circ = \tfrac{\sqrt2}{2}, so R(45∘)=22[1−111]R(45^\circ) = \dfrac{\sqrt2}{2}\begin{bmatrix} 1 & -1 \\ 1 & 1 \end{bmatrix}. Check: ı^\hat\imath lands at (22,22)(\tfrac{\sqrt2}{2}, \tfrac{\sqrt2}{2}), on the diagonal.
  3. The unit vector is u^=(3,4)/5=(0.6,0.8)\hat u = (3,4)/5 = (0.6, 0.8). Then u12=0.36u_1^2 = 0.36, u1u2=0.48u_1u_2 = 0.48 and u22=0.64u_2^2 = 0.64, so P=[0.360.480.480.64]P = \begin{bmatrix} 0.36 & 0.48 \\ 0.48 & 0.64 \end{bmatrix}. Check: P(3,4)=(1.08+1.92, 1.44+2.56)=(3,4)P(3,4) = (1.08 + 1.92,\ 1.44 + 2.56) = (3, 4), so points on the line stay put. And P(−4,3)=(−1.44+1.44, −1.92+1.92)=(0,0)P(-4, 3) = (-1.44 + 1.44,\ -1.92 + 1.92) = (0, 0), so the perpendicular direction is crushed.
  4. A horizontal shear is [1k01]\begin{bmatrix} 1 & k \\ 0 & 1 \end{bmatrix}, which sends (0,2)(0,2) to (2k,2)(2k, 2). Setting 2k=32k = 3 gives k=1.5k = 1.5, so the matrix is [11.501]\begin{bmatrix} 1 & 1.5 \\ 0 & 1 \end{bmatrix}.
✏️ Pen & paperRecovering a matrix from two arbitrary vectors●●○

A linear transformation satisfies T(1,1)=(3,1)T(1, 1) = (3, 1) and T(1,−1)=(1,3)T(1, -1) = (1, 3). Find its matrix. (Hint: write ı^\hat\imath and ȷ^\hat\jmath as combinations of (1,1)(1,1) and (1,−1)(1,-1) first.)

Show worked solution

Since ı^=12(1,1)+12(1,−1)\hat\imath = \tfrac12(1,1) + \tfrac12(1,-1) and ȷ^=12(1,1)−12(1,−1)\hat\jmath = \tfrac12(1,1) - \tfrac12(1,-1), linearity gives

T(ı^)=12(3,1)+12(1,3)=(2,2),T(ȷ^)=12(3,1)−12(1,3)=(1,−1).T(\hat\imath) = \tfrac12(3,1) + \tfrac12(1,3) = (2, 2), \qquad T(\hat\jmath) = \tfrac12(3,1) - \tfrac12(1,3) = (1, -1).

These are the columns:

A=[212−1].A = \begin{bmatrix} 2 & 1 \\ 2 & -1 \end{bmatrix}.

Check: A(1,1)=(2+1, 2−1)=(3,1)A(1,1) = (2 + 1,\ 2 - 1) = (3, 1) ✓ and A(1,−1)=(2−1, 2+1)=(1,3)A(1,-1) = (2 - 1,\ 2 + 1) = (1, 3) ✓.

The lesson: the values of a linear map on any basis pin it down completely, not just on ı^,ȷ^\hat\imath, \hat\jmath. Changing which basis you describe a map in is the subject of 1.6 Change of Basis & Diagonalization.

Matrix multiplication is composition

What happens if you apply one transformation and then another? Suppose BB acts first and AA second. A vector x⃗\vec x goes to Bx⃗B\vec x, then to A(Bx⃗)A(B\vec x). The combined map is still linear (a combination survives each step), so it has its own matrix. We define the product ABAB to be that matrix:

(AB) x⃗=A (B x⃗)for every x⃗.(AB)\,\vec x = A\,(B\,\vec x) \quad\text{for every } \vec x.

To find its columns, ask where ı^\hat\imath and ȷ^\hat\jmath end up. BB sends ı^\hat\imath to its first column b⃗1\vec b_1, and then AA sends that to Ab⃗1A\vec b_1. Likewise for every column:

AB=A[∣∣∣b⃗1b⃗2⋯b⃗n∣∣∣]=[∣∣∣Ab⃗1Ab⃗2⋯Ab⃗n∣∣∣].AB = A\begin{bmatrix} | & | & & | \\ \vec b_1 & \vec b_2 & \cdots & \vec b_n \\ | & | & & | \end{bmatrix} = \begin{bmatrix} | & | & & | \\ A\vec b_1 & A\vec b_2 & \cdots & A\vec b_n \\ | & | & & | \end{bmatrix}.

Column jj of ABAB is AA times column jj of BB. If you compute each Ab⃗jA\vec b_j with the row picture, entry (i,j)(i, j) of ABAB comes out as (row ii of AA) · (column jj of BB). That’s the familiar multiplication rule, and now you know why it’s the rule: it’s the only rule that makes (AB)x⃗=A(Bx⃗)(AB)\vec x = A(B\vec x) true.

The sizes have to match: if BB is p×np\times n (from Rn\mathbb{R}^n to Rp\mathbb{R}^p) and AA is m×pm \times p (from Rp\mathbb{R}^p to Rm\mathbb{R}^m), then ABAB is m×nm\times n. The output of BB must fit into the input of AA. Composition also explains for free why matrix multiplication is associative, (AB)C=A(BC)(AB)C = A(BC): both sides mean “do CC, then BB, then AA”.

Worked example: rotate, then shear

Let R=R(90∘)=[0−110]R = R(90^\circ) = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} and S=[1101]S = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}. Find the matrix for “rotate, then shear”.

Rotate first means RR is on the right, so we want SRSR. Track the basis vectors:

  • ı^→R(0,1)→S(0+1, 1)=(1,1)\hat\imath \xrightarrow{R} (0, 1) \xrightarrow{S} (0 + 1,\ 1) = (1, 1)
  • ȷ^→R(−1,0)→S(−1+0, 0)=(−1,0)\hat\jmath \xrightarrow{R} (-1, 0) \xrightarrow{S} (-1 + 0,\ 0) = (-1, 0)
SR=[1−110].SR = \begin{bmatrix} 1 & -1 \\ 1 & 0 \end{bmatrix}.

Check with the row-times-column rule: entry (1,1)(1,1) is (1,1)⋅(0,1)=1(1, 1)\cdot(0, 1) = 1, entry (1,2)(1,2) is (1,1)⋅(−1,0)=−1(1,1)\cdot(-1, 0) = -1, entry (2,1)(2,1) is (0,1)⋅(0,1)=1(0,1)\cdot(0,1) = 1, and entry (2,2)(2,2) is (0,1)⋅(−1,0)=0(0,1)\cdot(-1,0) = 0. ✓

Order matters: AB ≠ BA

Now swap the order: shear first, then rotate. That’s RSRS:

  • ı^→S(1,0)→R(0,1)\hat\imath \xrightarrow{S} (1, 0) \xrightarrow{R} (0, 1)
  • ȷ^→S(1,1)→R(−1,1)\hat\jmath \xrightarrow{S} (1, 1) \xrightarrow{R} (-1, 1)
RS=[0−111]≠[1−110]=SR.RS = \begin{bmatrix} 0 & -1 \\ 1 & 1 \end{bmatrix} \neq \begin{bmatrix} 1 & -1 \\ 1 & 0 \end{bmatrix} = SR.

Matrix multiplication is not commutative. Geometrically there’s nothing strange about this. Shearing pushes things horizontally, and whether something is horizontal depends on whether you rotated it yet. Put on your socks and then your shoes, or your shoes and then your socks: same two operations, different outcome. Try it in the sim’s compose mode.

Some pairs do commute: any matrix with the identity II (which does nothing), any two scalings D1D2=D2D1D_1D_2 = D_2D_1, and any two rotations of the plane, R(α)R(β)=R(β)R(α)=R(α+β)R(\alpha)R(\beta) = R(\beta)R(\alpha) = R(\alpha+\beta), since turning by α\alpha then β\beta is a turn by α+β\alpha + \beta either way. But commuting is the exception, not the rule. In 3D, even rotations stop commuting, which is a headache you’ll meet in 12.3 Rigid-Body Dynamics & Rotations.

✅ Concept check

In a game engine you want to first rotate a sprite by $R$ and then scale it by $D$. Which matrix do you apply to each vertex $\vec x$?

✏️ Pen & paperRotations add up●●○
  1. Multiply R(α)R(β)R(\alpha)R(\beta) out entry by entry.
  2. Using the geometric fact that rotating by β\beta and then by α\alpha is a rotation by α+β\alpha + \beta, deduce the angle-addition formulas for cos⁡(α+β)\cos(\alpha + \beta) and sin⁡(α+β)\sin(\alpha + \beta).
  3. What is R(θ)R(−θ)R(\theta)R(-\theta)? Interpret it.
Show worked solution
  1. Row times column:
R(α)R(β)=[cos⁡α−sin⁡αsin⁡αcos⁡α][cos⁡β−sin⁡βsin⁡βcos⁡β]=[cos⁡αcos⁡β−sin⁡αsin⁡β−cos⁡αsin⁡β−sin⁡αcos⁡βsin⁡αcos⁡β+cos⁡αsin⁡β−sin⁡αsin⁡β+cos⁡αcos⁡β].R(\alpha)R(\beta) = \begin{bmatrix} \cos\alpha & -\sin\alpha \\ \sin\alpha & \cos\alpha \end{bmatrix} \begin{bmatrix} \cos\beta & -\sin\beta \\ \sin\beta & \cos\beta \end{bmatrix} = \begin{bmatrix} \cos\alpha\cos\beta - \sin\alpha\sin\beta & -\cos\alpha\sin\beta - \sin\alpha\cos\beta \\ \sin\alpha\cos\beta + \cos\alpha\sin\beta & -\sin\alpha\sin\beta + \cos\alpha\cos\beta \end{bmatrix}.
  1. Geometrically this product must equal R(α+β)=[cos⁡(α+β)−sin⁡(α+β)sin⁡(α+β)cos⁡(α+β)]R(\alpha + \beta) = \begin{bmatrix} \cos(\alpha+\beta) & -\sin(\alpha+\beta) \\ \sin(\alpha+\beta) & \cos(\alpha+\beta) \end{bmatrix}. Comparing first columns gives
cos⁡(α+β)=cos⁡αcos⁡β−sin⁡αsin⁡β,sin⁡(α+β)=sin⁡αcos⁡β+cos⁡αsin⁡β.\cos(\alpha+\beta) = \cos\alpha\cos\beta - \sin\alpha\sin\beta, \qquad \sin(\alpha+\beta) = \sin\alpha\cos\beta + \cos\alpha\sin\beta.

The second column gives the same two identities again. You never need to memorize these formulas: they’re just “rotations compose”.

  1. R(θ)R(−θ)=R(0)=IR(\theta)R(-\theta) = R(0) = I. Rotating by −θ-\theta undoes rotating by θ\theta, so R(−θ)R(-\theta) is the inverse of R(θ)R(\theta). Since cos⁡(−θ)=cos⁡θ\cos(-\theta) = \cos\theta and sin⁡(−θ)=−sin⁡θ\sin(-\theta) = -\sin\theta, R(−θ)R(-\theta) is R(θ)R(\theta) with its off-diagonal signs flipped, which is its transpose: R(θ)−1=R(θ)TR(\theta)^{-1} = R(\theta)^{T}.
✏️ Pen & paperTwo reflections make a rotation●●●

Let F(φ)=[cos⁡2φsin⁡2φsin⁡2φ−cos⁡2φ]F(\varphi) = \begin{bmatrix} \cos 2\varphi & \sin 2\varphi \\ \sin 2\varphi & -\cos 2\varphi \end{bmatrix} be the reflection across the line at angle φ\varphi.

  1. Verify that F(0)F(0) and F(45∘)F(45^\circ) are the reflections across the xx-axis and across y=xy = x.
  2. Compute F(45∘)F(0)F(45^\circ)F(0) and identify it.
  3. Show that in general F(ψ)F(φ)=R(2(ψ−φ))F(\psi)F(\varphi) = R\big(2(\psi - \varphi)\big). Why does it make sense that two orientation-reversing maps compose to an orientation-preserving one?
Show worked solution
  1. F(0)=[100−1]F(0) = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} (flip across the xx-axis). F(45∘)F(45^\circ) has 2φ=90∘2\varphi = 90^\circ, so F(45∘)=[0110]F(45^\circ) = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} (swap, i.e. flip across y=xy = x).
  2. F(45∘)F(0)=[0110][100−1]=[0−110]=R(90∘)F(45^\circ)F(0) = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} = R(90^\circ). Tracking ı^\hat\imath: the first flip leaves it alone and the swap sends it to ȷ^\hat\jmath, a quarter turn.
  3. Write cψ=cos⁡2ψc_\psi = \cos2\psi, sψ=sin⁡2ψs_\psi = \sin2\psi, and similarly for φ\varphi. Then
F(ψ)F(φ)=[cψcφ+sψsφcψsφ−sψcφsψcφ−cψsφsψsφ+cψcφ].F(\psi)F(\varphi) = \begin{bmatrix} c_\psi c_\varphi + s_\psi s_\varphi & c_\psi s_\varphi - s_\psi c_\varphi \\ s_\psi c_\varphi - c_\psi s_\varphi & s_\psi s_\varphi + c_\psi c_\varphi \end{bmatrix}.

By the angle-difference formulas, cψcφ+sψsφ=cos⁡(2ψ−2φ)c_\psi c_\varphi + s_\psi s_\varphi = \cos(2\psi - 2\varphi) and sψcφ−cψsφ=sin⁡(2ψ−2φ)s_\psi c_\varphi - c_\psi s_\varphi = \sin(2\psi - 2\varphi), and the top-right entry is the negative of the bottom-left one. So the product is R(2ψ−2φ)R(2\psi - 2\varphi). Each reflection reverses orientation (clockwise ↔ counterclockwise). Reversing twice restores it, so the result must be a rotation. With ψ=45∘\psi = 45^\circ and φ=0\varphi = 0 we recover part 2.

Three dimensions, briefly

Everything carries over to R3\mathbb{R}^3 unchanged. A 3×33\times3 matrix has three columns: where ı^\hat\imath, ȷ^\hat\jmath and k^\hat k land.

Rz(θ)=[cos⁡θ−sin⁡θ0sin⁡θcos⁡θ0001],S=[102010001].R_z(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix}, \qquad S = \begin{bmatrix} 1 & 0 & 2 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}.

RzR_z rotates around the zz-axis: k^\hat k lands on itself (third column (0,0,1)(0,0,1)), and the xyxy-plane turns exactly like the 2D rotation. SS is a shear in space. It leaves the floor (ı^\hat\imath, ȷ^\hat\jmath) alone and sends k^\hat k to (2,0,1)(2, 0, 1), so a box’s top slides 2 units in xx for every unit of height. Matrix–vector products and composition work exactly as before: Ax⃗=x1a⃗1+x2a⃗2+x3a⃗3A\vec x = x_1\vec a_1 + x_2\vec a_2 + x_3\vec a_3, and column jj of ABAB is Ab⃗jA\vec b_j.

In a flight simulator, the aircraft’s orientation is a 3×33\times3 rotation matrix whose columns are the aircraft’s nose, right-wing and belly directions written in world coordinates, which is the “columns are where the basis lands” idea in practice. You’ll build one in 12.4 6-DOF Aircraft Equations of Motion.

Matrices in code

In NumPy, A @ x is the matrix–vector product and A @ B the matrix product. This cell transforms the grid and an airplane outline so you can experiment with any matrix:

🐍 PythonTransform the plane
⌘/Ctrl + Enter

Notice the trick in M @ plane.T: stacking many points as the columns of one matrix transforms all of them in a single product. That’s because column jj of MXMX is MM times column jj of XX, which is the composition rule again. It’s how GPUs transform millions of vertices per frame.

Now implement the ideas yourself, without @.

🧪 Coding exerciseExercise: matrix–vector products from columns, and composing rotations
⌘/Ctrl + Enter

The velocity gradient: a matrix inside every fluid parcel

Here is where matrices meet fluids. Pick a point x⃗0\vec x_0 in a flow with velocity field u⃗(x⃗)\vec u(\vec x), and look at a nearby point x⃗0+δx⃗\vec x_0 + \delta\vec x. For a tiny separation, the velocity difference is approximately linear in δx⃗\delta\vec x:

u⃗(x⃗0+δx⃗)≈u⃗(x⃗0)+J δx⃗,J=[∂u∂x∂u∂y∂v∂x∂v∂y].\vec u(\vec x_0 + \delta\vec x) \approx \vec u(\vec x_0) + J\,\delta\vec x, \qquad J = \begin{bmatrix} \dfrac{\partial u}{\partial x} & \dfrac{\partial u}{\partial y} \\[2mm] \dfrac{\partial v}{\partial x} & \dfrac{\partial v}{\partial y} \end{bmatrix}.

This is the multivariable version of the tangent-line approximation f(x0+h)≈f(x0)+f′(x0) hf(x_0 + h) \approx f(x_0) + f'(x_0)\,h. The derivative of a vector field is a matrix, called the velocity gradient tensor ∇u⃗\nabla\vec u, and you’ll derive it properly in 2.2 Linearization, the Chain Rule & the Jacobian. Relative to its center, then, a small blob of fluid moves according to ddtδx⃗=J δx⃗\dfrac{d}{dt}\delta\vec x = J\,\delta\vec x, a linear system of ODEs. The matrix JJ is the local motion of the fluid.

Take simple shear flow, u⃗=(y,0)\vec u = (y, 0): fluid at height yy moves right with speed yy, like the air near a wing surface. Then

J=[0100].J = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix}.

Following the blob for a time tt, each of its points moves by δx⃗(t)=(I+tJ) δx⃗(0)=[1t01]δx⃗(0)\delta\vec x(t) = (I + tJ)\,\delta\vec x(0) = \begin{bmatrix} 1 & t \\ 0 & 1 \end{bmatrix}\delta\vec x(0). That’s a shear matrix! A round blob becomes a tilted ellipse, which is what the end of the video and the sim’s “Fluid blob” preset show.

🐍 PythonA fluid blob in shear flow
⌘/Ctrl + Enter

Key Takeaways

  • A linear transformation fixes the origin and keeps grid lines straight, parallel and evenly spaced. Algebraically, T(av⃗+bw⃗)=aT(v⃗)+bT(w⃗)T(a\vec v + b\vec w) = aT(\vec v) + bT(\vec w).
  • The columns of a matrix are where the basis vectors land. Knowing T(ı^)T(\hat\imath) and T(ȷ^)T(\hat\jmath) determines TT everywhere.
  • Ax⃗A\vec x is a combination of the columns of AA weighted by the entries of x⃗\vec x (the column picture), and equivalently each entry is a row · vector dot product (the row picture).
  • Rotation, shear, scaling, reflection and projection matrices can all be written down by asking where ı^\hat\imath and ȷ^\hat\jmath go. Projections crush a direction and can’t be undone.
  • Matrix multiplication is composition: (AB)x⃗=A(Bx⃗)(AB)\vec x = A(B\vec x), so BB acts first. Column jj of ABAB is AA times column jj of BB.
  • Order matters: in general AB≠BAAB \neq BA (rotate-then-shear differs from shear-then-rotate), although rotations of the plane commute with each other.
  • Everything extends to 3×33\times3 and m×nm\times n matrices: one column per input basis vector.
  • The velocity gradient ∇u⃗\nabla\vec u is the matrix that locally stretches, shears and rotates a fluid parcel, and every Euler step of a linear system is a multiplication by I+hJI + hJ.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.

Where this leads

These later lessons build directly on this one: