Composing maps
The product AB is the map you get by applying B first and then A: (AB)x=A(Bx). Entry (i,j) of the product is row i of A dotted with column j of B, so the shapes must fit: an m×n matrix times an n×p matrix gives an m×p matrix. The product is associative, (AB)C=A(BC), and it distributes over sums, but in general it is not commutative: AB=BA because the order of two actions matters.
(AB)ij=k=1∑nAikBkj,(AB)T=BTAT(1) Computing AB for an m×n and an n×p matrix takes mnp multiply-adds (about 2mnp floating-point operations). For two n×n matrices that is n3, so doubling n makes the work eight times larger. A matrix times a vector costs only n2. Because the product is associative, the order of the brackets does not change the answer but can change the cost enormously.
Order matters, twiceWith
A=(1021) and
B=(0110):
AB=(2110) but
BA=(0112), so the order changes the answer. Brackets change the cost: for
n×n matrices
A,B and a vector
v,
(AB)v costs
n3+n2 multiply-adds while
A(Bv) costs
2n2. For
n=1000 that is a thousand million against two million.
- Shape rule: (m×n)(n×p)=(m×p); the inner sizes must match.
- Not commutative: AB=BA in general, so never swap factors.
- Cost: mnp multiply-adds; matrix times vector is n2, matrix times matrix is n3.
Bracket your products from the vector sideWhen a chain of matrices ends in a vector, multiply from the vector side: A(B(Cv)) uses only matrix-vector products. Portfolio risk wTΣw is computed as w⋅(Σw), never by first forming a matrix from w. Lab · what's graded
- The product AB20 pts
- The product BA15 pts
- The shape of a product10 pts
- Counting multiply-adds15 pts
- Two ways to bracket20 pts
- Why the bracketing matters20 pts