Something weโve explored over the past few sections is the idea of performing two operations back to back. We could stretch a vector and then rotate it, for example. Can we describe the combined operation with just one matrix?
Yes! Applying two linear transformations back to back corresponds to multiplying two matrices together, which we will explore here.
from pathlib import Path
import sys
_notes_root = next(p for p in [Path.cwd(), *Path.cwd().parents] if (p / 'myst.yml').exists())
if str(_notes_root / 'ch03') not in sys.path:
sys.path.insert(0, str(_notes_root / 'ch03'))
from friendly_visuals import mapping, transform, projection_line, projection_plane
from chapter3_visuals import save_figure, widget, show_3d
Before we figure out how to multiply two matrices, letโs check when we can.
For example, a 2ร3 matrix can multiply a 3ร4 matrix, and the answer has shape 2ร4. A 2ร3 matrix cannot multiply a 2ร4 matrix: the inner numbers, 3 and 2, donโt match. This means that matrix multiplication is not commutative: just because AB is defined (meaning it is legal, by the golden rule, to multiply A on the right by B) does not mean BA is defined.
This is the same rule we used for matrix-vector multiplication. A vector is just a matrix with one column. Now the second matrix can have several columns.
Would rotating first and stretching second give the same result? Letโs check:
HR=[20โ01โ][01โโ10โ]=[01โโ20โ].
This is different from RH. Our same input now follows
[21โ]ย Rย โ[โ12โ]ย Hย โ[โ22โ].
Why did the order matter? The stretch always doubles the horizontal coordinate. Rotating changes which part of the original vector points horizontally. Try both orders below.
widget('composition-widget.html', 'Stretch then rotate, or rotate then stretch')
Loading...
In general, AB๎ =BA. Unlike multiplication of real numbers, matrix multiplication is not commutative.
Sometimes reversing the order changes even the shape of the answer. For example, let
The first product has shape 2ร2: append the sum of two entries, then discard it, and youโre back where you started. The second has shape 3ร3: discard the third entry, then replace it with the sum of the first two. You wonโt generally get the original third entry back.
Sometimes a product is defined in only one order. For example, let
But DC is not defined: its shapes would be (3ร4)(2ร3), and the inner sizes, 4 and 2, do not match.
It is possible for AB=BA in special cases. For instance, doubling every coordinate and then rotating has the same effect as rotating and then doubling every coordinate. But this is not always true: we cannot assume that AB=BA for arbitrary matrices.
The first property lets us change the parentheses, keeping the matrices in the same order. Both sides apply C first, then B, then A. Changing the parentheses is different from reversing the order!
The identity matrix leaves vectors unchanged, so for a matrix A of shape nรd,
The distributive properties let us expand expressions much as we do with numbers. We just need to keep the factors in their original order. Letโs start with
Their sum is the same matrix! This is an example of A(B+C)=AB+AC. Notice that A stays on the left of both terms. Similarly, in (B+C)A=BA+CA, it stays on the right.
Now suppose A and B are square matrices of the same shape. Recall that (A+B)2 means (A+B)(A+B). Expanding gives
(A+B)2โ=A(A+B)+B(A+B)=A2+AB+BA+B2.โ
With real numbers, we would combine the middle two terms. Can we do that here? Only if AB=BA. In general, they are different matrices, so AB+BA cannot be replaced by 2AB.
The horizontal stretch and rotation from earlier give a concrete example where they donโt commute. Doubling every coordinate and rotating gives an example where they do. The distributive property works in both cases; whether we can combine terms depends on the particular matrices.
Activity: Expand before simplifying
Let A and B be square matrices of the same shape.
Expand (AโB)2, keeping every product in order.
Expand (AโB)(A+B). Is it always equal to A2โB2?
What additional condition on A and B makes both familiar scalar formulas work?