Skip to content
BIASED

Matrices and linear systems

Block 1 of 8~2 min

The matrix product is composition

The topic Vectors, spaces and linear maps showed that every linear map Rn→Rm\mathbb R^n \to \mathbb R^m is an m×nm \times n matrix and that a linear layer computes x↦Wx+bx \mapsto Wx + b. Two questions were left open: how to compute with such maps, and when an equation Ax=bAx = b can be solved for xx. This topic answers both.

Start with composition. Apply B∈Rn×pB \in \mathbb R^{n \times p} first and A∈Rm×nA \in \mathbb R^{m \times n} second. The result x↦A(Bx)x \mapsto A(Bx) is linear again, so it is a matrix too. We call that matrix the product ABAB and define it by

(AB) x=A(Bx)for every x.(AB)\,x = A(Bx) \quad \text{for every } x.

Column jj of ABAB is AA applied to column jj of BB. Entry by entry, row ii of AA meets column jj of BB:

(AB)ij=∑k=1nAikBkj.(AB)_{ij} = \sum_{k=1}^{n} A_{ik} B_{kj}.

The inner sizes must match: an m×nm \times n matrix times an n×pn \times p matrix is m×pm \times p.

Order matters. Take the matrix that will run through this topic and one more:

A=(2−111),B=(1011).A = \begin{pmatrix} 2 & -1 \\ 1 & 1 \end{pmatrix}, \qquad B = \begin{pmatrix} 1 & 0 \\ 1 & 1 \end{pmatrix}.

Then AB=(1−121)AB = \begin{pmatrix} 1 & -1 \\ 2 & 1 \end{pmatrix}, but BA=(2−130)BA = \begin{pmatrix} 2 & -1 \\ 3 & 0 \end{pmatrix}. "First BB, then AA" is a different map from "first AA, then BB". Grouping, on the other hand, is free: (AB)C=A(BC)(AB)C = A(BC), because composition is associative. Transposing reverses the order: (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top.

One consequence for networks: two linear layers with no activation between them are no deeper than one. Without biases, W2(W1x)=(W2W1) xW_2(W_1 x) = (W_2 W_1)\,x is a single layer with the matrix W2W1W_2 W_1. That is why every hidden layer is followed by a nonlinearity.

Answers are saved on the server and tied to this browser until you sign in.