3.2 Matrix multiplication

We are going to define a way to multiply certain matrices together. After that we will see several different ways to understand this definition, and we will see how the definition arises as a kind of function composition.

Definition 3.2.1.

Let A=(ai⁢j) be a m×n matrix and B=(bi⁢j) be an n×p matrix. Then the matrix product A⁢B is defined to be the m×p matrix whose i,j entry is

∑k=1nai⁢k⁢bk⁢j. (3.1)

Before we even start thinking about this definition we record one key point about it. There are two ns in the definition above: one is the number of columns of A and the other is the number of rows of B. These really must be the same. We only define the matrix product A⁢B when the number of columns of A equals the number of rows of B. The reason for this will become clear when we interpret matrix multiplication in terms of function composition later.

Example 3.2.1.

The 1,2 entry of a matrix product A⁢B is obtained by putting i=1 and j=2 in the formula (3.1). If A=(ai⁢j) is m×n and B=(bi⁢j) is n×p then this is

a11⁢b12+a12⁢b22+a13⁢b32+⋯+a1⁢n⁢bn⁢2

You can see that we are multiplying each entry in the first row of A by the corresponding entry in the second column of B and adding up the results. In general, the i,j entry of A⁢B is obtained by multiplying the entries of row i of A with the entries of column j of B and adding them up.

Example 3.2.2.

Let’s look at a generic example first. Let

A=(a11a12a21a22),B=(b11b12b21b22).

The number of columns of A equals the number of rows of B, so the matrix product A⁢B is defined, and since (in the notation of the definition) m=n=p=2, the size of A⁢B is m×p which is 2×2. From the formula, we get

A⁢B=(a11⁢b11+a12⁢b21a11⁢b12+a12⁢b22a21⁢b11+a22⁢b21a21⁢b12+a22⁢b22).
Example 3.2.3.

Making the previous example concrete, if

A=(1234),B=(5678).

then A is 2×2, B is 2×2, so the matrix product A⁢B is defined and will be another 2×2 matrix:

A⁢B =(1×5+2×71×6+2×83×5+4×73×6+4×8)
=(19224350).

Matrix multiplication is so important that it is helpful to have several different ways of looking at it. The formula above is useful when we want to prove general properties of matrix multiplication, but we can get further insight when we examine the definition carefully from different points of view.

3.2.1 Matrix multiplication happens columnwise

A very important special case of matrix multiplication is when we multiply a m×n matrix by an n×1 column vector. Let

A=(abcdef),𝐱=(xyz).

Then we have

A⁢𝐱=(a⁢x+b⁢y+c⁢zd⁢x+e⁢y+f⁢z)

Another way to write the result of this matrix multiplication is

x⁢(ad)+y⁢(be)+z⁢(cf)

showing that the result is obtained by adding up scalar multiples of the columns of A. If we write 𝐜j for the jth column of A then the expression

x⁢𝐜1+y⁢𝐜2+z⁢𝐜3,

where we add up scalar multiples of the 𝐜js, is called a linear combination of 𝐜1, 𝐜2, and 𝐜3. Linear combinations are a fundamental idea and we will return to them again and again in the rest of MATH0005.

Definition 3.2.2.

Let 𝐯1,𝐯2,… be matrices all of the same shape. A linear combination of 𝐯1,𝐯2,… is a matrix of the form

a1⁢𝐯1+a2⁢𝐯2+⋯

where the ai are numbers.

This result is true whenever we multiply an m×n matrix and an n×1 column vector, not just in the example above.

Proposition 3.2.1.

Let A=(ai⁢j) be an m×n matrix and 𝐱 an n×1 column vector with entries x1,…,xn. If 𝐜1,…,𝐜n are the columns of A then

A⁢𝐱=∑k=1nxk⁢𝐜k.
Proof.

From the matrix multiplication formula (3.1) we get

A⁢𝐱=(∑k=1na1⁢k⁢xk∑k=1na2⁢k⁢xk⋮∑k=1nam⁢k⁢xk)=∑k=1nxk⁢(a1⁢ka2⁢k⋮am⁢k)

The column vector whose entries are a1⁢k, a2⁢k, …am⁢k is exactly the kth column of A, so this completes the proof. ∎

Definition 3.2.3.

For a fixed n, the standard basis vectors 𝐞1,…,𝐞n are the vectors

(100⋮0),(010⋮0),…,(00⋮01).

The vector 𝐞i with a 1 in position i and zeroes elsewhere is called the ith standard basis vector.

For example, if n=3 then there are three standard basis vectors

𝐞1=(100),𝐞2=(010),𝐞3=(001).

The special case of the proposition above when we multiply a matrix by a standard basis vector is often useful, so we’ll record it here.

Corollary 3.2.2.

Let A be a m×n matrix and 𝐞j the jth standard basis vector of height n. Then A⁢𝐞j is equal to the jth column of A.

Proof.

According to Proposition 3.2.1 we have A⁢𝐞j=∑k=1nxk⁢𝐜k where xk is the kth entry of 𝐞j and 𝐜k is the kth column of A. The entries of 𝐞j are all zero except for the jth which is 1, so

A⁢𝐞j=0×𝐜1+⋯+1×𝐜j+⋯+0×𝐜n=𝐜j.∎
Example 3.2.4.

Let A=(1234). You should verify that A⁢(10) equals the first column of A and A⁢(01) equals the second column of A.

The next theorem tells us that we can do any matrix multiplication A⁢B column-by-column, multiplying A into each of the columns of B in turn.

Theorem 3.2.3.

Let A be an m×n matrix and B an n×p matrix with columns 𝐝1,…,𝐝p. Then

A⁢B=(|⋯|A⁢𝐝1⋯A⁢𝐝p|⋯|).

The notation means that the first column of A⁢B is equal to what you get by multiplying A into the first column of B, the second column of A⁢B is what you get by multiplying A into the second column of B, and so on. That’s what it means to say that matrix multiplication works columnwise.

Proof.

From the matrix multiplication formula (3.1) the jth column of A⁢B has entries

(∑k=1na1⁢k⁢bk⁢j∑k=1na2⁢k⁢bk⁢j⋮∑k=1nam⁢k⁢bk⁢j) (3.2)

The entries bk⁢j for k=1,2,…,n are exactly the entries in column j of B, so (3.2) is A⁢𝐝j as claimed. ∎

Corollary 3.2.4.

Every column of A⁢B is a linear combination of the columns of A.

Proof.

Theorem 3.2.3 tells us that each column of A⁢B equals A⁢𝐝 for certain vectors 𝐝, and Proposition 3.2.1 tells us that any such vector A⁢𝐝 is a linear combination of the columns of A. ∎

Example 3.2.5.

Let’s look at how Proposition 3.2.1 and Theorem 3.2.3 apply to Example 3.2.3, when A was (1234) and the columns of B are 𝐝1=(57) and 𝐝2=(68).

You can check that

A⁢𝐝1 =(1943)
=5⁢(13)+7⁢(24)
A⁢𝐝2 =(2250)
=6⁢(13)+8⁢(24)

and that these are the columns of A⁢B we computed before.

3.2.2 Matrix multiplication happens rowwise

There are analogous results when we multiply an 1×n row vector and an n×p matrix.

Proposition 3.2.5.

Let 𝐚 be a 1×n row vector with entries a1,…,an and let B be an n×p matrix with rows 𝐬1,…,𝐬n. Then 𝐚⁢B=∑k=1nak⁢𝐬k.

Proof.

From the matrix multiplication formula (3.1) we get

𝐚⁢B =(∑k=1nak⁢bk⁢1⋯∑k=1nak⁢bk⁢p)
=∑k=1nak⁢(bk⁢1⋯bk⁢p)
=∑k=1nak⁢𝐬k.∎

In particular, 𝐚⁢B is a linear combination of the rows of B.

Theorem 3.2.6.

Let A be a m×n matrix with rows 𝐫1,…,𝐫m and let B be an n×p matrix. Then

A⁢B=(—𝐫1⁢B—⋯⋯⋯—𝐫m⁢B—)

The notation means that the first row of A⁢B is equal to 𝐫1⁢B, the second row is equal to 𝐫2⁢B, and so on.

Proof.

From the matrix multiplication formula (3.1), the ith row of A⁢B has entries

(∑k=1nai⁢k⁢bk⁢1⋯∑k=1nai⁢k⁢bk⁢p)
=∑k=1nai⁢k⁢(bk⁢1⋯bk⁢p). (3.3)

Row i of A is 𝐫i=(ai⁢1ai⁢2⋯ai⁢n), so 𝐫i⁢B agrees with (3.3) by Proposition 3.2.5. ∎

The theorem combined with Proposition 3.2.5 show that the rows of A⁢B are linear combinations of the rows of B.

Example 3.2.6.

Returning to the example where

A=(1234),B=(5678)

the rows of A are 𝐫1=(12) and 𝐫2=(34) and the rows of B are 𝐬1=(56) and 𝐬2=(78). We have

𝐫1⁢B =(12)⁢(5678)
=𝐬1+2⁢𝐬2
=(1922)
𝐫2⁢B =(34)⁢(5678)
=3⁢𝐬1+4⁢𝐬2
=(4350).

and these are the rows of the matrix product A⁢B.

3.2.3 Row times column

The matrix multiplication formula says that the i,j entry of A⁢B is

∑k=1nai⁢k⁢bk⁢j.

Let’s think about where the entries in this sum come from. The entries from A involved are ai,1,ai,2,…,ai,n. These are exactly the entries in the ith row of A.

The entries from B in this sum are b1,j,b2,j,…,bn,j. These are the entries from the jth column of B. So to get the i,j entry of A⁢B, we matrix multiply the ith row of A by the jth column of B. That is, if the ith row of A is 𝐫i and the jth column of B is 𝐜j, then

A⁢B=(𝐫i⁢𝐜j). (3.4)

(If you’re thinking ‘wait, isn’t 𝐫i⁢𝐜j a 1×1 matrix, not a number?’ then you are correct. We will identify the 1×1 matrix (x) with the number x.)

As an example, consider

A=(123456789),B=(212323434)

Multiplying the second row 𝐫2 of A into the third column 𝐜3 of B gives

(456)⁢(234)=8+15+24=47

which is the 2,3 entry of

A⁢B=(201420473247745074).
Example 3.2.7.

When the result of a matrix multiplication is a 1×1 matrix we will think of it as a number. This is like a dot product, if you’ve seen those before.

(123)⁢(456)=1×4+2×5+3×6=32.
Example 3.2.8.

Let A=(123456), a 3×2 matrix, and 𝐜=(78), a 2×1 column vector. The number of columns of A and the number of rows of 𝐜 are equal, so we can compute A⁢𝐜.

A⁢𝐜=(1×7+2×83×7+4×85×7+6×8).
Example 3.2.9.

Let

A=(12),B=(101010).

A is 1×2, B is 2×3, so the matrix product A⁢B is defined, and is a 1×3 matrix. The columns of B are 𝐜1=(10), 𝐜2=(01), and 𝐜3=(10). The product A⁢B is therefore

(A⁢𝐜1A⁢𝐜2A⁢𝐜3) =(1×1+2×01×0+2×11×1+2×0)
=(121)
Example 3.2.10.

Let

A=(1234),B=(5678).

Then A is 2×2, B is 2×2, so the matrix product A⁢B is defined and will be another 2×2 matrix:

A⁢B=(1×5+2×71×6+2×83×5+4×73×6+4×8).

3.2.4 Matrix multiplication motivation

In this section we’ll try to answer two questions: where does this strange-looking notion of matrix multiplication come from? Why can we only multiply A and B if the number of columns of A equals the number of rows of B?

Definition 3.2.4.

Let A be a m×n matrix. Then TA:ℝn→ℝm is the function defined by

TA⁢(𝐱)=A⁢𝐱.

Notice that this definition really does make sense. If 𝐱∈ℝn then it is an n×1 column vector, so the matrix product A⁢𝐱 exists and has size m×1, so it is an element of ℝm.

Now suppose we have an m×n matrix A and a q×p matrix B, so that TA:ℝn→ℝm and TB:ℝp→ℝq. Can we form the composition TA∘TB? The answer is no, unless q=n, that is, unless the number of columns of A equals the number of rows of B. So let’s assume that q=n so that B is n×p and the composition

TA∘TB:ℝn→ℝp

makes sense. What can we say about it?

Theorem 3.2.7.

If A is m×n and B is n×p then TA∘TB=TA⁢B.

You will prove this on a problem sheet.

The theorem shows that matrix multiplication is related to composition of functions. That’s useful because it suggests something: we know that function composition is always associative, so can we use that to show matrix multiplication is associative too? That is, if the products A⁢B and B⁢C make sense, is A⁢(B⁢C) equal to (A⁢B)⁢C? This is not exactly obvious if you just write down the horrible formulas for the i, j entries of both matrices. If we believe the theorem though it’s easy: we know

TA∘(TB∘TC)=(TA∘TB)∘TC

because function composition is associative, and so

TA∘TB⁢C =TA⁢B∘TC
TA⁢(B⁢C) =T(A⁢B)⁢C.

If TX=TY then X=Y (for example, you could evaluate at the standard basis vector 𝐞j to see that the jth column of X equals the jth column of Y for any j), so we get A⁢(B⁢C)=(A⁢B)⁢C.

Since we didn’t prove the theorem here, we’ll prove the associativity result in a more pedestrian way in the next section.