Module 2, topic 14, week 7, Thursday
Matrices: operations and matrix multiplication
Matrices from the beginning: adding and scaling, the transpose, a matrix times a vector, matrix multiplication, determinant and inverse, Gaussian elimination, rank, and matrices as transformations. Worked by hand.
Introduction: from one vector to many
The second lesson of Module 2. The last lesson worked with one vector at a time. Real data never comes as one vector: a table has hundreds of rows, an image has rows and columns of pixels, a model has thousands of weights. A matrix is the tool that holds many vectors together and calculates with all of them at once. This guide teaches matrices from the very beginning: what a matrix is, how to add matrices and multiply them by a number, the transpose, the product of a matrix and a vector, and then the central operation of the lesson, matrix multiplication: the rule, when it is possible, why its order matters. After that come the determinant and the inverse of a small matrix, and systems of equations written with a matrix. Then three connected ideas, each built on the one before: Gaussian elimination (the method that solves a system of any size), the rank of a matrix (how many of its rows carry information of their own), and the inverse of a 3 x 3 matrix (found with the same elimination). The last ideas are matrices as transformations of the plane and the places where matrices are used. Every idea comes as a definition and worked examples calculated by hand. Then it walks through 26 solved problems from the easiest to the hardest, and at the end there are 41 practice problems with a hint and a full solution for each.
You need the last lesson: vectors, their addition, and above all the dot product. Matrix multiplication is nothing but many dot products. As in the whole module, the work is done by hand; Python appears only in one short part near the end.
What you will learn
- 1.What a matrix is, its size, its entries, and the special matrices (zero, square, identity, diagonal, symmetric, triangular)
- 2.Adding and subtracting matrices, multiplying a matrix by a number, and the transpose
- 3.A matrix times a vector, read in two ways: dot products with the rows, and a combination of the columns
- 4.Matrix multiplication: the rule, when two matrices can be multiplied, and the size of the result
- 5.The rules of matrix multiplication: what stays true from ordinary numbers and what does not (the order matters)
- 6.The identity matrix, powers of a matrix, the determinant and the inverse of a 2 x 2 matrix, the determinant of a 3 x 3 matrix
- 7.Systems of linear equations written as , and solved with the inverse
- 8.Gaussian elimination: the augmented matrix, the three row operations, the echelon form and back substitution
- 9.The rank of a matrix, linear independence, and what the rank says about the solutions of a system
- 10.The inverse of a 3 x 3 matrix, found with row operations
- 11.Matrices as transformations: stretching, rotating, and doing one after the other
- 12.Where matrices are used: a table of data, predictions for many rows at once, a layer of a neural network
| Written | Meaning |
|---|---|
| , , | matrices (capital letters) |
| , , | vectors (small letters, printed in bold), written as columns when a matrix multiplies them |
| the size of a matrix: 2 rows and 3 columns | |
| the entry of in row 1 and column 2 | |
| the transpose of | |
| the identity matrix | |
| the determinant of | |
| the inverse of | |
| the rank of | |
| a row operation: row 2 minus 3 times row 1 | |
| in a calculation | ordinary multiplication of two numbers, as in the last lesson |
A matrix is written with its rows under each other:
1What is a matrix?
1.1From vectors to a matrix
In the last lesson one employee was a vector of four numbers. Three employees are three vectors, and written under each other they form a table of numbers:
| age | years | salary | score |
|---|---|---|---|
| 29 | 4 | 1710 | 3.8 |
| 28 | 0 | 4670 | 3.6 |
| 37 | 2 | 1870 | 3.2 |
A rectangular table of numbers is called a matrix. It is written without the headings, between two brackets:
Every row is one employee (a vector with 4 components), and every column is one feature (a vector with 3 components). This is exactly how a table of data is given to a model: one row for every example, one column for every feature. The whole of machine learning calculates with such matrices.
1.2Size and entries
- has 2 rows (horizontal) and 3 columns (vertical). Its size is , read "2 by 3". The rows are always named first.
- The numbers are the entries. The entry in row and column is written : first the row, then the column.
- The rows of are the vectors and . Its columns are the vectors , and .
- Two matrices are equal only when they have the same size and every entry is equal.
A vector is a special matrix: a column vector with components is a matrix of the size , and a row vector is a matrix of the size . From this lesson on, a vector that is multiplied by a matrix is a column.
Inside a sentence a column would take three lines, so there the vector is still written as , as in the last lesson.
1.3Special matrices
| Name | What it is | Example |
|---|---|---|
| Zero matrix | every entry is 0 | |
| Square matrix | as many rows as columns () | any or matrix |
| Main diagonal | the entries of a square matrix | from the top left to the bottom right |
| Diagonal matrix | square, and 0 everywhere outside the main diagonal | |
| Identity matrix | diagonal, with 1 on the main diagonal | |
| Symmetric matrix | square, and the same when rows and columns are swapped | |
| Upper triangular matrix | square, and 0 everywhere below the main diagonal | |
| Lower triangular matrix | square, and 0 everywhere above the main diagonal |
is the 3 x 3 identity matrix, is a diagonal matrix, and is symmetric: it is a mirror image of itself across the main diagonal (, , ).
Try it yourself
Show a hint
Show the solution
Answer: The size is , and .
has 3 rows and 4 columns, so its size is . Row 2 is , and its third entry is . Row 3 is , and its first entry is .
The two entries are not the same: and would be different places too. The first number is always the row.
2Adding, subtracting and scaling matrices
2.1Addition and subtraction
Two matrices of the same size are added entry by entry, exactly like vectors:
A matrix and a matrix cannot be added: some entries would have no partner.
In data terms: if holds the sales of every product (columns) in every shop (rows) in January and the same for February, then holds the sales of both months.
2.2Multiplying by a number
A matrix is multiplied by a number (a scalar) by multiplying every entry:
2.3The rules
For matrices of the same size, and numbers and :
| Rule | In symbols |
|---|---|
| The order of a sum does not matter | |
| Grouping does not matter | |
| Adding the zero matrix changes nothing | |
| A number goes into a sum | |
| A sum of numbers can be split |
They are the rules of vectors, and for the same reason: everything is done entry by entry with ordinary numbers. So matrix equations with only sums and numbers are solved like ordinary equations.
Example. Find the matrix with , for and .
2.4The transpose
The transpose of a matrix, written , turns its rows into columns: the first row becomes the first column, the second row the second column.
- A matrix becomes a matrix. In general becomes .
- The entry in row , column of is the entry in row , column of .
- Transposing twice gives the matrix back: .
- , and .
- A matrix is symmetric exactly when .
- The transpose of a column vector is a row vector.
With the transpose, the dot product of the last lesson can be written as a matrix product: (a row times a column). You will see this notation in every machine learning text.
Try it yourself
Show a hint
Show the solution
Answer: and .
The entry in row 2, column 1 of is : subtracting a negative number adds.
3A matrix times a vector
3.1The rule
A matrix of the size can be multiplied by a vector with components. The result is a vector with components: each component is the dot product of one row of with .
The first row gives , the second row gives .
The sizes must fit: the number of columns of the matrix must equal the number of components of the vector, otherwise the dot products are not possible. The result has as many components as the matrix has rows.
Example with a 2 x 3 matrix.
A vector with 3 components went in, a vector with 2 components came out.
3.2A second way to read it: a combination of the columns
The same result can be read by columns. is the linear combination of the columns of , with the components of as the numbers:
Both readings give . The row reading is the one you calculate with. The column reading explains what the result can be: every is a mixture of the columns of .
A picture of the column reading needs smaller numbers. For and the product is 1 time the first column plus 2 times the second, . It is the linear combination of the last lesson, with the columns of the matrix as the vectors.
3.3What it is used for
Many dot products at once. A cafe sold, on two days, these numbers of teas, coffees and cakes, and the prices are 1.5, 3.0 and 4.5:
One multiplication gives the money of every day. With 365 rows it would give the money of every day of the year.
The identity matrix changes nothing: for every vector .
Try it yourself
Show a hint
Show the solution
Answer: .
Both readings agree, as they always must.
4Matrix multiplication
4.1The rule
The product of two matrices is built from dot products:
the entry in row and column of is the dot product of row of with column of .
Entry by entry:
| Entry | Row of | Column of | Dot product |
|---|---|---|---|
| row 1, column 1 | |||
| row 1, column 2 | |||
| row 2, column 1 | |||
| row 2, column 2 |
A way to remember it: go along a row of the first matrix and down a column of the second.
Another way to see the same rule: every column of is times the matching column of . So matrix multiplication is the matrix-times-vector rule of Part 3, done for every column.
Press an entry of the product: the row and the column it comes from are marked.
Sizes: (2 × 2)(2 × 2) gives 2 × 2.
Row 1 of A times column 1 of B: 1 × 5 + 2 × 7 = 19
Compare A B with B A, and C D with D C: the same two matrices, another order, another result.
4.2When is the product possible? The sizes
A row of and a column of must have the same number of components. So:
exists only when the number of columns of equals the number of rows of .
The result has the rows of the first matrix and the columns of the second: the two outer numbers. The two inner numbers must match and then disappear.
| not possible (3 is not 2) | ||
| (a matrix times a vector) |
Always check the sizes before you calculate, and write down the size of the result. It is the best protection against mistakes.
Choose the sizes of A and B and see whether the product A B exists.
A has m rows and n columns
B has p rows and q columns
(2 × 3)(3 × 2): the inner numbers are 3 and 3.
They are equal, so A B exists. Its size is the two outer numbers: 2 × 2.
In the other order, B A is (3 × 2)(2 × 3): it exists and is 3 × 3.
4.3An example with different sizes
is and is : the inner numbers are both 3, so exists and is . Every entry is a dot product of two vectors with 3 components:
In the other order, is : it exists too, but it is a matrix.
The first entry is row 1 of times column 1 of : . and do not even have the same size.
4.4The order matters
With numbers, . With matrices this is not true. Take and from above and multiply in the other order:
but . In general
Matrix multiplication is not commutative. This is the most important difference from ordinary numbers, and the source of most mistakes. Never change the order of the matrices in a product.
4.5What stays true, and what does not
| Rule | In symbols | True? |
|---|---|---|
| Grouping does not matter | yes | |
| A product goes into a sum | and | yes |
| A number can be moved to the front | yes | |
| The order can be changed | no, in general | |
| A product is zero only when a factor is zero | means or | no |
| A matrix can be cancelled | means | no, in general |
A product can be zero although no factor is zero:
4.6The identity matrix
For every matrix whose sizes fit:
The identity matrix plays the role of the number 1. Check with :
4.7Powers of a matrix
A square matrix can be multiplied by itself: , .
Careful: is not the matrix of the squared entries ( is wrong).
4.8The transpose of a product
The order turns around. With the matrices from above: , so , and
Try it yourself
Show a hint
Show the solution
Answer: and .
Not one of the four entries is the same in both products.
5The determinant and the inverse
5.1Dividing by a matrix?
With numbers, the equation is solved by multiplying with the inverse of 3: . The number is the inverse of 3 because .
For a square matrix , the inverse is the matrix with
Not every matrix has an inverse, in the same way as the number 0 has none. Whether a matrix has one is decided by a single number, its determinant.
5.2The determinant of a 2 x 2 matrix
Multiply the main diagonal, multiply the other diagonal, subtract.
- If is not 0, the matrix has an inverse. It is called invertible.
- If , the matrix has no inverse. It is called singular.
In the second matrix the rows are parallel vectors: . That is what a determinant of 0 means for a 2 x 2 matrix: one row is a multiple of the other (and the same holds for the columns).
Two useful facts: , and .
5.3The inverse of a 2 x 2 matrix
Swap the two entries of the main diagonal, change the sign of the other two, divide by the determinant.
Example. .
Always check an inverse by multiplying. The first entry of the product is , and the others are found in the same way:
Rules for inverses: , and (the order turns around, as with the transpose).
For larger matrices the inverse exists under the same condition (the determinant is not 0). The formula above is only for 2 x 2 matrices; Part 9 shows the method for a 3 x 3 matrix.
5.4The determinant of a 3 x 3 matrix
For a 3 x 3 matrix, go along the first row. Each entry is multiplied by the determinant of the 2 x 2 matrix that is left when its row and its column are crossed out, and the signs go :
Example.
The determinant is 1, not 0, so this matrix is invertible.
Try it yourself
Show a hint
Show the solution
Answer: and .
The product is the identity matrix, so the inverse is right. The entries can also be typed as fractions: is 3/2.
6Systems of equations
6.1A system as one matrix equation
Two equations with two unknowns:
Put the numbers in front of the unknowns into a matrix, the unknowns into a vector and the right sides into a vector:
By the rule of Part 3, is the vector . So the whole system is one short equation:
6.2Solving with the inverse
Multiply both sides from the left by :
The inverse of this was found in Part 5:
So and . Check in the equations: and .
Notice the order: , not . The inverse stands on the left because it was multiplied from the left.
6.3When is there exactly one solution?
- If is not 0, the system has exactly one solution, .
- If , the two equations say the same thing or contradict each other: the system has no solution, or infinitely many.
Here the second equation is half of the first: infinitely many solutions.
Here the left sides are parallel, the right sides are not: no solution.
Both systems have the matrix with the determinant 0.
exactly one solution
2x + y = 5 and x + 3y = 10 cross in the point (1, 3)
no solution
4x + 6y = 10 and 2x + 3y = 7 are parallel and never meet
infinitely many solutions
4x + 6y = 10 and 2x + 3y = 5 are the same line
This is why linear algebra matters for learning from data: finding the weights of a linear regression means solving a system of equations, with one unknown for every weight.
The inverse formula is known so far only for 2 x 2 matrices. The next three parts go further: a method for systems of any size, a number that tells how much information a matrix holds, and the inverse of a 3 x 3 matrix.
Try it yourself
Show a hint
Show the solution
Answer: and .
Check in the equations: and .
7Gaussian elimination
7.1A method for every system
The inverse formula of Part 5 works for a 2 x 2 matrix. Real systems have many unknowns: three, ten, a thousand. Gaussian elimination is the method that solves them all, and it is the method a computer uses. Its idea is the one you know from school: remove an unknown from an equation by subtracting a multiple of another equation.
Subtract 3 times the first equation from the second. The unknown disappears:
Now the first equation gives , so .
7.2The augmented matrix
In this calculation only the numbers matter. So write the matrix and the right side next to each other, with a line between them. This is the augmented matrix:
Each row is one equation.
7.3The three row operations
| Operation | Written | With equations |
|---|---|---|
| Swap two rows | write the equations in another order | |
| Multiply a row by a number that is not 0 | multiply both sides of an equation | |
| Add a multiple of one row to another row | subtract 3 times equation 1 from equation 2 |
means: row 2 is replaced by row 2 minus 3 times row 1. Row 1 itself stays as it is. None of the three operations changes the solutions of the system, because each of them is an allowed step with equations.
The example from above:
The new row 2, entry by entry: , , . The operation is done on the whole row, the right side included.
7.4Echelon form and back substitution
The first entry of a row that is not 0 is called its pivot. The goal of elimination is the echelon form, a staircase:
- below every pivot there are only zeros,
- every pivot stands to the right of the pivot in the row above,
- rows of only zeros, if there are any, are at the bottom.
A system in echelon form is solved from the bottom to the top. This is called back substitution: the last row has one unknown; put its value into the row above, and so on.
7.5Three unknowns
Step 1: zeros below the first pivot. The first pivot is the 1 in row 1.
Step 2: zeros below the second pivot. The second pivot is the 1 in row 2. Row 1 is not touched any more.
This is the echelon form. The pivots are 1, 1 and 3.
Step 3: back substitution.
The solution is , , . Check in the three equations: , , .
Gaussian elimination of the example, one step at a time. The row that is changed or read is filled.
The augmented matrix
One row for every equation: x + y + z = 6, 2x + 3y + z = 11, x + 2y + 3z = 14. The first pivot is the 1 at the top left.
7.6The recipe
- Write the augmented matrix.
- Use the first row to make zeros below the first pivot.
- Use the second row to make zeros below the second pivot, and so on. A row that was already used is not changed again.
- If the place of a pivot holds a 0, swap the row with a row below it that has a number there.
- Solve from the last row upwards.
- Put the solution into the original equations.
A swap, when the first pivot is missing:
Now , so , and then .
7.7No solution, or infinitely many
Elimination also shows when a system has no single solution. Take the two systems of Part 6 in a smaller form:
The last row says : it is always true and tells nothing. One equation is left for two unknowns, so there are infinitely many solutions: choose any , then .
The last row says , which is never true: there is no solution.
| The echelon form has | The system has |
|---|---|
| a pivot for every unknown, and no row | exactly one solution |
| a row with zeros on the left and a number that is not 0 on the right | no solution |
| fewer pivots than unknowns, and no row | infinitely many solutions |
7.8The determinant from the echelon form
Adding a multiple of one row to another row does not change the determinant, and the determinant of a triangular matrix is the product of the entries on its main diagonal. In the example with three unknowns only such operations were used, so
Check along the first row, as in Part 5: . (The other two operations do change the determinant: a swap of two rows changes its sign, and multiplying a row by a number multiplies the determinant by that number.) For large matrices a computer finds the determinant in this way.
Try it yourself
Show a hint
Show the solution
Answer: , , .
Check in the original equations: , and .
8The rank of a matrix
8.1Rows that say nothing new
In the system , the second equation repeats the first, and elimination turned it into a row of zeros. The rank counts the rows that are left.
The rank of a matrix is the number of pivots in its echelon form, which is the number of rows that are not zero rows. The rank belongs to the matrix alone, without a right side.
Two pivots: rank 2.
One pivot: rank 1.
A 3 x 3 example:
Two pivots, so . The reason can be seen in itself: the third row is the sum of the first two, . It brings no new information.
8.2Linear independence
Vectors are called linearly dependent when one of them is a linear combination of the others. When none of them is, they are linearly independent.
- The rows and are dependent: the second is 2 times the first.
- The rows and are independent: neither is a multiple of the other.
- The three rows of are dependent, but any two of them are independent.
The rank is the largest number of linearly independent rows. A remarkable fact: the columns give the same number, . In the columns are dependent too: the third column is the second minus the first, .
8.3What the rank can be
- The rank is never larger than the number of rows, and never larger than the number of columns. A matrix has at most the rank 2.
- Only the zero matrix has the rank 0.
- A matrix with the largest rank its size allows has full rank.
Two pivots: rank 2, full rank. (The second pivot stands in the third column. That is allowed: a pivot only has to be to the right of the pivot above.)
For a square matrix with rows, the following statements say the same thing. If one is true, all are true:
| Statement | For | For |
|---|---|---|
| the rank is (full rank) | rank 2: yes | rank 1: no |
| the determinant is not 0 | : yes | : no |
| the inverse exists | yes | no |
| the rows are linearly independent | yes | no |
| has exactly one solution for every | yes | no |
8.4Rank and the number of solutions
Compare the rank of with the rank of the augmented matrix:
| Ranks | The system has |
|---|---|
| both ranks equal the number of unknowns | exactly one solution |
| both ranks are equal, but smaller than the number of unknowns | infinitely many solutions |
| the augmented matrix has a larger rank than | no solution |
In Part 7 the system with the last row had the rank 1 on both sides, with 2 unknowns: infinitely many solutions. The system with the last row had the rank 1 for and 2 for the augmented matrix: no solution.
8.5Rank in data
In a table of data a column can be a combination of other columns: a price in manat next to the same price in dollars, or a column total next to the two columns it is the sum of. Such a column carries no new information, and the matrix of the data does not have full rank. A linear model cannot decide how to share the weight between such columns. In machine learning this is called multicollinearity, and you will meet it in linear regression (Topic 22). Before training, such columns are removed.
Try it yourself
Show a hint
Show the solution
Answer: , so has no inverse.
The pivots are the 1 in row 1 and the 1 in row 2 (in the third column): two pivots, rank 2. A 3 x 3 matrix needs the rank 3 for an inverse. The reason is in the rows: the third row is the sum of the first two, .
9The inverse of a 3 x 3 matrix
9.1The idea: eliminate with the identity matrix next to it
The formula of Part 5 is for 2 x 2 matrices only. For larger matrices the inverse is found with row operations:
write and the identity matrix next to each other, and use row operations until the left half is the identity matrix. Then the right half is the inverse.
This is elimination carried further. First make zeros below the pivots, as in Part 7. Then make every pivot equal to 1 and make zeros above the pivots too. The method is called Gauss-Jordan elimination.
Why it works: the right half records every operation that is done. The operations that turn into are together a multiplication by , and the same operations turn into .
9.2First with a 2 x 2 matrix
For the formula gives (it is one of the practice problems). The new method:
The left half is , and the right half is the same inverse.
9.3A 3 x 3 matrix
Take the matrix of Part 5 with the determinant 1:
Zeros below the pivots.
All three pivots are already 1. Zeros above the pivots, starting with the last column.
Check with . The first row of the product:
The second row gives and the third in the same way.
9.4When there is no inverse
If a row of zeros appears in the left half, the rank of is smaller than 3 and the left half can never become : the matrix has no inverse. Stop there. The matrix of Part 8 is such a matrix.
9.5Solving a system with it
The matrix of this system is the matrix that was just inverted, so
Check in the first equation: .
For one system, elimination (Part 7) is less work than finding the inverse. The inverse pays off when the same matrix is used with many different right sides.
Try it yourself
Show a hint
Show the solution
Answer: .
Check with the third row of times the columns of the inverse: , and .
10Matrices as transformations
10.1A matrix moves every point
Multiplying by a matrix turns a vector into another vector . Do this with every point of the plane, and the matrix transforms the whole plane. Three simple matrices show what can happen.
Stretching. doubles the first component and keeps the second:
Rotating by 90 degrees (to the left). :
The dot product of and is : the vector really turned by a right angle, and its length did not change.
The columns tell the story. is the first column of , and is the second column. So the columns of a matrix are the places where the two standard unit vectors land. To understand a 2 x 2 matrix, look at its columns.
10.2The determinant is the change of area
The unit square has the area 1. After the transformation by it becomes a parallelogram whose sides are the columns of . Its area is the size of .
The stretch has the determinant 2 (areas double), the rotation has the determinant 1 (areas stay). And a determinant of 0 now has a picture: the square is squeezed flat onto a line, with the area 0. A flattened plane cannot be unfolded again, which is why such a matrix has no inverse.
Choose a matrix, or move the sliders, and watch what it does to the unit square.
(1, 0) lands on the first column (2, 1),
(0, 1) lands on the second column (1, 2).
det(A) = 2 × 2 − 1 × 1 = 3
Every area is multiplied by 3. The matrix has an inverse.
10.3One transformation after another
First rotate with , then stretch with : the vector becomes , and then . Because grouping does not matter, this is :
doing one transformation after another is multiplying their matrices. The matrix that acts first stands on the right.
Step by step it is the same: , then .
In the other order, first stretch and then rotate, the matrix is , and : a different result. Here is the reason why is not : stretching a rotated shape is not the same as rotating a stretched shape.
first rotate, then stretch: S R
x → R x = (−1, 2) → S (R x) = (−2, 2)
first stretch, then rotate: R S
x → S x = (4, 1) → R (S x) = (−1, 4)
The inverse matrix is the transformation that undoes the first one: .
Try it yourself
Show a hint
Show the solution
Answer: lands on , and areas are multiplied by 2.
The columns show what this matrix does: lands on and lands on . Both are turned to the left by half a right angle and made longer, so the unit square becomes a larger, turned square with the area 2.
11Where matrices are used
11.1A table of data
A table with one row for every example and one column for every feature is a matrix, usually called . Three flats with the features area, rooms and age:
11.2Predictions for all rows at once
In the last lesson one prediction was . With the matrix , the predictions for all flats are one matrix-times-vector product:
With the weights and the bias 20000, the first row gives , and the other rows work in the same way:
The first number is the prediction of the last lesson. A model with a million rows does the same calculation; only the matrix is longer. This is linear regression (Topic 22).
11.3A layer of a neural network
A layer of a neural network has several neurons, and every neuron has its own vector of weights. Put these weight vectors into the rows of a matrix . Then the whole layer is
Two neurons, two dot products, one matrix product. A deep network is a chain of such layers: matrix after matrix, each followed by a simple function. That is why graphics cards, which are built to multiply matrices quickly, train neural networks.
11.4More places
| Where | The matrix |
|---|---|
| An image in grey tones | the brightness of every pixel, rows and columns as on the screen |
| Word embeddings | one row for every word |
| Recommendation systems | users in the rows, films in the columns, ratings as entries |
| Principal Component Analysis | a matrix that turns the data into its most useful directions |
Try it yourself
Show a hint
Show the solution
Answer: The predictions are .
Three students, three dot products, one product. The matrix has one row for every student and one column for every feature.
12Common mistakes
Multiplying entry by entry. is not . Every entry of a product is a dot product of a row and a column.
Changing the order. and are different matrices. In the inverse must stand on the left.
Not checking the sizes. does not exist. The inner numbers must be equal.
Mixing up rows and columns. The size names the rows first: is 2 rows and 3 columns. is row 1, column 2.
Squaring the entries. means , not the matrix of the squared entries.
The wrong sign in the determinant. : main diagonal first, minus the other diagonal.
Forgetting to divide by the determinant. The inverse is the swapped and signed matrix divided by .
Inverting a singular matrix. If the determinant is 0 there is no inverse. Calculate the determinant first.
Cancelling a matrix. From it does not follow that , and from it does not follow that one of them is zero.
Keeping the order in a transpose or an inverse of a product. and : the order turns around.
Adding a number to a matrix. is not defined in mathematics. (NumPy adds 3 to every entry; that is a convenience of NumPy.)
Changing only the left side of a row. A row operation is done on the whole row of the augmented matrix. If the right side is forgotten, the system is a different one.
Changing the wrong row. changes row 2. Row 1 stays exactly as it was.
Counting rows instead of pivots. The rank is not the number of rows. Bring the matrix to echelon form first, then count the rows that are not zero rows.
Using the 2 x 2 formula for a 3 x 3 inverse. Swapping and changing signs works only for 2 x 2 matrices. A 3 x 3 inverse is found with row operations on next to .
| Mistake | What is right |
|---|---|
| entry by entry | row of times column of , a dot product for every entry |
| the order matters | |
| the inner numbers must be equal | |
| as squared entries | |
| the inverse without | divide every entry by the determinant |
| a row operation without the right side | the whole row changes, the right side too |
| rank = number of rows | rank = number of pivots in the echelon form |
13Solved problems (from the easiest to the hardest)
Each problem has the question, the idea behind it with the part of the lesson it uses, and the calculation step by step. Cover the solution and try first.
Level 1: The basic operations
Problem 1: Size and entries
For the matrix give the size, the entries and , the second row and the third column.
Idea. Rows are counted first, then columns (Part 1). In the first number is the row.
Why: a row of a matrix has 3 components and a column has 2. Mixing up and is the classic slip; here does not even exist, because there is no third row.
Problem 2: Add and subtract
For and find and .
Idea. Same size, so entry by entry (Part 2).
Why: the entry in row 2, column 1 of is . Check a subtraction by adding back: must give .
Problem 3: A combination of two matrices
For and find .
Idea. Scale each matrix first, then subtract (Part 2).
Why: it is the linear combination of the last lesson, now with matrices. Writing and down first keeps the signs safe.
Problem 4: Transpose, and is it symmetric?
Find the transpose of . Then decide whether is symmetric.
Idea. The transpose turns rows into columns. A matrix is symmetric when it equals its transpose (Part 2).
All three pairs are equal, so : the matrix is symmetric.
Why: for a symmetric matrix it is enough to compare the entries that are mirror images across the main diagonal. A matrix that is not square, like , can never be symmetric.
Problem 5: A matrix times a vector
Find for and .
Idea. Each component of the result is the dot product of one row with (Part 3).
Why: the two readings of Part 3 must give the same vector. The column reading is a cheap second calculation and a good check.
Problem 6: The first product
Find for and .
Idea. Row of times column of for every entry (Part 4). Both are , so the product is .
Why: write every entry as a sum of products before adding, as with the dot product. The zero in makes the first row easy: it is just 2 times the first row of .
Problem 7: Does the product exist?
is , is , is . Which of the products , , , exist, and what are their sizes?
Idea. Write the sizes next to each other. The inner numbers must be equal; the outer numbers are the size of the result (Part 4).
In and the inner numbers are equal (3 and 3, 2 and 2). In they are 4 and 2, in they are 3 and 2.
Why: exists but does not. So the question "is ?" often has no meaning at all: one side may not exist.
Level 2: Products, determinants, inverses and elimination
Problem 8: Different sizes
Find for and .
Idea. gives a matrix. Each entry is a dot product of two vectors with 3 components (Part 4).
Why: the size check told us before any calculation that the answer has 4 entries, each a sum of 3 products.
Problem 9: The order matters
For and find and .
Idea. Two separate products (Part 4).
Why: the two results are different matrices. There is no shortcut from one to the other: when you need , you must calculate it.
Problem 10: The square of a matrix
Find for .
Idea. , a matrix product (Part 4).
Why: the matrix of the squared entries would be : the entry 5 shows that it is wrong. Only the diagonal of this triangular matrix is squared.
Problem 11: Is it invertible?
Find the determinants of and . Which of them has an inverse?
Idea. . A matrix has an inverse exactly when its determinant is not 0 (Part 5).
is not 0, so is invertible. , so is singular and has no inverse.
Why: in the first row is 2 times the second, . Parallel rows always give the determinant 0.
Problem 12: An inverse, with its check
Find the inverse of and check it.
Idea. Swap the diagonal entries, change the sign of the other two, divide by the determinant (Part 5).
The check:
Why: the check takes one minute and catches every mistake: a wrong sign, a forgotten division, two swapped entries. Never give an inverse without it.
Problem 13: A system of equations
Solve the system , with the inverse matrix.
Idea. Write it as . Then (Part 6).
So and . Check: and .
Why: the factor was kept outside until the end, so all the work was done with whole numbers. Putting the solution back into both equations is the final check.
Problem 14: When is the matrix singular?
For which number has the matrix no inverse?
Idea. No inverse means the determinant is 0 (Part 5). Write the determinant with the unknown and solve.
Check: the matrix is then , and : the rows are parallel.
Why: as with perpendicular vectors in the last lesson, a geometric condition became one small equation. For every other the matrix is invertible.
Problem 15: Sales of a week
A stall sells apples and pears. On three days it sold , and kilograms (apples, pears). A kilogram of apples costs 10, a kilogram of pears 20. Write the sales as a matrix and find the money of every day with one product.
Idea. One row for every day, one column for every fruit; the prices are a vector. The product gives one dot product for every row (Part 3).
Why: is and has 2 components, so the result has 3 components: one for every day. The order of the columns (apples, pears) must match the order of the prices.
Problem 16: Elimination with two unknowns
Solve the system , with Gaussian elimination.
Idea. Write the augmented matrix, make a zero below the first pivot, then solve from the bottom (Part 7).
Check: and .
Why: the new row 2 is , , : the right side is changed together with the left. The check uses the original equations, not the changed ones.
Problem 17: The rank of two matrices
Find the rank of and of .
Idea. Bring each matrix to echelon form and count the pivots (Part 8).
has one pivot, so . has two pivots, so .
Why: in the second row is 2 times the first, so the rows are linearly dependent and . In they are independent, , and has an inverse.
Problem 18: How many solutions?
Without solving completely, decide how many solutions each system has. System (a): , . System (b): , .
Idea. Eliminate and read the last row (Part 7).
In (a) the last row says : infinitely many solutions. In (b) it says : no solution.
Why: both systems have the same matrix, with the rank 1. In (a) the augmented matrix also has the rank 1, which is less than the 2 unknowns. In (b) the augmented matrix has the rank 2, larger than the rank of the matrix (Part 8).
Level 3: Putting ideas together
Problem 19: A 3 x 3 determinant
Find the determinant of .
Idea. Go along the first row with the signs ; each entry is multiplied by the 2 x 2 determinant that is left when its row and column are crossed out (Part 5).
Why: the zero in the first row saved one of the three small determinants. When a row has zeros, the calculation along that row is shorter.
Problem 20: The transpose of a product
For and check that .
Idea. Calculate both sides separately (Part 4). was found in Problem 9.
Why: the order turns around. would be , a different matrix: it is the transpose of , not of .
Problem 21: Rotate, then stretch
The vector is first rotated by 90 degrees to the left with and then stretched with . Find the result step by step, then find the single matrix that does both.
Idea. A transformation is a matrix times a vector. One after the other is the product of the matrices, with the first one on the right (Part 10).
Both at once:
Why: the matrix that acts first, , stands next to the vector, on the right. Reading from right to left gives the order of the actions.
Problem 22: Predictions for a whole table
A model predicts the price of a flat with the weights for (area, rooms, age) and the bias 20000. Find the predictions for the three flats , and with one matrix product.
Idea. The flats are the rows of a matrix . Then holds one dot product for every flat, and the bias is added to each (Part 11).
Why: is here only by chance. With 1000 flats it would be , and would have 1000 components: the number of rows is the number of predictions.
Problem 23: A matrix equation
Find the matrix with , for and .
Idea. Multiply both sides from the left by : (Parts 5 and 6).
The check:
Why: the inverse must be multiplied from the left on both sides, because stands on the left of . would be a different matrix and a wrong answer.
Problem 24: Three equations, three unknowns
Solve the system , , with Gaussian elimination.
Idea. Zeros below the first pivot, then below the second, then back substitution (Part 7).
Check: , and .
Why: the work is always the same three steps, whatever the numbers are. That is why this method can be given to a computer, for three unknowns or for three thousand.
Problem 25: The rank of a 3 x 3 matrix
Find the rank of . Does it have an inverse?
Idea. Echelon form, then count the pivots. A square matrix has an inverse only with full rank (Part 8).
Two pivots: , smaller than 3, so there is no inverse. The check with the determinant:
Why: the third row is the sum of the first two, . Rank smaller than 3, determinant 0 and no inverse are three ways to say the same thing.
Problem 26: The inverse of a 3 x 3 matrix
Find the inverse of and check it.
Idea. Write next to and use row operations until the left half is (Part 9).
The check, row 1 and row 3 of times the columns of :
Why: first the zeros below the pivots, then the zeros above them, always with whole rows of six numbers. The second row of the check gives in the same way.
14Practice problems
Forty-one problems in three levels. Solve each one on paper, type your result into the answer boxes and press Check. Under every problem there is a hint and a full solution: look at the hint only when you are stuck, and at the solution only after you have an answer of your own.
Level 1: The basic operations
- 1.Give the size of and its entry .
Show a hint
Rows first. In the row is 2 and the column is 1.Show one possible solution
Answer: The size is , and .
The matrix has 2 rows and 3 columns, so its size is . Row 2 is , and its first entry is 4.
- 2.Find for and .
Show a hint
Add entry by entry.Show one possible solution
Answer: .
- 3.Find for the same and .
Show a hint
Subtract entry by entry; subtracting a negative number adds.Show one possible solution
Answer: .
- 4.Find for .
Show a hint
Multiply every entry by 3.Show one possible solution
Answer: .
- 5.Find for and .
Show a hint
First , then add .Show one possible solution
Answer: .
- 6.Find the transpose of .
Show a hint
The rows become the columns.Show one possible solution
Answer: .
The first row becomes the first column and the second row the second column. The matrix becomes :
- 7.Find for and .
Show a hint
One dot product for every row.Show one possible solution
Answer: .
- 8.Find for and .
Show a hint
The matrix is , so the result has 2 components.Show one possible solution
Answer: .
- 9.Find for and .
Show a hint
Row of times column of .Show one possible solution
Answer: .
- 10. is . is . is . Give the size of , and say whether exists.
Show a hint
Compare the inner numbers.Show one possible solution
Answer: is . does not exist.
For the inner numbers are 3 and 3: equal, so the product exists and has the two outer numbers as its size. For they are 3 and 2, so there is no product.
- 11.Find the determinant of .
Show a hint
.Show one possible solution
Answer: .
- 12.Which of the matrices and is symmetric?
Show a hint
Compare the two entries outside the main diagonal.Show one possible solution
Answer: is symmetric, is not.
In the entries and are both 7, so . In they are 2 and 3, so is a different matrix.
Level 2: Products, determinants, inverses and elimination
- 13.Find and for and .
Show a hint
Two separate products. Do not expect the same result.Show one possible solution
Answer: and .
- 14.Find for and .
Show a hint
gives a matrix.Show one possible solution
Answer: .
- 15.Find for .
Show a hint
, not the squared entries.Show one possible solution
Answer: .
The matrix of the squared entries would have a 1 at the top right, not a 5.
- 16.Find the inverse of .
Show a hint
The determinant is 1, so nothing has to be divided.Show one possible solution
Answer: .
Swap 2 and 3, change the signs of 1 and 5. Check with the first row of : and .
- 17.Find the inverse of .
Show a hint
The determinant is negative. Divide every entry by it.Show one possible solution
Answer: .
Check with the first row of : and .
- 18.For which is the matrix singular?
Show a hint
Set the determinant equal to 0.Show one possible solution
Answer: .
Then the rows are and , and the second is twice the first.
- 19.Solve the system , with the inverse matrix.
Show a hint
has the determinant 1.Show one possible solution
Answer: and .
Check: and .
- 20.Find and with .
Show a hint
Two matrices are equal when every entry is equal.Show one possible solution
Answer: and .
The other two entries fit: and .
- 21.Find for and as a combination of the columns of .
Show a hint
is 2 times the first column plus times the second.Show one possible solution
Answer: .
The row reading gives the same: and .
- 22.A cinema sold, on two evenings, and tickets (adults, children). An adult ticket costs 8 and a child ticket 5. Find the money of each evening with one matrix-times-vector product.
Show a hint
The evenings are the rows of the matrix, the prices are the vector.Show one possible solution
Answer: The money of the two evenings is .
- 23.For find . What do you notice about the result?
Show a hint
First write , then multiply.Show one possible solution
Answer: . It is symmetric.
The entries outside the diagonal are equal. is symmetric for every matrix .
- 24.For and find , and , and check that .
Show a hint
First the product , then its determinant.Show one possible solution
Answer: , and .
Indeed .
- 25.Solve the system , with Gaussian elimination.
Show a hint
Subtract 3 times the first row from the second.Show one possible solution
Answer: and .
Row 2 says . Row 1 gives . Check: .
- 26.Find the rank of and of .
Show a hint
Bring each to echelon form with and count the pivots.Show one possible solution
Answer: and .
In one pivot is left. In there are two pivots.
- 27.How many solutions has the system , ?
Show a hint
Eliminate and read the last row.Show one possible solution
Answer: No solution.
The last row says . That is never true. The left sides are parallel, but the right sides do not fit: twice the first equation would be .
Level 3: Putting ideas together
- 28.Find the determinant of the 3 x 3 matrix .
Show a hint
Go along the first row with the signs . The third entry is 0.Show one possible solution
Answer: The determinant is .
- 29.Solve the system , with the inverse matrix.
Show a hint
The determinant is .Show one possible solution
Answer: and .
Check: and .
- 30.Find the matrix with for and .
Show a hint
, with the inverse on the left.Show one possible solution
Answer: .
Check:
- 31.Rotate the points and by 90 degrees to the left with .
Show a hint
Multiply by each point.Show one possible solution
Answer: goes to , and goes to .
Each result is perpendicular to its point: the dot products are 0.
- 32.A vector is first stretched with and then rotated with . Find the single matrix that does both, and apply it to .
Show a hint
The matrix that acts first stands on the right: the product is .Show one possible solution
Answer: , and the result is .
Step by step: , then .
- 33.The unit square is transformed by . What is the area of the parallelogram it becomes?
Show a hint
The area is the size of the determinant.Show one possible solution
Answer: The area is 5.
The unit square has the area 1, so the parallelogram has the area 5.
- 34.A model predicts with . Find the predictions for , and the bias 2.
Show a hint
One dot product for every row, then add 2 to each.Show one possible solution
Answer: The predictions are .
- 35.A layer of a neural network calculates . Find the output for , and .
Show a hint
First , then add the vector .Show one possible solution
Answer: The output is .
- 36.For and find in two ways: by inverting the product, and as .
Show a hint
. The inverses of and are easy to find.Show one possible solution
Answer: both ways.
The other way:
- 37.Find for . Can you guess ?
Show a hint
First , then .Show one possible solution
Answer: , and .
Every multiplication by adds 1 to the entry in the corner, so .
- 38.Solve with Gaussian elimination: , , .
Show a hint
and first, then .Show one possible solution
Answer: , , .
Back substitution: , then gives , then . Check in the third equation: .
- 39.Find the rank of . Does this matrix have an inverse?
Show a hint
, , then .Show one possible solution
Answer: The rank is 2. There is no inverse.
Two pivots are left, so the rank is 2. A 3 x 3 matrix needs the rank 3 for an inverse. The third row is the sum of the first two.
- 40.Find the inverse of with row operations.
Show a hint
Write the identity matrix next to it. The zeros below the pivots are already there, so only and then are needed.Show one possible solution
Answer: The inverse is .
The right half is the inverse. Check, row 1 of the matrix times the columns of the inverse: , and .
- 41.A table of data has three columns, and the third is the sum of the first two: . What is its rank?
Show a hint
The third column brings nothing new, so the rank cannot be 3. Are the first two columns multiples of each other?Show one possible solution
Answer: The rank is 2.
The new third row is of the new second row, so one more step makes it . Two pivots: rank 2. By columns: the third column is the first plus the second, and the first two, and , are not multiples of each other.
15Check yourself
Twelve quick questions. Your answers are saved in this browser, so you can leave the page and continue later. Each finished round is kept as an attempt below the questions; you can delete the attempts at any time.
0 of 12 answered.
16Checking your work with NumPy
This is the only code of the lesson. A matrix is a 2D NumPy array (Topics 8 and 9), and @ is the matrix product.
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
x = np.array([5, 6])
print(A + B)
print(A.T)
print(A @ x)
print(A @ B)
print(B @ A)
print(round(np.linalg.det(A), 2))
print(np.linalg.inv(A))
print(np.linalg.matrix_rank(A))
print(np.linalg.solve(A, x))Output
[[ 6 8] [10 12]] [[1 3] [2 4]] [17 39] [[19 22] [43 50]] [[23 34] [31 46]] -2.0 [[-2. 1. ] [ 1.5 -0.5]] 2 [-4. 4.5]
Change it and run: put the matrices of a problem you solved by hand into A and B, and compare the results.
| By hand | In NumPy |
|---|---|
| , , | A + B, A - B, c * A |
A.T | |
| and | A @ x and A @ B |
np.linalg.det(A) | |
np.linalg.inv(A) | |
np.linalg.matrix_rank(A) | |
| the solution of | np.linalg.solve(A, b) |
The last line solves the system with the right side ; NumPy uses elimination for it, not the inverse. The determinant is rounded in the code because a computer calculates with rounded decimals and can print -2.0000000000000004.
One trap, the same as with vectors: A * B multiplies entry by entry. It is not the matrix product. The matrix product is A @ B.
17Quick summary
| Idea | Definition or rule |
|---|---|
| Matrix | a rectangular table of numbers; size = rows columns |
| Entry | row , column |
| Addition, subtraction | entry by entry, only for the same size |
| Scalar multiplication | every entry times the number |
| Transpose | rows become columns; |
| Symmetric matrix | |
| Matrix times vector | each component is a row of dotted with ; also a combination of the columns |
| Matrix product | entry = row of dotted with column of |
| Sizes | : the inner numbers must be equal |
| Order | is in general not |
| Identity matrix | |
| Determinant (2 x 2) | ; the factor by which areas change |
| Inverse (2 x 2) | , only when is not 0 |
| Singular matrix | , no inverse |
| System of equations | , with the solution |
| Gaussian elimination | row operations on the augmented matrix until the echelon form, then back substitution |
| Row operations | swap two rows; multiply a row by a number that is not 0; add a multiple of a row to another row |
| Rank | the number of pivots in the echelon form = the number of linearly independent rows |
| Full rank (square matrix) | the same as: determinant not 0, inverse exists, exactly one solution |
| Inverse of a 3 x 3 matrix | row operations from next to until next to |
| One transformation after another | the product of the matrices, the first one on the right |
| Predictions for a table |