Asiman Data School

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. 1.What a matrix is, its size, its entries, and the special matrices (zero, square, identity, diagonal, symmetric, triangular)
  2. 2.Adding and subtracting matrices, multiplying a matrix by a number, and the transpose
  3. 3.A matrix times a vector, read in two ways: dot products with the rows, and a combination of the columns
  4. 4.Matrix multiplication: the rule, when two matrices can be multiplied, and the size of the result
  5. 5.The rules of matrix multiplication: what stays true from ordinary numbers and what does not (the order matters)
  6. 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. 7.Systems of linear equations written as Ax=bA\vx = \vb, and solved with the inverse
  8. 8.Gaussian elimination: the augmented matrix, the three row operations, the echelon form and back substitution
  9. 9.The rank of a matrix, linear independence, and what the rank says about the solutions of a system
  10. 10.The inverse of a 3 x 3 matrix, found with row operations
  11. 11.Matrices as transformations: stretching, rotating, and doing one after the other
  12. 12.Where matrices are used: a table of data, predictions for many rows at once, a layer of a neural network
Notation used in this lesson
WrittenMeaning
AA, BB, XXmatrices (capital letters)
a\va, x\vx, b\vbvectors (small letters, printed in bold), written as columns when a matrix multiplies them
2×32 \times 3the size of a matrix: 2 rows and 3 columns
a12a_{12}the entry of AA in row 1 and column 2
ATA\Tthe transpose of AA
IIthe identity matrix
det⁡(A)\det(A)the determinant of AA
A−1A\invthe inverse of AA
rank⁡(A)\rank(A)the rank of AA
R2−3R1R_2 - 3R_1a row operation: row 2 minus 3 times row 1
2×52 \times 5 in a calculationordinary multiplication of two numbers, as in the last lesson

A matrix is written with its rows under each other:

A=[1234]A = \mat{1 & 2 \\ 3 & 4}

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:

Three employees, one row for each
ageyearssalaryscore
29417103.8
28046703.6
37218703.2

A rectangular table of numbers is called a matrix. It is written without the headings, between two brackets:

[29417103.828046703.637218703.2]\mat{29 & 4 & 1710 & 3.8 \\ 28 & 0 & 4670 & 3.6 \\ 37 & 2 & 1870 & 3.2}

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

M=[20−1453]M = \mat{2 & 0 & -1 \\ 4 & 5 & 3}
  • MM has 2 rows (horizontal) and 3 columns (vertical). Its size is 2×32 \times 3, read "2 by 3". The rows are always named first.
  • The numbers are the entries. The entry in row ii and column jj is written mijm_{ij}: first the row, then the column.
m11=2m12=0m13=−1m21=4m22=5m23=3\begin{aligned} m_{11} &= 2 & m_{12} &= 0 & m_{13} &= -1 \\ m_{21} &= 4 & m_{22} &= 5 & m_{23} &= 3 \end{aligned}
  • The rows of MM are the vectors (2,0,−1)(2, 0, -1) and (4,5,3)(4, 5, 3). Its columns are the vectors (2,4)(2, 4), (0,5)(0, 5) and (−1,3)(-1, 3).
  • Two matrices are equal only when they have the same size and every entry is equal.
M =
The matrix M has 2 rows and 3 columns. Row 2 is filled, column 3 is outlined. Where they cross sits the entry in row 2 and column 3, the number 3.

A vector is a special matrix: a column vector with nn components is a matrix of the size n×1n \times 1, and a row vector is a matrix of the size 1×n1 \times n. From this lesson on, a vector that is multiplied by a matrix is a column.

[56]a column vector, size 2×1[56]a row vector, size 1×2\begin{aligned} &\mat{5 \\ 6} && \text{a column vector, size } 2 \times 1 \\[6pt] &\mat{5 & 6} && \text{a row vector, size } 1 \times 2 \end{aligned}

Inside a sentence a column would take three lines, so there the vector is still written as x=(5,6)\vx = (5, 6), as in the last lesson.

1.3Special matrices

Matrices with a name
NameWhat it isExample
Zero matrix 00every entry is 0[0000]\mat{0 & 0 \\ 0 & 0}
Square matrixas many rows as columns (n×nn \times n)any 2×22 \times 2 or 3×33 \times 3 matrix
Main diagonalthe entries a11,a22,a33,…a_{11}, a_{22}, a_{33}, \dots of a square matrixfrom the top left to the bottom right
Diagonal matrixsquare, and 0 everywhere outside the main diagonal[2005]\mat{2 & 0 \\ 0 & 5}
Identity matrix IIdiagonal, with 1 on the main diagonal[1001]\mat{1 & 0 \\ 0 & 1}
Symmetric matrixsquare, and the same when rows and columns are swapped[1772]\mat{1 & 7 \\ 7 & 2}
Upper triangular matrixsquare, and 0 everywhere below the main diagonal[1203]\mat{1 & 2 \\ 0 & 3}
Lower triangular matrixsquare, and 0 everywhere above the main diagonal[1023]\mat{1 & 0 \\ 2 & 3}
I=[100010001]D=[20005000−1]S=[174720403]\begin{aligned} I &= \mat{1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1} \qquad D = \mat{2 & 0 & 0 \\ 0 & 5 & 0 \\ 0 & 0 & -1} \\[6pt] S &= \mat{1 & 7 & 4 \\ 7 & 2 & 0 \\ 4 & 0 & 3} \end{aligned}

II is the 3 x 3 identity matrix, DD is a diagonal matrix, and SS is symmetric: it is a mirror image of itself across the main diagonal (s12=s21=7s_{12} = s_{21} = 7, s13=s31=4s_{13} = s_{31} = 4, s23=s32=0s_{23} = s_{32} = 0).

Try it yourself

For the matrix B=[7−204153−69082]B = \mat{7 & -2 & 0 & 4 \\ 1 & 5 & 3 & -6 \\ 9 & 0 & 8 & 2} give the size and the entries b23b_{23} and b31b_{31}.

Show a hint
Count the rows first, then the columns. In b23b_{23} the row is 2 and the column is 3.
Show the solution

Answer: The size is 3×43 \times 4, b23=3b_{23} = 3 and b31=9b_{31} = 9.

BB has 3 rows and 4 columns, so its size is 3×43 \times 4. Row 2 is (1,5,3,−6)(1, 5, 3, -6), and its third entry is b23=3b_{23} = 3. Row 3 is (9,0,8,2)(9, 0, 8, 2), and its first entry is b31=9b_{31} = 9.

The two entries are not the same: b23b_{23} and b32b_{32} 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:

[1234]+[5678]=[1+52+63+74+8]=[681012][1234]−[5678]=[1−52−63−74−8]=[−4−4−4−4]\begin{aligned} \mat{1 & 2 \\ 3 & 4} + \mat{5 & 6 \\ 7 & 8} &= \mat{1 + 5 & 2 + 6 \\ 3 + 7 & 4 + 8} \\[4pt] &= \mat{6 & 8 \\ 10 & 12} \\[10pt] \mat{1 & 2 \\ 3 & 4} - \mat{5 & 6 \\ 7 & 8} &= \mat{1 - 5 & 2 - 6 \\ 3 - 7 & 4 - 8} \\[4pt] &= \mat{-4 & -4 \\ -4 & -4} \end{aligned}

A 2×22 \times 2 matrix and a 2×32 \times 3 matrix cannot be added: some entries would have no partner.

In data terms: if AA holds the sales of every product (columns) in every shop (rows) in January and BB the same for February, then A+BA + B holds the sales of both months.

2.2Multiplying by a number

A matrix is multiplied by a number (a scalar) by multiplying every entry:

3[1234]=[36912]−1[1234]=[−1−2−3−4]\begin{aligned} 3 \mat{1 & 2 \\ 3 & 4} &= \mat{3 & 6 \\ 9 & 12} \\[6pt] -1 \mat{1 & 2 \\ 3 & 4} &= \mat{-1 & -2 \\ -3 & -4} \end{aligned}

2.3The rules

For matrices of the same size, and numbers cc and dd:

The rules of adding and scaling matrices
RuleIn symbols
The order of a sum does not matterA+B=B+AA + B = B + A
Grouping does not matter(A+B)+C=A+(B+C)(A + B) + C = A + (B + C)
Adding the zero matrix changes nothingA+0=AA + 0 = A
A number goes into a sumc (A+B)=cA+cBc\,(A + B) = cA + cB
A sum of numbers can be split(c+d) A=cA+dA(c + d)\,A = cA + dA

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 XX with X+A=BX + A = B, for A=[1234]A = \mat{1 & 2 \\ 3 & 4} and B=[5678]B = \mat{5 & 6 \\ 7 & 8}.

X=B−A=[4444]X = B - A = \mat{4 & 4 \\ 4 & 4}

2.4The transpose

The transpose of a matrix, written ATA\T, turns its rows into columns: the first row becomes the first column, the second row the second column.

M=[20−1453]MT=[2405−13]M = \mat{2 & 0 & -1 \\ 4 & 5 & 3} \qquad M\T = \mat{2 & 4 \\ 0 & 5 \\ -1 & 3}
M =
MT =
The transpose turns rows into columns: row 1 of M (filled) is column 1 of the transpose, row 2 (outlined) is column 2.
  • A 2×32 \times 3 matrix becomes a 3×23 \times 2 matrix. In general m×nm \times n becomes n×mn \times m.
  • The entry in row ii, column jj of ATA\T is the entry in row jj, column ii of AA.
  • Transposing twice gives the matrix back: (AT)T=A(A\T)\T = A.
  • (A+B)T=AT+BT(A + B)\T = A\T + B\T, and (cA)T=c AT(cA)\T = c\,A\T.
  • A matrix is symmetric exactly when AT=AA\T = A.
  • 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⋅b=aTb\va \cdot \vb = \va\T \vb (a row times a column). You will see this notation in every machine learning text.

Try it yourself

For A=[2−143]A = \mat{2 & -1 \\ 4 & 3} and B=[10−25]B = \mat{1 & 0 \\ -2 & 5} find 3A−2B3A - 2B and (A+B)T(A + B)\T.
3A−2B3A - 2B
(A+B)T(A + B)\T

Show a hint
Write 3A3A and 2B2B first, then subtract entry by entry. For the second matrix add first, then turn the rows into columns.
Show the solution

Answer: 3A−2B=[4−316−1]3A - 2B = \mat{4 & -3 \\ 16 & -1} and (A+B)T=[32−18](A + B)\T = \mat{3 & 2 \\ -1 & 8}.

3A=[6−3129],2B=[20−410]3A−2B=[6−2−3−012−(−4)9−10]=[4−316−1]A+B=[3−128](A+B)T=[32−18]\begin{aligned} 3A &= \mat{6 & -3 \\ 12 & 9}, \quad 2B = \mat{2 & 0 \\ -4 & 10} \\[6pt] 3A - 2B &= \mat{6 - 2 & -3 - 0 \\ 12 - (-4) & 9 - 10} \\[4pt] &= \mat{4 & -3 \\ 16 & -1} \\[6pt] A + B &= \mat{3 & -1 \\ 2 & 8} \\[6pt] (A + B)\T &= \mat{3 & 2 \\ -1 & 8} \end{aligned}

The entry in row 2, column 1 of 3A−2B3A - 2B is 12−(−4)=1612 - (-4) = 16: subtracting a negative number adds.

3A matrix times a vector

3.1The rule

A matrix AA of the size m×nm \times n can be multiplied by a vector x\vx with nn components. The result is a vector with mm components: each component is the dot product of one row of AA with x\vx.

A=[1234],x=[56]A = \mat{1 & 2 \\ 3 & 4}, \quad \vx = \mat{5 \\ 6}
Ax=[1×5+2×63×5+4×6]=[1739]A\vx = \mat{1 \times 5 + 2 \times 6 \\ 3 \times 5 + 4 \times 6} = \mat{17 \\ 39}

The first row (1,2)(1, 2) gives (1,2)⋅(5,6)=17(1, 2) \cdot (5, 6) = 17, the second row (3,4)(3, 4) gives (3,4)⋅(5,6)=39(3, 4) \cdot (5, 6) = 39.

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.

[102−131][214]=[1×2+0×1+2×4−1×2+3×1+1×4]=[105]\begin{aligned} \mat{1 & 0 & 2 \\ -1 & 3 & 1} \mat{2 \\ 1 \\ 4} &= \mat{1 \times 2 + 0 \times 1 + 2 \times 4 \\ -1 \times 2 + 3 \times 1 + 1 \times 4} \\[6pt] &= \mat{10 \\ 5} \end{aligned}

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. AxA\vx is the linear combination of the columns of AA, with the components of x\vx as the numbers:

Ax=5[13]+6[24]=[515]+[1224]=[1739]A\vx = 5 \mat{1 \\ 3} + 6 \mat{2 \\ 4} = \mat{5 \\ 15} + \mat{12 \\ 24} = \mat{17 \\ 39}

Both readings give (17,39)(17, 39). The row reading is the one you calculate with. The column reading explains what the result can be: every AxA\vx is a mixture of the columns of AA.

A picture of the column reading needs smaller numbers. For A=[2112]A = \mat{2 & 1 \\ 1 & 2} and x=(1,2)\vx = (1, 2) the product is 1 time the first column plus 2 times the second, Ax=(4,5)A\vx = (4, 5). It is the linear combination of the last lesson, with the columns of the matrix as the vectors.

-112345-11234561 × (2, 1)2 × (1, 2)A x = (4, 5)
A matrix times a vector as a combination of the columns: 1 step along the first column (2, 1), then 2 steps along the second column (1, 2). The result is (4, 5).

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:

S=[12581076],p=[1.53.04.5]S = \mat{12 & 5 & 8 \\ 10 & 7 & 6}, \quad \vp = \mat{1.5 \\ 3.0 \\ 4.5}
Sp=[12×1.5+5×3.0+8×4.510×1.5+7×3.0+6×4.5]=[6963]S\vp = \mat{12 \times 1.5 + 5 \times 3.0 + 8 \times 4.5 \\ 10 \times 1.5 + 7 \times 3.0 + 6 \times 4.5} = \mat{69 \\ 63}

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: Ix=xI\vx = \vx for every vector x\vx.

[1001][56]=[1×5+0×60×5+1×6]=[56]\mat{1 & 0 \\ 0 & 1} \mat{5 \\ 6} = \mat{1 \times 5 + 0 \times 6 \\ 0 \times 5 + 1 \times 6} = \mat{5 \\ 6}

Try it yourself

Find AxA\vx for A=[3−124]A = \mat{3 & -1 \\ 2 & 4} and x=(2,1)\vx = (2, 1). Then check your result with the columns of AA.

Show a hint
One dot product for every row. For the check: 2 times the first column plus 1 time the second column.
Show the solution

Answer: Ax=(5,8)A\vx = (5, 8).

Ax=[3×2+(−1)×12×2+4×1]=[58]by columns:2[32]+1[−14]=[6−14+4]=[58]\begin{aligned} A\vx &= \mat{3 \times 2 + (-1) \times 1 \\ 2 \times 2 + 4 \times 1} = \mat{5 \\ 8} \\[8pt] \text{by columns:} \quad & 2 \mat{3 \\ 2} + 1 \mat{-1 \\ 4} \\[4pt] &= \mat{6 - 1 \\ 4 + 4} = \mat{5 \\ 8} \end{aligned}

Both readings agree, as they always must.

4Matrix multiplication

4.1The rule

The product ABAB of two matrices is built from dot products:

the entry in row ii and column jj of ABAB is the dot product of row ii of AA with column jj of BB.

A=[1234],B=[5678]A = \mat{1 & 2 \\ 3 & 4}, \quad B = \mat{5 & 6 \\ 7 & 8}
AB=[1×5+2×71×6+2×83×5+4×73×6+4×8]=[19224350]\begin{aligned} AB &= \mat{1 \times 5 + 2 \times 7 & 1 \times 6 + 2 \times 8 \\ 3 \times 5 + 4 \times 7 & 3 \times 6 + 4 \times 8} \\[6pt] &= \mat{19 & 22 \\ 43 & 50} \end{aligned}

Entry by entry:

The four entries of the product, each a dot product
EntryRow of AAColumn of BBDot product
row 1, column 1(1,2)(1, 2)(5,7)(5, 7)5+14=195 + 14 = 19
row 1, column 2(1,2)(1, 2)(6,8)(6, 8)6+16=226 + 16 = 22
row 2, column 1(3,4)(3, 4)(5,7)(5, 7)15+28=4315 + 28 = 43
row 2, column 2(3,4)(3, 4)(6,8)(6, 8)18+32=5018 + 32 = 50
A
B
A B
One entry of a product: row 1 of A (filled) times column 2 of B (outlined) gives the entry in row 1, column 2 of A B. 1 × 6 + 2 × 8 = 22.

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 ABAB is AA times the matching column of BB. 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.

A
B
A B

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 AA and a column of BB must have the same number of components. So:

ABAB exists only when the number of columns of AA equals the number of rows of BB.

(m×n)  (n⏟these two must be equal×p)  ⟶  (m×p)(m \times \underbrace{n)\;(n}_{\text{these two must be equal}} \times p) \;\longrightarrow\; (m \times p)

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.

The size of a product
AABBABAB
2×32 \times 33×23 \times 22×22 \times 2
3×23 \times 22×32 \times 33×33 \times 3
2×32 \times 33×43 \times 42×42 \times 4
2×32 \times 32×32 \times 3not possible (3 is not 2)
3×33 \times 33×13 \times 13×13 \times 1 (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

A: 2 × 3
B: 3 × 2
A B: 2 × 2

(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

C=[102−131],D=[312110]C = \mat{1 & 0 & 2 \\ -1 & 3 & 1}, \quad D = \mat{3 & 1 \\ 2 & 1 \\ 1 & 0}

CC is 2×32 \times 3 and DD is 3×23 \times 2: the inner numbers are both 3, so CDCD exists and is 2×22 \times 2. Every entry is a dot product of two vectors with 3 components:

row 1, column 1:1×3+0×2+2×1=5row 1, column 2:1×1+0×1+2×0=1row 2, column 1:−1×3+3×2+1×1=4row 2, column 2:−1×1+3×1+1×0=2\begin{aligned} \text{row 1, column 1:} \quad & 1 \times 3 + 0 \times 2 + 2 \times 1 = 5 \\ \text{row 1, column 2:} \quad & 1 \times 1 + 0 \times 1 + 2 \times 0 = 1 \\ \text{row 2, column 1:} \quad & -1 \times 3 + 3 \times 2 + 1 \times 1 = 4 \\ \text{row 2, column 2:} \quad & -1 \times 1 + 3 \times 1 + 1 \times 0 = 2 \end{aligned}
CD=[5142]CD = \mat{5 & 1 \\ 4 & 2}

In the other order, DCDC is (3×2)(2×3)(3 \times 2)(2 \times 3): it exists too, but it is a 3×33 \times 3 matrix.

DC=[3−10+36+12−10+34+11+00+02+0]=[237135102]\begin{aligned} DC &= \mat{3 - 1 & 0 + 3 & 6 + 1 \\ 2 - 1 & 0 + 3 & 4 + 1 \\ 1 + 0 & 0 + 0 & 2 + 0} \\[6pt] &= \mat{2 & 3 & 7 \\ 1 & 3 & 5 \\ 1 & 0 & 2} \end{aligned}

The first entry is row 1 of DD times column 1 of CC: 3×1+1×(−1)=3−13 \times 1 + 1 \times (-1) = 3 - 1. CDCD and DCDC do not even have the same size.

4.4The order matters

With numbers, 3×5=5×33 \times 5 = 5 \times 3. With matrices this is not true. Take AA and BB from above and multiply in the other order:

BA=[5×1+6×35×2+6×47×1+8×37×2+8×4]=[23343146]\begin{aligned} BA &= \mat{5 \times 1 + 6 \times 3 & 5 \times 2 + 6 \times 4 \\ 7 \times 1 + 8 \times 3 & 7 \times 2 + 8 \times 4} \\[6pt] &= \mat{23 & 34 \\ 31 & 46} \end{aligned}

AB=[19224350]AB = \mat{19 & 22 \\ 43 & 50} but BA=[23343146]BA = \mat{23 & 34 \\ 31 & 46}. In general

AB≠BAAB \neq BA

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

The rules of ordinary numbers, tested on matrices
RuleIn symbolsTrue?
Grouping does not matter(AB) C=A (BC)(AB)\,C = A\,(BC)yes
A product goes into a sumA (B+C)=AB+ACA\,(B + C) = AB + AC and (A+B) C=AC+BC(A + B)\,C = AC + BCyes
A number can be moved to the front(cA) B=c (AB)=A (cB)(cA)\,B = c\,(AB) = A\,(cB)yes
The order can be changedAB=BAAB = BAno, in general
A product is zero only when a factor is zeroAB=0AB = 0 means A=0A = 0 or B=0B = 0no
A matrix can be cancelledAB=ACAB = AC means B=CB = Cno, in general

A product can be zero although no factor is zero:

[1111][1−1−11]=[1−1−1+11−1−1+1]=[0000]\mat{1 & 1 \\ 1 & 1} \mat{1 & -1 \\ -1 & 1} = \mat{1 - 1 & -1 + 1 \\ 1 - 1 & -1 + 1} = \mat{0 & 0 \\ 0 & 0}

4.6The identity matrix

For every matrix AA whose sizes fit:

AI=AIA=AAI = A \qquad IA = A

The identity matrix plays the role of the number 1. Check with A=[1234]A = \mat{1 & 2 \\ 3 & 4}:

AI=[1×1+2×01×0+2×13×1+4×03×0+4×1]=[1234]AI = \mat{1 \times 1 + 2 \times 0 & 1 \times 0 + 2 \times 1 \\ 3 \times 1 + 4 \times 0 & 3 \times 0 + 4 \times 1} = \mat{1 & 2 \\ 3 & 4}

4.7Powers of a matrix

A square matrix can be multiplied by itself: A2=AAA^2 = AA, A3=AAAA^3 = AAA.

A2=[1234][1234]=[1+62+83+126+16]=[7101522]\begin{aligned} A^2 &= \mat{1 & 2 \\ 3 & 4} \mat{1 & 2 \\ 3 & 4} \\[6pt] &= \mat{1 + 6 & 2 + 8 \\ 3 + 12 & 6 + 16} = \mat{7 & 10 \\ 15 & 22} \end{aligned}

Careful: A2A^2 is not the matrix of the squared entries ([14916]\mat{1 & 4 \\ 9 & 16} is wrong).

4.8The transpose of a product

(AB)T=BTAT(AB)\T = B\T A\T

The order turns around. With the matrices from above: AB=[19224350]AB = \mat{19 & 22 \\ 43 & 50}, so (AB)T=[19432250](AB)\T = \mat{19 & 43 \\ 22 & 50}, and

BTAT=[5768][1324]=[5+1415+286+1618+32]=[19432250]\begin{aligned} B\T A\T &= \mat{5 & 7 \\ 6 & 8} \mat{1 & 3 \\ 2 & 4} \\[6pt] &= \mat{5 + 14 & 15 + 28 \\ 6 + 16 & 18 + 32} = \mat{19 & 43 \\ 22 & 50} \end{aligned}

Try it yourself

For A=[21−13]A = \mat{2 & 1 \\ -1 & 3} and B=[1240]B = \mat{1 & 2 \\ 4 & 0} find ABAB and BABA.
ABAB
BABA

Show a hint
Two separate products. Go along a row of the first matrix and down a column of the second, and do not expect the same result twice.
Show the solution

Answer: AB=[6411−2]AB = \mat{6 & 4 \\ 11 & -2} and BA=[0784]BA = \mat{0 & 7 \\ 8 & 4}.

AB=[2×1+1×42×2+1×0−1×1+3×4−1×2+3×0]=[6411−2]BA=[1×2+2×(−1)1×1+2×34×2+0×(−1)4×1+0×3]=[0784]\begin{aligned} AB &= \mat{2 \times 1 + 1 \times 4 & 2 \times 2 + 1 \times 0 \\ -1 \times 1 + 3 \times 4 & -1 \times 2 + 3 \times 0} \\[4pt] &= \mat{6 & 4 \\ 11 & -2} \\[8pt] BA &= \mat{1 \times 2 + 2 \times (-1) & 1 \times 1 + 2 \times 3 \\ 4 \times 2 + 0 \times (-1) & 4 \times 1 + 0 \times 3} \\[4pt] &= \mat{0 & 7 \\ 8 & 4} \end{aligned}

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 3x=123x = 12 is solved by multiplying with the inverse of 3: x=13×12=4x = \tfrac{1}{3} \times 12 = 4. The number 13\tfrac{1}{3} is the inverse of 3 because 13×3=1\tfrac{1}{3} \times 3 = 1.

For a square matrix AA, the inverse A−1A\inv is the matrix with

A−1A=IandAA−1=IA\inv A = I \qquad \text{and} \qquad A A\inv = I

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

A=[abcd]det⁡(A)=ad−bcA = \mat{a & b \\ c & d} \qquad \det(A) = ad - bc

Multiply the main diagonal, multiply the other diagonal, subtract.

det⁡[1234]=1×4−2×3=−2det⁡[4623]=4×3−6×2=0\begin{aligned} \det \mat{1 & 2 \\ 3 & 4} &= 1 \times 4 - 2 \times 3 = -2 \\[6pt] \det \mat{4 & 6 \\ 2 & 3} &= 4 \times 3 - 6 \times 2 = 0 \end{aligned}
  • If det⁡(A)\det(A) is not 0, the matrix has an inverse. It is called invertible.
  • If det⁡(A)=0\det(A) = 0, the matrix has no inverse. It is called singular.

In the second matrix the rows are parallel vectors: (4,6)=2 (2,3)(4, 6) = 2\,(2, 3). 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: det⁡(AB)=det⁡(A)×det⁡(B)\det(AB) = \det(A) \times \det(B), and det⁡(AT)=det⁡(A)\det(A\T) = \det(A).

5.3The inverse of a 2 x 2 matrix

A=[abcd]A−1=1det⁡(A)[d−b−ca]A = \mat{a & b \\ c & d} \qquad A\inv = \frac{1}{\det(A)} \mat{d & -b \\ -c & a}

Swap the two entries of the main diagonal, change the sign of the other two, divide by the determinant.

Example. A=[2113]A = \mat{2 & 1 \\ 1 & 3}.

det⁡(A)=2×3−1×1=5A−1=15[3−1−12]=[0.6−0.2−0.20.4]\begin{aligned} \det(A) &= 2 \times 3 - 1 \times 1 = 5 \\[6pt] A\inv &= \frac{1}{5} \mat{3 & -1 \\ -1 & 2} = \mat{0.6 & -0.2 \\ -0.2 & 0.4} \end{aligned}

Always check an inverse by multiplying. The first entry of the product is 2×0.6+1×(−0.2)=1.2−0.22 \times 0.6 + 1 \times (-0.2) = 1.2 - 0.2, and the others are found in the same way:

AA−1=[2113][0.6−0.2−0.20.4]=[1.2−0.2−0.4+0.40.6−0.6−0.2+1.2]=[1001]\begin{aligned} A A\inv &= \mat{2 & 1 \\ 1 & 3} \mat{0.6 & -0.2 \\ -0.2 & 0.4} \\[6pt] &= \mat{1.2 - 0.2 & -0.4 + 0.4 \\ 0.6 - 0.6 & -0.2 + 1.2} = \mat{1 & 0 \\ 0 & 1} \end{aligned}

Rules for inverses: (A−1)−1=A(A\inv)\inv = A, and (AB)−1=B−1A−1(AB)\inv = B\inv A\inv (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 +  −  ++\;-\;+:

det⁡[abcdefghi]=adet⁡[efhi]−bdet⁡[dfgi]+cdet⁡[degh]\begin{aligned} \det \mat{a & b & c \\ d & e & f \\ g & h & i} = {} & a \det \mat{e & f \\ h & i} - b \det \mat{d & f \\ g & i} \\[4pt] & + c \det \mat{d & e \\ g & h} \end{aligned}

Example.

det⁡[123014560]=1 (1×0−4×6)−2 (0×0−4×5)+3 (0×6−1×5)=1×(−24)−2×(−20)+3×(−5)=−24+40−15=1\begin{aligned} &\det \mat{1 & 2 & 3 \\ 0 & 1 & 4 \\ 5 & 6 & 0} \\[4pt] &= 1\,(1 \times 0 - 4 \times 6) - 2\,(0 \times 0 - 4 \times 5) \\ &\quad + 3\,(0 \times 6 - 1 \times 5) \\ &= 1 \times (-24) - 2 \times (-20) + 3 \times (-5) \\ &= -24 + 40 - 15 \\ &= 1 \end{aligned}

The determinant is 1, not 0, so this matrix is invertible.

Try it yourself

Find the determinant and the inverse of A=[2413]A = \mat{2 & 4 \\ 1 & 3}. Check the inverse by multiplying.
A−1A\inv

Show a hint
The determinant is ad−bcad - bc. For the inverse swap 2 and 3, change the signs of 4 and 1, and divide every entry by the determinant.
Show the solution

Answer: det⁡(A)=2\det(A) = 2 and A−1=[1.5−2−0.51]A\inv = \mat{1.5 & -2 \\ -0.5 & 1}.

det⁡(A)=2×3−4×1=2A−1=12[3−4−12]=[1.5−2−0.51]AA−1=[3−2−4+41.5−1.5−2+3]=[1001]\begin{aligned} \det(A) &= 2 \times 3 - 4 \times 1 = 2 \\[6pt] A\inv &= \frac{1}{2} \mat{3 & -4 \\ -1 & 2} = \mat{1.5 & -2 \\ -0.5 & 1} \\[6pt] A A\inv &= \mat{3 - 2 & -4 + 4 \\ 1.5 - 1.5 & -2 + 3} = \mat{1 & 0 \\ 0 & 1} \end{aligned}

The product is the identity matrix, so the inverse is right. The entries can also be typed as fractions: 32\tfrac{3}{2} is 3/2.

6Systems of equations

6.1A system as one matrix equation

Two equations with two unknowns:

2x+y=5x+3y=10\begin{aligned} 2x + y &= 5 \\ x + 3y &= 10 \end{aligned}

Put the numbers in front of the unknowns into a matrix, the unknowns into a vector and the right sides into a vector:

A=[2113],x=[xy],b=[510]A = \mat{2 & 1 \\ 1 & 3}, \quad \vx = \mat{x \\ y}, \quad \vb = \mat{5 \\ 10}

By the rule of Part 3, AxA\vx is the vector (2x+y,  x+3y)(2x + y,\; x + 3y). So the whole system is one short equation:

Ax=bA\vx = \vb

6.2Solving with the inverse

Multiply both sides from the left by A−1A\inv:

A−1Ax=A−1bIx=A−1bx=A−1b\begin{aligned} A\inv A\vx &= A\inv \vb \\ I\vx &= A\inv \vb \\ \vx &= A\inv \vb \end{aligned}

The inverse of this AA was found in Part 5:

x=A−1b=[0.6−0.2−0.20.4][510]=[0.6×5−0.2×10−0.2×5+0.4×10]=[13]\begin{aligned} \vx = A\inv \vb &= \mat{0.6 & -0.2 \\ -0.2 & 0.4} \mat{5 \\ 10} \\[6pt] &= \mat{0.6 \times 5 - 0.2 \times 10 \\ -0.2 \times 5 + 0.4 \times 10} = \mat{1 \\ 3} \end{aligned}

So x=1x = 1 and y=3y = 3. Check in the equations: 2×1+3=52 \times 1 + 3 = 5 and 1+3×3=101 + 3 \times 3 = 10.

Notice the order: A−1bA\inv \vb, not bA−1\vb A\inv. The inverse stands on the left because it was multiplied from the left.

6.3When is there exactly one solution?

  • If det⁡(A)\det(A) is not 0, the system has exactly one solution, x=A−1b\vx = A\inv \vb.
  • If det⁡(A)=0\det(A) = 0, the two equations say the same thing or contradict each other: the system has no solution, or infinitely many.
4x+6y=102x+3y=5\begin{aligned} 4x + 6y &= 10 \\ 2x + 3y &= 5 \end{aligned}

Here the second equation is half of the first: infinitely many solutions.

4x+6y=102x+3y=7\begin{aligned} 4x + 6y &= 10 \\ 2x + 3y &= 7 \end{aligned}

Here the left sides are parallel, the right sides are not: no solution.

Both systems have the matrix [4623]\mat{4 & 6 \\ 2 & 3} with the determinant 0.

-11234-112345

exactly one solution

2x + y = 5 and x + 3y = 10 cross in the point (1, 3)

-11234-112345

no solution

4x + 6y = 10 and 2x + 3y = 7 are parallel and never meet

-11234-112345

infinitely many solutions

4x + 6y = 10 and 2x + 3y = 5 are the same line

Every equation with two unknowns is a line, and a solution of the system is a point that lies on both lines.

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

Solve the system 3x+y=93x + y = 9, 5x+2y=165x + 2y = 16 with the inverse matrix.

Show a hint
Write the system as Ax=bA\vx = \vb with A=[3152]A = \mat{3 & 1 \\ 5 & 2}. Its determinant is 1, so nothing has to be divided.
Show the solution

Answer: x=2x = 2 and y=3y = 3.

det⁡(A)=3×2−1×5=1A−1=[2−1−53]x=A−1b=[2×9−1×16−5×9+3×16]=[23]\begin{aligned} \det(A) &= 3 \times 2 - 1 \times 5 = 1 \\[6pt] A\inv &= \mat{2 & -1 \\ -5 & 3} \\[6pt] \vx = A\inv \vb &= \mat{2 \times 9 - 1 \times 16 \\ -5 \times 9 + 3 \times 16} = \mat{2 \\ 3} \end{aligned}

Check in the equations: 3×2+3=93 \times 2 + 3 = 9 and 5×2+2×3=165 \times 2 + 2 \times 3 = 16.

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.

x+2y=53x+4y=11\begin{aligned} x + 2y &= 5 \\ 3x + 4y &= 11 \end{aligned}

Subtract 3 times the first equation from the second. The unknown xx disappears:

(3x+4y)−3 (x+2y)=11−3×5−2y=−4y=2\begin{aligned} (3x + 4y) - 3\,(x + 2y) &= 11 - 3 \times 5 \\ -2y &= -4 \\ y &= 2 \end{aligned}

Now the first equation gives x+2×2=5x + 2 \times 2 = 5, so x=1x = 1.

7.2The augmented matrix

In this calculation only the numbers matter. So write the matrix AA and the right side b\vb next to each other, with a line between them. This is the augmented matrix:

[1253411]\aug{rr}{r}{1 & 2 & 5 \\ 3 & 4 & 11}

Each row is one equation.

7.3The three row operations

The three row operations
OperationWrittenWith equations
Swap two rowsR1↔R2R_1 \leftrightarrow R_2write the equations in another order
Multiply a row by a number that is not 03R23R_2multiply both sides of an equation
Add a multiple of one row to another rowR2−3R1R_2 - 3R_1subtract 3 times equation 1 from equation 2

R2−3R1R_2 - 3R_1 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:

[1253411]  →  R2−3R1    [1250−2−4]\aug{rr}{r}{1 & 2 & 5 \\ 3 & 4 & 11} \;\xrightarrow{\;R_2 - 3R_1\;}\; \aug{rr}{r}{1 & 2 & 5 \\ 0 & -2 & -4}

The new row 2, entry by entry: 3−3×1=03 - 3 \times 1 = 0, 4−3×2=−24 - 3 \times 2 = -2, 11−3×5=−411 - 3 \times 5 = -4. 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

x+y+z=62x+3y+z=11x+2y+3z=14\begin{aligned} x + y + z &= 6 \\ 2x + 3y + z &= 11 \\ x + 2y + 3z &= 14 \end{aligned}

Step 1: zeros below the first pivot. The first pivot is the 1 in row 1.

[11162311112314]→  R3−R1    R2−2R1  [111601−1−10128]\begin{aligned} &\aug{rrr}{r}{1 & 1 & 1 & 6 \\ 2 & 3 & 1 & 11 \\ 1 & 2 & 3 & 14} \\[8pt] \xrightarrow[\;R_3 - R_1\;]{\;R_2 - 2R_1\;}\quad &\aug{rrr}{r}{1 & 1 & 1 & 6 \\ 0 & 1 & -1 & -1 \\ 0 & 1 & 2 & 8} \end{aligned}

Step 2: zeros below the second pivot. The second pivot is the 1 in row 2. Row 1 is not touched any more.

→  R3−R2  [111601−1−10039]\xrightarrow{\;R_3 - R_2\;}\quad \aug{rrr}{r}{1 & 1 & 1 & 6 \\ 0 & 1 & -1 & -1 \\ 0 & 0 & 3 & 9}

This is the echelon form. The pivots are 1, 1 and 3.

The echelon form of the example. The pivots (filled) go down like the steps of a staircase, and below every pivot there are only zeros.

Step 3: back substitution.

row 3:3z=9⇒z=3row 2:y−z=−1⇒y=−1+3=2row 1:x+y+z=6⇒x=6−2−3=1\begin{aligned} \text{row 3:} \quad & 3z = 9 & \Rightarrow \quad & z = 3 \\ \text{row 2:} \quad & y - z = -1 & \Rightarrow \quad & y = -1 + 3 = 2 \\ \text{row 1:} \quad & x + y + z = 6 & \Rightarrow \quad & x = 6 - 2 - 3 = 1 \end{aligned}

The solution is x=1x = 1, y=2y = 2, z=3z = 3. Check in the three equations: 1+2+3=61 + 2 + 3 = 6, 2+6+3=112 + 6 + 3 = 11, 1+4+9=141 + 4 + 9 = 14.

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.

Step 1 of 8

7.6The recipe

  1. Write the augmented matrix.
  2. Use the first row to make zeros below the first pivot.
  3. Use the second row to make zeros below the second pivot, and so on. A row that was already used is not changed again.
  4. If the place of a pivot holds a 0, swap the row with a row below it that has a number there.
  5. Solve from the last row upwards.
  6. Put the solution into the original equations.

A swap, when the first pivot is missing:

[024113]  →  R1↔R2    [113024]\aug{rr}{r}{0 & 2 & 4 \\ 1 & 1 & 3} \;\xrightarrow{\;R_1 \leftrightarrow R_2\;}\; \aug{rr}{r}{1 & 1 & 3 \\ 0 & 2 & 4}

Now 2y=42y = 4, so y=2y = 2, and then x=3−2=1x = 3 - 2 = 1.

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:

x+2y=32x+4y=6\begin{aligned} x + 2y &= 3 \\ 2x + 4y &= 6 \end{aligned}
[123246]  →  R2−2R1    [123000]\aug{rr}{r}{1 & 2 & 3 \\ 2 & 4 & 6} \;\xrightarrow{\;R_2 - 2R_1\;}\; \aug{rr}{r}{1 & 2 & 3 \\ 0 & 0 & 0}

The last row says 0=00 = 0: it is always true and tells nothing. One equation is left for two unknowns, so there are infinitely many solutions: choose any yy, then x=3−2yx = 3 - 2y.

x+2y=32x+4y=7\begin{aligned} x + 2y &= 3 \\ 2x + 4y &= 7 \end{aligned}
[123247]  →  R2−2R1    [123001]\aug{rr}{r}{1 & 2 & 3 \\ 2 & 4 & 7} \;\xrightarrow{\;R_2 - 2R_1\;}\; \aug{rr}{r}{1 & 2 & 3 \\ 0 & 0 & 1}

The last row says 0=10 = 1, which is never true: there is no solution.

What the echelon form says about the solutions
The echelon form hasThe system has
a pivot for every unknown, and no row 0=number0 = \text{number}exactly one solution
a row with zeros on the left and a number that is not 0 on the rightno solution
fewer pivots than unknowns, and no row 0=number0 = \text{number}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

det⁡[111231123]=det⁡[11101−1003]=1×1×3=3\begin{aligned} \det \mat{1 & 1 & 1 \\ 2 & 3 & 1 \\ 1 & 2 & 3} &= \det \mat{1 & 1 & 1 \\ 0 & 1 & -1 \\ 0 & 0 & 3} \\[6pt] &= 1 \times 1 \times 3 = 3 \end{aligned}

Check along the first row, as in Part 5: 1 (9−2)−1 (6−1)+1 (4−3)=7−5+1=31\,(9 - 2) - 1\,(6 - 1) + 1\,(4 - 3) = 7 - 5 + 1 = 3. (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

Solve the system with Gaussian elimination: x+y+z=4x + y + z = 4, 2x+3y+z=42x + 3y + z = 4, x+2y+2z=6x + 2y + 2z = 6.

Show a hint
First R2−2R1R_2 - 2R_1 and R3−R1R_3 - R_1, then R3−R2R_3 - R_2. Solve from the last row upwards, and be careful with the minus signs.
Show the solution

Answer: x=2x = 2, y=−1y = -1, z=3z = 3.

[111423141226]→  R3−R1    R2−2R1  [111401−1−40112]→  R3−R2  [111401−1−40026]\begin{aligned} &\aug{rrr}{r}{1 & 1 & 1 & 4 \\ 2 & 3 & 1 & 4 \\ 1 & 2 & 2 & 6} \\[8pt] \xrightarrow[\;R_3 - R_1\;]{\;R_2 - 2R_1\;}\quad &\aug{rrr}{r}{1 & 1 & 1 & 4 \\ 0 & 1 & -1 & -4 \\ 0 & 1 & 1 & 2} \\[8pt] \xrightarrow{\;R_3 - R_2\;}\quad &\aug{rrr}{r}{1 & 1 & 1 & 4 \\ 0 & 1 & -1 & -4 \\ 0 & 0 & 2 & 6} \end{aligned}
row 3:    2z=6⇒    z=3row 2:    y−z=−4⇒    y=−4+3=−1row 1:    x+y+z=4⇒    x=4+1−3=2\begin{aligned} \text{row 3:} \;\; & 2z = 6 & \Rightarrow \;\; & z = 3 \\ \text{row 2:} \;\; & y - z = -4 & \Rightarrow \;\; & y = -4 + 3 = -1 \\ \text{row 1:} \;\; & x + y + z = 4 & \Rightarrow \;\; & x = 4 + 1 - 3 = 2 \end{aligned}

Check in the original equations: 2−1+3=42 - 1 + 3 = 4, 4−3+3=44 - 3 + 3 = 4 and 2−2+6=62 - 2 + 6 = 6.

8The rank of a matrix

8.1Rows that say nothing new

In the system x+2y=3x + 2y = 3, 2x+4y=62x + 4y = 6 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 AA alone, without a right side.

[1234]  →  R2−3R1    [120−2]\mat{1 & 2 \\ 3 & 4} \;\xrightarrow{\;R_2 - 3R_1\;}\; \mat{1 & 2 \\ 0 & -2}

Two pivots: rank 2.

[1224]  →  R2−2R1    [1200]\mat{1 & 2 \\ 2 & 4} \;\xrightarrow{\;R_2 - 2R_1\;}\; \mat{1 & 2 \\ 0 & 0}

One pivot: rank 1.

A 3 x 3 example:

M=[121011132]→  R3−R1  [121011011]→  R3−R2  [121011000]\begin{aligned} M = {} &\mat{1 & 2 & 1 \\ 0 & 1 & 1 \\ 1 & 3 & 2} \\[8pt] \xrightarrow{\;R_3 - R_1\;}\quad &\mat{1 & 2 & 1 \\ 0 & 1 & 1 \\ 0 & 1 & 1} \\[8pt] \xrightarrow{\;R_3 - R_2\;}\quad &\mat{1 & 2 & 1 \\ 0 & 1 & 1 \\ 0 & 0 & 0} \end{aligned}

Two pivots, so rank⁡(M)=2\rank(M) = 2. The reason can be seen in MM itself: the third row is the sum of the first two, (1,3,2)=(1,2,1)+(0,1,1)(1, 3, 2) = (1, 2, 1) + (0, 1, 1). 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 (1,2)(1, 2) and (2,4)(2, 4) are dependent: the second is 2 times the first.
  • The rows (1,2)(1, 2) and (3,4)(3, 4) are independent: neither is a multiple of the other.
  • The three rows of MM 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, rank⁡(AT)=rank⁡(A)\rank(A\T) = \rank(A). In MM the columns are dependent too: the third column is the second minus the first, (1,1,2)=(2,1,3)−(1,0,1)(1, 1, 2) = (2, 1, 3) - (1, 0, 1).

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 2×32 \times 3 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.
[123247]  →  R2−2R1    [123001]\mat{1 & 2 & 3 \\ 2 & 4 & 7} \;\xrightarrow{\;R_2 - 2R_1\;}\; \mat{1 & 2 & 3 \\ 0 & 0 & 1}

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 nn rows, the following statements say the same thing. If one is true, all are true:

Five ways to say that a square matrix is invertible
StatementFor [1234]\mat{1 & 2 \\ 3 & 4}For [1224]\mat{1 & 2 \\ 2 & 4}
the rank is nn (full rank)rank 2: yesrank 1: no
the determinant is not 0−2-2: yes00: no
the inverse existsyesno
the rows are linearly independentyesno
Ax=bA\vx = \vb has exactly one solution for every b\vbyesno

8.4Rank and the number of solutions

Compare the rank of AA with the rank of the augmented matrix:

The ranks and the solutions of a system
RanksThe system Ax=bA\vx = \vb has
both ranks equal the number of unknownsexactly one solution
both ranks are equal, but smaller than the number of unknownsinfinitely many solutions
the augmented matrix has a larger rank than AAno solution

In Part 7 the system with the last row 0=00 = 0 had the rank 1 on both sides, with 2 unknowns: infinitely many solutions. The system with the last row 0=10 = 1 had the rank 1 for AA 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

Find the rank of A=[121243364]A = \mat{1 & 2 & 1 \\ 2 & 4 & 3 \\ 3 & 6 & 4}. Does AA have an inverse?

Show a hint
R2−2R1R_2 - 2R_1 and R3−3R1R_3 - 3R_1, then R3−R2R_3 - R_2. Count the pivots; a pivot does not have to stand on the main diagonal.
Show the solution

Answer: rank⁡(A)=2\rank(A) = 2, so AA has no inverse.

[121243364]→  R3−3R1    R2−2R1  [121001001]→  R3−R2  [121001000]\begin{aligned} &\mat{1 & 2 & 1 \\ 2 & 4 & 3 \\ 3 & 6 & 4} \\[8pt] \xrightarrow[\;R_3 - 3R_1\;]{\;R_2 - 2R_1\;}\quad &\mat{1 & 2 & 1 \\ 0 & 0 & 1 \\ 0 & 0 & 1} \\[8pt] \xrightarrow{\;R_3 - R_2\;}\quad &\mat{1 & 2 & 1 \\ 0 & 0 & 1 \\ 0 & 0 & 0} \end{aligned}

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, (3,6,4)=(1,2,1)+(2,4,3)(3, 6, 4) = (1, 2, 1) + (2, 4, 3).

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 AA 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.

[ A  |  I ]⟶[ I  |  A−1 ]\left[\, A \;\middle\vert\; I \,\right] \quad \longrightarrow \quad \left[\, I \;\middle\vert\; A\inv \,\right]

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 AA into II are together a multiplication by A−1A\inv, and the same operations turn II into A−1A\inv.

9.2First with a 2 x 2 matrix

For A=[1234]A = \mat{1 & 2 \\ 3 & 4} the formula gives A−1=[−211.5−0.5]A\inv = \mat{-2 & 1 \\ 1.5 & -0.5} (it is one of the practice problems). The new method:

[12103401]→  R2−3R1  [12100−2−31]→  R2÷(−2)  [1210011.5−0.5]→  R1−2R2  [10−21011.5−0.5]\begin{aligned} &\aug{rr}{rr}{1 & 2 & 1 & 0 \\ 3 & 4 & 0 & 1} \\[8pt] \xrightarrow{\;R_2 - 3R_1\;}\quad &\aug{rr}{rr}{1 & 2 & 1 & 0 \\ 0 & -2 & -3 & 1} \\[8pt] \xrightarrow{\;R_2 \div (-2)\;}\quad &\aug{rr}{rr}{1 & 2 & 1 & 0 \\ 0 & 1 & 1.5 & -0.5} \\[8pt] \xrightarrow{\;R_1 - 2R_2\;}\quad &\aug{rr}{rr}{1 & 0 & -2 & 1 \\ 0 & 1 & 1.5 & -0.5} \end{aligned}

The left half is II, and the right half is the same inverse.

9.3A 3 x 3 matrix

Take the matrix of Part 5 with the determinant 1:

[123100014010560001]\aug{rrr}{rrr}{1 & 2 & 3 & 1 & 0 & 0 \\ 0 & 1 & 4 & 0 & 1 & 0 \\ 5 & 6 & 0 & 0 & 0 & 1}

Zeros below the pivots.

→  R3−5R1  [1231000140100−4−15−501]→  R3+4R2  [123100014010001−541]\begin{aligned} \xrightarrow{\;R_3 - 5R_1\;}\quad &\aug{rrr}{rrr}{1 & 2 & 3 & 1 & 0 & 0 \\ 0 & 1 & 4 & 0 & 1 & 0 \\ 0 & -4 & -15 & -5 & 0 & 1} \\[8pt] \xrightarrow{\;R_3 + 4R_2\;}\quad &\aug{rrr}{rrr}{1 & 2 & 3 & 1 & 0 & 0 \\ 0 & 1 & 4 & 0 & 1 & 0 \\ 0 & 0 & 1 & -5 & 4 & 1} \end{aligned}

All three pivots are already 1. Zeros above the pivots, starting with the last column.

→  R1−3R3    R2−4R3  [12016−12−301020−15−4001−541]→  R1−2R2  [100−2418501020−15−4001−541]\begin{aligned} \xrightarrow[\;R_1 - 3R_3\;]{\;R_2 - 4R_3\;}\quad &\aug{rrr}{rrr}{1 & 2 & 0 & 16 & -12 & -3 \\ 0 & 1 & 0 & 20 & -15 & -4 \\ 0 & 0 & 1 & -5 & 4 & 1} \\[8pt] \xrightarrow{\;R_1 - 2R_2\;}\quad &\aug{rrr}{rrr}{1 & 0 & 0 & -24 & 18 & 5 \\ 0 & 1 & 0 & 20 & -15 & -4 \\ 0 & 0 & 1 & -5 & 4 & 1} \end{aligned}
A−1=[−2418520−15−4−541]A\inv = \mat{-24 & 18 & 5 \\ 20 & -15 & -4 \\ -5 & 4 & 1}

Check with AA−1=IA A\inv = I. The first row of the product:

1×(−24)+2×20+3×(−5)=−24+40−15=11×18+2×(−15)+3×4=18−30+12=01×5+2×(−4)+3×1=5−8+3=0\begin{aligned} & 1 \times (-24) + 2 \times 20 + 3 \times (-5) \\ & \quad = -24 + 40 - 15 = 1 \\[4pt] & 1 \times 18 + 2 \times (-15) + 3 \times 4 \\ & \quad = 18 - 30 + 12 = 0 \\[4pt] & 1 \times 5 + 2 \times (-4) + 3 \times 1 \\ & \quad = 5 - 8 + 3 = 0 \end{aligned}

The second row gives (0,1,0)(0, 1, 0) and the third (0,0,1)(0, 0, 1) in the same way.

9.4When there is no inverse

If a row of zeros appears in the left half, the rank of AA is smaller than 3 and the left half can never become II: the matrix has no inverse. Stop there. The matrix MM of Part 8 is such a matrix.

9.5Solving a system with it

x+2y+3z=1y+4z=25x+6y=3\begin{aligned} x + 2y + 3z &= 1 \\ y + 4z &= 2 \\ 5x + 6y &= 3 \end{aligned}

The matrix of this system is the matrix that was just inverted, so

x=A−1b=[−24×1+18×2+5×320×1−15×2−4×3−5×1+4×2+1×3]=[27−226]\begin{aligned} \vx = A\inv \vb &= \mat{-24 \times 1 + 18 \times 2 + 5 \times 3 \\ 20 \times 1 - 15 \times 2 - 4 \times 3 \\ -5 \times 1 + 4 \times 2 + 1 \times 3} \\[6pt] &= \mat{27 \\ -22 \\ 6} \end{aligned}

Check in the first equation: 27−44+18=127 - 44 + 18 = 1.

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

Find the inverse of A=[120010241]A = \mat{1 & 2 & 0 \\ 0 & 1 & 0 \\ 2 & 4 & 1} with row operations.
A−1A\inv

Show a hint
Write the identity matrix next to AA. One operation makes the zeros below the pivots (R3−2R1R_3 - 2R_1), one makes the zero above (R1−2R2R_1 - 2R_2).
Show the solution

Answer: A−1=[1−20010−201]A\inv = \mat{1 & -2 & 0 \\ 0 & 1 & 0 \\ -2 & 0 & 1}.

[120100010010241001]→  R3−2R1  [120100010010001−201]→  R1−2R2  [1001−20010010001−201]\begin{aligned} &\aug{rrr}{rrr}{1 & 2 & 0 & 1 & 0 & 0 \\ 0 & 1 & 0 & 0 & 1 & 0 \\ 2 & 4 & 1 & 0 & 0 & 1} \\[8pt] \xrightarrow{\;R_3 - 2R_1\;}\quad &\aug{rrr}{rrr}{1 & 2 & 0 & 1 & 0 & 0 \\ 0 & 1 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & -2 & 0 & 1} \\[8pt] \xrightarrow{\;R_1 - 2R_2\;}\quad &\aug{rrr}{rrr}{1 & 0 & 0 & 1 & -2 & 0 \\ 0 & 1 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & -2 & 0 & 1} \end{aligned}

Check with the third row of AA times the columns of the inverse: 2×1+4×0+1×(−2)=02 \times 1 + 4 \times 0 + 1 \times (-2) = 0, 2×(−2)+4×1+1×0=02 \times (-2) + 4 \times 1 + 1 \times 0 = 0 and 2×0+4×0+1×1=12 \times 0 + 4 \times 0 + 1 \times 1 = 1.

10Matrices as transformations

10.1A matrix moves every point

Multiplying by a matrix turns a vector x\vx into another vector AxA\vx. Do this with every point of the plane, and the matrix transforms the whole plane. Three simple matrices show what can happen.

Stretching. S=[2001]S = \mat{2 & 0 \\ 0 & 1} doubles the first component and keeps the second:

Sx=[2001][31]=[61]S\vx = \mat{2 & 0 \\ 0 & 1} \mat{3 \\ 1} = \mat{6 \\ 1}
-11234-1123beforeafter S
The matrix S doubles the first component: the unit square becomes a rectangle twice as wide.

Rotating by 90 degrees (to the left). R=[0−110]R = \mat{0 & -1 \\ 1 & 0}:

R[10]=[01]  R[01]=[−10]  R[21]=[−12]R \mat{1 \\ 0} = \mat{0 \\ 1} \quad\; R \mat{0 \\ 1} = \mat{-1 \\ 0} \quad\; R \mat{2 \\ 1} = \mat{-1 \\ 2}
-3-2-1123-1123x = (2, 1)R x = (−1, 2)
The matrix R turns every vector by 90 degrees to the left. The length does not change.

The dot product of (2,1)(2, 1) and (−1,2)(-1, 2) is −2+2=0-2 + 2 = 0: the vector really turned by a right angle, and its length did not change.

The columns tell the story. A (1,0)A\,(1, 0) is the first column of AA, and A (0,1)A\,(0, 1) 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 AA it becomes a parallelogram whose sides are the columns of AA. Its area is the size of det⁡(A)\det(A).

T=[2112]det⁡(T)=2×2−1×1=3T = \mat{2 & 1 \\ 1 & 2} \qquad \det(T) = 2 \times 2 - 1 \times 1 = 3
-11234-11234(2, 1)(1, 2)area 3area 1
The matrix T turns the unit square (area 1) into a parallelogram whose sides are the columns of T. Its area is the determinant, 3.

The stretch SS has the determinant 2 (areas double), the rotation RR 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.

-4-3-2-11234-4-3-2-11234
A =

(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 RR, then stretch with SS: the vector x\vx becomes RxR\vx, and then S (Rx)S\,(R\vx). Because grouping does not matter, this is (SR) x(SR)\,\vx:

doing one transformation after another is multiplying their matrices. The matrix that acts first stands on the right.

SR=[2001][0−110]=[0−210]SR = \mat{2 & 0 \\ 0 & 1} \mat{0 & -1 \\ 1 & 0} = \mat{0 & -2 \\ 1 & 0}
(SR)[21]=[0×2−2×11×2+0×1]=[−22](SR) \mat{2 \\ 1} = \mat{0 \times 2 - 2 \times 1 \\ 1 \times 2 + 0 \times 1} = \mat{-2 \\ 2}

Step by step it is the same: Rx=(−1,2)R\vx = (-1, 2), then S (−1,2)=(−2,2)S\,(-1, 2) = (-2, 2).

In the other order, first stretch and then rotate, the matrix is RS=[0−120]RS = \mat{0 & -1 \\ 2 & 0}, and (RS) (2,1)=(−1,4)(RS)\,(2, 1) = (-1, 4): a different result. Here is the reason why ABAB is not BABA: stretching a rotated shape is not the same as rotating a stretched shape.

-4-3-2-112345-112345xR xS R x

first rotate, then stretch: S R

x → R x = (−1, 2) → S (R x) = (−2, 2)

-4-3-2-112345-112345xS xR S x

first stretch, then rotate: R S

x → S x = (4, 1) → R (S x) = (−1, 4)

The same two transformations in the two possible orders. The results are different vectors, so S R and R S are different matrices.

The inverse matrix is the transformation that undoes the first one: A−1(Ax)=xA\inv (A\vx) = \vx.

Try it yourself

The matrix A=[1−111]A = \mat{1 & -1 \\ 1 & 1} transforms the plane. Where does the vector (2,1)(2, 1) land, and by which factor do areas change?

Show a hint
Multiply AA by the vector. The factor for areas is the size of the determinant.
Show the solution

Answer: (2,1)(2, 1) lands on (1,3)(1, 3), and areas are multiplied by 2.

A[21]=[1×2+(−1)×11×2+1×1]=[13]det⁡(A)=1×1−(−1)×1=2\begin{aligned} A \mat{2 \\ 1} &= \mat{1 \times 2 + (-1) \times 1 \\ 1 \times 2 + 1 \times 1} = \mat{1 \\ 3} \\[6pt] \det(A) &= 1 \times 1 - (-1) \times 1 = 2 \end{aligned}

The columns show what this matrix does: (1,0)(1, 0) lands on (1,1)(1, 1) and (0,1)(0, 1) lands on (−1,1)(-1, 1). 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 XX. Three flats with the features area, rooms and age:

X=[803105523012045]X = \mat{80 & 3 & 10 \\ 55 & 2 & 30 \\ 120 & 4 & 5}

11.2Predictions for all rows at once

In the last lesson one prediction was w⋅x+bias\vw \cdot \vx + \text{bias}. With the matrix XX, the predictions for all flats are one matrix-times-vector product:

predictions=Xw+bias\text{predictions} = X\vw + \text{bias}

With the weights w=(1200,15000,−800)\vw = (1200, 15000, -800) and the bias 20000, the first row gives 80×1200+3×15000+10×(−800)80 \times 1200 + 3 \times 15000 + 10 \times (-800), and the other rows work in the same way:

Xw=[96000+45000−800066000+30000−24000144000+60000−4000]=[13300072000200000]predictions=(153000,  92000,  220000)\begin{aligned} X\vw &= \mat{96000 + 45000 - 8000 \\ 66000 + 30000 - 24000 \\ 144000 + 60000 - 4000} \\[6pt] &= \mat{133000 \\ 72000 \\ 200000} \\[6pt] \text{predictions} &= (153000,\; 92000,\; 220000) \end{aligned}

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 WW. Then the whole layer is

output=Wx+b\text{output} = W\vx + \vb
W=[1−10.52],x=[21],b=[0−1]W = \mat{1 & -1 \\ 0.5 & 2}, \quad \vx = \mat{2 \\ 1}, \quad \vb = \mat{0 \\ -1}
Wx=[1×2+(−1)×10.5×2+2×1]=[13]Wx+b=[1+03−1]=[12]\begin{aligned} W\vx &= \mat{1 \times 2 + (-1) \times 1 \\ 0.5 \times 2 + 2 \times 1} = \mat{1 \\ 3} \\[6pt] W\vx + \vb &= \mat{1 + 0 \\ 3 - 1} = \mat{1 \\ 2} \end{aligned}

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

More places where a matrix appears
WhereThe matrix
An image in grey tonesthe brightness of every pixel, rows and columns as on the screen
Word embeddingsone row for every word
Recommendation systemsusers in the rows, films in the columns, ratings as entries
Principal Component Analysisa matrix that turns the data into its most useful directions

Try it yourself

A model predicts an exam score from the hours of study and the hours of sleep with the weights w=(10,2)\vw = (10, 2) and the bias 20. Find the predictions for three students with (3,7)(3, 7), (5,6)(5, 6) and (1,8)(1, 8) hours with one matrix-times-vector product.

Show a hint
The students are the rows of the matrix XX. Calculate XwX\vw, then add 20 to every component.
Show the solution

Answer: The predictions are (64,82,46)(64, 82, 46).

Xw=[375618][102]=[30+1450+1210+16]=[446226]Xw+20=(64,  82,  46)\begin{aligned} X\vw &= \mat{3 & 7 \\ 5 & 6 \\ 1 & 8} \mat{10 \\ 2} \\[6pt] &= \mat{30 + 14 \\ 50 + 12 \\ 10 + 16} = \mat{44 \\ 62 \\ 26} \\[6pt] X\vw + 20 &= (64,\; 82,\; 46) \end{aligned}

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. [1234][5678]\mat{1 & 2 \\ 3 & 4} \mat{5 & 6 \\ 7 & 8} is not [5122132]\mat{5 & 12 \\ 21 & 32}. Every entry of a product is a dot product of a row and a column.

Changing the order. ABAB and BABA are different matrices. In x=A−1b\vx = A\inv \vb the inverse must stand on the left.

Not checking the sizes. (2×3)(2×3)(2 \times 3)(2 \times 3) does not exist. The inner numbers must be equal.

Mixing up rows and columns. The size names the rows first: 2×32 \times 3 is 2 rows and 3 columns. a12a_{12} is row 1, column 2.

Squaring the entries. A2A^2 means AAAA, not the matrix of the squared entries.

The wrong sign in the determinant. det⁡[abcd]=ad−bc\det \mat{a & b \\ c & d} = ad - bc: main diagonal first, minus the other diagonal.

Forgetting to divide by the determinant. The inverse is the swapped and signed matrix divided by det⁡(A)\det(A).

Inverting a singular matrix. If the determinant is 0 there is no inverse. Calculate the determinant first.

Cancelling a matrix. From AB=ACAB = AC it does not follow that B=CB = C, and from AB=0AB = 0 it does not follow that one of them is zero.

Keeping the order in a transpose or an inverse of a product. (AB)T=BTAT(AB)\T = B\T A\T and (AB)−1=B−1A−1(AB)\inv = B\inv A\inv: the order turns around.

Adding a number to a matrix. A+3A + 3 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. R2−3R1R_2 - 3R_1 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 AA next to II.

Common mistakes and what is right
MistakeWhat is right
ABAB entry by entryrow of AA times column of BB, a dot product for every entry
AB=BAAB = BAthe order matters
(2×3)(2×3)(2 \times 3)(2 \times 3)the inner numbers must be equal
A2A^2 as squared entriesA2=AAA^2 = AA
det⁡=ad+bc\det = ad + bcdet⁡=ad−bc\det = ad - bc
the inverse without 1det⁡\dfrac{1}{\det}divide every entry by the determinant
(AB)T=ATBT(AB)\T = A\T B\T(AB)T=BTAT(AB)\T = B\T A\T
x=bA−1\vx = \vb A\invx=A−1b\vx = A\inv \vb
a row operation without the right sidethe whole row changes, the right side too
rank = number of rowsrank = 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 A=[4−17025]A = \mat{4 & -1 & 7 \\ 0 & 2 & 5} give the size, the entries a13a_{13} and a21a_{21}, the second row and the third column.

Idea. Rows are counted first, then columns (Part 1). In aija_{ij} the first number is the row.

size:2×3(2 rows, 3 columns)a13=7(row 1, column 3)a21=0(row 2, column 1)second row:(0,2,5)third column:(7,5)\begin{aligned} \text{size:} \quad & 2 \times 3 \quad \text{(2 rows, 3 columns)} \\ & a_{13} = 7 \quad \text{(row 1, column 3)} \\ & a_{21} = 0 \quad \text{(row 2, column 1)} \\ \text{second row:} \quad & (0, 2, 5) \\ \text{third column:} \quad & (7, 5) \end{aligned}

Why: a row of a 2×32 \times 3 matrix has 3 components and a column has 2. Mixing up a13a_{13} and a31a_{31} is the classic slip; here a31a_{31} does not even exist, because there is no third row.

Problem 2: Add and subtract

For A=[3−104]A = \mat{3 & -1 \\ 0 & 4} and B=[15−22]B = \mat{1 & 5 \\ -2 & 2} find A+BA + B and A−BA - B.

Idea. Same size, so entry by entry (Part 2).

A+B=[3+1−1+50+(−2)4+2]=[44−26]A−B=[3−1−1−50−(−2)4−2]=[2−622]\begin{aligned} A + B &= \mat{3 + 1 & -1 + 5 \\ 0 + (-2) & 4 + 2} = \mat{4 & 4 \\ -2 & 6} \\[6pt] A - B &= \mat{3 - 1 & -1 - 5 \\ 0 - (-2) & 4 - 2} = \mat{2 & -6 \\ 2 & 2} \end{aligned}

Why: the entry in row 2, column 1 of A−BA - B is 0−(−2)=20 - (-2) = 2. Check a subtraction by adding back: B+(A−B)B + (A - B) must give AA.

Problem 3: A combination of two matrices

For A=[1234]A = \mat{1 & 2 \\ 3 & 4} and B=[01−12]B = \mat{0 & 1 \\ -1 & 2} find 2A−3B2A - 3B.

Idea. Scale each matrix first, then subtract (Part 2).

2A=[2468],3B=[03−36]2A−3B=[2−04−36−(−3)8−6]=[2192]\begin{aligned} 2A &= \mat{2 & 4 \\ 6 & 8}, \quad 3B = \mat{0 & 3 \\ -3 & 6} \\[6pt] 2A - 3B &= \mat{2 - 0 & 4 - 3 \\ 6 - (-3) & 8 - 6} = \mat{2 & 1 \\ 9 & 2} \end{aligned}

Why: it is the linear combination of the last lesson, now with matrices. Writing 2A2A and 3B3B down first keeps the signs safe.

Problem 4: Transpose, and is it symmetric?

Find the transpose of M=[123456]M = \mat{1 & 2 & 3 \\ 4 & 5 & 6}. Then decide whether S=[123256369]S = \mat{1 & 2 & 3 \\ 2 & 5 & 6 \\ 3 & 6 & 9} is symmetric.

Idea. The transpose turns rows into columns. A matrix is symmetric when it equals its transpose (Part 2).

MT=[142536](2×3 became 3×2)M\T = \mat{1 & 4 \\ 2 & 5 \\ 3 & 6} \qquad (2 \times 3 \text{ became } 3 \times 2)
s12=s21=2s13=s31=3s23=s32=6\begin{aligned} s_{12} &= s_{21} = 2 \\ s_{13} &= s_{31} = 3 \\ s_{23} &= s_{32} = 6 \end{aligned}

All three pairs are equal, so ST=SS\T = S: 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 MM, can never be symmetric.

Problem 5: A matrix times a vector

Find AxA\vx for A=[2103]A = \mat{2 & 1 \\ 0 & 3} and x=(4,5)\vx = (4, 5).

Idea. Each component of the result is the dot product of one row with x\vx (Part 3).

Ax=[2×4+1×50×4+3×5]=[1315]check by columns:4[20]+5[13]=[80]+[515]=[1315]\begin{aligned} A\vx &= \mat{2 \times 4 + 1 \times 5 \\ 0 \times 4 + 3 \times 5} = \mat{13 \\ 15} \\[8pt] \text{check by columns:} \quad & 4 \mat{2 \\ 0} + 5 \mat{1 \\ 3} \\[4pt] &= \mat{8 \\ 0} + \mat{5 \\ 15} = \mat{13 \\ 15} \end{aligned}

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 ABAB for A=[2013]A = \mat{2 & 0 \\ 1 & 3} and B=[142−1]B = \mat{1 & 4 \\ 2 & -1}.

Idea. Row of AA times column of BB for every entry (Part 4). Both are 2×22 \times 2, so the product is 2×22 \times 2.

AB=[2×1+0×22×4+0×(−1)1×1+3×21×4+3×(−1)]=[2871]\begin{aligned} AB &= \mat{2 \times 1 + 0 \times 2 & 2 \times 4 + 0 \times (-1) \\ 1 \times 1 + 3 \times 2 & 1 \times 4 + 3 \times (-1)} \\[6pt] &= \mat{2 & 8 \\ 7 & 1} \end{aligned}

Why: write every entry as a sum of products before adding, as with the dot product. The zero in AA makes the first row easy: it is just 2 times the first row of BB.

Problem 7: Does the product exist?

AA is 2×32 \times 3, BB is 3×43 \times 4, CC is 2×22 \times 2. Which of the products ABAB, BABA, CACA, ACAC 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).

AB:(2×3)(3×4)  ⇒  exists, 2×4BA:(3×4)(2×3)  ⇒  does not existCA:(2×2)(2×3)  ⇒  exists, 2×3AC:(2×3)(2×2)  ⇒  does not exist\begin{aligned} AB: \quad & (2 \times 3)(3 \times 4) \;\Rightarrow\; \text{exists, } 2 \times 4 \\ BA: \quad & (3 \times 4)(2 \times 3) \;\Rightarrow\; \text{does not exist} \\ CA: \quad & (2 \times 2)(2 \times 3) \;\Rightarrow\; \text{exists, } 2 \times 3 \\ AC: \quad & (2 \times 3)(2 \times 2) \;\Rightarrow\; \text{does not exist} \end{aligned}

In ABAB and CACA the inner numbers are equal (3 and 3, 2 and 2). In BABA they are 4 and 2, in ACAC they are 3 and 2.

Why: ABAB exists but BABA does not. So the question "is AB=BAAB = BA?" often has no meaning at all: one side may not exist.

Level 2: Products, determinants, inverses and elimination

Problem 8: Different sizes

Find ABAB for A=[120013]A = \mat{1 & 2 & 0 \\ 0 & 1 & 3} and B=[210110]B = \mat{2 & 1 \\ 0 & 1 \\ 1 & 0}.

Idea. (2×3)(3×2)(2 \times 3)(3 \times 2) gives a 2×22 \times 2 matrix. Each entry is a dot product of two vectors with 3 components (Part 4).

row 1, column 1:1×2+2×0+0×1=2row 1, column 2:1×1+2×1+0×0=3row 2, column 1:0×2+1×0+3×1=3row 2, column 2:0×1+1×1+3×0=1\begin{aligned} \text{row 1, column 1:} \quad & 1 \times 2 + 2 \times 0 + 0 \times 1 = 2 \\ \text{row 1, column 2:} \quad & 1 \times 1 + 2 \times 1 + 0 \times 0 = 3 \\ \text{row 2, column 1:} \quad & 0 \times 2 + 1 \times 0 + 3 \times 1 = 3 \\ \text{row 2, column 2:} \quad & 0 \times 1 + 1 \times 1 + 3 \times 0 = 1 \end{aligned}
AB=[2331]AB = \mat{2 & 3 \\ 3 & 1}

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 A=[1201]A = \mat{1 & 2 \\ 0 & 1} and B=[3012]B = \mat{3 & 0 \\ 1 & 2} find ABAB and BABA.

Idea. Two separate products (Part 4).

AB=[1×3+2×11×0+2×20×3+1×10×0+1×2]=[5412]BA=[3×1+0×03×2+0×11×1+2×01×2+2×1]=[3614]\begin{aligned} AB &= \mat{1 \times 3 + 2 \times 1 & 1 \times 0 + 2 \times 2 \\ 0 \times 3 + 1 \times 1 & 0 \times 0 + 1 \times 2} = \mat{5 & 4 \\ 1 & 2} \\[6pt] BA &= \mat{3 \times 1 + 0 \times 0 & 3 \times 2 + 0 \times 1 \\ 1 \times 1 + 2 \times 0 & 1 \times 2 + 2 \times 1} = \mat{3 & 6 \\ 1 & 4} \end{aligned}

Why: the two results are different matrices. There is no shortcut from one to the other: when you need BABA, you must calculate it.

Problem 10: The square of a matrix

Find A2A^2 for A=[2103]A = \mat{2 & 1 \\ 0 & 3}.

Idea. A2=AAA^2 = AA, a matrix product (Part 4).

A2=[2×2+1×02×1+1×30×2+3×00×1+3×3]=[4509]A^2 = \mat{2 \times 2 + 1 \times 0 & 2 \times 1 + 1 \times 3 \\ 0 \times 2 + 3 \times 0 & 0 \times 1 + 3 \times 3} = \mat{4 & 5 \\ 0 & 9}

Why: the matrix of the squared entries would be [4109]\mat{4 & 1 \\ 0 & 9}: 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 A=[3124]A = \mat{3 & 1 \\ 2 & 4} and B=[4623]B = \mat{4 & 6 \\ 2 & 3}. Which of them has an inverse?

Idea. det⁡=ad−bc\det = ad - bc. A matrix has an inverse exactly when its determinant is not 0 (Part 5).

det⁡(A)=3×4−1×2=10det⁡(B)=4×3−6×2=0\begin{aligned} \det(A) &= 3 \times 4 - 1 \times 2 = 10 \\ \det(B) &= 4 \times 3 - 6 \times 2 = 0 \end{aligned}

det⁡(A)\det(A) is not 0, so AA is invertible. det⁡(B)=0\det(B) = 0, so BB is singular and has no inverse.

Why: in BB the first row is 2 times the second, (4,6)=2 (2,3)(4, 6) = 2\,(2, 3). Parallel rows always give the determinant 0.

Problem 12: An inverse, with its check

Find the inverse of A=[4726]A = \mat{4 & 7 \\ 2 & 6} and check it.

Idea. Swap the diagonal entries, change the sign of the other two, divide by the determinant (Part 5).

det⁡(A)=4×6−7×2=10A−1=110[6−7−24]=[0.6−0.7−0.20.4]\begin{aligned} \det(A) &= 4 \times 6 - 7 \times 2 = 10 \\[6pt] A\inv &= \frac{1}{10} \mat{6 & -7 \\ -2 & 4} = \mat{0.6 & -0.7 \\ -0.2 & 0.4} \end{aligned}

The check:

AA−1=[4726][0.6−0.7−0.20.4]=[2.4−1.4−2.8+2.81.2−1.2−1.4+2.4]=[1001]\begin{aligned} A A\inv &= \mat{4 & 7 \\ 2 & 6} \mat{0.6 & -0.7 \\ -0.2 & 0.4} \\[6pt] &= \mat{2.4 - 1.4 & -2.8 + 2.8 \\ 1.2 - 1.2 & -1.4 + 2.4} = \mat{1 & 0 \\ 0 & 1} \end{aligned}

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 3x+2y=123x + 2y = 12, x+4y=14x + 4y = 14 with the inverse matrix.

Idea. Write it as Ax=bA\vx = \vb. Then x=A−1b\vx = A\inv \vb (Part 6).

A=[3214],b=[1214]det⁡(A)=3×4−2×1=10A−1=110[4−2−13]x=A−1b=110[4×12−2×14−1×12+3×14]=110[2030]=[23]\begin{aligned} A &= \mat{3 & 2 \\ 1 & 4}, \quad \vb = \mat{12 \\ 14} \\[6pt] \det(A) &= 3 \times 4 - 2 \times 1 = 10 \\[6pt] A\inv &= \frac{1}{10} \mat{4 & -2 \\ -1 & 3} \\[6pt] \vx = A\inv \vb &= \frac{1}{10} \mat{4 \times 12 - 2 \times 14 \\ -1 \times 12 + 3 \times 14} \\[6pt] &= \frac{1}{10} \mat{20 \\ 30} = \mat{2 \\ 3} \end{aligned}

So x=2x = 2 and y=3y = 3. Check: 3×2+2×3=123 \times 2 + 2 \times 3 = 12 and 2+4×3=142 + 4 \times 3 = 14.

Why: the factor 110\tfrac{1}{10} 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 kk has the matrix [k346]\mat{k & 3 \\ 4 & 6} no inverse?

Idea. No inverse means the determinant is 0 (Part 5). Write the determinant with the unknown and solve.

det⁡=k×6−3×4=6k−126k−12=0k=2\begin{aligned} \det &= k \times 6 - 3 \times 4 = 6k - 12 \\ 6k - 12 &= 0 \\ k &= 2 \end{aligned}

Check: the matrix is then [2346]\mat{2 & 3 \\ 4 & 6}, and (4,6)=2 (2,3)(4, 6) = 2\,(2, 3): the rows are parallel.

Why: as with perpendicular vectors in the last lesson, a geometric condition became one small equation. For every other kk the matrix is invertible.

Problem 15: Sales of a week

A stall sells apples and pears. On three days it sold (4,2)(4, 2), (3,5)(3, 5) and (6,1)(6, 1) 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).

S=[423561],p=[1020]S = \mat{4 & 2 \\ 3 & 5 \\ 6 & 1}, \quad \vp = \mat{10 \\ 20}
Sp=[4×10+2×203×10+5×206×10+1×20]=[8013080]S\vp = \mat{4 \times 10 + 2 \times 20 \\ 3 \times 10 + 5 \times 20 \\ 6 \times 10 + 1 \times 20} = \mat{80 \\ 130 \\ 80}

Why: SS is 3×23 \times 2 and p\vp 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 x+3y=7x + 3y = 7, 2x+5y=122x + 5y = 12 with Gaussian elimination.

Idea. Write the augmented matrix, make a zero below the first pivot, then solve from the bottom (Part 7).

[1372512]  →  R2−2R1    [1370−1−2]\aug{rr}{r}{1 & 3 & 7 \\ 2 & 5 & 12} \;\xrightarrow{\;R_2 - 2R_1\;}\; \aug{rr}{r}{1 & 3 & 7 \\ 0 & -1 & -2}
row 2:−y=−2⇒y=2row 1:x+3y=7⇒x=7−6=1\begin{aligned} \text{row 2:} \quad & -y = -2 & \Rightarrow \quad & y = 2 \\ \text{row 1:} \quad & x + 3y = 7 & \Rightarrow \quad & x = 7 - 6 = 1 \end{aligned}

Check: 1+3×2=71 + 3 \times 2 = 7 and 2×1+5×2=122 \times 1 + 5 \times 2 = 12.

Why: the new row 2 is 2−2×1=02 - 2 \times 1 = 0, 5−2×3=−15 - 2 \times 3 = -1, 12−2×7=−212 - 2 \times 7 = -2: 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 A=[1326]A = \mat{1 & 3 \\ 2 & 6} and of B=[1325]B = \mat{1 & 3 \\ 2 & 5}.

Idea. Bring each matrix to echelon form and count the pivots (Part 8).

A:[1326]  →  R2−2R1    [1300]B:[1325]  →  R2−2R1    [130−1]\begin{aligned} A: \quad & \mat{1 & 3 \\ 2 & 6} \;\xrightarrow{\;R_2 - 2R_1\;}\; \mat{1 & 3 \\ 0 & 0} \\[6pt] B: \quad & \mat{1 & 3 \\ 2 & 5} \;\xrightarrow{\;R_2 - 2R_1\;}\; \mat{1 & 3 \\ 0 & -1} \end{aligned}

AA has one pivot, so rank⁡(A)=1\rank(A) = 1. BB has two pivots, so rank⁡(B)=2\rank(B) = 2.

Why: in AA the second row is 2 times the first, so the rows are linearly dependent and det⁡(A)=6−6=0\det(A) = 6 - 6 = 0. In BB they are independent, det⁡(B)=5−6=−1\det(B) = 5 - 6 = -1, and BB has an inverse.

Problem 18: How many solutions?

Without solving completely, decide how many solutions each system has. System (a): x+2y=4x + 2y = 4, 3x+6y=123x + 6y = 12. System (b): x+2y=4x + 2y = 4, 3x+6y=103x + 6y = 10.

Idea. Eliminate and read the last row (Part 7).

(a)[1243612]  →  R2−3R1    [124000](b)[1243610]  →  R2−3R1    [12400−2]\begin{aligned} \text{(a)} \quad & \aug{rr}{r}{1 & 2 & 4 \\ 3 & 6 & 12} \;\xrightarrow{\;R_2 - 3R_1\;}\; \aug{rr}{r}{1 & 2 & 4 \\ 0 & 0 & 0} \\[6pt] \text{(b)} \quad & \aug{rr}{r}{1 & 2 & 4 \\ 3 & 6 & 10} \;\xrightarrow{\;R_2 - 3R_1\;}\; \aug{rr}{r}{1 & 2 & 4 \\ 0 & 0 & -2} \end{aligned}

In (a) the last row says 0=00 = 0: infinitely many solutions. In (b) it says 0=−20 = -2: 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 A=[210131021]A = \mat{2 & 1 & 0 \\ 1 & 3 & 1 \\ 0 & 2 & 1}.

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).

det⁡(A)=2det⁡[3121]−1det⁡[1101]+0=2 (3×1−1×2)−1 (1×1−1×0)=2×1−1×1=1\begin{aligned} \det(A) &= 2 \det \mat{3 & 1 \\ 2 & 1} - 1 \det \mat{1 & 1 \\ 0 & 1} + 0 \\[6pt] &= 2\,(3 \times 1 - 1 \times 2) - 1\,(1 \times 1 - 1 \times 0) \\ &= 2 \times 1 - 1 \times 1 \\ &= 1 \end{aligned}

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 A=[1201]A = \mat{1 & 2 \\ 0 & 1} and B=[3012]B = \mat{3 & 0 \\ 1 & 2} check that (AB)T=BTAT(AB)\T = B\T A\T.

Idea. Calculate both sides separately (Part 4). ABAB was found in Problem 9.

AB=[5412],(AB)T=[5142]BT=[3102],AT=[1021]BTAT=[3×1+1×23×0+1×10×1+2×20×0+2×1]=[5142]\begin{aligned} AB &= \mat{5 & 4 \\ 1 & 2}, \quad (AB)\T = \mat{5 & 1 \\ 4 & 2} \\[6pt] B\T &= \mat{3 & 1 \\ 0 & 2}, \quad A\T = \mat{1 & 0 \\ 2 & 1} \\[6pt] B\T A\T &= \mat{3 \times 1 + 1 \times 2 & 3 \times 0 + 1 \times 1 \\ 0 \times 1 + 2 \times 2 & 0 \times 0 + 2 \times 1} \\[4pt] &= \mat{5 & 1 \\ 4 & 2} \end{aligned}

Why: the order turns around. ATBTA\T B\T would be [3164]\mat{3 & 1 \\ 6 & 4}, a different matrix: it is the transpose of BABA, not of ABAB.

Problem 21: Rotate, then stretch

The vector x=(2,1)\vx = (2, 1) is first rotated by 90 degrees to the left with R=[0−110]R = \mat{0 & -1 \\ 1 & 0} and then stretched with S=[3001]S = \mat{3 & 0 \\ 0 & 1}. 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).

step 1:Rx=[0×2+(−1)×11×2+0×1]=[−12]step 2:S (Rx)=[3×(−1)+0×20×(−1)+1×2]=[−32]\begin{aligned} \text{step 1:} \quad R\vx &= \mat{0 \times 2 + (-1) \times 1 \\ 1 \times 2 + 0 \times 1} = \mat{-1 \\ 2} \\[6pt] \text{step 2:} \quad S\,(R\vx) &= \mat{3 \times (-1) + 0 \times 2 \\ 0 \times (-1) + 1 \times 2} = \mat{-3 \\ 2} \end{aligned}

Both at once:

SR=[3001][0−110]=[0−310](SR) x=[0×2+(−3)×11×2+0×1]=[−32]\begin{aligned} SR &= \mat{3 & 0 \\ 0 & 1} \mat{0 & -1 \\ 1 & 0} = \mat{0 & -3 \\ 1 & 0} \\[6pt] (SR)\,\vx &= \mat{0 \times 2 + (-3) \times 1 \\ 1 \times 2 + 0 \times 1} = \mat{-3 \\ 2} \end{aligned}

Why: the matrix that acts first, RR, stands next to the vector, on the right. Reading SRxSR\vx 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 w=(1200,15000,−800)\vw = (1200, 15000, -800) for (area, rooms, age) and the bias 20000. Find the predictions for the three flats (80,3,10)(80, 3, 10), (55,2,30)(55, 2, 30) and (120,4,5)(120, 4, 5) with one matrix product.

Idea. The flats are the rows of a matrix XX. Then XwX\vw holds one dot product for every flat, and the bias is added to each (Part 11).

X=[803105523012045]Xw=[96000+45000−800066000+30000−24000144000+60000−4000]=[13300072000200000]Xw+20000=(153000,  92000,  220000)\begin{aligned} X &= \mat{80 & 3 & 10 \\ 55 & 2 & 30 \\ 120 & 4 & 5} \\[6pt] X\vw &= \mat{96000 + 45000 - 8000 \\ 66000 + 30000 - 24000 \\ 144000 + 60000 - 4000} \\[6pt] &= \mat{133000 \\ 72000 \\ 200000} \\[6pt] X\vw + 20000 &= (153000,\; 92000,\; 220000) \end{aligned}

Why: XX is 3×33 \times 3 here only by chance. With 1000 flats it would be 1000×31000 \times 3, and XwX\vw would have 1000 components: the number of rows is the number of predictions.

Problem 23: A matrix equation

Find the matrix XX with AX=BAX = B, for A=[2111]A = \mat{2 & 1 \\ 1 & 1} and B=[5433]B = \mat{5 & 4 \\ 3 & 3}.

Idea. Multiply both sides from the left by A−1A\inv: X=A−1BX = A\inv B (Parts 5 and 6).

det⁡(A)=2×1−1×1=1A−1=[1−1−12]X=A−1B=[1−1−12][5433]=[5−34−3−5+6−4+6]=[2112]\begin{aligned} \det(A) &= 2 \times 1 - 1 \times 1 = 1 \\[6pt] A\inv &= \mat{1 & -1 \\ -1 & 2} \\[6pt] X = A\inv B &= \mat{1 & -1 \\ -1 & 2} \mat{5 & 4 \\ 3 & 3} \\[6pt] &= \mat{5 - 3 & 4 - 3 \\ -5 + 6 & -4 + 6} = \mat{2 & 1 \\ 1 & 2} \end{aligned}

The check:

AX=[2×2+1×12×1+1×21×2+1×11×1+1×2]=[5433]AX = \mat{2 \times 2 + 1 \times 1 & 2 \times 1 + 1 \times 2 \\ 1 \times 2 + 1 \times 1 & 1 \times 1 + 1 \times 2} = \mat{5 & 4 \\ 3 & 3}

Why: the inverse must be multiplied from the left on both sides, because AA stands on the left of XX. BA−1B A\inv would be a different matrix and a wrong answer.

Problem 24: Three equations, three unknowns

Solve the system x+2y+z=8x + 2y + z = 8, 2x+5y+3z=212x + 5y + 3z = 21, x+3y+4z=19x + 3y + 4z = 19 with Gaussian elimination.

Idea. Zeros below the first pivot, then below the second, then back substitution (Part 7).

[12182532113419]→  R3−R1    R2−2R1  [1218011501311]→  R3−R2  [121801150026]\begin{aligned} &\aug{rrr}{r}{1 & 2 & 1 & 8 \\ 2 & 5 & 3 & 21 \\ 1 & 3 & 4 & 19} \\[8pt] \xrightarrow[\;R_3 - R_1\;]{\;R_2 - 2R_1\;}\quad &\aug{rrr}{r}{1 & 2 & 1 & 8 \\ 0 & 1 & 1 & 5 \\ 0 & 1 & 3 & 11} \\[8pt] \xrightarrow{\;R_3 - R_2\;}\quad &\aug{rrr}{r}{1 & 2 & 1 & 8 \\ 0 & 1 & 1 & 5 \\ 0 & 0 & 2 & 6} \end{aligned}
row 3:2z=6⇒z=3row 2:y+z=5⇒y=2row 1:x+2y+z=8⇒x=8−4−3=1\begin{aligned} \text{row 3:} \quad & 2z = 6 & \Rightarrow \quad & z = 3 \\ \text{row 2:} \quad & y + z = 5 & \Rightarrow \quad & y = 2 \\ \text{row 1:} \quad & x + 2y + z = 8 & \Rightarrow \quad & x = 8 - 4 - 3 = 1 \end{aligned}

Check: 1+4+3=81 + 4 + 3 = 8, 2+10+9=212 + 10 + 9 = 21 and 1+6+12=191 + 6 + 12 = 19.

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 A=[1232573710]A = \mat{1 & 2 & 3 \\ 2 & 5 & 7 \\ 3 & 7 & 10}. Does it have an inverse?

Idea. Echelon form, then count the pivots. A square matrix has an inverse only with full rank (Part 8).

[1232573710]→  R3−3R1    R2−2R1  [123011011]→  R3−R2  [123011000]\begin{aligned} &\mat{1 & 2 & 3 \\ 2 & 5 & 7 \\ 3 & 7 & 10} \\[8pt] \xrightarrow[\;R_3 - 3R_1\;]{\;R_2 - 2R_1\;}\quad &\mat{1 & 2 & 3 \\ 0 & 1 & 1 \\ 0 & 1 & 1} \\[8pt] \xrightarrow{\;R_3 - R_2\;}\quad &\mat{1 & 2 & 3 \\ 0 & 1 & 1 \\ 0 & 0 & 0} \end{aligned}

Two pivots: rank⁡(A)=2\rank(A) = 2, smaller than 3, so there is no inverse. The check with the determinant:

1 (50−49)−2 (20−21)+3 (14−15)=1+2−3=0\begin{aligned} & 1\,(50 - 49) - 2\,(20 - 21) + 3\,(14 - 15) \\ & \quad = 1 + 2 - 3 = 0 \end{aligned}

Why: the third row is the sum of the first two, (3,7,10)=(1,2,3)+(2,5,7)(3, 7, 10) = (1, 2, 3) + (2, 5, 7). 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 A=[101011113]A = \mat{1 & 0 & 1 \\ 0 & 1 & 1 \\ 1 & 1 & 3} and check it.

Idea. Write AA next to II and use row operations until the left half is II (Part 9).

[101100011010113001]→  R3−R1  [101100011010012−101]→  R3−R2  [101100011010001−1−11]→  R2−R3    R1−R3  [10021−101012−1001−1−11]\begin{aligned} &\aug{rrr}{rrr}{1 & 0 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 \\ 1 & 1 & 3 & 0 & 0 & 1} \\[8pt] \xrightarrow{\;R_3 - R_1\;}\quad &\aug{rrr}{rrr}{1 & 0 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 \\ 0 & 1 & 2 & -1 & 0 & 1} \\[8pt] \xrightarrow{\;R_3 - R_2\;}\quad &\aug{rrr}{rrr}{1 & 0 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 \\ 0 & 0 & 1 & -1 & -1 & 1} \\[8pt] \xrightarrow[\;R_2 - R_3\;]{\;R_1 - R_3\;}\quad &\aug{rrr}{rrr}{1 & 0 & 0 & 2 & 1 & -1 \\ 0 & 1 & 0 & 1 & 2 & -1 \\ 0 & 0 & 1 & -1 & -1 & 1} \end{aligned}
A−1=[21−112−1−1−11]A\inv = \mat{2 & 1 & -1 \\ 1 & 2 & -1 \\ -1 & -1 & 1}

The check, row 1 and row 3 of AA times the columns of A−1A\inv:

row 1:2+0−1=11+0−1=0−1+0+1=0row 3:2+1−3=01+2−3=0−1−1+3=1\begin{aligned} \text{row 1:} \quad & 2 + 0 - 1 = 1 \\ & 1 + 0 - 1 = 0 \\ & -1 + 0 + 1 = 0 \\[4pt] \text{row 3:} \quad & 2 + 1 - 3 = 0 \\ & 1 + 2 - 3 = 0 \\ & -1 - 1 + 3 = 1 \end{aligned}

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 (0,1,0)(0, 1, 0) 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. 1.Give the size of A=[123456]A = \mat{1 & 2 & 3 \\ 4 & 5 & 6} and its entry a21a_{21}.

    Show a hint
    Rows first. In a21a_{21} the row is 2 and the column is 1.
    Show one possible solution

    Answer: The size is 2×32 \times 3, and a21=4a_{21} = 4.

    The matrix has 2 rows and 3 columns, so its size is 2×32 \times 3. Row 2 is (4,5,6)(4, 5, 6), and its first entry is 4.

  2. 2.Find A+BA + B for A=[2−103]A = \mat{2 & -1 \\ 0 & 3} and B=[14−25]B = \mat{1 & 4 \\ -2 & 5}.
    A+BA + B

    Show a hint
    Add entry by entry.
    Show one possible solution

    Answer: A+B=[33−28]A + B = \mat{3 & 3 \\ -2 & 8}.

    A+B=[2+1−1+40+(−2)3+5]=[33−28]A + B = \mat{2 + 1 & -1 + 4 \\ 0 + (-2) & 3 + 5} = \mat{3 & 3 \\ -2 & 8}
  3. 3.Find A−BA - B for the same A=[2−103]A = \mat{2 & -1 \\ 0 & 3} and B=[14−25]B = \mat{1 & 4 \\ -2 & 5}.
    A−BA - B

    Show a hint
    Subtract entry by entry; subtracting a negative number adds.
    Show one possible solution

    Answer: A−B=[1−52−2]A - B = \mat{1 & -5 \\ 2 & -2}.

    A−B=[2−1−1−40−(−2)3−5]=[1−52−2]A - B = \mat{2 - 1 & -1 - 4 \\ 0 - (-2) & 3 - 5} = \mat{1 & -5 \\ 2 & -2}
  4. 4.Find 3A3A for A=[1−204]A = \mat{1 & -2 \\ 0 & 4}.
    3A3A

    Show a hint
    Multiply every entry by 3.
    Show one possible solution

    Answer: 3A=[3−6012]3A = \mat{3 & -6 \\ 0 & 12}.

    3A=[3×13×(−2)3×03×4]=[3−6012]3A = \mat{3 \times 1 & 3 \times (-2) \\ 3 \times 0 & 3 \times 4} = \mat{3 & -6 \\ 0 & 12}
  5. 5.Find 2A+B2A + B for A=[102−1]A = \mat{1 & 0 \\ 2 & -1} and B=[3102]B = \mat{3 & 1 \\ 0 & 2}.
    2A+B2A + B

    Show a hint
    First 2A2A, then add BB.
    Show one possible solution

    Answer: 2A+B=[5140]2A + B = \mat{5 & 1 \\ 4 & 0}.

    2A=[204−2]2A+B=[2+30+14+0−2+2]=[5140]\begin{aligned} 2A &= \mat{2 & 0 \\ 4 & -2} \\[6pt] 2A + B &= \mat{2 + 3 & 0 + 1 \\ 4 + 0 & -2 + 2} = \mat{5 & 1 \\ 4 & 0} \end{aligned}
  6. 6.Find the transpose of A=[123456]A = \mat{1 & 2 & 3 \\ 4 & 5 & 6}.
    ATA\T

    Show a hint
    The rows become the columns.
    Show one possible solution

    Answer: AT=[142536]A\T = \mat{1 & 4 \\ 2 & 5 \\ 3 & 6}.

    The first row (1,2,3)(1, 2, 3) becomes the first column and the second row (4,5,6)(4, 5, 6) the second column. The 2×32 \times 3 matrix becomes 3×23 \times 2:

    AT=[142536]A\T = \mat{1 & 4 \\ 2 & 5 \\ 3 & 6}
  7. 7.Find AxA\vx for A=[2113]A = \mat{2 & 1 \\ 1 & 3} and x=(3,2)\vx = (3, 2).

    Show a hint
    One dot product for every row.
    Show one possible solution

    Answer: Ax=(8,9)A\vx = (8, 9).

    Ax=[2×3+1×21×3+3×2]=[89]A\vx = \mat{2 \times 3 + 1 \times 2 \\ 1 \times 3 + 3 \times 2} = \mat{8 \\ 9}
  8. 8.Find AxA\vx for A=[10201−1]A = \mat{1 & 0 & 2 \\ 0 & 1 & -1} and x=(4,5,1)\vx = (4, 5, 1).

    Show a hint
    The matrix is 2×32 \times 3, so the result has 2 components.
    Show one possible solution

    Answer: Ax=(6,4)A\vx = (6, 4).

    Ax=[1×4+0×5+2×10×4+1×5+(−1)×1]=[64]A\vx = \mat{1 \times 4 + 0 \times 5 + 2 \times 1 \\ 0 \times 4 + 1 \times 5 + (-1) \times 1} = \mat{6 \\ 4}
  9. 9.Find ABAB for A=[2101]A = \mat{2 & 1 \\ 0 & 1} and B=[1320]B = \mat{1 & 3 \\ 2 & 0}.
    ABAB

    Show a hint
    Row of AA times column of BB.
    Show one possible solution

    Answer: AB=[4620]AB = \mat{4 & 6 \\ 2 & 0}.

    AB=[2×1+1×22×3+1×00×1+1×20×3+1×0]=[4620]\begin{aligned} AB &= \mat{2 \times 1 + 1 \times 2 & 2 \times 3 + 1 \times 0 \\ 0 \times 1 + 1 \times 2 & 0 \times 3 + 1 \times 0} \\[4pt] &= \mat{4 & 6 \\ 2 & 0} \end{aligned}
  10. 10.AA is 2×32 \times 3. BB is 3×43 \times 4. CC is 2×32 \times 3. Give the size of ABAB, and say whether ACAC exists.

    Show a hint
    Compare the inner numbers.
    Show one possible solution

    Answer: ABAB is 2×42 \times 4. ACAC does not exist.

    AB:(2×3)(3×4)  ⇒  2×4AC:(2×3)(2×3)  ⇒  no product\begin{aligned} AB: \quad & (2 \times 3)(3 \times 4) \;\Rightarrow\; 2 \times 4 \\ AC: \quad & (2 \times 3)(2 \times 3) \;\Rightarrow\; \text{no product} \end{aligned}

    For ABAB the inner numbers are 3 and 3: equal, so the product exists and has the two outer numbers as its size. For ACAC they are 3 and 2, so there is no product.

  11. 11.Find the determinant of A=[3124]A = \mat{3 & 1 \\ 2 & 4}.

    Show a hint
    ad−bcad - bc.
    Show one possible solution

    Answer: det⁡(A)=10\det(A) = 10.

    det⁡(A)=3×4−1×2=12−2=10\det(A) = 3 \times 4 - 1 \times 2 = 12 - 2 = 10
  12. 12.Which of the matrices A=[1772]A = \mat{1 & 7 \\ 7 & 2} and B=[1234]B = \mat{1 & 2 \\ 3 & 4} is symmetric?

    Show a hint
    Compare the two entries outside the main diagonal.
    Show one possible solution

    Answer: AA is symmetric, BB is not.

    In AA the entries a12a_{12} and a21a_{21} are both 7, so AT=AA\T = A. In BB they are 2 and 3, so BTB\T is a different matrix.

Level 2: Products, determinants, inverses and elimination

  1. 13.Find ABAB and BABA for A=[1102]A = \mat{1 & 1 \\ 0 & 2} and B=[2013]B = \mat{2 & 0 \\ 1 & 3}.
    ABAB
    BABA

    Show a hint
    Two separate products. Do not expect the same result.
    Show one possible solution

    Answer: AB=[3326]AB = \mat{3 & 3 \\ 2 & 6} and BA=[2217]BA = \mat{2 & 2 \\ 1 & 7}.

    AB=[1×2+1×11×0+1×30×2+2×10×0+2×3]=[3326]BA=[2×1+0×02×1+0×21×1+3×01×1+3×2]=[2217]\begin{aligned} AB &= \mat{1 \times 2 + 1 \times 1 & 1 \times 0 + 1 \times 3 \\ 0 \times 2 + 2 \times 1 & 0 \times 0 + 2 \times 3} \\[4pt] &= \mat{3 & 3 \\ 2 & 6} \\[8pt] BA &= \mat{2 \times 1 + 0 \times 0 & 2 \times 1 + 0 \times 2 \\ 1 \times 1 + 3 \times 0 & 1 \times 1 + 3 \times 2} \\[4pt] &= \mat{2 & 2 \\ 1 & 7} \end{aligned}
  2. 14.Find ABAB for A=[120013]A = \mat{1 & 2 & 0 \\ 0 & 1 & 3} and B=[102102]B = \mat{1 & 0 \\ 2 & 1 \\ 0 & 2}.
    ABAB

    Show a hint
    (2×3)(3×2)(2 \times 3)(3 \times 2) gives a 2×22 \times 2 matrix.
    Show one possible solution

    Answer: AB=[5227]AB = \mat{5 & 2 \\ 2 & 7}.

    row 1, column 1:1×1+2×2+0×0=5row 1, column 2:1×0+2×1+0×2=2row 2, column 1:0×1+1×2+3×0=2row 2, column 2:0×0+1×1+3×2=7\begin{aligned} \text{row 1, column 1:} \quad & 1 \times 1 + 2 \times 2 + 0 \times 0 = 5 \\ \text{row 1, column 2:} \quad & 1 \times 0 + 2 \times 1 + 0 \times 2 = 2 \\ \text{row 2, column 1:} \quad & 0 \times 1 + 1 \times 2 + 3 \times 0 = 2 \\ \text{row 2, column 2:} \quad & 0 \times 0 + 1 \times 1 + 3 \times 2 = 7 \end{aligned}
  3. 15.Find A2A^2 for A=[3102]A = \mat{3 & 1 \\ 0 & 2}.
    A2A^2

    Show a hint
    A2=AAA^2 = AA, not the squared entries.
    Show one possible solution

    Answer: A2=[9504]A^2 = \mat{9 & 5 \\ 0 & 4}.

    A2=[3×3+1×03×1+1×20×3+2×00×1+2×2]=[9504]\begin{aligned} A^2 &= \mat{3 \times 3 + 1 \times 0 & 3 \times 1 + 1 \times 2 \\ 0 \times 3 + 2 \times 0 & 0 \times 1 + 2 \times 2} \\[4pt] &= \mat{9 & 5 \\ 0 & 4} \end{aligned}

    The matrix of the squared entries would have a 1 at the top right, not a 5.

  4. 16.Find the inverse of A=[2153]A = \mat{2 & 1 \\ 5 & 3}.
    A−1A\inv

    Show a hint
    The determinant is 1, so nothing has to be divided.
    Show one possible solution

    Answer: A−1=[3−1−52]A\inv = \mat{3 & -1 \\ -5 & 2}.

    det⁡(A)=2×3−1×5=1A−1=[3−1−52]\begin{aligned} \det(A) &= 2 \times 3 - 1 \times 5 = 1 \\[6pt] A\inv &= \mat{3 & -1 \\ -5 & 2} \end{aligned}

    Swap 2 and 3, change the signs of 1 and 5. Check with the first row of AA: 2×3+1×(−5)=12 \times 3 + 1 \times (-5) = 1 and 2×(−1)+1×2=02 \times (-1) + 1 \times 2 = 0.

  5. 17.Find the inverse of A=[1234]A = \mat{1 & 2 \\ 3 & 4}.
    A−1A\inv

    Show a hint
    The determinant is negative. Divide every entry by it.
    Show one possible solution

    Answer: A−1=[−211.5−0.5]A\inv = \mat{-2 & 1 \\ 1.5 & -0.5}.

    det⁡(A)=1×4−2×3=−2A−1=1−2[4−2−31]=[−211.5−0.5]\begin{aligned} \det(A) &= 1 \times 4 - 2 \times 3 = -2 \\[6pt] A\inv &= \frac{1}{-2} \mat{4 & -2 \\ -3 & 1} = \mat{-2 & 1 \\ 1.5 & -0.5} \end{aligned}

    Check with the first row of AA: 1×(−2)+2×1.5=11 \times (-2) + 2 \times 1.5 = 1 and 1×1+2×(−0.5)=01 \times 1 + 2 \times (-0.5) = 0.

  6. 18.For which kk is the matrix [2k46]\mat{2 & k \\ 4 & 6} singular?

    Show a hint
    Set the determinant equal to 0.
    Show one possible solution

    Answer: k=3k = 3.

    det⁡=2×6−k×4=12−4k12−4k=0k=3\begin{aligned} \det &= 2 \times 6 - k \times 4 = 12 - 4k \\ 12 - 4k &= 0 \\ k &= 3 \end{aligned}

    Then the rows are (2,3)(2, 3) and (4,6)(4, 6), and the second is twice the first.

  7. 19.Solve the system 2x+y=72x + y = 7, x+y=4x + y = 4 with the inverse matrix.

    Show a hint
    A=[2111]A = \mat{2 & 1 \\ 1 & 1} has the determinant 1.
    Show one possible solution

    Answer: x=3x = 3 and y=1y = 1.

    A−1=[1−1−12]x=A−1b=[1×7−1×4−1×7+2×4]=[31]\begin{aligned} A\inv &= \mat{1 & -1 \\ -1 & 2} \\[6pt] \vx = A\inv \vb &= \mat{1 \times 7 - 1 \times 4 \\ -1 \times 7 + 2 \times 4} = \mat{3 \\ 1} \end{aligned}

    Check: 2×3+1=72 \times 3 + 1 = 7 and 3+1=43 + 1 = 4.

  8. 20.Find xx and yy with [x12y]+[3412]=[5536]\mat{x & 1 \\ 2 & y} + \mat{3 & 4 \\ 1 & 2} = \mat{5 & 5 \\ 3 & 6}.

    Show a hint
    Two matrices are equal when every entry is equal.
    Show one possible solution

    Answer: x=2x = 2 and y=4y = 4.

    row 1, column 1:x+3=5⇒x=2row 2, column 2:y+2=6⇒y=4\begin{aligned} \text{row 1, column 1:} \quad & x + 3 = 5 & \Rightarrow \quad & x = 2 \\ \text{row 2, column 2:} \quad & y + 2 = 6 & \Rightarrow \quad & y = 4 \end{aligned}

    The other two entries fit: 1+4=51 + 4 = 5 and 2+1=32 + 1 = 3.

  9. 21.Find AxA\vx for A=[1321]A = \mat{1 & 3 \\ 2 & 1} and x=(2,−1)\vx = (2, -1) as a combination of the columns of AA.

    Show a hint
    AxA\vx is 2 times the first column plus (−1)(-1) times the second.
    Show one possible solution

    Answer: Ax=(−1,3)A\vx = (-1, 3).

    Ax=2[12]+(−1)[31]=[24]−[31]=[−13]A\vx = 2 \mat{1 \\ 2} + (-1) \mat{3 \\ 1} = \mat{2 \\ 4} - \mat{3 \\ 1} = \mat{-1 \\ 3}

    The row reading gives the same: 1×2+3×(−1)=−11 \times 2 + 3 \times (-1) = -1 and 2×2+1×(−1)=32 \times 2 + 1 \times (-1) = 3.

  10. 22.A cinema sold, on two evenings, (30,12)(30, 12) and (45,20)(45, 20) 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 (300,460)(300, 460).

    S=[30124520],p=[85]S = \mat{30 & 12 \\ 45 & 20}, \quad \vp = \mat{8 \\ 5}
    Sp=[30×8+12×545×8+20×5]=[240+60360+100]=[300460]\begin{aligned} S\vp &= \mat{30 \times 8 + 12 \times 5 \\ 45 \times 8 + 20 \times 5} \\[6pt] &= \mat{240 + 60 \\ 360 + 100} = \mat{300 \\ 460} \end{aligned}
  11. 23.For A=[1201]A = \mat{1 & 2 \\ 0 & 1} find ATAA\T A. What do you notice about the result?
    ATAA\T A

    Show a hint
    First write ATA\T, then multiply.
    Show one possible solution

    Answer: ATA=[1225]A\T A = \mat{1 & 2 \\ 2 & 5}. It is symmetric.

    AT=[1021]ATA=[1×1+0×01×2+0×12×1+1×02×2+1×1]=[1225]\begin{aligned} A\T &= \mat{1 & 0 \\ 2 & 1} \\[6pt] A\T A &= \mat{1 \times 1 + 0 \times 0 & 1 \times 2 + 0 \times 1 \\ 2 \times 1 + 1 \times 0 & 2 \times 2 + 1 \times 1} \\[4pt] &= \mat{1 & 2 \\ 2 & 5} \end{aligned}

    The entries outside the diagonal are equal. ATAA\T A is symmetric for every matrix AA.

  12. 24.For A=[1234]A = \mat{1 & 2 \\ 3 & 4} and B=[2011]B = \mat{2 & 0 \\ 1 & 1} find det⁡(A)\det(A), det⁡(B)\det(B) and det⁡(AB)\det(AB), and check that det⁡(AB)=det⁡(A)×det⁡(B)\det(AB) = \det(A) \times \det(B).

    Show a hint
    First the product ABAB, then its determinant.
    Show one possible solution

    Answer: det⁡(A)=−2\det(A) = -2, det⁡(B)=2\det(B) = 2 and det⁡(AB)=−4\det(AB) = -4.

    det⁡(A)=4−6=−2det⁡(B)=2−0=2AB=[1×2+2×11×0+2×13×2+4×13×0+4×1]=[42104]det⁡(AB)=4×4−2×10=−4\begin{aligned} \det(A) &= 4 - 6 = -2 \\ \det(B) &= 2 - 0 = 2 \\[6pt] AB &= \mat{1 \times 2 + 2 \times 1 & 1 \times 0 + 2 \times 1 \\ 3 \times 2 + 4 \times 1 & 3 \times 0 + 4 \times 1} \\[4pt] &= \mat{4 & 2 \\ 10 & 4} \\[6pt] \det(AB) &= 4 \times 4 - 2 \times 10 = -4 \end{aligned}

    Indeed −2×2=−4-2 \times 2 = -4.

  13. 25.Solve the system x+2y=8x + 2y = 8, 3x+7y=273x + 7y = 27 with Gaussian elimination.

    Show a hint
    Subtract 3 times the first row from the second.
    Show one possible solution

    Answer: x=2x = 2 and y=3y = 3.

    [1283727]  →  R2−3R1    [128013]\aug{rr}{r}{1 & 2 & 8 \\ 3 & 7 & 27} \;\xrightarrow{\;R_2 - 3R_1\;}\; \aug{rr}{r}{1 & 2 & 8 \\ 0 & 1 & 3}

    Row 2 says y=3y = 3. Row 1 gives x=8−2×3=2x = 8 - 2 \times 3 = 2. Check: 3×2+7×3=273 \times 2 + 7 \times 3 = 27.

  14. 26.Find the rank of A=[1236]A = \mat{1 & 2 \\ 3 & 6} and of B=[1235]B = \mat{1 & 2 \\ 3 & 5}.

    Show a hint
    Bring each to echelon form with R2−3R1R_2 - 3R_1 and count the pivots.
    Show one possible solution

    Answer: rank⁡(A)=1\rank(A) = 1 and rank⁡(B)=2\rank(B) = 2.

    A:[1236]  →  R2−3R1    [1200]B:[1235]  →  R2−3R1    [120−1]\begin{aligned} A: \quad & \mat{1 & 2 \\ 3 & 6} \;\xrightarrow{\;R_2 - 3R_1\;}\; \mat{1 & 2 \\ 0 & 0} \\[6pt] B: \quad & \mat{1 & 2 \\ 3 & 5} \;\xrightarrow{\;R_2 - 3R_1\;}\; \mat{1 & 2 \\ 0 & -1} \end{aligned}

    In AA one pivot is left. In BB there are two pivots.

  15. 27.How many solutions has the system x+y=2x + y = 2, 2x+2y=52x + 2y = 5?

    Show a hint
    Eliminate and read the last row.
    Show one possible solution

    Answer: No solution.

    [112225]  →  R2−2R1    [112001]\aug{rr}{r}{1 & 1 & 2 \\ 2 & 2 & 5} \;\xrightarrow{\;R_2 - 2R_1\;}\; \aug{rr}{r}{1 & 1 & 2 \\ 0 & 0 & 1}

    The last row says 0=10 = 1. That is never true. The left sides are parallel, but the right sides do not fit: twice the first equation would be 2x+2y=42x + 2y = 4.

Level 3: Putting ideas together

  1. 28.Find the determinant of the 3 x 3 matrix [120312011]\mat{1 & 2 & 0 \\ 3 & 1 & 2 \\ 0 & 1 & 1}.

    Show a hint
    Go along the first row with the signs +  −  ++\;-\;+. The third entry is 0.
    Show one possible solution

    Answer: The determinant is −7-7.

    det⁡=1det⁡[1211]−2det⁡[3201]+0=1 (1×1−2×1)−2 (3×1−2×0)=−1−6=−7\begin{aligned} \det &= 1 \det \mat{1 & 2 \\ 1 & 1} - 2 \det \mat{3 & 2 \\ 0 & 1} + 0 \\[6pt] &= 1\,(1 \times 1 - 2 \times 1) - 2\,(3 \times 1 - 2 \times 0) \\ &= -1 - 6 \\ &= -7 \end{aligned}
  2. 29.Solve the system 4x+3y=104x + 3y = 10, 3x+2y=73x + 2y = 7 with the inverse matrix.

    Show a hint
    The determinant is −1-1.
    Show one possible solution

    Answer: x=1x = 1 and y=2y = 2.

    det⁡(A)=4×2−3×3=−1A−1=1−1[2−3−34]=[−233−4]x=A−1b=[−2×10+3×73×10−4×7]=[12]\begin{aligned} \det(A) &= 4 \times 2 - 3 \times 3 = -1 \\[6pt] A\inv &= \frac{1}{-1} \mat{2 & -3 \\ -3 & 4} = \mat{-2 & 3 \\ 3 & -4} \\[6pt] \vx = A\inv \vb &= \mat{-2 \times 10 + 3 \times 7 \\ 3 \times 10 - 4 \times 7} = \mat{1 \\ 2} \end{aligned}

    Check: 4+6=104 + 6 = 10 and 3+4=73 + 4 = 7.

  3. 30.Find the matrix XX with AX=BAX = B for A=[1213]A = \mat{1 & 2 \\ 1 & 3} and B=[47510]B = \mat{4 & 7 \\ 5 & 10}.
    XX

    Show a hint
    X=A−1BX = A\inv B, with the inverse on the left.
    Show one possible solution

    Answer: X=[2113]X = \mat{2 & 1 \\ 1 & 3}.

    det⁡(A)=3−2=1A−1=[3−2−11]X=A−1B=[3−2−11][47510]=[12−1021−20−4+5−7+10]=[2113]\begin{aligned} \det(A) &= 3 - 2 = 1 \\[6pt] A\inv &= \mat{3 & -2 \\ -1 & 1} \\[6pt] X = A\inv B &= \mat{3 & -2 \\ -1 & 1} \mat{4 & 7 \\ 5 & 10} \\[6pt] &= \mat{12 - 10 & 21 - 20 \\ -4 + 5 & -7 + 10} = \mat{2 & 1 \\ 1 & 3} \end{aligned}

    Check:

    AX=[1×2+2×11×1+2×31×2+3×11×1+3×3]=[47510]\begin{aligned} AX &= \mat{1 \times 2 + 2 \times 1 & 1 \times 1 + 2 \times 3 \\ 1 \times 2 + 3 \times 1 & 1 \times 1 + 3 \times 3} \\[4pt] &= \mat{4 & 7 \\ 5 & 10} \end{aligned}
  4. 31.Rotate the points (3,0)(3, 0) and (1,2)(1, 2) by 90 degrees to the left with R=[0−110]R = \mat{0 & -1 \\ 1 & 0}.

    Show a hint
    Multiply RR by each point.
    Show one possible solution

    Answer: (3,0)(3, 0) goes to (0,3)(0, 3), and (1,2)(1, 2) goes to (−2,1)(-2, 1).

    R[30]=[0×3−1×01×3+0×0]=[03]R[12]=[0×1−1×21×1+0×2]=[−21]\begin{aligned} R \mat{3 \\ 0} &= \mat{0 \times 3 - 1 \times 0 \\ 1 \times 3 + 0 \times 0} = \mat{0 \\ 3} \\[6pt] R \mat{1 \\ 2} &= \mat{0 \times 1 - 1 \times 2 \\ 1 \times 1 + 0 \times 2} = \mat{-2 \\ 1} \end{aligned}

    Each result is perpendicular to its point: the dot products are 0.

  5. 32.A vector is first stretched with S=[2001]S = \mat{2 & 0 \\ 0 & 1} and then rotated with R=[0−110]R = \mat{0 & -1 \\ 1 & 0}. Find the single matrix that does both, and apply it to (1,1)(1, 1).
    the single matrix

    Show a hint
    The matrix that acts first stands on the right: the product is RSRS.
    Show one possible solution

    Answer: RS=[0−120]RS = \mat{0 & -1 \\ 2 & 0}, and the result is (−1,2)(-1, 2).

    RS=[0−110][2001]=[0+00−12+00+0]=[0−120](RS)[11]=[0−12+0]=[−12]\begin{aligned} RS &= \mat{0 & -1 \\ 1 & 0} \mat{2 & 0 \\ 0 & 1} \\[6pt] &= \mat{0 + 0 & 0 - 1 \\ 2 + 0 & 0 + 0} = \mat{0 & -1 \\ 2 & 0} \\[6pt] (RS) \mat{1 \\ 1} &= \mat{0 - 1 \\ 2 + 0} = \mat{-1 \\ 2} \end{aligned}

    Step by step: S (1,1)=(2,1)S\,(1, 1) = (2, 1), then R (2,1)=(−1,2)R\,(2, 1) = (-1, 2).

  6. 33.The unit square is transformed by A=[3112]A = \mat{3 & 1 \\ 1 & 2}. 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.

    det⁡(A)=3×2−1×1=5\det(A) = 3 \times 2 - 1 \times 1 = 5

    The unit square has the area 1, so the parallelogram has the area 5.

  7. 34.A model predicts with Xw+biasX\vw + \text{bias}. Find the predictions for X=[214305]X = \mat{2 & 1 \\ 4 & 3 \\ 0 & 5}, w=(3,−1)\vw = (3, -1) 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 (7,11,−3)(7, 11, -3).

    Xw=[2×3+1×(−1)4×3+3×(−1)0×3+5×(−1)]=[59−5]Xw+2=(7,  11,  −3)\begin{aligned} X\vw &= \mat{2 \times 3 + 1 \times (-1) \\ 4 \times 3 + 3 \times (-1) \\ 0 \times 3 + 5 \times (-1)} = \mat{5 \\ 9 \\ -5} \\[6pt] X\vw + 2 &= (7,\; 11,\; -3) \end{aligned}
  8. 35.A layer of a neural network calculates Wx+bW\vx + \vb. Find the output for W=[12−11]W = \mat{1 & 2 \\ -1 & 1}, x=(3,1)\vx = (3, 1) and b=(−1,2)\vb = (-1, 2).

    Show a hint
    First WxW\vx, then add the vector b\vb.
    Show one possible solution

    Answer: The output is (4,0)(4, 0).

    Wx=[1×3+2×1−1×3+1×1]=[5−2]Wx+b=[5−1−2+2]=[40]\begin{aligned} W\vx &= \mat{1 \times 3 + 2 \times 1 \\ -1 \times 3 + 1 \times 1} = \mat{5 \\ -2} \\[6pt] W\vx + \vb &= \mat{5 - 1 \\ -2 + 2} = \mat{4 \\ 0} \end{aligned}
  9. 36.For A=[1101]A = \mat{1 & 1 \\ 0 & 1} and B=[2001]B = \mat{2 & 0 \\ 0 & 1} find (AB)−1(AB)\inv in two ways: by inverting the product, and as B−1A−1B\inv A\inv.
    (AB)−1(AB)\inv

    Show a hint
    AB=[2101]AB = \mat{2 & 1 \\ 0 & 1}. The inverses of AA and BB are easy to find.
    Show one possible solution

    Answer: (AB)−1=[0.5−0.501](AB)\inv = \mat{0.5 & -0.5 \\ 0 & 1} both ways.

    AB=[2101],det⁡(AB)=2(AB)−1=12[1−102]=[0.5−0.501]\begin{aligned} AB &= \mat{2 & 1 \\ 0 & 1}, \quad \det(AB) = 2 \\[6pt] (AB)\inv &= \frac{1}{2} \mat{1 & -1 \\ 0 & 2} = \mat{0.5 & -0.5 \\ 0 & 1} \end{aligned}

    The other way:

    A−1=[1−101],B−1=[0.5001]B−1A−1=[0.5+0−0.5+00+00+1]=[0.5−0.501]\begin{aligned} A\inv &= \mat{1 & -1 \\ 0 & 1}, \quad B\inv = \mat{0.5 & 0 \\ 0 & 1} \\[6pt] B\inv A\inv &= \mat{0.5 + 0 & -0.5 + 0 \\ 0 + 0 & 0 + 1} \\[4pt] &= \mat{0.5 & -0.5 \\ 0 & 1} \end{aligned}
  10. 37.Find A3A^3 for A=[1101]A = \mat{1 & 1 \\ 0 & 1}. Can you guess A10A^{10}?
    A3A^3
    A10A^{10}

    Show a hint
    First A2=AAA^2 = AA, then A3=A2AA^3 = A^2 A.
    Show one possible solution

    Answer: A3=[1301]A^3 = \mat{1 & 3 \\ 0 & 1}, and A10=[11001]A^{10} = \mat{1 & 10 \\ 0 & 1}.

    A2=[1101][1101]=[1+01+10+00+1]=[1201]A3=A2A=[1201][1101]=[1+01+20+00+1]=[1301]\begin{aligned} A^2 &= \mat{1 & 1 \\ 0 & 1} \mat{1 & 1 \\ 0 & 1} \\[6pt] &= \mat{1 + 0 & 1 + 1 \\ 0 + 0 & 0 + 1} = \mat{1 & 2 \\ 0 & 1} \\[6pt] A^3 &= A^2 A = \mat{1 & 2 \\ 0 & 1} \mat{1 & 1 \\ 0 & 1} \\[6pt] &= \mat{1 + 0 & 1 + 2 \\ 0 + 0 & 0 + 1} = \mat{1 & 3 \\ 0 & 1} \end{aligned}

    Every multiplication by AA adds 1 to the entry in the corner, so A10=[11001]A^{10} = \mat{1 & 10 \\ 0 & 1}.

  11. 38.Solve with Gaussian elimination: x+y+z=6x + y + z = 6, x+2y+3z=14x + 2y + 3z = 14, 2x+3y+5z=232x + 3y + 5z = 23.

    Show a hint
    R2−R1R_2 - R_1 and R3−2R1R_3 - 2R_1 first, then R3−R2R_3 - R_2.
    Show one possible solution

    Answer: x=1x = 1, y=2y = 2, z=3z = 3.

    [11161231423523]→  R3−2R1    R2−R1  [1116012801311]→  R3−R2  [111601280013]\begin{aligned} &\aug{rrr}{r}{1 & 1 & 1 & 6 \\ 1 & 2 & 3 & 14 \\ 2 & 3 & 5 & 23} \\[8pt] \xrightarrow[\;R_3 - 2R_1\;]{\;R_2 - R_1\;}\quad &\aug{rrr}{r}{1 & 1 & 1 & 6 \\ 0 & 1 & 2 & 8 \\ 0 & 1 & 3 & 11} \\[8pt] \xrightarrow{\;R_3 - R_2\;}\quad &\aug{rrr}{r}{1 & 1 & 1 & 6 \\ 0 & 1 & 2 & 8 \\ 0 & 0 & 1 & 3} \end{aligned}

    Back substitution: z=3z = 3, then y+2×3=8y + 2 \times 3 = 8 gives y=2y = 2, then x=6−2−3=1x = 6 - 2 - 3 = 1. Check in the third equation: 2+6+15=232 + 6 + 15 = 23.

  12. 39.Find the rank of [112235347]\mat{1 & 1 & 2 \\ 2 & 3 & 5 \\ 3 & 4 & 7}. Does this matrix have an inverse?

    Show a hint
    R2−2R1R_2 - 2R_1, R3−3R1R_3 - 3R_1, then R3−R2R_3 - R_2.
    Show one possible solution

    Answer: The rank is 2. There is no inverse.

    [112235347]→  R3−3R1    R2−2R1  [112011011]→  R3−R2  [112011000]\begin{aligned} &\mat{1 & 1 & 2 \\ 2 & 3 & 5 \\ 3 & 4 & 7} \\[8pt] \xrightarrow[\;R_3 - 3R_1\;]{\;R_2 - 2R_1\;}\quad &\mat{1 & 1 & 2 \\ 0 & 1 & 1 \\ 0 & 1 & 1} \\[8pt] \xrightarrow{\;R_3 - R_2\;}\quad &\mat{1 & 1 & 2 \\ 0 & 1 & 1 \\ 0 & 0 & 0} \end{aligned}

    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.

  13. 40.Find the inverse of [110011001]\mat{1 & 1 & 0 \\ 0 & 1 & 1 \\ 0 & 0 & 1} with row operations.
    the inverse

    Show a hint
    Write the identity matrix next to it. The zeros below the pivots are already there, so only R2−R3R_2 - R_3 and then R1−R2R_1 - R_2 are needed.
    Show one possible solution

    Answer: The inverse is [1−1101−1001]\mat{1 & -1 & 1 \\ 0 & 1 & -1 \\ 0 & 0 & 1}.

    [110100011010001001]→  R2−R3  [11010001001−1001001]→  R1−R2  [1001−1101001−1001001]\begin{aligned} &\aug{rrr}{rrr}{1 & 1 & 0 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 \\ 0 & 0 & 1 & 0 & 0 & 1} \\[8pt] \xrightarrow{\;R_2 - R_3\;}\quad &\aug{rrr}{rrr}{1 & 1 & 0 & 1 & 0 & 0 \\ 0 & 1 & 0 & 0 & 1 & -1 \\ 0 & 0 & 1 & 0 & 0 & 1} \\[8pt] \xrightarrow{\;R_1 - R_2\;}\quad &\aug{rrr}{rrr}{1 & 0 & 0 & 1 & -1 & 1 \\ 0 & 1 & 0 & 0 & 1 & -1 \\ 0 & 0 & 1 & 0 & 0 & 1} \end{aligned}

    The right half is the inverse. Check, row 1 of the matrix times the columns of the inverse: 1+0=11 + 0 = 1, −1+1=0-1 + 1 = 0 and 1−1=01 - 1 = 0.

  14. 41.A table of data has three columns, and the third is the sum of the first two: [123415224]\mat{1 & 2 & 3 \\ 4 & 1 & 5 \\ 2 & 2 & 4}. 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.

    [123415224]→  R3−2R1    R2−4R1  [1230−7−70−2−2]\begin{aligned} &\mat{1 & 2 & 3 \\ 4 & 1 & 5 \\ 2 & 2 & 4} \\[8pt] \xrightarrow[\;R_3 - 2R_1\;]{\;R_2 - 4R_1\;}\quad &\mat{1 & 2 & 3 \\ 0 & -7 & -7 \\ 0 & -2 & -2} \end{aligned}

    The new third row is 27\tfrac{2}{7} of the new second row, so one more step makes it (0,0,0)(0, 0, 0). Two pivots: rank 2. By columns: the third column is the first plus the second, and the first two, (1,4,2)(1, 4, 2) and (2,1,2)(2, 1, 2), 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.

Question 1

1.What is the size of [123456]\mat{1 & 2 & 3 \\ 4 & 5 & 6}?

Question 2

2.For A=[4−17025]A = \mat{4 & -1 & 7 \\ 0 & 2 & 5}, what is the entry a12a_{12}?

Question 3

3.What is [1234][11]\mat{1 & 2 \\ 3 & 4} \mat{1 \\ 1}?

Question 4

4.AA is 2×32 \times 3 and BB is 3×43 \times 4. What is the size of ABAB?

Question 5

5.What is [1234][0110]\mat{1 & 2 \\ 3 & 4} \mat{0 & 1 \\ 1 & 0}?

Question 6

6.Which statement is true for all matrices whose products exist?

Question 7

7.What is the determinant of [5231]\mat{5 & 2 \\ 3 & 1}?

Question 8

8.Which matrix has no inverse?

Question 9

9.After elimination, the last row of an augmented matrix is [005]\aug{rr}{r}{0 & 0 & 5}. What does it say about the system?

Question 10

10.What is the rank of [1224]\mat{1 & 2 \\ 2 & 4}?

Question 11

11.A vector x\vx is first rotated with the matrix RR and then stretched with the matrix SS. Which product gives the result?

Question 12

12.The unit square is transformed by [2003]\mat{2 & 0 \\ 0 & 3}. What is the area of the result?

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.

Python
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 and in NumPy
By handIn NumPy
A+BA + B, A−BA - B, cAcAA + B, A - B, c * A
ATA\TA.T
AxA\vx and ABABA @ x and A @ B
det⁡(A)\det(A)np.linalg.det(A)
A−1A\invnp.linalg.inv(A)
rank⁡(A)\rank(A)np.linalg.matrix_rank(A)
the solution of Ax=bA\vx = \vbnp.linalg.solve(A, b)

The last line solves the system Ax=bA\vx = \vb with the right side (5,6)(5, 6); 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

Every idea of the lesson with its definition or rule
IdeaDefinition or rule
Matrixa rectangular table of numbers; size m×nm \times n = rows ×\times columns
Entry aija_{ij}row ii, column jj
Addition, subtractionentry by entry, only for the same size
Scalar multiplicationevery entry times the number
Transpose ATA\Trows become columns; (AB)T=BTAT(AB)\T = B\T A\T
Symmetric matrixAT=AA\T = A
Matrix times vectoreach component is a row of AA dotted with x\vx; also a combination of the columns
Matrix product ABABentry (i,j)(i, j) = row ii of AA dotted with column jj of BB
Sizes(m×n)(n×p)=(m×p)(m \times n)(n \times p) = (m \times p): the inner numbers must be equal
OrderABAB is in general not BABA
Identity matrixAI=IA=AAI = IA = A
Determinant (2 x 2)ad−bcad - bc; the factor by which areas change
Inverse (2 x 2)1det⁡(A)[d−b−ca]\dfrac{1}{\det(A)} \mat{d & -b \\ -c & a}, only when det⁡(A)\det(A) is not 0
Singular matrixdet⁡(A)=0\det(A) = 0, no inverse
System of equationsAx=bA\vx = \vb, with the solution x=A−1b\vx = A\inv \vb
Gaussian eliminationrow operations on the augmented matrix until the echelon form, then back substitution
Row operationsswap two rows; multiply a row by a number that is not 0; add a multiple of a row to another row
Rankthe 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 matrixrow operations from AA next to II until II next to A−1A\inv
One transformation after anotherthe product of the matrices, the first one on the right
Predictions for a tableXw+biasX\vw + \text{bias}