Matrix multiplication
Multiply an n×m matrix by an m×p one: each cell of the product is the dot product of a row of A and a column of B.
If A is n×m and B is m×p, the product C = A·B is n×p and
- Time:
O(n·m·p) - Space:
O(n·p) - CLRS, section 4.1
- Grimaldi, chapter 7
- Johnsonbaugh, chapter 3
A is 2×3 and B is 3×2; the product is 2×2 and each cell is a sum of three products.
def matrix_multiply(A, B):n, m, p = len(A), len(B), len(B[0])if len(A[0]) != m:return NoneC = [[0] * p for _ in range(n)]for i in range(n):for j in range(p):total = 0for k in range(m):total = total + A[i][k] * B[k][j]C[i][j] = totalreturn C
Start
i—
j—
total—
A
| 1 | 2 | 3 |
| 4 | 5 | 6 |
B
| 7 | 8 |
| 9 | 10 |
| 11 | 12 |
C
The table is still empty.
Local variables
Orange = changed in this step
A list
[1, 2, 3][4, 5, 6]
B list
[7, 8][9, 10][11, 12]
Call stack
- matrix_multiply()line 1
Step 1 of 50
Explanation
The idea
If A is n×m and B is m×p, the product C = A·B is n×p and
C[i][j] = Σ A[i][k] · B[k][j] for k = 1 … m
that is, the dot product of row i of A and column j of B. The number of columns of A must equal the
number of rows of B.
Cost
For each of the n·p cells, m multiplications: O(n·m·p); O(n³) for square matrices. Strassen's method uses
7 multiplications instead of 8 at each split and reaches O(n^2.81).
Notes
- Matrix multiplication is not commutative: usually
A·B ≠ B·A. - The order in which several matrices are multiplied changes the cost (see "matrix-chain multiplication").
What to watch in the trace
- The three tables sit side by side; for each cell of
C, see which row ofAand column ofBare read.