How to think about matrix multiplication

Tivadar Danka small portrait Tivadar Danka

Understanding math will make you a better engineer.

So, I am writing the best and most comprehensive book about it.

There is more than one way to think about matrix multiplication. Just by looking at the definition

[a11a1nan1ann][b11b1nbn1bnn]=[i=1na1ibi1i=1na1ibini=1nanibi1i=1nanibin], \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} b_{11} & \dots & b_{1n} \\ \vdots & \ddots & \vdots \\ b_{n1} & \dots & b_{nn} \end{bmatrix} = \begin{bmatrix} \sum_{i=1}^{n} a_{1i}b_{i1} & \dots & \sum_{i=1}^{n} a_{1i}b_{in} \\ \vdots & \ddots & \vdots \\ \sum_{i=1}^{n} a_{ni}b_{i1} & \dots & \sum_{i=1}^{n} a_{ni}b_{in} \end{bmatrix}, matrix multiplication is not easy to understand. However, there are multiple ways of looking at it, each one revealing invaluable insights.

Let's take a look at them! First, let's unravel the definition and visualize what happens.

For instance, the element in the 2nd row and 1st column of the product matrix is created from the 2nd row of the left and 1st column of the right matrices by summing their elementwise product.

2022-03-matrix-multiplication-01-visualization.png

To move beyond the definition, first, we introduce some notations. A matrix is built from rows and vectors, each of which can be viewed as individual vectors. You can think of them as a horizontal stack of column vectors or a vertical stack of row vectors:

A=[a11a12a1na21a22a2nan1an2ann]=[a1a2an]=[a1a2an], \begin{align*} A = \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ a_{21} & a_{22} & \dots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & \dots & a_{nn} \end{bmatrix} = \begin{bmatrix} \mathbf{a}^1 \\ \mathbf{a}^2 \\ \vdots \\ \mathbf{a}^n \end{bmatrix} = \begin{bmatrix} \mathbf{a}_1 | \mathbf{a}_2 | \dots | \mathbf{a}_n \end{bmatrix} \end{align*},

where ai\mathbf{a}^i represents the rows and ai\mathbf{a}_i represent the columns.

Matrix multiplication as the mixture of column vectors

Now, let's start by multiplying a matrix and a row vector.

By writing out the definition, it turns out that the product is just a linear combination of the columns, where the coefficients are determined by the vector we are multiplying with!

That is, we get

[a11a1nan1ann][x1xn]=[i=1na1ixii=1nanixi]=i=1nxi[a1iani].\begin{align*} \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} x_1 \\ \vdots \\ x_n \end{bmatrix} &= \begin{bmatrix} \sum_{i=1}^{n} a_{1i}x_i \\ \vdots \\ \sum_{i=1}^{n} a_{ni}x_i \\ \end{bmatrix} \\ &= \sum_{i=1}^n x_i \begin{bmatrix} a_{1i} \\ \vdots \\ a_{ni} \end{bmatrix}. \end{align*}

Taking this one step further, we can stack another vector.

This way, we can see that the product of an n×nn \times n and an n×2n \times 2 matrix equals the product of the left matrix and the columns of the right matrix, horizontally stacked:

[a11a1nan1ann][b11b12bn1bn2]=[Ab1Ab2].\begin{align*} \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} b_{11} & b_{12} \\ \vdots & \vdots \\ b_{n1} & b_{n2} \end{bmatrix} = \begin{bmatrix} A \mathbf{b}_1 | A \mathbf{b}_2 \end{bmatrix}. \end{align*}

Applying the same logic, we can finally see that the product matrix is nothing else than the left matrix times the columns of the right matrix, horizontally stacked. This is an extremely powerful way of thinking about matrix multiplication:

[a11a1nan1ann][b11b1nbn1bnn]=[Ab1Abn].\begin{align*} \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} b_{11} & \dots & b_{1n} \\ \vdots & \ddots & \vdots \\ b_{n1} & \dots & b_{nn} \end{bmatrix} = \begin{bmatrix} A \mathbf{b}_1 | \dots | A \mathbf{b}_n \end{bmatrix}. \end{align*}

By switching our viewpoint a bit, we can also get the product as vertically stacked row vectors, as shown below:

[a11a1nan1ann][b11b1nbn1bnn]=[a1BanB].\begin{align*} \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} b_{11} & \dots & b_{1n} \\ \vdots & \ddots & \vdots \\ b_{n1} & \dots & b_{nn} \end{bmatrix} = \begin{bmatrix} \mathbf{a}^1 B \\ \vdots \\ \mathbf{a}^n B \end{bmatrix}. \end{align*}

Matrix multiplication as dot products

There is another interpretation of matrix multiplication.

Let's rewind and go back to the beginning, studying the product of a matrix A\textstyle A and a column vector x\textstyle x.

Do the sums in the result look familiar?

[a11a1nan1ann][x1xn]=[i=1na1ixii=1nanixi]\begin{align*} \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} x_1 \\ \vdots \\ x_n \end{bmatrix} = \begin{bmatrix} \sum_{i=1}^{n} a_{1i} x_i \\ \vdots \\ \sum_{i=1}^{n} a_{ni} x_i \end{bmatrix} \end{align*}

These sums are just the dot product of the row vectors of A\textstyle A, taken with the column vector x\textstyle x! That is, we have

[a11a1nan1ann][x1xn]=[a1,xan,x].\begin{align*} \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} x_1 \\ \vdots \\ x_n \end{bmatrix} = \begin{bmatrix} \langle \mathbf{a}^1, x \rangle \\ \vdots \\ \langle \mathbf{a}^n, x \rangle \end{bmatrix}. \end{align*}

In general, the product of A\textstyle A and B\textstyle B is simply the dot products of row vectors from A\textstyle A and column vectors from B\textstyle B!

[a11a1nan1ann][b11b1nbn1bnn]=[a1,b1a1,bnan,b1an,bn]\begin{align*} \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} b_{11} & \dots & b_{1n} \\ \vdots & \ddots & \vdots \\ b_{n1} & \dots & b_{nn} \end{bmatrix} = \begin{bmatrix} \langle \mathbf{a}^1, \mathbf{b}_1 \rangle & \dots & \langle \mathbf{a}^1, \mathbf{b}_n \rangle \\ \vdots & \ddots & \vdots \\ \langle \mathbf{a}^n, \mathbf{b}_1 \rangle & \dots & \langle \mathbf{a}^n, \mathbf{b}_n \rangle \\ \end{bmatrix} \end{align*}

Conclusion

To sum up, we have three interpretations: matrix multiplication as

  1. vertically stacking row vectors,
  2. horizontally stacking column vectors,
  3. and as dot products of row vectors with column vectors.

[a11a1nan1ann][b11b1nbn1bnn]=[a1BanB]=[Ab1Abn]=[a1,b1a1,bnan,b1an,bn]\begin{align*} \begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \end{bmatrix} \begin{bmatrix} b_{11} & \dots & b_{1n} \\ \vdots & \ddots & \vdots \\ b_{n1} & \dots & b_{nn} \end{bmatrix} &= \begin{bmatrix} \mathbf{a}^1 B \\ \vdots \\ \mathbf{a}^n B \end{bmatrix} \\ &= \begin{bmatrix} A \mathbf{b}_1 | \dots | A \mathbf{b}_n \end{bmatrix} \\ &= \begin{bmatrix} \langle \mathbf{a}^1, \mathbf{b}_1 \rangle & \dots & \langle \mathbf{a}^1, \mathbf{b}_n \rangle \\ \vdots & \ddots & \vdots \\ \langle \mathbf{a}^n, \mathbf{b}_1 \rangle & \dots & \langle \mathbf{a}^n, \mathbf{b}_n \rangle \\ \end{bmatrix} \end{align*}

When studying matrices, each of them is immensely useful.

Having a deep understanding of math will make you a better engineer.

I want to help you with this, so I am writing a comprehensive book that takes you from high school math to the advanced stuff.
Join me on this journey and let's do this together!