Matrix Multiplication

Matrix multiplication repeats the matrix-vector idea.

If AA multiplies a matrix BB, each column of BB acts like an input vector. The output is a new matrix whose entries come from row-column dot products.

Order matters. In ABAB, rows come from AA and columns come from BB. Swapping the order changes the row-column pairings, and sometimes the swapped product is not even defined.

Let:

A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}

and:

B=[5678]B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}

The first entry of ABAB is row 1 of AA dotted with column 1 of BB:

(15)+(27)=19(1 \cdot 5) + (2 \cdot 7) = 19

The top-right entry uses row 1 of AA and column 2 of BB:

(16)+(28)=22(1 \cdot 6) + (2 \cdot 8) = 22

The full product is:

AB=[19224350]AB = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}

The diagram highlights the top-right entry: row 1 of AA with column 2 of BB.

1234A5678B=19224350AB[1, 2] · [6, 8] = 22
One entry of a matrix product comes from one row and one column.
MATH-C03-T05-001Exercise: Compute one entry

Using the matrices above, what is the top-right entry of ABAB?

Compute it first, then check your number.

HintRow and column

Use row 1 of AA and column 2 of BB.

SolutionTop-right entry
(16)+(28)=6+16=22(1 \cdot 6) + (2 \cdot 8) = 6 + 16 = 22

Top-right means first row of AA and second column of BB. Matrix multiplication fills each output entry this way.

MATH-C03-T05-005Exercise: Check product order

Let AA have shape 2 x 3 and BB have shape 3 x 4.

Enter 1 if ABAB is defined but BABA 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

ABAB has shape:

(2 x 3)(3 x 4)

Here the middle dimensions match. But BABA would be:

(3 x 4)(2 x 3)

In BABA, the middle dimensions are 4 and 2. They do not match, so BABA 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 AA, columns from BB.

MATH-C03-T05-002Exercise: Predict product shape

If AA has shape 3 x 2 and BB has shape 2 x 4, what is the shape of ABAB?

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:

(3×2)(2×4)3×4(3 \times 2)(2 \times 4) \rightarrow 3 \times 4

The output has 3 rows from AA and 4 columns from BB.

When Shapes Do Not Fit

Matrix multiplication is not always allowed.

If AA has shape 3 x 2 and BB has shape 4 x 2, then ABAB is not defined. The 2 columns of AA cannot pair with the 4 rows of BB.

MATH-C03-T05-003Exercise: Spot invalid multiplication

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 ABAB is 19. It is not:

1 x 5 = 5

It is:

(15)+(27)=19(1 \cdot 5) + (2 \cdot 7) = 19
MATH-C03-T05-004Exercise: Avoid entry-wise multiplication

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 AA.

SolutionDot products, not matching cells

Enter 1. Each entry of ABAB comes from a row of AA dotted with a column of BB.

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.