Matrix-Matrix Multiplication¶
1. What is Matrix-Matrix Multiplication?¶
- Matrix-matrix multiplication involves multiplying two matrices together to produce a new matrix.
- If you have two matrices $A$ and $B$, the product $C = A \times B$ results in a new matrix $C$.
- This operation is fundamental in linear algebra, computer graphics, machine learning, and many other areas.
2. Conditions for Matrix-Matrix Multiplication¶
- If matrix $A$ has dimensions $m \times n$ (with $m$ rows and $n$ columns) and matrix $B$ has dimensions $n \times p$ (with $n$ rows and $p$ columns), the number of columns in $A$ must equal the number of rows in $B$.
- The resulting matrix $C$ will have dimensions $m \times p$.
3. The Step-by-Step Process of Matrix-Matrix Multiplication¶
Given:
- Matrix $A$ of size $m \times n$:
$$
A = \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ a_{21} & a_{22} & \dots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \dots & a_{mn} \end{bmatrix}
$$
- Matrix $B$ of size $n \times p$:
$$
B = \begin{bmatrix} b_{11} & b_{12} & \dots & b_{1p} \\ b_{21} & b_{22} & \dots & b_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ b_{n1} & b_{n2} & \dots & b_{np} \end{bmatrix}
$$
The resulting matrix $C = A \times B$ will have dimensions $m \times p$, where each element $c_{ij}$ is calculated as:
$$
c_{ij} = a_{i1} \times b_{1j} + a_{i2} \times b_{2j} + \dots + a_{in} \times b_{nj}
$$
- In other words, each element of the resulting matrix $C$ is the dot product of the $i$-th row of matrix $A$ with the $j$-th column of matrix $B$.
Key Points to Remember¶
- The number of columns in the first matrix must equal the number of rows in the second matrix for multiplication to be possible.
- The resulting matrix will have the dimensions of the rows of the first matrix by the columns of the second matrix.
- Each element of the resulting matrix is the dot product of the corresponding row in the first matrix and the corresponding column in the second matrix.
4. Step-by-Step Examples¶
Let’s break down some examples to illustrate this process.
Example 1¶
Matrices:¶
$$
A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}
$$
Step-by-Step Multiplication:¶
- Matrix $A$ has dimensions $2 \times 2$ and matrix $B$ has dimensions $2 \times 2$. Since the number of columns in $A$ equals the number of rows in $B$, we can multiply them.
- The resulting matrix $C$ will have dimensions $2 \times 2$.
Calculate each element of $C$:
- First row, first column ($c_{11}$):
$$
c_{11} = (1 \times 5) + (2 \times 7) = 5 + 14 = 19
$$
- First row, second column ($c_{12}$):
$$
c_{12} = (1 \times 6) + (2 \times 8) = 6 + 16 = 22
$$
- Second row, first column ($c_{21}$):
$$
c_{21} = (3 \times 5) + (4 \times 7) = 15 + 28 = 43
$$
- Second row, second column ($c_{22}$):
$$
c_{22} = (3 \times 6) + (4 \times 8) = 18 + 32 = 50
$$
Result:¶
$$
C = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}
$$
Example 2¶
Matrices:¶
$$
A = \begin{bmatrix} 2 & 0 & -1 \\ 1 & 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 1 & 2 \\ 0 & 1 \\ 4 & 0 \end{bmatrix}
$$
Step-by-Step Multiplication:¶
- Matrix $A$ has dimensions $2 \times 3$ and matrix $B$ has dimensions $3 \times 2$. Since the number of columns in $A$ equals the number of rows in $B$, we can multiply them.
- The resulting matrix $C$ will have dimensions $2 \times 2$.
Calculate each element of $C$:
- First row, first column ($c_{11}$):
$$
c_{11} = (2 \times 1) + (0 \times 0) + (-1 \times 4) = 2 + 0 - 4 = -2
$$
- First row, second column ($c_{12}$):
$$
c_{12} = (2 \times 2) + (0 \times 1) + (-1 \times 0) = 4 + 0 + 0 = 4
$$
- Second row, first column ($c_{21}$):
$$
c_{21} = (1 \times 1) + (3 \times 0) + (4 \times 4) = 1 + 0 + 16 = 17
$$
- Second row, second column ($c_{22}$):
$$
c_{22} = (1 \times 2) + (3 \times 1) + (4 \times 0) = 2 + 3 + 0 = 5
$$
Result:¶
$$
C = \begin{bmatrix} -2 & 4 \\ 17 & 5 \end{bmatrix}
$$
Example 3¶
Matrices:¶
$$
A = \begin{bmatrix} 3 & -1 & 2 \\ 0 & 4 & 1 \end{bmatrix}, \quad B = \begin{bmatrix} 1 & 2 \\ -2 & 0 \\ 3 & -1 \end{bmatrix}
$$
Step-by-Step Multiplication:¶
- Matrix $A$ has dimensions $2 \times 3$ and matrix $B$ has dimensions $3 \times 2$. Since the number of columns in $A$ equals the number of rows in $B$, we can multiply them.
- The resulting matrix $C$ will have dimensions $2 \times 2$.
Calculate each element of $C$:
- First row, first column ($c_{11}$):
$$
c_{11} = (3 \times 1) + (-1 \times -2) + (2 \times 3) = 3 + 2 + 6 = 11
$$
- First row, second column ($c_{12}$):
$$
c_{12} = (3 \times 2) + (-1 \times 0) + (2 \times -1) = 6 + 0 - 2 = 4
$$
- Second row, first column ($c_{21}$):
$$
c_{21} = (0 \times 1) + (4 \times -2) + (1 \times 3) = 0 - 8 + 3 = -5
$$
- Second row, second column ($c_{22}$):
$$
c_{22} = (0 \times 2) + (4 \times 0) + (1 \times -1) = 0 + 0 - 1 = -1
$$
Result:¶
$$
C = \begin{bmatrix} 11 & 4 \\ -5 & -1 \end{bmatrix}
$$