Glossary

Select one of the keywords on the left…

MatricesMatrix Arithmetic

Reading time: ~40 min

Matrix Multiplication

We learned in the last chapter that matrices can represent linear transformations. However, there are many other things that matrices can represent! Also, matrices do not always have to be square matrices, but can have many different dimensional values. Let's explore this with a hypothetical scenario.

Four friends are in a new city and they're looking for a restaurant to eat at together. What are some features they might look for in a restaurant?

  • Outdoor seating
  • Vegetarian food
  • Low price
  • Spicy food
  • Kid's menu
  • Good wine selection

Each friend has preferences for how important these things are, which we can quantify with a value 0 (not important) to 4 (very important). If we create a table with friends on the left-most column and restaurant features across the top row, we can fill in the cells of the table with each friend's preference for that feature.

What we have built here is a 4 by 2 matrix. We have four rows to represent the four , and two columns to represent the two .

The friends have found three restaurants within walking distance, and they have pulled up the websites for each. Lucky for them, each restaurants' website has listed the quality of the features that the friends have quantified: availability of outdoor seating, and vegetarian options.

This is a 2 by 3 matrix. There are two rows to represent the two and three rows, one to represent each .

Begin side-by-side column display.

What we would like to do is somehow synthesize these two tables of information so we can get a sense of how much each person might like each restaurant. We can use a procedure called matrix multiplication to do this. The result will be a new table with the friends as and the restaurants as .

How should we fill out this table? The value of each cell should represent how much each person might like each restaurant.

For example, the first cell will represent how much Alice might like .

Alice has a preference of 1 for Outdoor Seating, and Gauss Grill has a value of 3 for Outdoor Seating, and we multiply these to get .

Alice has a preference of 4 for Vegetarian food, but Gauss Grill only has a value of 1 for Vegetarian food, and we mutiply these to get .

We sum together all the products to get , which we can write in the first cell.

How much might Bob like Laplace Lounge? + = .

When we finish our process, we get a matrix of 4 rows and 3 columns. This makes sense! We had 4 people and 3 restaurants, so we will end up with a row for each person and a column for each restaurant.

We can then sum the values in each column to figure out which restaurant is most popular (this is left as an exercise for the reader).

End side-by-side column display.

What we have just done is matrix multiplication.

Align these.

x
=

Formal definition of Matrix Multiplication

The formal defintion for matrix multiplication is as follows:

Given matrix A with dimensions rA,cA and matrix B with dimensions rB,cB

The value of the cell xij in A×B is:

ai1b1j + ... aiNbNj

where N=cA=rB.

Notice that, for this algorithm to work, the number of in the first matrix has to be equal to the number of in the second matrix.

For example, if in our restaurant example, each friend had a preference level for spicy food, our preference matrix would be 4x3.

We are now attempting to multiply a 4x3 matrix by a 2x3 matrix, but we don't have any information about which restaurants have spicy food! So we cannot multiply the two matrices.

Could end section with a simple checkmark multiple choice, for which multiplications are possible.

Matrix Factorisation

This type of matrix is used in all sorts of online recommender systems. Movies can be categorized by their genres like Comedy, Action, Romance, or Horror. Songs can be categorized into genres with ever-increasing specificity like Rock, Classical, Pop, Rap, Electro-Funk, Indie Folk, or Norwegian Black Metal. When you watch a movie on Netflix, or listen to a song on Spotify, there's likely a very large matrix somewhere, remembering your taste!

However, this process is slightly different from what we did above. The company running the streaming service doesn't know what its users' tastes are. It does know what movies they have watched, and whether they liked them or not. From this information they attempt to figure out each user's possible genre preferences using a process called matrix factorisation. Much like in integer factorisation, where an integer can be written as a product of prime numbers, matrix factorisation is about working backwards from an incomplete product matrix to find possible preference matrices. This algorithm is much more complex than integer factorisation, so we need complex machine leaerning algorithms to perform it.

Multiplying Linear Transformations

We have now learned two different ways to think about matrix multiplication. In the first chapter we learned that multiplying a 2x2 matrix by a 2x1 vector, can be thought of as a linear transformation. And we just learned to the detailed rules for how to multiply matrices of any size, like a preference matrix. Let's go back to thinking about matrices as linear transformations.

Recall the 2x2 matrix representing the rotation of 90º about the origin. Let's call it R90.

Rotate 90º

What if we multiply this matrix by the 2x2 matrix for rotation of 180º, R180?

Rotate 180º

The resulting matrix is this:

Rotate 270º

This matrix is the linear transformation for a .

That's right,

R90 x R180 = R270

In this case, multiplying two rotation matrices give us a new rotation matrix with an angle equal to the sum of the first two angles. A more general way to say this is that when we multiplied two transformation matrices, the resulting transformation matrix has the effect applying BOTH of the original two matrices in succession.

This works for all rotation values:

  • R90 x R90 = R80
  • R180 x R180 = R360 = I

What about other types of transformations?

Perhaps let them draw points (like a spaceship), and then the points are transformed by each transformation? {.fixme} Should be displayed horizontally, with their matrices across the bottom.

x
=
CLEAR
CALCULATE
Identity
Rotate 90º
Rotate 180º
Rotate 270º
Reflect x=0
Reflect y=x
Reflect y=0
Reflect y=-x
Shear x 1
Shear x -1
Shear y 1
Shear y -1
Scale by 2
Scale by 1/2
Scale x by 2
Scale y by 1/2

Matrix Addition

Matrices can also be added. Matrix addition does not happen very often, but it is very simple to learn.

We write matrix arithmetic just as you might expect:

A+B

  • Two matrices can be added only if they have the same dimensions.
  • The resulting matrix will be the same dimension as the matrices added.
  • Each value in location (i,j) of the resulting matrix will be the sum of the values at (i,j) in the other two matrices.

Add these two matrices!

1 2
3 4

+

9 8
7 6

=

Great.

This code could be a lot simpler! And why is this not going below the tables?

Scalar Multiplication

Another operation we can perform with a matrix is scalar multiplication. A scalar is what we call a real number in matrix and vector arithmetic.

We write scalar multiplication as

sA

Scalar multiplication is as simple as multiplying every cell in a matrix A times a scalar s.

2

3 1
-4 0

=

Note that while it is possible to add two matrices, and to multiply a matrix by a scalar, the operation of adding a scalar to a matrix is not defined.

Properties of Matrix Arithmetic

Recall operators like addition and multiplication, and how it's useful to think about their properties. Commutative, distributive, and associative properties.

Associative

Is matrix multiplication associative?. If it is, then the equation below will be true.

AxBxC=AxBxC

A good first question to ask is: will the dimensions of the matrices allow this?

If AxBxC is possible, then

columnsB=rowsC

BxC will have rowsB number of rows, and will be equal to columnsA. This means that when attempting AxBxC, we know we can perform AxB. AxB will then have columnsB columns, so we can multiply it by C.

We know we can perform this multiplication based on the dimensions, but will we get the same result? Recall from our section on multiplication that it can be thought of as successive linear transformations.

It turns out that as long as we keep the ordering of the matrices, we will get the same result. Whether we do BxC first or AxB first, it does not matter.

Matrix multiplication is associative.

Commutative

Is matrix multiplication commutative?. If it is, then the equation below will be true.

AxB=BxA

Recall that when we multiply two matrices, the number of columns of the first must match the number of rows of the second. This means that if we can multiply AxB, columnsA=rowsB, but it is not necessarily true that rowsA=columnsB. Therefore, it does not follow that we can multiply BxA, and matrix multiplication is not commutative.

What if both matrices are square matrices? We can then perform both AxB and BxA, however we do not know if they will be equal.

If we think of the matrices as transformations, we can imagine scenarios wherein applying two different transformations will be different depending on which direction you multiply them.

If we perform a 90º Rotation, and then a reflection across the x-axis, the final transformation will look like this:

Rotate 90º
x
Reflect x=0
=
Reflect y=x

However, if we reverse the order of the transformations, the final transformation will look like this.

Reflect x=0
x
Rotate 90º
=
Reflect y=-x

There are many more examples of how matrix multiplication does not meet the commutative property, and we encourage you to experiment!

Distributive

Is matrix multiplication distributive over matrix addition?. If it is, then the equation below will be true.

AxB+C=AxB+AxC

Could demonstrate this by adding basis vectors and applying transformations to them. Or could somehow leave it as an exercise for the reader.