skip to content

Why can the matrix products AB and BA differ, even when both are defined?

level: middleimportance: must knowfreq 71%

answer

  1. the order of operations carries meaning
  2. the rightmost factor acts first
  3. rotate-then-stretch versus stretch-then-rotate
  4. for non-square operands even the shapes disagree

basics

~20 s

A matrix product is a composition of linear maps, and composition depends on order. In AB the right factor acts first, so rotating then stretching is a different transformation from stretching then rotating. Only special pairs commute.

solid answer

~50 s

Matrix multiplication composes transformations, and the rightmost factor acts first: `(AB)x` means apply `B` to `x`, then apply `A`. Doing two different things in the opposite order generally lands somewhere else. Take `R = [[0,-1],[1,0]]`, a 90-degree counter-clockwise rotation, and `S = [[2,0],[0,1]]`, which doubles the x-coordinate. Then `RS = [[0,-1],[2,0]]` but `SR = [[0,-2],[1,0]]`. Geometrically, `RS` stretches horizontally and then rotates that stretched direction onto the vertical axis, while `SR` rotates first so the stretch afterwards acts along a different direction. For non-square operands it is starker: if `A` is `3 x 2` and `B` is `2 x 3`, then `AB` is `3 x 3` and `BA` is `2 x 2`, so they are not even comparable. Commuting pairs exist — the identity, scalar multiples of it, diagonal matrices with each other, a matrix with its own powers or inverse — but they are the exception.

go deeper

for a junior

Remember the headline: swapping the order of a matrix product usually changes the answer, and for non-square operands it may not even be defined. Know that AB means the right-hand matrix acts on the vector first.

for a middle

Be ready to produce a concrete counterexample on a whiteboard and evaluate both products by hand, then describe the geometric difference. Also state which laws still hold — associativity, distributivity, the identity — so the picture is precise rather than vague.

for a senior

Show that order-sensitivity changes how you manipulate expressions: cancel factors from a consistent side, reverse factors under inverse and transpose, and never merge cross terms. Be able to spot an order bug in someone else's derivation.

for a principal

Own the convention question. Whether vectors are treated as columns acting from the right or rows acting from the left flips every product order in a codebase and a document set; picking one and stating it explicitly prevents a recurring class of subtle errors.

## Order is baked into the definition A matrix encodes a linear map, and a matrix product encodes the *composition* of two maps. The universal convention is that a matrix acts on a column vector from the left, so in `(AB)x` the vector meets `B` first and the result of that meets `A`. Read right to left: **`AB` means "do B, then do A"**. Composition of operations is order-sensitive in everyday life too — putting on socks then shoes is not the same as shoes then socks — and matrices inherit that sensitivity exactly. ## A concrete 2x2 pair Let ``` R = [[0, -1], [1, 0]] rotate 90 degrees counter-clockwise S = [[2, 0], [0, 1]] double the x-coordinate, leave y alone ``` Multiply them both ways: ``` RS = [[0, -1], SR = [[0, -2], [2, 0]] [1, 0]] ``` These are different matrices, so the two compositions are different transformations. A quick sanity check on a single basis vector makes it tangible. Take the vector `(1, 0)`: - Under `RS` (stretch first, then rotate): `S` sends `(1,0)` to `(2,0)`; `R` sends `(2,0)` to `(0,2)`. Net result `(0,2)`. - Under `SR` (rotate first, then stretch): `R` sends `(1,0)` to `(0,1)`; `S` leaves `(0,1)` alone because it only touches the x-coordinate. Net result `(0,1)`. The same input, the same two ingredients, two different outputs. Applied to the unit square, `RS` produces a rectangle that is twice as tall as it is wide, while `SR` produces one that is twice as wide as it is tall. Notice, though, that both cover the same area — the *amount* of stretching is order-independent even though the *direction* is not. ## The shape argument Before any geometry, shapes already break commutativity. If `A` is `3 x 2` and `B` is `2 x 3`, then `AB` is `3 x 3` and `BA` is `2 x 2`. And if `A` is `3 x 2` and `B` is `2 x 4`, then `AB` is `3 x 4` while `BA` is not defined at all. Asking whether `AB` equals `BA` is only a meaningful question for square matrices of the same size — and even there, equality is unusual. ## What matrix multiplication *does* obey Non-commutativity is the one familiar law that fails; several others hold and are worth stating so the picture is not "matrix algebra is lawless": - **Associativity**: `(AB)C = A(BC)`. Grouping is free; ordering is not. - **Distributivity**: `A(B + C) = AB + AC` and `(B + C)A = BA + CA`. Note that both one-sided forms have to be written separately, precisely because you cannot move `A` across. - **Scalars commute freely**: `(cA)B = c(AB) = A(cB)` for a number `c`. - **Identity acts as a neutral element**: `IA = A = AI` for the appropriately sized identity. ## When do two matrices commute? Special cases where `AB = BA` genuinely holds: - Either factor is the identity, or a scalar multiple of the identity. - Both are diagonal matrices of the same size — each just rescales the axes, and rescalings along fixed axes do not interfere. - One matrix is a power of the other, or its inverse: `A` commutes with `A`, with `A^2`, and with `A^-1` whenever that exists. - Two rotations of the plane about the same origin — a 30-degree turn followed by a 50-degree turn is a 80-degree turn either way. The deeper pattern behind most of these is that commuting maps stretch along a shared set of directions. Matrices that act along *different* preferred directions, like the rotation and the axis-aligned stretch above, are exactly the ones that fail to commute. ## Practical consequences Because order carries meaning, algebraic manipulations that feel automatic with numbers are invalid here. You cannot expand `(A + B)^2` as `A^2 + 2AB + B^2`; the honest expansion is `A^2 + AB + BA + B^2`, and those two middle terms do not merge. Likewise `(AB)^-1` is `B^-1 A^-1`, not `A^-1 B^-1` — the factors reverse, exactly as they do under a transpose. When you cancel a factor from an equation you must cancel from a consistent side: from `AB = AC` with `A` invertible you may left-multiply by `A^-1` to get `B = C`, but from `BA = CA` you must right-multiply instead. In an interview, the strongest answer names the reason (composition order), gives a concrete two-matrix counterexample you can evaluate on the spot, and then volunteers the exceptions, showing that you know commutation is a property of particular pairs rather than a coin flip.

  • In the expression ABx acting on a column vector x, which matrix touches x first?
    `B` does. Matrices act on column vectors from the left, so the expression is read right to left: `x` meets `B`, the result meets `A`. This is why `AB` is described as "apply B, then A", and it is the source of most sign and order confusion when people write products in the wrong sequence.
  • Name a pair of matrices that always commute, and say why.
    Two diagonal matrices of the same size. Each one only rescales the coordinate axes independently, so both orders end up multiplying entry `i` by the same two numbers. Any matrix also commutes with the identity, with scalar multiples of the identity, with its own powers, and with its inverse when that exists.
  • How does (A + B) squared expand for matrices?
    As `A^2 + AB + BA + B^2`. The two cross terms cannot be combined into `2AB` because `AB` and `BA` need not be equal. Getting this wrong is a classic slip, and the same caution applies to `(AB)^-1`, which equals `B^-1 A^-1` with the factors reversed, not `A^-1 B^-1`.

Turning a key then opening a door is not the same as opening a door then turning a key. Matrices compose the same way: the operation on the right happens first, and swapping it changes what you end up with.

saying these in an interview costs you the question

  • Says AB equals BA because multiplication is commutative for numbers
  • Reads AB as applying A first, then B
  • Claims no two matrices ever commute
  • Expands (A + B) squared as A squared plus 2AB plus B squared
  • Writes the inverse of AB as A inverse times B inverse

context