๐Ÿงฎ Linear Algebra Kingdom ยท Linear Algebra

Matrix Operations

Arithmetic on rectangles of numbers: add and scale matrices, multiply them row by column, work out which products are even defined and what size they come out, take a transpose, recognise a symmetric matrix and the identity, and see for yourself that AB and BA are usually different.

In short

  • An entry is named by its row first and its column second, and the transpose is what you get by reading every address backwards.
  • Adding and scaling happen entry by entry and need matching sizes; multiplying pairs a whole row with a whole column, and needs the inner sizes to agree.
  • The size of AB is the outer pair: rows of the left matrix, columns of the right one.
  • AB and BA are usually different matrices, so the order of a product is part of the answer.

A rectangle of numbers, and its address system

Millwright Orin runs the Array Mill on rows and columns, and so does the whole of linear algebra. A matrix is a rectangle of numbers written inside brackets:

A = [[1, 2, 3], [4, 5, 6]]

That matrix has two rows and three columns, so we call it 2 by 3. The size is always said rows first, and it is worth saying out loud, because almost every rule in this craft is a rule about sizes.

Each number inside is an entry, and each entry has an address: its row, counted from the top, and its column, counted from the left โ€” in that order, every time. In the matrix above, the entry in row 2, column 1 is 4, and the entry in row 1, column 2 is 2. They are different entries, and swapping the two numbers in an address is the single most common slip in this whole kingdom.

There is a name for reading every address backwards on purpose. The transpose of A, written A with a small T after it, turns each row of A into a column:

A = [[1, 2, 3], [4, 5, 6]] has transpose [[1, 4], [2, 5], [3, 6]]

A 2 by 3 matrix transposes into a 3 by 2 one. Nothing is added, taken away or changed in sign; the entries only move, and the entry at row i, column j lands at row j, column i.

A matrix with the same number of rows as columns is square, and a square matrix that equals its own transpose is symmetric โ€” every entry matches its mirror across the diagonal that runs from the top left to the bottom right. The entries on that diagonal are free to be anything at all; it is the pairs on either side of it that have to agree.

Adding and scaling: entry by entry

Two matrices can be added only when they are the same size, and then the rule is as gentle as it looks: add the entries that share an address.

[[1, 2], [3, 4]] + [[5, 6], [7, 8]] = [[6, 8], [10, 12]]

Nothing crosses between positions. The entry in row 1, column 2 of the answer is built from the entries in row 1, column 2 of each matrix, and from nothing else. Subtracting works the same way, and the order matters exactly as it does for numbers: A - B and B - A have opposite signs everywhere.

Scaling is just as direct. Multiplying a matrix by a number โ€” a scalar โ€” multiplies every entry:

3[[1, -2], [0, 4]] = [[3, -6], [0, 12]]

Two traps live here, and they are worth naming before you meet them. First, a scalar written in front of a bracket reaches everything inside that bracket: in 3(A + B) it reaches both matrices, while in 3A + B it reaches only A. Second, a minus sign in front of a scaled matrix belongs to the whole of it, so in 4A - 3B every entry of 3B is taken away, sign included.

Because adding and scaling behave, matrix equations behave too. A + X = B is solved exactly the way 3 + x = 7 is solved: X = B - A.

Rows meet columns: the product

Multiplication is where matrices stop looking like tidy lists of numbers and start doing something. It is not entry by entry.

When is AB defined? Write the two sizes side by side:

A is 2 by 3 B is 3 by 4

The two inner numbers are 3 and 3. When they agree, the product is defined, and its size is the two outer numbers: 2 by 4. When they disagree, AB simply does not exist โ€” there is nothing to work out and nothing to write down.

How is one entry built? The entry of AB in row i, column j comes from row i of A and column j of B. Pair them off in order, multiply each pair, and add:

A = [[1, 2], [3, 4]] B = [[5, 6], [7, 8]] row 1 of A is [1, 2], column 1 of B is [5, 7] (1)(5) + (2)(7) = 5 + 14 = 19

so the entry in row 1, column 1 of AB is 19. Do the other three pairings and AB = [[19, 22], [43, 50]].

The order matters. For ordinary numbers 3 times 4 and 4 times 3 agree; for matrices they usually do not. With the A and B above, BA = [[23, 34], [31, 46]], which is nowhere near AB. Some special pairs do agree โ€” any matrix agrees with a scalar copy of the identity โ€” but that is a coincidence to be checked, never a rule to lean on. Computing BA and calling it AB is the signature mistake of this kingdom, and it hides very well, because the answer looks perfectly reasonable.

The transpose knows about the order too: the transpose of AB is the transpose of B times the transpose of A, in that order. Transposing turns the product around.

A matrix acting on a vector

A vector is a matrix with one column, and the product rule already covers it. If A is 3 by 3 and x has three entries, then Ax has three entries: one per row of A.

A = [[2, 0, 1], [1, 3, -1], [0, 4, 2]] x = [1, -2, 3] row 1: (2)(1) + (0)(-2) + (1)(3) = 5 row 2: (1)(1) + (3)(-2) + (-1)(3) = -8 row 3: (0)(1) + (4)(-2) + (2)(3) = -2 so Ax = [5, -8, -2]

Each row of A meets the whole vector, and gives back one number. That is why Ax has as many entries as A has rows, and why the vector needs as many entries as A has columns โ€” the inner numbers again.

Reading Ax this way is the reason matrices are worth having at all: A takes a vector in and hands a vector back, so a matrix is a machine that moves vectors around. Every later craft in this kingdom โ€” spans, systems, transformations, eigenvectors โ€” is a question about what that machine does.

Watch one thing here. Pairing the columns of A with x instead of its rows quietly computes the transpose of A times x, and the answer will look sensible while being about a different machine entirely.

The identity, and powers

The identity matrix I is square, with 1 down the main diagonal and 0 everywhere else:

I = [[1, 0], [0, 1]]

It does for matrices what the number one does for ordinary arithmetic: AI = A and IA = A, for every A of the right size. Work out one entry and you can see why โ€” each column of I is all zeros but for a single 1, and that 1 picks exactly one entry out of the row it meets.

Because a square matrix can be multiplied by itself, powers make sense. A squared means AA, worked out with the row-meets-column rule and not by squaring each entry:

A = [[1, 2], [0, 3]] A squared = [[1, 8], [0, 9]] (squaring the entries would give [[1, 4], [0, 9]], which is a different matrix)

Brackets expand as usual, as long as you keep the order of every factor. Since A and I agree in either order, (A + I) squared really is A squared + 2A + I โ€” but for two general matrices, (A + B) squared is A squared + AB + BA + B squared, and the two middle terms cannot be joined into 2AB.

Worked examples

Example 1

A = [[3, -1], [0, 4]] and B = [[2, 5], [-3, 1]]. Work out 3A - 2B.

  1. Scale each matrix on its own first: 3A = [[9, -3], [0, 12]].
  2. And 2B = [[4, 10], [-6, 2]].
  3. Subtract entry by entry, keeping each entry in its own row and column: 9 - 4 = 5, -3 - 10 = -13, 0 - (-6) = 6, 12 - 2 = 10.
  4. So 3A - 2B = [[5, -13], [6, 10]].

Example 2

A = [[1, 2], [3, 4]] and B = [[0, -1], [2, 5]]. Work out AB, and then BA.

  1. Both matrices are 2 by 2, so the inner numbers agree and AB is 2 by 2 as well.
  2. Row 1 of A against column 1 of B: (1)(0) + (2)(2) = 4. Row 1 against column 2: (1)(-1) + (2)(5) = 9.
  3. Row 2 of A against column 1 of B: (3)(0) + (4)(2) = 8. Row 2 against column 2: (3)(-1) + (4)(5) = 17.
  4. So AB = [[4, 9], [8, 17]].
  5. Now BA takes rows of B against columns of A: BA = [[-3, -4], [17, 24]]. The two products are different matrices, which is the ordinary state of affairs.

Example 3

A = [[2, -1, 0], [1, 1, 3]] and x = [4, 2, -1]. Is Ax defined, and what is it?

  1. A is 2 by 3 and x, being a single column, is 3 by 1. The inner numbers are both 3, so Ax is defined and is 2 by 1 โ€” a vector with two entries.
  2. Row 1 of A meets the whole vector: (2)(4) + (-1)(2) + (0)(-1) = 8 - 2 + 0 = 6.
  3. Row 2 of A meets it as well: (1)(4) + (1)(2) + (3)(-1) = 4 + 2 - 3 = 3.
  4. So Ax = (6, 3).

Practice problems, with solutions

Three problems of increasing difficulty, each with the full working. In the game these are generated fresh every time; these three are fixed so this page always shows the same ones.

Problem 1

Difficulty 1 of 5

A = [[6, 3], [6, 4]] B = [[8, 9], [1, 5]] Work out A + B. Write the matrix row by row, entries separated by commas and rows by a semicolon, like 1,2;3,4.

Answer: [[14,12],[7,9]]

  1. Two matrices of the same size are added entry by entry: nothing crosses between positions.
  2. Row 1: 6 + 8 = 14, 3 + 9 = 12.
  3. Row 2: 6 + 1 = 7, 4 + 5 = 9.
  4. So A + B = [[14, 12], [7, 9]].

Problem 2

Difficulty 3 of 5

A = [[4, -5, 7], [1, 2, 5]] B = [[0, 2], [6, -5], [5, 1]] A is 2 by 3 and B is 3 by 2, so AB is 2 by 2. Work out the entry of AB in the 1st row and the 2nd column.

Answer: 40

  1. The 1st row of A is [4, -5, 7].
  2. The 2nd column of B is [2, -5, 1].
  3. Pair them off and add: (4)(2) + (-5)(-5) + (7)(1) = 40.

Problem 3

Difficulty 4 of 5

A = [[4, 8], [1, -7]] B = [[-5, 4], [-4, -1]] Work out BA. Write the matrix row by row, entries separated by commas and rows by a semicolon, like 1,2;3,4.

Answer: [[-16,-68],[-17,-25]]

  1. BA pairs the rows of the left matrix with the columns of the right one.
  2. The 1st row meets the 1st column: (-5)(4) + (4)(1) = -16.
  3. The 1st row meets the 2nd column: (-5)(8) + (4)(-7) = -68.
  4. The 2nd row meets the 1st column: (-4)(4) + (-1)(1) = -17.
  5. The 2nd row meets the 2nd column: (-4)(8) + (-1)(-7) = -25.
  6. So BA = [[-16, -68], [-17, -25]].

Common mistakes

  • Working out BA and calling it AB. The order of a matrix product changes the answer, and the wrong one still looks like a perfectly good matrix.
  • Multiplying matching entries together instead of pairing a row with a column. That gives a grid of products, not a matrix product.
  • Naming an entry by its column first, which finds the entry of the transpose instead of the one that was asked for.
  • Letting a scalar in front of a bracket reach only the first matrix, so that 3(A + B) comes out as 3A + B.
  • Squaring each entry when A squared is asked for. A matrix power is a product of the whole matrix with itself.

What you should be able to do

  • Add matrices, multiply one by a scalar, and take a transpose.
  • Multiply two matrices, and give a single entry of a product without computing the rest.
  • Decide whether a product is defined and what size it is.
  • Recognise the identity and a symmetric matrix, and show that matrix multiplication is not commutative.

Where this fits in the curriculum

Common Core

  • HSN-VM.C.6

    High school โ€” Use matrices to represent and manipulate data, for example to represent payoffs or incidence relationships in a network.

    HSN-VM.C.6 is a (+) standard โ€” beyond the college- and career-ready threshold, i.e. precalculus rather than Algebra II.

  • HSN-VM.C.7

    High school โ€” Multiply matrices by scalars to produce new matrices, as when all the payoffs in a game are doubled.

    HSN-VM.C.7 is a (+) standard โ€” beyond the college- and career-ready threshold, i.e. precalculus rather than Algebra II.

  • HSN-VM.C.8

    High school โ€” Add, subtract and multiply matrices of appropriate dimensions.

    HSN-VM.C.8 is a (+) standard โ€” beyond the college- and career-ready threshold, i.e. precalculus rather than Algebra II. The transpose and the symmetric matrix are named nowhere in the Common Core; they arrive with the university course.

  • HSN-VM.C.9

    High school โ€” Understand that, unlike multiplication of numbers, matrix multiplication for square matrices is not commutative, but that it is still associative and distributive.

    HSN-VM.C.9 is a (+) standard โ€” beyond the college- and career-ready threshold, i.e. precalculus rather than Algebra II.

Learn these first

This leads on to