II.10 · Linear Algebra
Apply A, then B: the combined matrix is BA.
Keep the intermediate vector visible, compare BA with AB, follow rectangular dimensions, and compose affine maps without confusing their translations.
Apply A, then B; keep the middle state visible
A acts first and B second, so the combined matrix is BA, not AB.
- matrix A
- 2×2 [1, 1; 0, 1]
- matrix B (applied second)
- 2×2 [1, 0; 1, 1]
- input vector
- (2, 1)
- intermediate state after A
- (3, 1)
- combined linear transformation
- 2×2 [1, 1; 1, 2]
- final state after B
- (3, 4)
- opposite order for comparison
- 2×2 [2, 1; 1, 1]
Exact expressions use =. Rounded computations use ≈. Every view uses this same state.
A acts first and B second, so the combined matrix is BA, not AB.
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
Move and track . Before choosing Swap order, predict whether the final point stays fixed. Use 2→3→2 to read the dimensions without relying on a plane picture. Then choose Affine bridge and isolate the combined translation.
Intuition: replace a pipeline without erasing its order
Two transformations form a pipeline. consumes the original input; consumes the output of . A matrix product packages that pipeline as one linear map, but the visible intermediate state explains what the package means.
Function notation already fixes the order:
The later operation appears on the left, so the combined matrix is .
Definition
If is and is , their product is the matrix whose entries are
Its th column is applied to the th column of . Therefore records where the complete composition sends every input basis vector.
Mechanics: follow dimensions and the middle space
The dimension chain is
The shared is the intermediate space. A map followed by a map produces a combined map. Reversing the factors describes a different route and yields a matrix.
Derivation: associativity connects the pipeline
Expand one output component:
This proves . Regrouping is valid, so matrix multiplication is associative. Swapping factors is a different operation, so multiplication is generally not commutative.
Noncommutativity in geometry
Consider a horizontal shear and a quarter-turn. Shearing a shape and then turning it does not usually place its slanted edge where turning first and shearing does. The stage draws both and exactly; unequal results are structural, not a rendering artifact.
Some particular matrices do commute, including every matrix with the identity. One agreeing example does not establish a general commutative law.
Affine composition
Suppose the first map is and the second is . Substitution gives
The linear parts multiply in the same order. The first translation is itself transformed by before the second translation is added. Simply adding is generally wrong.
Worked examples
For
we obtain , then . Direct multiplication gives , and .
Inspect the worked calculation
Apply A to x and keep Ax visible.
- matrix A
- 2×2 [1, 1; 0, 1]
- matrix B (applied second)
- 2×2 [1, 0; 1, 1]
- input vector
- (2, 1)
- intermediate state after A
- (3, 1)
- combined linear transformation
- 2×2 [1, 1; 1, 2]
- final state after B
- (3, 4)
- opposite order for comparison
- 2×2 [2, 1; 1, 1]
The opposite product is , which is different. Both are valid products; they encode different pipelines.
Executable reference code
The Python reference validates rectangular shapes and computes both a composition and its application. The NumPy file checks sequential and combined evaluations, demonstrates noncommutativity, and keeps affine translation separate.
from fractions import Fraction as F
def matvec(a, x):
return tuple(sum((entry * value for entry, value in zip(row, x)), F(0)) for row in a)
def matmul(left, right):
if len(left[0]) != len(right):
raise ValueError("inner dimensions must match")
columns = tuple(zip(*right))
return tuple(tuple(sum((x * y for x, y in zip(row, column)), F(0)) for column in columns) for row in left)
A = ((F(1), F(1)), (F(0), F(1)))
B = ((F(1), F(0)), (F(1), F(1)))
x = (F(2), F(1))
BA = matmul(B, A)
assert matvec(B, matvec(A, x)) == matvec(BA, x) == (3, 4)
assert BA != matmul(A, B)
b, c = (F(1), F(-1)), (F(0), F(2))
combined_translation = tuple(u + v for u, v in zip(matvec(B, b), c))
assert combined_translation == (1, 2)import numpy as np
A = np.array([[1.0, 1.0], [0.0, 1.0]])
B = np.array([[1.0, 0.0], [1.0, 1.0]])
x = np.array([2.0, 1.0])
b, c = np.array([1.0, -1.0]), np.array([0.0, 2.0])
np.testing.assert_allclose(B @ (A @ x), (B @ A) @ x)
assert not np.array_equal(B @ A, A @ B)
np.testing.assert_allclose(B @ (A @ x + b) + c, (B @ A) @ x + (B @ b + c))Explore
Set to the identity and explain why both orders agree. Set and compare with two visible applications. In the rectangular preset, name the input, middle, and output dimensions. In the affine preset, set and change only ; explain why the final shift follows .
Common mistakes
Do not read left to right as a timeline. Do not multiply entries position by position. Do not assume , and do not infer that both orders are even defined for rectangular matrices. For affine maps, do not fold translations into the linear product or add them without transforming the earlier shift.
Numerical considerations
Exact rational examples make sequential and combined outputs identical. Floating-point software may produce slightly different last bits because the two evaluation orders group sums differently. In deep pipelines, shapes and operation order should be asserted explicitly, while performance libraries choose stable kernels appropriate to the hardware.
Engineering and ML connections
Layers, coordinate changes, camera transforms, and data-processing stages compose in this same order. Several linear layers with no intervening nonlinearity collapse mathematically to one matrix; nonlinear activations prevent that collapse and give deep networks more expressive structure. Bias vectors follow the affine composition law above.
Exercises
Summary and checkpoints
Matrix multiplication represents composition: first and second become . Check that you can follow the intermediate space, compute a product by row–column pairings, explain noncommutativity, and compose affine translations as . Next we study transformations that preserve Euclidean geometry.