What a matrix is
A matrix is a rectangular table of numbers. A matrix with rows and columns is an matrix, read " by ": rows first, then columns. That pair of numbers is its shape.
Each number in the table is an entry. For a matrix , the entry in row and column is : again, row first.
A workshop makes chairs and tables. A chair needs 4 legs, 8 screws and 1 board; a table needs 4 legs, 12 screws and 3 boards. Put the parts in rows and the products in columns:
is : three parts, two products. Its first column lists what a chair needs, and its second what a table needs.
Write for the entries of . Then is screws per table, and is boards per chair. Every entry has units: parts per item.
Rule: An m × n matrix
This is : 2 rows, 3 columns, 6 entries. In the first index is the row and the second the column.
A vector written as a column (lesson 3.1.1) is a matrix with one column: a vector with components is . A matrix with one row, , is a row vector. A matrix with as many rows as columns is square.
Try it
np.array takes a list of rows. .shape gives (rows, columns).
NumPy counts from 0, so is P[1, 1] and is P[2, 0]. P[:, 0] is the first column, the chair; P[2] is the third row, the boards.
A matrix times a vector
An order of 6 chairs and 2 tables is the vector , a column with one entry per product. The parts it needs make the vector , and there are two ways to work it out.
Row by row. Legs: 4 per chair times 6 chairs, plus 4 per table times 2 tables, so . That is the dot product of the legs row with : multiply matching components and add (lesson 3.1.2). Each entry of is one row of dotted with .
Column by column. Six chairs need 6 copies of the chair column, and 2 tables need 2 copies of the table column. So is the linear combination : vectors scaled by numbers and added, as in lesson 3.1.1. The entries of are the weights.
| part | row | 6 × chair column | 2 × table column | total |
|---|---|---|---|---|
| legs | 24 | 8 | 32 | |
| screws | 48 | 24 | 72 | |
| boards | 6 | 6 | 12 |
Both ways add the same six products, in a different order, so they agree: . The units work out too: parts per item times items gives parts.
Rule: Matrix times vector
Entry of is row of dotted with . The whole of is a combination of the columns of , weighted by the entries of .
Try it
@ is the matrix product. The second line dots each row with x; the third combines the two columns.
A 1-D array such as x has no rows or columns of its own. On the right of @, NumPy reads it as a column.
Shapes, and a picture in the plane
needs one entry for each column of , because each entry weights a column. The answer has one entry for each row. So an matrix turns vectors with components into vectors with . turns an order (2 numbers) into a parts list (3 numbers).
A matrix turns arrows in the plane into arrows in the plane. Take
with columns and . For the rows give . The columns give , the same .
A negative entry turns its column round. For , ; row by row, and .
as a combination of the columns of , drawn tip to tail. The entries of are the weights: gives , and gives .
Try it
Each line prints , then by @, then the combination of the columns.
The last lines multiply , which has 2 columns, by a vector with 3 entries. NumPy refuses with a ValueError.
A matrix times a matrix
Two shops send orders: shop 1 wants 6 chairs and 2 tables, shop 2 wants 10 chairs and 1 table. Put each order in a column of a matrix , with rows for the products:
The parts for shop are times column of . Side by side, these columns make the product :
The first column is the parts list found above. Shop 2 needs legs, screws and boards.
Rule: Matrix times matrix
Column of is times column of . Entry of is row of dotted with column of . An matrix times an matrix gives an matrix.
The inner sizes must match: each row of must be as long as each column of . In the workshop the inner size counts the products.
is parts by products and is products by shops, so is parts by shops. The sum in each entry runs over the products.
The picture shows the rule for , which is , times , which is .
Entry of comes from row 2 of and column 1 of . The 3 entries of the row pair off with the 3 entries of the column; that shared size drops out of the shape of the answer.
A second example, with two square matrices: from above and
Each entry of is a row of dotted with a column of :
| entry | row of | column of | dot product |
|---|---|---|---|
Try it
The first lines build the shops' parts lists, all at once with P @ B and one column at a time.
The loop rebuilds the table above: for each entry, the row of , the column of and their dot product.
Order matters, grouping does not
With numbers, . With matrices, swapping the order usually changes the answer, and can even make it meaningless. There are four cases to know.
- Both orders exist, with different answers. , while .
- Both orders exist, with different shapes. is and is , so is but is .
- Only one order exists. is . would be : rows of length 2 against columns of length 3, so it is not defined.
- Sometimes the order does not matter. Two matrices with zeros off the diagonal (the entries from top left to bottom right), such as and , give the same product either way. That is the exception, so always check.
The extreme case is a row times a column. The row is and the column is . Their product is : the single number , their dot product.
The other order is , a whole table. Lesson 3.2.3 writes the row as a column turned on its side.
The grouping of a product, unlike its order, never matters. With , multiplying by gives the same as times : both are .
Rule: Order and grouping
Keep the matrices in their order. The brackets can go anywhere, so a product of three is written .
Try it
against , the two groupings of , and against .
np.array_equal checks that the two diagonal matrices give the same product in both orders. The row u and column v are 2-D arrays, so that NumPy keeps their shapes.
After the two groupings comes A * C, which is not a matrix product (see below).
@, not *
In NumPy, A * C multiplies matching entries: , , and , giving . That is not the matrix product . Use @ for the product.
Lesson 3.2.2 reads a matrix as a map that moves the plane, and lesson 3.3.1 runs the product backwards to solve . Case study 3.2.5 writes the variance of a portfolio as a product of a row, a matrix and a column.
Common mistakes
- Rows first. An matrix has rows, and is in row , column . In NumPy, counting from 0, is
A[0, 1]. - One entry of per column. needs as long as a row of , and has as many entries as has rows.
- Check the inner sizes. works and gives . If the inner sizes differ, there is no product.
- Keep the order. and usually differ, may have different shapes, and one of them may not exist.
- A row times a column is a number; a column times a row is a table.
*is not the matrix product. In NumPy it multiplies entry by entry; the product is@.
Matrices and their products
0 of 13 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- An matrix has rows and columns; is the entry in row , column .
- : entry is row of dotted with ; the whole is , a combination of the columns.
- : column is times column of ; entry is row of dotted with column of .
- Shapes: . The inner sizes must match.
- in general, but . A row times a column is a number; a column times a row is a matrix.
- In NumPy, the product is
@;*multiplies entry by entry.