7  Matrix multiplication

Asio facture azafady!

Was matrix multiplication not defined in the previous chapter (Chapter 37)? Yes, but that was called the Hadamard product or element-wise multiplication. As may happen throughout this book, identical terms and symbols can be used with meanings that depend on context. Here, another multiplication is introduced, which is the most used and useful matrix multiplication.

Matrix multiplication is something encountered all the time. Consider again the purchase matrix introduced earlier (Chapter 5). Rabe bought 6 bananas, 3 toko of strawberries, and 0.5 kapoaka of voatsinefy. Rakoto bought 3 bananas, 1 toko of strawberries, and no voatsinefy. Sanda bought 10 toko of strawberries. Finally, Dora bought 1 banana and 2 kapoaka of voatsinefy. The data can be represented as a grocery matrix \(A\).

\[ A = \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \\ \end{pmatrix} \]

Now, assume one banana is 500 Ar, a toko of strawberries is 1000 Ar, and a kapoaka of voatsinefy is 2000 Ar.

This formula above, called the dot product, is what is calculated on a receipt. Mathematicians denote it as \(\cdot\) and call it matrix multiplication. That is, if

\[ P = \begin{pmatrix} 500 \\ 1000 \\ 2000 \end{pmatrix} \] then \[A \cdot P = \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \end{pmatrix} \begin{pmatrix} 500 \\ 1000 \\ 2000 \end{pmatrix} = \begin{pmatrix} 6 \times 500 + 3 \times 1000 + 0.5 \times 2000 \\ 3 \times 500 + 1 \times 1000 + 0 \times 2000 \\ 0 \times 500 + 10 \times 1000 + 0 \times 2000 \\ 1 \times 500 + 0 \times 1000 + 2 \times 2000 \end{pmatrix} = \begin{pmatrix} 3000 + 3000 + 1000 \\ 1500 + 1000 + 0 \\ 0 + 10000 + 0 \\ 500 + 0 + 4000 \end{pmatrix} = \begin{pmatrix} 7000 \\ 2500 \\ 10000 \\ 4500 \end{pmatrix}\]

The multiplication makes sense only if the number of columns in the first matrix equals the number of rows in the second matrix—otherwise, there are no pairs to multiply.

Formal

Matrix multiplication is an operation that takes two matrices and produces a single new matrix by taking the dot product of rows from the first matrix and columns from the second.

For matrix multiplication to be possible, the number of columns in the first matrix must equal the number of rows in the second matrix.

If Matrix \(A\) has dimensions \(m \times n\) and Matrix \(B\) has dimensions \(n \times p\):

Their product \(C = A \cdot B\) is a matrix of dimension \(m \times p\). Each entry \(c_{i,j}\) in the product matrix \(C\) represents the dot product of the \(i^{th}\) row of \(A\) and the \(j^{th}\) column of \(B\):

\[ c_{i,j} = \sum_{k=1}^{n} a_{i,k} \cdot b_{k,j} = a_{i,1}b_{1,j} + a_{i,2}b_{2,j} + \dots + a_{i,n}b_{n,j} \tag{7.1}\]

Examples

  • The prices of fruits differ across markets. In another market, the prices of the fruits were different, so both set of prices are combined into a matrix with two columns

    \[ Q = \begin{pmatrix} 500 & 600 \\ 1000 & 800 \\ 2000 & 2500 \end{pmatrix} \]

    The product \(A \cdot Q\) will give the total for each market \[ A \cdot Q = \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \\ \end{pmatrix} \cdot \begin{pmatrix} 500 & 600 \\ 1000 & 800 \\ 2000 & 2500 \end{pmatrix} = \begin{pmatrix} 7000 & 7250 \\ 2500 & 2600 \\ 10000 & 8000 \\ 4500 & 5600 \end{pmatrix} \]

  • Multiplication with the identity matrix \(I\)1

\[ A \cdot I = \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \end{pmatrix} \cdot \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix} = \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \end{pmatrix} \]

Multiplying a matrix by the identity matrix leaves the matrix unchanged, just as multiplying a number by 1 does in arithmetic.

Tips and warning

  • There is no equivalent for direct matrix division. Instead, division is defined as multiplication by the inverse2.

  • The inverse of a matrix \(A\) is denoted as \(A^{-1}\) such that \(A \cdot A^{-1} = I\).

  • A linear model is essentially \(A x + b = y\), which represents a matrix multiplication of \(A\) times \(x\) plus a vector \(b\).

  • Understanding matrix multiplication conceptually—and performing it by hand—is essential; however, R can execute these operations at almost no computational cost3 (Chapter 37).


  1. also denoted as \(I_n\) to explicitly indicate the dimensions↩︎

  2. The inverse of a matrix does not necessarily exist.↩︎

  3. unless the matrices are gigantic↩︎