Matrix Multiplication
Matrix multiplication repeats the matrix-vector idea.
If multiplies a matrix , each column of acts like an input vector. The output is a new matrix whose entries come from row-column dot products.
Order matters. In , rows come from and columns come from . Swapping the order changes the row-column pairings, and sometimes the swapped product is not even defined.
Let:
and:
The first entry of is row 1 of dotted with column 1 of :
The top-right entry uses row 1 of and column 2 of :
The full product is:
The diagram highlights the top-right entry: row 1 of with column 2 of
.
Using the matrices above, what is the top-right entry of ?
Compute it first, then check your number.
HintRow and column
Use row 1 of and column 2 of .
SolutionTop-right entry
Top-right means first row of and second column of . Matrix multiplication fills each output entry this way.
Let have shape 2 x 3 and have shape 3 x 4.
Enter 1 if is defined but is not defined.
Compute it first, then check your number.
HintWrite both shapes
Compare (2 x 3)(3 x 4) with (3 x 4)(2 x 3).
SolutionOrder changes the middle dimensions
has shape:
(2 x 3)(3 x 4)
Here the middle dimensions match. But would be:
(3 x 4)(2 x 3)
In , the middle dimensions are 4 and 2. They do not match, so
is not defined.
Shape Rule
If:
A has shape m x n
B has shape n x p
then:
AB has shape m x p
The middle dimensions must match.
The result keeps the outside dimensions: rows from , columns from .
If has shape 3 x 2 and has shape 2 x 4, what is the shape of
?
Compute it first, then check your number.
HintKeep the outside
(3 x 2)(2 x 4) keeps 3 and 4.
SolutionOutside dimensions
The middle dimensions are both 2, so the product is allowed:
The output has 3 rows from and 4 columns from .
When Shapes Do Not Fit
Matrix multiplication is not always allowed.
If has shape 3 x 2 and has shape 4 x 2, then is not defined.
The 2 columns of cannot pair with the 4 rows of .
Enter 1 if (3 x 2)(4 x 2) is a valid matrix product, or 0 if it is not.
Compute it first, then check your number.
HintCheck middle dimensions
The middle dimensions are the second number of the first shape and the first number of the second shape.
SolutionMiddle mismatch
Enter 0. The product would require:
(3 x 2)(4 x 2)
^ ^
The middle dimensions are 2 and 4. They do not match, so the product is
not defined.
Not Coordinate-Wise Multiplication
Matrix multiplication is not entry-by-entry multiplication.
For the matrices above, the top-left entry of is 19. It is not:
1 x 5 = 5
It is:
Enter 1 if matrix multiplication uses row-column dot products, not simple
coordinate-wise multiplication.
Compute it first, then check your number.
HintOne entry uses many numbers
The top-left entry used both 1 and 2 from the first row of .
SolutionDot products, not matching cells
Enter 1. Each entry of comes from a row of dotted with a column
of .
Code Mirror
In NumPy, @ performs matrix multiplication:
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)
The output is:
[[19 22]
[43 50]]
Next, we name three matrix patterns that appear often.